private void InitializePriorityQueue(IList <IEnumerable <T> > inputList)
        {
            foreach (var list in inputList)
            {
                ComparableSortedSequenceEnumerator <T> enumerator = new ComparableSortedSequenceEnumerator <T>(list, this.keyComparer);
                bool enumeratorIsNotEmpty = this.PrepareEnumerator(enumerator);
                if (enumeratorIsNotEmpty == false)
                {
                    // Since enumerator does not have any items that fit the filter, discard the enumerator.
                    enumerator.Dispose();
                    continue;
                }

                this.InsertToPriortyQueueIfNecessary(enumerator);
            }
        }
        private bool InsertToPriortyQueueIfNecessary(ComparableSortedSequenceEnumerator <T> enumerator)
        {
            Diagnostics.Assert(enumerator != null, ClassName, "Input enumerator must not be null.");

            while (true)
            {
                bool wasPushed = this.priorityQueue.TryPush(enumerator);
                if (wasPushed)
                {
                    return(true);
                }

                var isNotEmpty = enumerator.MoveNext();
                if (isNotEmpty == false)
                {
                    enumerator.Dispose();
                    return(false);
                }
            }
        }
        /// <summary>
        /// Prepares the enumerator to be added to the priority queue.
        /// Preparation includes moving the enumerator until the current key obides filters.
        /// </summary>
        /// <param name="enumerator">The enumerator.</param>
        /// <returns>Boolean that indicates that the enumerator is now empty.</returns>
        private bool PrepareEnumerator(ComparableSortedSequenceEnumerator <T> enumerator)
        {
            while (true)
            {
                bool itemExists = enumerator.MoveNext();
                if (itemExists == false)
                {
                    return(false);
                }

                if (this.useFirstKey)
                {
                    int firstKeyComparison = this.keyComparer.Compare(this.firstKey, enumerator.Current);

                    // If firstKey for the filter is greater than the current, continue moving the enumerator.
                    if (firstKeyComparison > 0)
                    {
                        continue;
                    }
                }

                if (this.useLastKey)
                {
                    int lastKeyComparison = this.keyComparer.Compare(this.lastKey, enumerator.Current);

                    // If lastKey for the filter is less than the current, indicate that the enumerator is practically empty
                    if (lastKeyComparison < 0)
                    {
                        return(false);
                    }
                }

                // If current item does not fit the filter, go to next.
                if (this.keyFilter != null && this.keyFilter.Invoke(enumerator.Current) == false)
                {
                    continue;
                }

                return(true);
            }
        }
 public int CompareTo(ComparableSortedSequenceEnumerator <T> other)
 {
     return(this.keyComparer.Compare(this.Current, other.Current));
 }