Exemplo n.º 1
0
        /// <summary>
        /// Evaluate a predicate Expression and determine whether a key range can
        /// be found that completely satisfies the expression. If this method returns
        /// true then the key range returned by <see cref="GetKeyRange"/> will return
        /// only records which match the expression.
        /// </summary>
        /// <param name="expression">The expression to evaluate.</param>
        /// <returns>
        /// True if the key range returned by <see cref="GetKeyRange"/> will perfectly
        /// match all records found by the expression.
        /// </returns>
        public static bool KeyRangeIsExact(Expression <Predicate <KeyValuePair <TKey, TValue> > > expression)
        {
            if (null == expression)
            {
                throw new ArgumentNullException("expression");
            }

            return(KeyExpressionEvaluator <TKey> .KeyRangeIsExact(expression.Body, KeyMemberInfo));
        }
        /// <summary>
        /// Optimize a where statement which uses this collection.
        /// </summary>
        /// <param name="expression">
        /// The predicate determining which items should be enumerated.
        /// </param>
        /// <returns>
        /// An enumerator matching only the records matched by the predicate.
        /// </returns>
        public PersistentDictionaryLinqKeyEnumerable <TKey, TValue> Where(Expression <Predicate <TKey> > expression)
        {
            if (null == expression)
            {
                throw new ArgumentNullException("expression");
            }

            Predicate <TKey> predicate = KeyExpressionEvaluator <TKey> .KeyRangeIsExact(expression) ? x => true : expression.Compile();

            return(new PersistentDictionaryLinqKeyEnumerable <TKey, TValue>(
                       this.Dictionary,
                       expression,
                       predicate,
                       false));
        }
        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
        /// </returns>
        public IEnumerator <TKey> GetEnumerator()
        {
            // Consider: we could get the enumeration of key ranges, sort them and union overlapping ranges.
            // Enumerating the data as several different ranges would be more efficient when the expression
            // specifies an OR and the ranges are highly disjoint.
            KeyRange <TKey> range = KeyExpressionEvaluator <TKey> .GetKeyRange(this.expression);

            this.dictionary.TraceWhere(range, this.isReversed);
            if (this.isReversed)
            {
                return(new PersistentDictionaryReverseEnumerator <TKey, TValue, TKey>(
                           this.dictionary, range, c => c.RetrieveCurrentKey(), this.predicate));
            }

            return(new PersistentDictionaryEnumerator <TKey, TValue, TKey>(
                       this.dictionary, range, c => c.RetrieveCurrentKey(), this.predicate));
        }