Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
/**
* A generic BloomFilter implementation for probabilistic membership checking.
* <p>
* 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.
* </p>
*
Expand All @@ -20,11 +22,14 @@ public class BloomFilter<T> {
private final Hash<T>[] hashFunctions;

/**
* Constructs a BloomFilter with a specified number of hash functions and bit array size.
* Constructs a BloomFilter with a specified number of hash functions and bit
* array size.
*
* @param numberOfHashFunctions the number of hash functions to use
* @param bitArraySize the size of the bit array, which determines the capacity of the filter
* @throws IllegalArgumentException if numberOfHashFunctions or bitArraySize is less than 1
* @param bitArraySize the size of the bit array, which determines the
* capacity of the filter
* @throws IllegalArgumentException if numberOfHashFunctions or bitArraySize is
* less than 1
*/
@SuppressWarnings("unchecked")
public BloomFilter(int numberOfHashFunctions, int bitArraySize) {
Expand All @@ -38,7 +43,8 @@ public BloomFilter(int numberOfHashFunctions, int bitArraySize) {
}

/**
* Initializes the hash functions with unique indices to ensure different hashing.
* Initializes the hash functions with unique indices to ensure different
* hashing.
*/
private void initializeHashFunctions() {
for (int i = 0; i < numberOfHashFunctions; i++) {
Expand All @@ -49,7 +55,8 @@ private void initializeHashFunctions() {
/**
* Inserts an element into the Bloom filter.
* <p>
* 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.
* </p>
*
Expand All @@ -65,13 +72,16 @@ public void insert(T key) {
/**
* Checks if an element might be in the Bloom filter.
* <p>
* 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.
* </p>
*
* @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<T> hash : hashFunctions) {
Expand All @@ -86,7 +96,8 @@ public boolean contains(T key) {
/**
* Inner class representing a hash function used by the Bloom filter.
* <p>
* 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.
* </p>
*
* @param <T> The type of elements to be hashed.
Expand Down Expand Up @@ -115,7 +126,46 @@ private static class Hash<T> {
* @return the computed hash value
*/
public int compute(T key) {
return index * asciiString(String.valueOf(key));
return index * asciiString(objectToString(key));
}

/**
* Converts the key to a string suitable for hashing. Handles arrays properly.
*/
private String objectToString(Object key) {
if (key == null) {
return "null";
}
Class<?> clazz = key.getClass();
if (clazz.isArray()) {
if (clazz == byte[].class) {
return java.util.Arrays.toString((byte[]) key);
}
if (clazz == short[].class) {
return java.util.Arrays.toString((short[]) key);
}
if (clazz == int[].class) {
return java.util.Arrays.toString((int[]) key);
}
if (clazz == long[].class) {
return java.util.Arrays.toString((long[]) key);
}
if (clazz == char[].class) {
return java.util.Arrays.toString((char[]) key);
}
if (clazz == float[].class) {
return java.util.Arrays.toString((float[]) key);
}
if (clazz == double[].class) {
return java.util.Arrays.toString((double[]) key);
}
if (clazz == boolean[].class) {
return java.util.Arrays.toString((boolean[]) key);
}
// For object arrays or multi-dimensional arrays
return java.util.Arrays.deepToString((Object[]) key);
}
return String.valueOf(key);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,39 @@
import java.util.ArrayList;
import java.util.List;

/**
* Utility class to print a matrix in spiral order.
* <p>
* Given a 2D array (matrix), this class provides a method to return the
* elements
* of the matrix in spiral order, starting from the top-left corner and moving
* clockwise.
* </p>
*
* @author Sadiul Hakim (https://github.com/sadiul-hakim)
*/
public class PrintAMatrixInSpiralOrder {

/**
* Search a key in row and column wise sorted matrix
* Returns the elements of the given matrix in spiral order.
*
* @param matrix the 2D array to traverse in spiral order
* @param row the number of rows in the matrix
* @param col the number of columns in the matrix
* @return a list containing the elements of the matrix in spiral order
*
* <p>
* Example:
*
* @param matrix matrix to be searched
* @param row number of rows matrix has
* @param col number of columns matrix has
* @author Sadiul Hakim : https://github.com/sadiul-hakim
* <pre>
* int[][] matrix = {
* {1, 2, 3},
* {4, 5, 6},
* {7, 8, 9}
* };
* print(matrix, 3, 3) returns [1, 2, 3, 6, 9, 8, 7, 4, 5]
* </pre>
* </p>
*/
public List<Integer> print(int[][] matrix, int row, int col) {
// r traverses matrix row wise from first
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.thealgorithms.others;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.Test;

class PrintAMatrixInSpiralOrderTest {

private final PrintAMatrixInSpiralOrder spiralPrinter = new PrintAMatrixInSpiralOrder();

@Test
void testSquareMatrix() {
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
List<Integer> expected = Arrays.asList(1, 2, 3, 6, 9, 8, 7, 4, 5);
assertEquals(expected, spiralPrinter.print(matrix, 3, 3));
}

@Test
void testRectangularMatrixMoreRows() {
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};
List<Integer> expected = Arrays.asList(1, 2, 3, 6, 9, 12, 11, 10, 7, 4, 5, 8);
assertEquals(expected, spiralPrinter.print(matrix, 4, 3));
}

@Test
void testRectangularMatrixMoreCols() {
int[][] matrix = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
List<Integer> expected = Arrays.asList(1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7);
assertEquals(expected, spiralPrinter.print(matrix, 3, 4));
}

@Test
void testSingleRow() {
int[][] matrix = {{1, 2, 3, 4}};
List<Integer> expected = Arrays.asList(1, 2, 3, 4);
assertEquals(expected, spiralPrinter.print(matrix, 1, 4));
}

@Test
void testSingleColumn() {
int[][] matrix = {{1}, {2}, {3}};
List<Integer> expected = Arrays.asList(1, 2, 3);
assertEquals(expected, spiralPrinter.print(matrix, 3, 1));
}

@Test
void testEmptyMatrix() {
int[][] matrix = new int[0][0];
List<Integer> expected = Collections.emptyList();
assertEquals(expected, spiralPrinter.print(matrix, 0, 0));
}

@Test
void testOneElementMatrix() {
int[][] matrix = {{42}};
List<Integer> expected = Collections.singletonList(42);
assertEquals(expected, spiralPrinter.print(matrix, 1, 1));
}

@Test
void testMatrixWithNegativeNumbers() {
int[][] matrix = {{-1, -2}, {-3, -4}};
List<Integer> expected = Arrays.asList(-1, -2, -4, -3);
assertEquals(expected, spiralPrinter.print(matrix, 2, 2));
}
}