Example #1
0
        /// <summary>
        /// Try to get a first and single occurrency value from all <see cref="ConcurrentDictionary{TKey, TValue}.Values"/> entries.
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="collection"></param>
        /// <param name="predicate"></param>
        /// <param name="value"></param>
        /// <exception cref="InvalidOperationException"></exception>
        /// <returns></returns>
        public static bool TryGetValueSingle <TKey, TValue>(this ConcurrentDictionary <TKey, TValue> collection, Predicate <TKey> predicate, out TValue value)
        {
            var result = collection.EntryWhereAsParallel(entry => predicate.Invoke(entry.Key));

            if (result == null)
            {
                value = default;
                return(false);
            }

            value = result[0].Value;
            return(true);
        }
Example #2
0
        /// <summary>
        /// Try to get all occurrency values from all <see cref="ConcurrentDictionary{TKey, TValue}.Keys"/> entries.
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="collection"></param>
        /// <param name="predicate"></param>
        /// <exception cref="InvalidOperationException"></exception>
        /// <returns></returns>
        public static TKey[] KeyFromValueWhereAsParallel <TKey, TValue>(this ConcurrentDictionary <TKey, TValue> collection, Predicate <TValue> predicate)
        {
            if (collection == null)
            {
                throw new InvalidOperationException("Collection must be not null.");
            }

            var result = collection.EntryWhereAsParallel(entry => predicate.Invoke(entry.Value));

            if (result == null)
            {
                return(Array.Empty <TKey>());
            }

            return(result.Select(entry => entry.Key).ToArray());
        }
Example #3
0
        /// <summary>
        /// Try to get a first and single occurrency key from all <see cref="ConcurrentDictionary{TKey, TValue}.Keys"/> entries.
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="collection"></param>
        /// <param name="predicate"></param>
        /// <param name="key"></param>
        /// <exception cref="InvalidOperationException"></exception>
        /// <returns></returns>
        public static bool TryGetKeySingle <TKey, TValue>(this ConcurrentDictionary <TKey, TValue> collection, Predicate <TValue> predicate, out TKey key)
        {
            if (collection == null)
            {
                throw new InvalidOperationException("Collection must be not null.");
            }

            var result = collection.EntryWhereAsParallel(entry => predicate.Invoke(entry.Value));

            if (result == null)
            {
                key = default;
                return(false);
            }

            key = result[0].Key;
            return(true);
        }