Пример #1
0
        /// <summary>
        /// Internal method for the Intersection operation.
        /// </summary>
        /// <param name="bag">The bag to perform the intersection on.</param>
        /// <returns>The result of the intersection.</returns>
        private Bag <T> IntersectionInternal(IBag <T> bag)
        {
            Guard.ArgumentNotNull(bag, "bag");


            var resultBag = new Bag <T>();

            using (var enumerator = bag.GetCountEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    var item = enumerator.Current;

                    int itemCount;

                    if (data.TryGetValue(item.Key, out itemCount))
                    {
                        resultBag.Add(item.Key,
                                      Math.Min(item.Value, itemCount)
                                      );
                    }
                }
            }

            return(resultBag);
        }
Пример #2
0
        /// <summary>
        /// Internal method for the Union operation.
        /// </summary>
        /// <param name="bag">The bag to perform the union with.</param>
        /// <returns>The result of the union operation.</returns>
        private Bag <T> UnionInternal(IBag <T> bag)
        {
            Guard.ArgumentNotNull(bag, "bag");

            var resultBag = new Bag <T>();

            foreach (var item in data)
            {
                resultBag.Add(item.Key, item.Value);
            }

            using (var enumerator = bag.GetCountEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    var item = enumerator.Current;

                    resultBag.Add(item.Key, item.Value);
                }
            }

            return(resultBag);
        }