• 0
Votes
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /var/www/html/cnasolution/application/views/question.php
Line: 191
Function: _error_handler

File: /var/www/html/cnasolution/application/controllers/Questions.php
Line: 419
Function: view

File: /var/www/html/cnasolution/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Why this SQLite Statement crashes my app? [duplicate]

What are Null Pointer Exceptions (java.lang.NullPointerException) and what causes them?

What methods/tools can be used to determine the cause so that you stop the exception from causing the program to terminate prematurely?

Download script demo [LINK]
Download script demo [LINK 2] Download script demo [LINK 2]

I've knocked up a basic version of what I'm looking for, it's not perfect but it'll do the job until something better comes along.

public class LimitedQueue : Queue {     public int Limit { get; set; }      public LimitedQueue(int limit) : base(limit)     {         Limit = limit;     }      public new void Enqueue(T item)     {         while (Count >= Limit)         {             Dequeue();         }         base.Enqueue(item);     } } 
see demo
  • 14
Reply Report

I would recommend that you pull up the C5 Library. Unlike SCG (System.Collections.Generic), C5 is programmed to interface and designed to be subclassed. Most public methods are virtual and none of the classes are sealed. This way, you won't have to use that icky "new" keyword which wouldn't trigger if your LimitedQueue were cast to a SCG.Queue. With C5 and using close to the same code as you had before, you would derive from the CircularQueue. The CircularQueue actually implements both a stack and a queue, so you can get both options with a limit nearly for free. I've rewritten it below with some 3.5 constructs:

using C5;  public class LimitedQueue : CircularQueue {     public int Limit { get; set; }      public LimitedQueue(int limit) : base(limit)     {         this.Limit = limit;     }      public override void Push(T item)     {         CheckLimit(false);         base.Push(item);     }      public override void Enqueue(T item)     {         CheckLimit(true);         base.Enqueue(item);     }      protected virtual void CheckLimit(bool enqueue)     {         while (this.Count >= this.Limit)         {             if (enqueue)             {                 this.Dequeue();             }             else             {                 this.Pop();             }         }     } } 

I think that this code should do exactly what you were looking for.

demo
  • 383
Reply Report

You should create your own class, a ringbuffer would probably fit your needs.

The data structures in .NET that allows you to specify capacity, except for array, uses this to build the internal data structure used to hold the internal data.

For instance, for a list, capacity is used to size an internal array. When you start adding elements to the list, it'll start filling this array from index 0 and up, and when it reaches your capacity, it increases the capacity to a new higher capacity, and continues filling it up.

  • 308
Reply Report

Why wouldn't you just use an array with a size of 2? A Queue is supposed to be able to dynamically grow and shrink.

Or create a wrapper class around an instance of Queue instance and each time one enqueues a object, check the size of the queue. If larger than 2, dequeue the first item.

  • 478
Reply Report

Well I hope this class will helps You:
Internally the Circular FIFO Buffer use a Queue with the specified size. Once the size of the buffer is reached, it will replaces older items with new ones.

NOTE: You can't remove items randomly. I set the method Remove(T item) to return false. if You want You can modify to remove items randomly

public class CircularFIFO : ICollection , IDisposable {     public Queue CircularBuffer;      ///      /// The default initial capacity.     ///      private int capacity = 32;      ///      /// Gets the actual capacity of the FIFO.     ///      public int Capacity     {         get { return capacity; }               }      ///      ///  Initialize a new instance of FIFO class that is empty and has the default initial capacity.     ///      public CircularFIFO()     {                     CircularBuffer = new Queue();     }      ///      /// Initialize a new instance of FIFO class that is empty and has the specified initial capacity.     ///      ///  Initial capacity of the FIFO.      public CircularFIFO(int size)     {         capacity = size;         CircularBuffer = new Queue(capacity);     }      ///      /// Adds an item to the end of the FIFO.     ///      ///  The item to add to the end of the FIFO.      public void Add(T item)     {         if (this.Count >= this.Capacity)             Remove();          CircularBuffer.Enqueue(item);     }      ///      /// Adds array of items to the end of the FIFO.     ///      ///  The array of items to add to the end of the FIFO.       public void Add(T[] item)     {          int enqueuedSize = 0;         int remainEnqueueSize = this.Capacity - this.Count;          for (; (enqueuedSize < item.Length && enqueuedSize < remainEnqueueSize); enqueuedSize++)             CircularBuffer.Enqueue(item[enqueuedSize]);          if ((item.Length - enqueuedSize) != 0)         {             Remove((item.Length - enqueuedSize));//remaining item size              for (; enqueuedSize < item.Length; enqueuedSize++)                 CircularBuffer.Enqueue(item[enqueuedSize]);         }                }      ///      /// Removes and Returns an item from the FIFO.     ///      ///  Item removed.      public T Remove()     {         T removedItem = CircularBuffer.Peek();         CircularBuffer.Dequeue();          return removedItem;     }      ///      /// Removes and Returns the array of items form the FIFO.     ///      ///  The size of item to be removed from the FIFO.      ///  Removed array of items      public T[] Remove(int size)     {         if (size > CircularBuffer.Count)             size = CircularBuffer.Count;          T[] removedItems = new T[size];          for (int i = 0; i < size; i++)         {             removedItems[i] = CircularBuffer.Peek();             CircularBuffer.Dequeue();         }          return removedItems;     }      ///      /// Returns the item at the beginning of the FIFO with out removing it.     ///      ///  Item Peeked.      public T Peek()     {         return CircularBuffer.Peek();     }      ///      /// Returns the array of item at the beginning of the FIFO with out removing it.     ///      ///  The size of the array items.      ///  Array of peeked items.      public T[] Peek(int size)     {         T[] arrayItems = new T[CircularBuffer.Count];         CircularBuffer.CopyTo(arrayItems, 0);          if (size > CircularBuffer.Count)             size = CircularBuffer.Count;          T[] peekedItems = new T[size];          Array.Copy(arrayItems, 0, peekedItems, 0, size);          return peekedItems;     }      ///      /// Gets the actual number of items presented in the FIFO.     ///      public int Count     {         get         {             return CircularBuffer.Count;         }     }      ///      /// Removes all the contents of the FIFO.     ///      public void Clear()     {         CircularBuffer.Clear();     }      ///      /// Resets and Initialize the instance of FIFO class that is empty and has the default initial capacity.     ///      public void Reset()     {         Dispose();         CircularBuffer = new Queue(capacity);     }      #region ICollection Members      ///      /// Determines whether an element is in the FIFO.     ///      ///  The item to locate in the FIFO.      ///      public bool Contains(T item)     {         return CircularBuffer.Contains(item);     }      ///      /// Copies the FIFO elements to an existing one-dimensional array.      ///      ///  The one-dimensional array that have at list a size of the FIFO      ///      public void CopyTo(T[] array, int arrayIndex)     {         if (array.Length >= CircularBuffer.Count)             CircularBuffer.CopyTo(array, 0);                }      public bool IsReadOnly     {         get { return false; }     }      public bool Remove(T item)     {         return false;      }      #endregion      #region IEnumerable Members      public IEnumerator GetEnumerator()     {        return CircularBuffer.GetEnumerator();     }      #endregion      #region IEnumerable Members      IEnumerator IEnumerable.GetEnumerator()     {         return CircularBuffer.GetEnumerator();     }      #endregion      #region IDisposable Members      ///      /// Releases all the resource used by the FIFO.     ///      public void Dispose()     {                   CircularBuffer.Clear();         CircularBuffer = null;         GC.Collect();     }      #endregion } 
  • 78
Reply Report