예제 #1
0
        // Adds obj to the tail of the queue. And possibly reorders the queue
        //
        public virtual void Enqueue(T obj)
        {
            WillEnqueue?.Invoke(this, new WillEnqueueEventArgs <T>(_array[_tail], obj));

            if (_size == _array.Length)
            {
                int newcapacity = (int)((long)_array.Length * (long)_growFactor / 100);
                if (newcapacity < _array.Length + _MinimumGrow)
                {
                    newcapacity = _array.Length + _MinimumGrow;
                }
                SetCapacity(newcapacity);
            }

            DateTime?lastDateTime    = null;
            DateTime?currentDateTime = null;

            if (_size > 0)
            {
                lastDateTime = _dateTimeGetter.Invoke(_array[_tail - 1]);
            }

            currentDateTime = _dateTimeGetter.Invoke(obj);

            // ToDo: Make this behaviour configurable
            if (_array.Any(q => q != null && _dateTimeGetter.Invoke(q) == currentDateTime.GetValueOrDefault()))
            {
                return; // Silently ignore double datetimestamps.
            }
            _array[_tail] = obj;

            if (currentDateTime < lastDateTime)
            {
                // Add to the list and reshuffle.
                List <T> list = new List <T>();
                foreach (var item in _array)
                {
                    list.Add(item);
                }

                _array = list.OrderBy(q => _dateTimeGetter.Invoke(q)).ToArray();
            }

            _tail = (_tail + 1) % _array.Length;
            _size++;
            _version++;

            DidEnqueue?.Invoke(this, new DidEnqueueEventArgs <T>(_array[_tail], _array[_tail - 1]));
        }
예제 #2
0
 /// <summary>
 /// Raises the WillEnqueue event.
 /// </summary>
 /// <param name="currentHeadOfQueue">Current head of queue.</param>
 /// <param name="itemToEnqueue">Item to enqueue.</param>
 protected virtual void OnWillEnqueue(T currentHeadOfQueue, T itemToEnqueue)
 {
     WillEnqueue?.Invoke(this, new WillEnqueueEventArgs <T>(currentHeadOfQueue, itemToEnqueue));
 }