|
| 1 | +using System.Collections; |
| 2 | +using System.Collections.Generic; |
| 3 | + |
| 4 | +namespace DataStructures.Bag; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Implementation of a Bag (or multiset) data structure using a basic linked list. |
| 8 | +/// </summary> |
| 9 | +/// <remarks> |
| 10 | +/// A bag (or multiset, or mset) is a modification of the concept of a set that, unlike a set, allows for multiple instances for each of its elements. |
| 11 | +/// The number of instances given for each element is called the multiplicity of that element in the multiset. |
| 12 | +/// As a consequence, an infinite number of multisets exist that contain only elements a and b, but vary in the multiplicities of their elements. |
| 13 | +/// See https://en.wikipedia.org/wiki/Multiset for more information. |
| 14 | +/// </remarks> |
| 15 | +/// <typeparam name="T">Generic Type.</typeparam> |
| 16 | +public class Bag<T> : IEnumerable<T> where T : notnull |
| 17 | +{ |
| 18 | + private BagNode<T>? head; |
| 19 | + private int totalCount; |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Initializes a new instance of the <see cref="Bag{T}" /> class. |
| 23 | + /// </summary> |
| 24 | + public Bag() |
| 25 | + { |
| 26 | + head = null; |
| 27 | + totalCount = 0; |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Adds an item to the bag. If the item already exists, increases its multiplicity. |
| 32 | + /// </summary> |
| 33 | + public void Add(T item) |
| 34 | + { |
| 35 | + // If the bag is empty, create the first node |
| 36 | + if (head == null) |
| 37 | + { |
| 38 | + head = new BagNode<T>(item); |
| 39 | + totalCount = 1; |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + // Check if item already exists |
| 44 | + var current = head; |
| 45 | + BagNode<T>? previous = null; |
| 46 | + |
| 47 | + while (current != null) |
| 48 | + { |
| 49 | + if (EqualityComparer<T>.Default.Equals(current.Item, item)) |
| 50 | + { |
| 51 | + current.Multiplicity++; |
| 52 | + totalCount++; |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + previous = current; |
| 57 | + current = current.Next; |
| 58 | + } |
| 59 | + |
| 60 | + previous!.Next = new BagNode<T>(item); |
| 61 | + totalCount++; |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Clears the bag. |
| 66 | + /// </summary> |
| 67 | + public void Clear() |
| 68 | + { |
| 69 | + head = null; |
| 70 | + totalCount = 0; |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Gets the number of items in the bag. |
| 75 | + /// </summary> |
| 76 | + public int Count => totalCount; |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Returns a boolean indicating whether the bag is empty. |
| 80 | + /// </summary> |
| 81 | + public bool IsEmpty() => head == null; |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Returns an enumerator that iterates through the bag. |
| 85 | + /// </summary> |
| 86 | + public IEnumerator<T> GetEnumerator() |
| 87 | + { |
| 88 | + var current = head; |
| 89 | + |
| 90 | + while (current != null) |
| 91 | + { |
| 92 | + // Yield the item as many times as its multiplicity, pretending they are separate items |
| 93 | + for (var i = 0; i < current.Multiplicity; i++) |
| 94 | + { |
| 95 | + yield return current.Item; |
| 96 | + } |
| 97 | + |
| 98 | + current = current.Next; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Returns an enumerator that iterates through the bag. |
| 104 | + /// </summary> |
| 105 | + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| 106 | +} |
0 commit comments