diff --git a/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java b/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java index d60b95110fc2..8c4204e1239f 100644 --- a/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java +++ b/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java @@ -1,18 +1,20 @@ package com.thealgorithms.datastructures.bloomfilter; +import java.util.Arrays; import java.util.BitSet; /** * A generic BloomFilter implementation for probabilistic membership checking. *
- * Bloom filters are space-efficient data structures that provide a fast way to test whether an - * element is a member of a set. They may produce false positives, indicating an element is + * Bloom filters are space-efficient data structures that provide a fast way to + * test whether an + * element is a member of a set. They may produce false positives, indicating an + * element is * in the set when it is not, but they will never produce false negatives. *
* * @param- * This method hashes the element using all defined hash functions and sets the corresponding + * This method hashes the element using all defined hash functions and sets the + * corresponding * bits in the bit array. *
* @@ -65,13 +72,16 @@ public void insert(T key) { /** * Checks if an element might be in the Bloom filter. *- * This method checks the bits at the positions computed by each hash function. If any of these - * bits are not set, the element is definitely not in the filter. If all bits are set, the element + * This method checks the bits at the positions computed by each hash function. + * If any of these + * bits are not set, the element is definitely not in the filter. If all bits + * are set, the element * might be in the filter. *
* * @param key the element to check for membership in the Bloom filter - * @return {@code true} if the element might be in the Bloom filter, {@code false} if it is definitely not + * @return {@code true} if the element might be in the Bloom filter, + * {@code false} if it is definitely not */ public boolean contains(T key) { for (Hash- * Each instance of this class represents a different hash function based on its index. + * Each instance of this class represents a different hash function based on its + * index. *
* * @param* The hash value is calculated by multiplying the index of the hash function * with the ASCII sum of the string representation of the key. + * For array types, the content of the array is used instead of the default + * toString. *
* * @param key the element to hash * @return the computed hash value */ public int compute(T key) { - return index * asciiString(String.valueOf(key)); + String keyString = switch (key) { + case Object[] arr -> Arrays.deepToString(arr); + case int[] arr -> Arrays.toString(arr); + case long[] arr -> Arrays.toString(arr); + case double[] arr -> Arrays.toString(arr); + case float[] arr -> Arrays.toString(arr); + case boolean[] arr -> Arrays.toString(arr); + case byte[] arr -> Arrays.toString(arr); + case char[] arr -> Arrays.toString(arr); + case short[] arr -> Arrays.toString(arr); + case null, default -> String.valueOf(key); + }; + return index * asciiString(keyString); } /** @@ -131,9 +156,9 @@ public int compute(T key) { private int asciiString(String word) { int sum = 0; for (char c : word.toCharArray()) { - sum += c; - } - return sum; + sum += c; + } + return sum; + } } } -} diff --git a/src/main/java/com/thealgorithms/others/SkylineProblem.java b/src/main/java/com/thealgorithms/others/SkylineProblem.java index e84a5c5b585b..d9e2a8cc2547 100644 --- a/src/main/java/com/thealgorithms/others/SkylineProblem.java +++ b/src/main/java/com/thealgorithms/others/SkylineProblem.java @@ -1,17 +1,58 @@ package com.thealgorithms.others; import java.util.ArrayList; +import java.util.List; +import java.util.Objects; /** - * The {@code SkylineProblem} class is used to solve the skyline problem using a - * divide-and-conquer approach. - * It reads input for building data, processes it to find the skyline, and - * prints the skyline. + *+ * Solves the classic skyline problem using a divide-and-conquer approach. Given + * a list of buildings (each defined by left, height, right), + * computes the silhouette (skyline) formed by these buildings when viewed from + * a distance. + *
+ *+ * Usage example: + * + *
+ * SkylineProblem sp = new SkylineProblem(); + * sp.building = new SkylineProblem.Building[3]; + * sp.add(1, 10, 5); + * sp.add(2, 15, 7); + * sp.add(3, 12, 9); + * List+ * + *skyline = sp.findSkyline(0, 2); + *
+ * This class is not thread-safe. + *
*/ public class SkylineProblem { - Building[] building; - int count; + /** + * Array of buildings to process. Must be initialized before use. + */ + private Building[] building; + + /** + * Number of buildings added so far. + */ + public int count; + + /** + * Sets the building array to the specified size. + * + * @param size The size of the building array. + * @throws IllegalArgumentException if size is negative + */ + public void setBuilding(int size) { + if (size < 0) { + throw new IllegalArgumentException("Size must be non-negative"); + } + this.building = new Building[size]; + this.count = 0; + } /** * Adds a building with the given left, height, and right values to the @@ -20,8 +61,23 @@ public class SkylineProblem { * @param left The left x-coordinate of the building. * @param height The height of the building. * @param right The right x-coordinate of the building. + * @throws IllegalArgumentException if left >= right or height < 0 + * @throws IllegalStateException if building array is not initialized or is + * full */ public void add(int left, int height, int right) { + if (building == null) { + throw new IllegalStateException("Building array not initialized"); + } + if (count >= building.length) { + throw new IllegalStateException("Building array is full"); + } + if (left >= right) { + throw new IllegalArgumentException("Left coordinate must be less than right coordinate"); + } + if (height < 0) { + throw new IllegalArgumentException("Height must be non-negative"); + } building[count++] = new Building(left, height, right); } @@ -29,92 +85,99 @@ public void add(int left, int height, int right) { * Computes the skyline for a range of buildings using the divide-and-conquer * strategy. * - * @param start The starting index of the buildings to process. - * @param end The ending index of the buildings to process. + * @param start The starting index of the buildings to process (inclusive). + * @param end The ending index of the buildings to process (inclusive). * @return A list of {@link Skyline} objects representing the computed skyline. + * @throws IllegalArgumentException if indices are out of bounds or building + * array is null */ - public ArrayList