mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 02:04:31 +08:00
Add G-Set (Grow-only Set) (#4975)
This commit is contained in:
@ -0,0 +1,65 @@
|
||||
package com.thealgorithms.datastructures.crdt;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* GSet (Grow-only Set) is a state-based CRDT (Conflict-free Replicated Data Type)
|
||||
* that allows only the addition of elements and ensures that once an element is added,
|
||||
* it cannot be removed. The merge operation of two G-Sets is their union.
|
||||
* This implementation supports adding elements, looking up elements, comparing with other G-Sets,
|
||||
* and merging with another G-Set to create a new G-Set containing all unique elements from both sets.
|
||||
* (https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type)
|
||||
*
|
||||
* @author itakurah (Niklas Hoefflin) (https://github.com/itakurah)
|
||||
*/
|
||||
|
||||
public class GSet<T> {
|
||||
private final Set<T> elements;
|
||||
|
||||
/**
|
||||
* Constructs an empty G-Set.
|
||||
*/
|
||||
public GSet() {
|
||||
this.elements = new HashSet<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an element to the G-Set.
|
||||
*
|
||||
* @param e the element to be added
|
||||
*/
|
||||
public void addElement(T e) {
|
||||
elements.add(e);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given element is present in the G-Set.
|
||||
*
|
||||
* @param e the element to be checked
|
||||
* @return true if the element is present, false otherwise
|
||||
*/
|
||||
public boolean lookup(T e) {
|
||||
return elements.contains(e);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares the G-Set with another G-Set to check if it is a subset.
|
||||
*
|
||||
* @param other the other G-Set to compare with
|
||||
* @return true if the current G-Set is a subset of the other, false otherwise
|
||||
*/
|
||||
public boolean compare(GSet<T> other) {
|
||||
return elements.containsAll(other.elements);
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges the current G-Set with another G-Set, creating a new G-Set
|
||||
* containing all unique elements from both sets.
|
||||
*
|
||||
* @param other the G-Set to merge with
|
||||
*/
|
||||
public void merge(GSet<T> other) {
|
||||
elements.addAll(other.elements);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user