diff --git a/DIRECTORY.md b/DIRECTORY.md index b621216da7f1..2ec4aac6a6da 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -27,7 +27,6 @@ * [NonRepeatingNumberFinder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinder.java) * [NumbersDifferentSigns](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumbersDifferentSigns.java) * [ReverseBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java) - * [SetKthBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SetKthBit.java) * [SingleBitOperations](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SingleBitOperations.java) * ciphers * a5 @@ -271,6 +270,7 @@ * [AmicableNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/AmicableNumber.java) * [Area](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/Area.java) * [Armstrong](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/Armstrong.java) + * [AutoCorrelation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/AutoCorrelation.java) * [AutomorphicNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/AutomorphicNumber.java) * [Average](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/Average.java) * [BinaryPow](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/BinaryPow.java) @@ -281,6 +281,7 @@ * [Combinations](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/Combinations.java) * [Convolution](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/Convolution.java) * [ConvolutionFFT](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/ConvolutionFFT.java) + * [CrossCorrelation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/CrossCorrelation.java) * [DeterminantOfMatrix](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/DeterminantOfMatrix.java) * [DigitalRoot](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/DigitalRoot.java) * [DistanceFormula](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/DistanceFormula.java) @@ -587,7 +588,6 @@ * [NonRepeatingNumberFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinderTest.java) * [NumbersDifferentSignsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumbersDifferentSignsTest.java) * [ReverseBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java) - * [SetKthBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SetKthBitTest.java) * [SingleBitOperationsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleBitOperationsTest.java) * ciphers * a5 @@ -712,6 +712,7 @@ * [AmicableNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/AmicableNumberTest.java) * [AreaTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/AreaTest.java) * [ArmstrongTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/ArmstrongTest.java) + * [AutoCorrelationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/AutoCorrelationTest.java) * [AutomorphicNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/AutomorphicNumberTest.java) * [AverageTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/AverageTest.java) * [BinaryPowTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/BinaryPowTest.java) @@ -719,6 +720,7 @@ * [CeilTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/CeilTest.java) * [CollatzConjectureTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/CollatzConjectureTest.java) * [CombinationsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/CombinationsTest.java) + * [CrossCorrelationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/CrossCorrelationTest.java) * [DigitalRootTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/DigitalRootTest.java) * [DistanceFormulaTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/DistanceFormulaTest.java) * [DudeneyNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/DudeneyNumberTest.java) diff --git a/src/main/java/com/thealgorithms/datastructures/crdt/TwoPSet.java b/src/main/java/com/thealgorithms/datastructures/crdt/TwoPSet.java index c0ce17b2802b..83dd3b72f2c8 100644 --- a/src/main/java/com/thealgorithms/datastructures/crdt/TwoPSet.java +++ b/src/main/java/com/thealgorithms/datastructures/crdt/TwoPSet.java @@ -1,5 +1,4 @@ -package com.thealgorithms.datastructures.crdt; - +import java.util.Collections; import java.util.HashSet; import java.util.Set; @@ -12,19 +11,35 @@ * comparing with other 2P-Sets, and merging two 2P-Sets while preserving the remove-wins semantics. * (https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type) * + * Changes made by @555vedant: + * - Made the constructor private and added a static factory method 'empty'. + * - Used 'Collections.unmodifiableSet' to create unmodifiable sets for 'setA' and 'setR' in the 'merge' method. + * - Updated the 'merge' method accordingly. + * * @author itakurah (Niklas Hoefflin) (https://github.com/itakurah) + * @author 555vedant (Your Name) (Your GitHub Profile) */ - public class TwoPSet { private final Set setA; private final Set setR; /** * Constructs an empty Two-Phase Set. + * Note: This constructor is made private. Use the static factory method 'empty()' to create an empty set. */ - public TwoPSet() { - this.setA = new HashSet<>(); - this.setR = new HashSet<>(); + private TwoPSet(Set setA, Set setR) { + this.setA = setA; + this.setR = setR; + } + + /** + * Creates an empty Two-Phase Set. + * + * @param the type of elements in the set. + * @return an empty Two-Phase Set. + */ + public static TwoPSet empty() { + return new TwoPSet<>(new HashSet<>(), new HashSet<>()); } /** @@ -74,11 +89,15 @@ public boolean compare(TwoPSet otherSet) { * @return A new 2P-Set containing the merged elements. */ public TwoPSet merge(TwoPSet otherSet) { - TwoPSet mergedSet = new TwoPSet<>(); - mergedSet.setA.addAll(this.setA); - mergedSet.setA.addAll(otherSet.setA); - mergedSet.setR.addAll(this.setR); - mergedSet.setR.addAll(otherSet.setR); - return mergedSet; + Set mergedSetA = new HashSet<>(setA); + mergedSetA.addAll(otherSet.setA); + + Set mergedSetR = new HashSet<>(setR); + mergedSetR.addAll(otherSet.setR); + + return new TwoPSet<>(Collections.unmodifiableSet(mergedSetA), Collections.unmodifiableSet(mergedSetR)); } } + + +