Example #1
0
        /// <summary>
        /// Creates a new instance of <see cref="SeriesQueue{T}"/>.
        /// </summary>
        /// <param name="items">The list of binary-tree ordered items usually retrieved as a result of <see cref="ToList"/> method.</param>
        /// <param name="nextOrdinal">Optionally provides the next ordinal the queue can dequeue.</param>
        /// <exception cref="InvalidOperationException">The <see cref="ISeriesItem{T}.Ordinal"/> of the element at head of the queue cannot be ahead of <paramref name="nextOrdinal"/> of the queue.</exception>
        public SeriesQueue(List <T> items = default, int?nextOrdinal = null)
        {
            priorityQueue = PriorityQueue <T> .Load(items ?? new List <T>());

            var queueHead = priorityQueue.Count == 0 ? 0 : priorityQueue.Peek().Ordinal;

            if (nextOrdinal.HasValue)
            {
                if (queueHead > nextOrdinal.Value)
                {
                    throw new InvalidOperationException($"The ordinal of the element at head of the queue '{queueHead}' cannot be ahead of next ordinal '{nextOrdinal}' of the queue.");
                }

                queueHead = nextOrdinal.Value;
            }

            NextOrdinal = queueHead;
        }