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