-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add bag datastructure #531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
siriak
merged 5 commits into
TheAlgorithms:master
from
kamikkels:feature/add-bag-datastructure
Oct 2, 2025
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b09935a
Add a basic Bag data structure
kamikkels c720f4d
Move bag to implement it's own linked list base
kamikkels e131505
Remove redundant totalcount
kamikkels c6d0a8b
Add test for generic IEnumerable.GetEnumerator()
kamikkels bb1abc5
Merge branch 'master' into feature/add-bag-datastructure
siriak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| using System.Linq; | ||
| using DataStructures.Bag; | ||
| using FluentAssertions; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace DataStructures.Tests; | ||
|
|
||
| internal class BagTests | ||
| { | ||
| [Test] | ||
| public void Add_ShouldIncreaseCount() | ||
| { | ||
| // Arrange & Act | ||
| var bag = new Bag<int> | ||
| { | ||
| 1, | ||
| 2, | ||
| 1 | ||
| }; | ||
|
|
||
| // Assert | ||
| bag.Count.Should().Be(3); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Add_ShouldHandleDuplicates() | ||
| { | ||
| // Arrange & Act | ||
| var bag = new Bag<string> | ||
| { | ||
| "apple", | ||
| "apple" | ||
| }; | ||
|
|
||
| // Assert | ||
| bag.Count.Should().Be(2); | ||
| bag.Should().Contain("apple"); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Clear_ShouldEmptyTheBag() | ||
| { | ||
| // Arrange | ||
| var bag = new Bag<int> | ||
| { | ||
| 1, | ||
| 2 | ||
| }; | ||
|
|
||
| // Act | ||
| bag.Clear(); | ||
|
|
||
| // Assert | ||
| bag.IsEmpty().Should().BeTrue(); | ||
| bag.Count.Should().Be(0); | ||
| } | ||
|
|
||
| [Test] | ||
| public void IsEmpty_ShouldReturnTrueForEmptyBag() | ||
| { | ||
| // Arrange | ||
| var bag = new Bag<int>(); | ||
|
|
||
| // Act & Assert | ||
| bag.IsEmpty().Should().BeTrue(); | ||
| } | ||
|
|
||
| [Test] | ||
| public void IsEmpty_ShouldReturnFalseForNonEmptyBag() | ||
| { | ||
| // Arrange | ||
| var bag = new Bag<int> | ||
| { | ||
| 1 | ||
| }; | ||
|
|
||
| // Act & Assert | ||
| bag.IsEmpty().Should().BeFalse(); | ||
| } | ||
|
|
||
| [Test] | ||
| public void GetEnumerator_ShouldIterateAllItems() | ||
| { | ||
| // Arrange | ||
| var bag = new Bag<int> | ||
| { | ||
| 1, | ||
| 2, | ||
| 1 | ||
| }; | ||
|
|
||
| // Act | ||
| var items = bag.ToList(); | ||
|
|
||
| // Assert | ||
| items.Count.Should().Be(3); | ||
| items.Should().Contain(1); | ||
| items.Should().Contain(2); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Count_ShouldReturnZeroForEmptyBag() | ||
| { | ||
| // Arrange | ||
| var bag = new Bag<int>(); | ||
|
|
||
| // Act & Assert | ||
| bag.Count.Should().Be(0); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Count_ShouldReturnCorrectCount() | ||
| { | ||
| // Arrange | ||
| var bag = new Bag<int> | ||
| { | ||
| 1, | ||
| 2, | ||
| 1 | ||
| }; | ||
|
|
||
| // Act & Assert | ||
| bag.Count.Should().Be(3); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| 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; | ||
| } | ||
|
|
||
| // Item not found, add it | ||
| if (previous != null) | ||
| { | ||
| 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> | ||
| /// Gets the total number of items in the bag, including all multiplicities. | ||
| /// </summary> | ||
| public int TotalCount => totalCount; | ||
|
|
||
kamikkels marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// <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() | ||
| { | ||
| return GetEnumerator(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| namespace DataStructures.Bag; | ||
|
|
||
| /// <summary> | ||
| /// Generic node class for Bag. | ||
| /// </summary> | ||
| /// <typeparam name="T">A type for node.</typeparam> | ||
| public class BagNode<T>(T item) | ||
| { | ||
| public T Item { get; } = item; | ||
|
|
||
| public int Multiplicity { get; set; } = 1; | ||
|
|
||
| public BagNode<T>? Next { get; set; } = null; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.