public ConcurrentHashSet(IEnumerable <T> collection, IEqualityComparer <T> comparer)
        {
            collection.ThrowIfNull(nameof(collection));
            comparer.ThrowIfNull(nameof(comparer));

            _set = new HashSet <T>(collection, comparer);
        }
    /// <summary>
    /// Returns a future to indicate whether the specified value
    /// is yielded by the data-source.
    /// </summary>
    /// <typeparam name="TSource">The type of data to be yielded</typeparam>
    /// <param name="source">The data-source</param>
    /// <param name="value">The value to detect from the data-source</param>
    /// <param name="comparer">The comparer to use to determine equality</param>
    public static IFuture <bool> Contains <TSource>(this IDataProducer <TSource> source, TSource value, IEqualityComparer <TSource> comparer)
    {
        source.ThrowIfNull("source");
        comparer.ThrowIfNull("comparer");

        return(source.Any(element => comparer.Equals(value, element)));
    }
        public ConcurrentHashSet(int capacity, IEqualityComparer <T> comparer)
        {
            capacity.ThrowIfValueIsOutOfRange(nameof(capacity), 0, int.MaxValue);
            comparer.ThrowIfNull(nameof(comparer));

            _set = new HashSet <T>(capacity, comparer);
        }
        public TimeBasedConcurrentDictionary(TimeSpan lifeTime, IEqualityComparer <TKey> comparer)
        {
            comparer.ThrowIfNull(nameof(comparer));

            _lifeTime   = lifeTime;
            _dictionary = new ConcurrentDictionary <TKey, TValue>(comparer);
        }
        /// <summary>
        /// Converts a given sequence into a <see cref="SynchronisedHashSet{T}"/> using a specified comparer.
        /// </summary>
        /// <typeparam name="T">The type of elements in the sequence.</typeparam>
        /// <param name="source">The source to convert.</param>
        /// <param name="comparer">The comparer.</param>
        /// <returns>
        /// A hash-set containing the unique values from the original source.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="comparer"/> parameter was null.</exception>
        public static SynchronisedHashSet <T> ToSynchronisedHashSet <T>(this IEnumerable <T> source, IEqualityComparer <T> comparer)
        {
            source.ThrowIfNull(nameof(source),
                               $"Unable to convert to HashSet. {nameof(source)} parameter cannot be null");
            comparer.ThrowIfNull(nameof(comparer),
                                 $"Unable to convert to HashSet. {nameof(comparer)} parameter cannot be null");

            return(new SynchronisedHashSet <T>(source, comparer));
        }
        public TimeBasedConcurrentDictionary(TimeSpan lifeTime, int concurrencyLevel, int capacity,
                                             IEqualityComparer <TKey> comparer)
        {
            concurrencyLevel.ThrowIfValueIsOutOfRange(nameof(concurrencyLevel), 1, int.MaxValue);
            capacity.ThrowIfValueIsOutOfRange(nameof(capacity), 0, int.MaxValue);
            comparer.ThrowIfNull(nameof(comparer));

            _lifeTime   = lifeTime;
            _dictionary = new ConcurrentDictionary <TKey, TValue>(concurrencyLevel, capacity, comparer);
        }
        public TimeBasedConcurrentDictionary(TimeSpan lifeTime, int concurrencyLevel,
                                             IEnumerable <KeyValuePair <TKey, TValue> > collection, IEqualityComparer <TKey> comparer)
        {
            concurrencyLevel.ThrowIfValueIsOutOfRange(nameof(concurrencyLevel), 1, int.MaxValue);
            collection.ThrowIfNull(nameof(collection));
            comparer.ThrowIfNull(nameof(comparer));

            _lifeTime   = lifeTime;
            _dictionary = new ConcurrentDictionary <TKey, TValue>(concurrencyLevel, collection, comparer);
        }
        /// <summary>
        /// Converts an IDataProducer into a dictionary.
        /// </summary>
        /// <param name="elementSelector">A transform function to produce a result element value from each element.</param>
        /// <param name="keySelector">A function to extract a key from each element.</param>
        /// <param name="keyComparer">Used to compare keys.</param>
        /// <param name="source">The data source.</param>
        /// <remarks>This will force all values to be buffered</remarks>
        public static IDictionary <TKey, TElement> ToDictionary <TSource, TKey, TElement>(
            this IDataProducer <TSource> source,
            Func <TSource, TKey> keySelector, Func <TSource, TElement> elementSelector, IEqualityComparer <TKey> keyComparer)
        {
            source.ThrowIfNull("source");
            keySelector.ThrowIfNull("keySelector");
            elementSelector.ThrowIfNull("elementSelector");
            keyComparer.ThrowIfNull("keyComparer");

            Dictionary <TKey, TElement> dict = new Dictionary <TKey, TElement>(keyComparer);

            source.DataProduced += t => dict.Add(keySelector(t), elementSelector(t));
            return(dict);
        }
        /// <summary>
        /// Converts an IDataProducer into a lookup.
        /// </summary>
        /// <param name="elementSelector">A transform function to produce a result element value from each element.</param>
        /// <param name="keySelector">A function to extract a key from each element.</param>
        /// <param name="keyComparer">Used to compare keys.</param>
        /// <param name="source">The data source.</param>
        /// <remarks>This will force all values to be buffered</remarks>
        public static ILookup <TKey, TElement> ToLookup <TSource, TKey, TElement>(
            this IDataProducer <TSource> source,
            Func <TSource, TKey> keySelector, Func <TSource, TElement> elementSelector, IEqualityComparer <TKey> keyComparer)
        {
            source.ThrowIfNull("source");
            keySelector.ThrowIfNull("keySelector");
            elementSelector.ThrowIfNull("elementSelector");
            keyComparer.ThrowIfNull("keyComparer");

            EditableLookup <TKey, TElement> lookup = new EditableLookup <TKey, TElement>(keyComparer);

            source.DataProduced += t => lookup.Add(keySelector(t), elementSelector(t));
            source.EndOfData    += () => lookup.TrimExcess();
            return(lookup);
        }
        /// <summary>
        /// Returns a data-producer that yields the first instance of each unique
        /// value in the sequence; subsequent identical values are ignored.
        /// </summary>
        /// <param name="source">The data-producer</param>
        /// <param name="comparer">Used to determine equaility between values</param>
        /// <remarks>This will force the first instance of each unique value to be buffered</remarks>
        public static IDataProducer <TSource> Distinct <TSource>(this IDataProducer <TSource> source, IEqualityComparer <TSource> comparer)
        {
            source.ThrowIfNull("source");
            comparer.ThrowIfNull("comparer");

            DataProducer <TSource> ret = new DataProducer <TSource>();

            HashSet <TSource> set = new HashSet <TSource>(comparer);

            source.DataProduced += value =>
            {
                if (set.Add(value))
                {
                    ret.Produce(value);
                }
            };
            source.EndOfData += () => ret.End();
            return(ret);
        }
        /// <summary>
        /// Groups the elements of a sequence according to a specified key selector function
        /// and creates a result value from each group and its key. The elements of each
        /// group are projected by using a specified function.
        /// </summary>
        /// <typeparam name="TElement">The return-type of the transform used to process the
        /// values within each grouping.</typeparam>
        /// <typeparam name="TKey">The return-type of the transform used to group the sequence</typeparam>
        /// <typeparam name="TResult">The final values to be yielded after processing</typeparam>
        /// <typeparam name="TSource">The values to be yielded by the original data-source</typeparam>
        /// <param name="comparer">Used to compare grouping keys</param>
        /// <param name="elementSelector">A function to map each source element to an element in the appropriate group</param>
        /// <param name="keySelector">A function to extract the key for each element in hte original sequence.</param>
        /// <param name="resultSelector">A function to create a result value from each group.</param>
        /// <param name="source">The data-source to be grouped</param>
        /// <remarks>This will force each unique grouping key to
        /// be buffered, but not the data itself</remarks>
        public static IDataProducer <TResult> GroupBy <TSource, TKey, TElement, TResult>
            (this IDataProducer <TSource> source,
            Func <TSource, TKey> keySelector,
            Func <TSource, TElement> elementSelector,
            Func <TKey, IDataProducer <TElement>, TResult> resultSelector,
            IEqualityComparer <TKey> comparer)
        {
            source.ThrowIfNull("source");
            keySelector.ThrowIfNull("keySelector");
            elementSelector.ThrowIfNull("elementSelector");
            resultSelector.ThrowIfNull("resultSelector");
            comparer.ThrowIfNull("comparer");

            DataProducer <TResult> ret = new DataProducer <TResult>();

            Dictionary <TKey, DataProducer <TElement> > dictionary = new Dictionary <TKey, DataProducer <TElement> >(comparer);

            source.DataProduced += value =>
            {
                TKey key = keySelector(value);

                DataProducer <TElement> subProducer;

                if (!dictionary.TryGetValue(key, out subProducer))
                {
                    subProducer     = new DataProducer <TElement>();
                    dictionary[key] = subProducer;
                    ret.Produce(resultSelector(key, subProducer));
                }
                subProducer.Produce(elementSelector(value));
            };

            source.EndOfData += () =>
            {
                foreach (DataProducer <TElement> value in dictionary.Values)
                {
                    value.End();
                }
                ret.End();
            };

            return(ret);
        }
Example #12
0
        public ConcurrentHashSet(IEqualityComparer <T> comparer)
        {
            comparer.ThrowIfNull(nameof(comparer));

            _set = new HashSet <T>(comparer);
        }
Example #13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ComponentRuleParameters"/> class.
 /// </summary>
 /// <param name="factory">The variable factory.</param>
 /// <param name="comparer">The comparer for comparing variable names.</param>
 public ComponentRuleParameters(IVariableFactory <IVariable> factory, IEqualityComparer <string> comparer)
 {
     Factory  = factory.ThrowIfNull(nameof(factory));
     Comparer = comparer.ThrowIfNull(nameof(comparer));
 }