public void RemoveAllCopies() { hashbag.Add(5); hashbag.Add(7); hashbag.Add(5); Assert.AreEqual(2, hashbag.ContainsCount(5)); Assert.AreEqual(1, hashbag.ContainsCount(7)); hashbag.RemoveAllCopies(5); Assert.AreEqual(0, hashbag.ContainsCount(5)); Assert.AreEqual(1, hashbag.ContainsCount(7)); hashbag.Add(5); hashbag.Add(8); hashbag.Add(5); hashbag.RemoveAllCopies(8); Assert.IsTrue(IC.eq(hashbag, 7, 5, 5)); }
public static void UnindexedCollectionEvents() { ICollection <int> coll = new ArrayList <int>(); ICollection <int> bag1 = new HashBag <int>(); bag1.AddAll(new[] { 3, 2, 5, 5, 7, 7, 5, 3, 7, 7 }); // Add change handler coll.CollectionChanged += o => Console.WriteLine("Collection changed"); // Add cleared handler coll.CollectionCleared += (o, a) => Console.WriteLine("Collection cleared"); // Add added handler coll.ItemsAdded += (o, args) => Console.WriteLine($"Item {args.Item} added"); // Add item count handler AddItemsAddedCounter(coll); AddItemsRemovedCounter(coll); coll.AddAll(bag1); coll.RemoveAll(new[] { 2, 5, 6, 3, 7, 2 }); coll.Clear(); ICollection <int> bag2 = new HashBag <int>(); // Add added handler with multiplicity bag2.ItemsAdded += (o, args) => Console.WriteLine($"{args.Count} copies of {args.Item} added"); bag2.AddAll(bag1); // Add removed handler with multiplicity bag2.ItemsRemoved += (o, args) => Console.WriteLine($"{args.Count} copies of {args.Item} removed"); bag2.RemoveAllCopies(7); }
public static void UnindexedCollectionEvents() { ICollection <int> coll = new ArrayList <int>(); ICollection <int> bag1 = new HashBag <int>(); bag1.AddAll(new[] { 3, 2, 5, 5, 7, 7, 5, 3, 7, 7 }); // Add change handler coll.CollectionChanged += delegate(Object c) { Console.WriteLine("Collection changed"); }; // Add cleared handler coll.CollectionCleared += delegate(Object c, ClearedEventArgs args) { Console.WriteLine("Collection cleared"); }; // Add added handler coll.ItemsAdded += delegate(Object c, ItemCountEventArgs <int> args) { Console.WriteLine("Item {0} added", args.Item); }; // Add item count handler AddItemsAddedCounter(coll); AddItemsRemovedCounter(coll); coll.AddAll(bag1); coll.RemoveAll(new[] { 2, 5, 6, 3, 7, 2 }); coll.Clear(); ICollection <int> bag2 = new HashBag <int>(); // Add added handler with multiplicity bag2.ItemsAdded += delegate(Object c, ItemCountEventArgs <int> args) { Console.WriteLine("{0} copies of {1} added", args.Count, args.Item); }; bag2.AddAll(bag1); // Add removed handler with multiplicity bag2.ItemsRemoved += delegate(Object c, ItemCountEventArgs <int> args) { Console.WriteLine("{0} copies of {1} removed", args.Count, args.Item); }; bag2.RemoveAllCopies(7); }