Skip to content

Commit e59a3b1

Browse files
authored
Add G-Set (Grow-only Set) (TheAlgorithms#4975)
1 parent 3001620 commit e59a3b1

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
* [MRUCache](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/caches/MRUCache.java)
8585
* crdt
8686
* [GCounter](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/crdt/GCounter.java)
87+
* [GSet](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/crdt/GSet.java)
8788
* [PNCounter](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/crdt/PNCounter.java)
8889
* disjointsetunion
8990
* [DisjointSetUnion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/disjointsetunion/DisjointSetUnion.java)
@@ -618,6 +619,7 @@
618619
* [MRUCacheTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/caches/MRUCacheTest.java)
619620
* crdt
620621
* [GCounterTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/crdt/GCounterTest.java)
622+
* [GSetTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/crdt/GSetTest.java)
621623
* [PNCounterTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/crdt/PNCounterTest.java)
622624
* disjointsetunion
623625
* [DisjointSetUnionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/disjointsetunion/DisjointSetUnionTest.java)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.thealgorithms.datastructures.crdt;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
/**
7+
* GSet (Grow-only Set) is a state-based CRDT (Conflict-free Replicated Data Type)
8+
* that allows only the addition of elements and ensures that once an element is added,
9+
* it cannot be removed. The merge operation of two G-Sets is their union.
10+
* This implementation supports adding elements, looking up elements, comparing with other G-Sets,
11+
* and merging with another G-Set to create a new G-Set containing all unique elements from both sets.
12+
* (https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type)
13+
*
14+
* @author itakurah (Niklas Hoefflin) (https://github.com/itakurah)
15+
*/
16+
17+
public class GSet<T> {
18+
private final Set<T> elements;
19+
20+
/**
21+
* Constructs an empty G-Set.
22+
*/
23+
public GSet() {
24+
this.elements = new HashSet<>();
25+
}
26+
27+
/**
28+
* Adds an element to the G-Set.
29+
*
30+
* @param e the element to be added
31+
*/
32+
public void addElement(T e) {
33+
elements.add(e);
34+
}
35+
36+
/**
37+
* Checks if the given element is present in the G-Set.
38+
*
39+
* @param e the element to be checked
40+
* @return true if the element is present, false otherwise
41+
*/
42+
public boolean lookup(T e) {
43+
return elements.contains(e);
44+
}
45+
46+
/**
47+
* Compares the G-Set with another G-Set to check if it is a subset.
48+
*
49+
* @param other the other G-Set to compare with
50+
* @return true if the current G-Set is a subset of the other, false otherwise
51+
*/
52+
public boolean compare(GSet<T> other) {
53+
return elements.containsAll(other.elements);
54+
}
55+
56+
/**
57+
* Merges the current G-Set with another G-Set, creating a new G-Set
58+
* containing all unique elements from both sets.
59+
*
60+
* @param other the G-Set to merge with
61+
*/
62+
public void merge(GSet<T> other) {
63+
elements.addAll(other.elements);
64+
}
65+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.thealgorithms.datastructures.crdt;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class GSetTest {
8+
9+
@Test
10+
void testAddElement() {
11+
GSet<String> gSet = new GSet<>();
12+
gSet.addElement("apple");
13+
gSet.addElement("orange");
14+
15+
assertTrue(gSet.lookup("apple"));
16+
assertTrue(gSet.lookup("orange"));
17+
assertFalse(gSet.lookup("banana"));
18+
}
19+
20+
@Test
21+
void testLookup() {
22+
GSet<Integer> gSet = new GSet<>();
23+
gSet.addElement(1);
24+
gSet.addElement(2);
25+
26+
assertTrue(gSet.lookup(1));
27+
assertTrue(gSet.lookup(2));
28+
assertFalse(gSet.lookup(3));
29+
}
30+
31+
@Test
32+
void testCompare() {
33+
GSet<String> gSet1 = new GSet<>();
34+
GSet<String> gSet2 = new GSet<>();
35+
36+
gSet1.addElement("apple");
37+
gSet1.addElement("orange");
38+
39+
gSet2.addElement("orange");
40+
gSet2.addElement("banana");
41+
42+
assertFalse(gSet1.compare(gSet2));
43+
44+
GSet<String> gSet3 = new GSet<>();
45+
gSet3.addElement("apple");
46+
gSet3.addElement("orange");
47+
48+
assertTrue(gSet1.compare(gSet3));
49+
}
50+
51+
@Test
52+
void testMerge() {
53+
GSet<String> gSet1 = new GSet<>();
54+
GSet<String> gSet2 = new GSet<>();
55+
56+
gSet1.addElement("apple");
57+
gSet1.addElement("orange");
58+
59+
gSet2.addElement("orange");
60+
gSet2.addElement("banana");
61+
62+
GSet<String> mergedSet = new GSet<>();
63+
mergedSet.merge(gSet1);
64+
mergedSet.merge(gSet2);
65+
66+
assertTrue(mergedSet.lookup("apple"));
67+
assertTrue(mergedSet.lookup("orange"));
68+
assertTrue(mergedSet.lookup("banana"));
69+
assertFalse(mergedSet.lookup("grape"));
70+
}
71+
}

0 commit comments

Comments
 (0)