/// <summary> /// Internal method for the subtraction operation. /// </summary> /// <param name="bag">The bag to subtract from this bag.</param> /// <returns>The result of the subtract operation.</returns> private Bag <T> SubtractInternal(IBag <T> bag) { Guard.ArgumentNotNull(bag, "bag"); var resultBag = new Bag <T>(data); using (var enumerator = bag.GetCountEnumerator()) { while (enumerator.MoveNext()) { var item = enumerator.Current; int itemCount; if (resultBag.data.TryGetValue(item.Key, out itemCount)) { if (itemCount - item.Value <= 0) { resultBag.RemoveAll(item.Key); } else { resultBag.Remove(item.Key, item.Value); } } } } return(resultBag); }
/// <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); }
/// <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); }