// 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])); }
/// <summary> /// Raises the DidEnqueue event. /// </summary> /// <param name="enqueuedItem">Enqueued item.</param> /// <param name="previousHeadOfQueue">Previous head of queue.</param> protected virtual void OnDidEnqueue(T enqueuedItem, T previousHeadOfQueue) { DidEnqueue?.Invoke(this, new DidEnqueueEventArgs <T>(enqueuedItem, previousHeadOfQueue)); }