Skip to content

Commit 1aba467

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 5e3ddc3 + beaec8a commit 1aba467

File tree

8 files changed

+316
-138
lines changed

8 files changed

+316
-138
lines changed

DIRECTORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,12 +938,14 @@
938938
* [CeilTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/CeilTest.java)
939939
* [CollatzConjectureTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/CollatzConjectureTest.java)
940940
* [CombinationsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/CombinationsTest.java)
941+
* [ConvolutionFFTTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/ConvolutionFFTTest.java)
941942
* [ConvolutionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/ConvolutionTest.java)
942943
* [CrossCorrelationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/CrossCorrelationTest.java)
943944
* [DeterminantOfMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/DeterminantOfMatrixTest.java)
944945
* [DigitalRootTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/DigitalRootTest.java)
945946
* [DistanceFormulaTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/DistanceFormulaTest.java)
946947
* [DudeneyNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/DudeneyNumberTest.java)
948+
* [EulerMethodTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/EulerMethodTest.java)
947949
* [EulersFunctionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/EulersFunctionTest.java)
948950
* [FactorialRecursionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FactorialRecursionTest.java)
949951
* [FactorialTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FactorialTest.java)
@@ -1031,6 +1033,7 @@
10311033
* [PalindromePrimeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/PalindromePrimeTest.java)
10321034
* [PalindromeSinglyLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/PalindromeSinglyLinkedListTest.java)
10331035
* [RangeInSortedArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/RangeInSortedArrayTest.java)
1036+
* [SparsityTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/SparsityTest.java)
10341037
* [ThreeSumProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/ThreeSumProblemTest.java)
10351038
* [TwoSumProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/TwoSumProblemTest.java)
10361039
* [WordBoggleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/WordBoggleTest.java)

src/main/java/com/thealgorithms/datastructures/buffers/CircularBuffer.java

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,62 @@
22

33
import java.util.concurrent.atomic.AtomicInteger;
44

5+
/**
6+
* The {@code CircularBuffer} class implements a generic circular (or ring) buffer.
7+
* A circular buffer is a fixed-size data structure that operates in a FIFO (First In, First Out) manner.
8+
* The buffer allows you to overwrite old data when the buffer is full and efficiently use limited memory.
9+
* When the buffer is full, adding a new item will overwrite the oldest data.
10+
*
11+
* @param <Item> The type of elements stored in the circular buffer.
12+
*/
513
public class CircularBuffer<Item> {
614
private final Item[] buffer;
715
private final CircularPointer putPointer;
816
private final CircularPointer getPointer;
917
private final AtomicInteger size = new AtomicInteger(0);
1018

19+
/**
20+
* Constructor to initialize the circular buffer with a specified size.
21+
*
22+
* @param size The size of the circular buffer.
23+
* @throws IllegalArgumentException if the size is zero or negative.
24+
*/
1125
public CircularBuffer(int size) {
26+
if (size <= 0) {
27+
throw new IllegalArgumentException("Buffer size must be positive");
28+
}
1229
// noinspection unchecked
1330
this.buffer = (Item[]) new Object[size];
1431
this.putPointer = new CircularPointer(0, size);
1532
this.getPointer = new CircularPointer(0, size);
1633
}
1734

35+
/**
36+
* Checks if the circular buffer is empty.
37+
* This method is based on the current size of the buffer.
38+
*
39+
* @return {@code true} if the buffer is empty, {@code false} otherwise.
40+
*/
1841
public boolean isEmpty() {
1942
return size.get() == 0;
2043
}
2144

45+
/**
46+
* Checks if the circular buffer is full.
47+
* The buffer is considered full when its size equals its capacity.
48+
*
49+
* @return {@code true} if the buffer is full, {@code false} otherwise.
50+
*/
2251
public boolean isFull() {
2352
return size.get() == buffer.length;
2453
}
2554

55+
/**
56+
* Retrieves and removes the item at the front of the buffer (FIFO).
57+
* This operation will move the {@code getPointer} forward.
58+
*
59+
* @return The item at the front of the buffer, or {@code null} if the buffer is empty.
60+
*/
2661
public Item get() {
2762
if (isEmpty()) {
2863
return null;
@@ -33,31 +68,64 @@ public Item get() {
3368
return item;
3469
}
3570

71+
/**
72+
* Adds an item to the end of the buffer (FIFO).
73+
* If the buffer is full, this operation will overwrite the oldest data.
74+
*
75+
* @param item The item to be added.
76+
* @throws IllegalArgumentException if the item is null.
77+
* @return {@code true} if the item was successfully added, {@code false} if the buffer was full and the item overwrote existing data.
78+
*/
3679
public boolean put(Item item) {
80+
if (item == null) {
81+
throw new IllegalArgumentException("Null items are not allowed");
82+
}
83+
84+
boolean wasEmpty = isEmpty();
3785
if (isFull()) {
38-
return false;
86+
getPointer.getAndIncrement(); // Move get pointer to discard oldest item
87+
} else {
88+
size.incrementAndGet();
3989
}
4090

4191
buffer[putPointer.getAndIncrement()] = item;
42-
size.incrementAndGet();
43-
return true;
92+
return wasEmpty;
4493
}
4594

95+
/**
96+
* The {@code CircularPointer} class is a helper class used to track the current index (pointer)
97+
* in the circular buffer.
98+
* The max value represents the capacity of the buffer.
99+
* The `CircularPointer` class ensures that the pointer automatically wraps around to 0
100+
* when it reaches the maximum index.
101+
* This is achieved in the `getAndIncrement` method, where the pointer
102+
* is incremented and then taken modulo the maximum value (`max`).
103+
* This operation ensures that the pointer always stays within the bounds of the buffer.
104+
*/
46105
private static class CircularPointer {
47106
private int pointer;
48107
private final int max;
49108

109+
/**
110+
* Constructor to initialize the circular pointer.
111+
*
112+
* @param pointer The initial position of the pointer.
113+
* @param max The maximum size (capacity) of the circular buffer.
114+
*/
50115
CircularPointer(int pointer, int max) {
51116
this.pointer = pointer;
52117
this.max = max;
53118
}
54119

120+
/**
121+
* Increments the pointer by 1 and wraps it around to 0 if it reaches the maximum value.
122+
* This ensures the pointer always stays within the buffer's bounds.
123+
*
124+
* @return The current pointer value before incrementing.
125+
*/
55126
public int getAndIncrement() {
56-
if (pointer == max) {
57-
pointer = 0;
58-
}
59127
int tmp = pointer;
60-
pointer++;
128+
pointer = (pointer + 1) % max;
61129
return tmp;
62130
}
63131
}

src/main/java/com/thealgorithms/maths/FFT.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,14 @@ public Complex divide(double n) {
165165
temp.img = this.img / n;
166166
return temp;
167167
}
168+
169+
public double real() {
170+
return real;
171+
}
172+
173+
public double imaginary() {
174+
return img;
175+
}
168176
}
169177

170178
/**
Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.thealgorithms.misc;
22

3-
import java.util.Scanner;
4-
53
/*
64
*A matrix is sparse if many of its coefficients are zero (In general if 2/3rd of matrix elements
75
*are 0, it is considered as sparse). The interest in sparsity arises because its exploitation can
@@ -16,12 +14,17 @@ private Sparsity() {
1614
}
1715

1816
/*
17+
* @param mat the input matrix
1918
* @return Sparsity of matrix
2019
*
2120
* where sparsity = number of zeroes/total elements in matrix
2221
*
2322
*/
2423
static double sparsity(double[][] mat) {
24+
if (mat == null || mat.length == 0) {
25+
throw new IllegalArgumentException("Matrix cannot be null or empty");
26+
}
27+
2528
int zero = 0;
2629
// Traversing the matrix to count number of zeroes
2730
for (int i = 0; i < mat.length; i++) {
@@ -32,25 +35,6 @@ static double sparsity(double[][] mat) {
3235
}
3336
}
3437
// return sparsity
35-
return ((double) zero / (mat.length * mat[1].length));
36-
}
37-
38-
// Driver method
39-
public static void main(String[] args) {
40-
Scanner in = new Scanner(System.in);
41-
System.out.println("Enter number of rows in matrix: ");
42-
int n = in.nextInt();
43-
System.out.println("Enter number of Columns in matrix: ");
44-
int m = in.nextInt();
45-
46-
System.out.println("Enter Matrix elements: ");
47-
double[][] mat = new double[n][m];
48-
for (int i = 0; i < n; i++) {
49-
for (int j = 0; j < m; j++) {
50-
mat[i][j] = in.nextDouble();
51-
}
52-
}
53-
System.out.println("Sparsity of matrix is: " + sparsity(mat));
54-
in.close();
38+
return ((double) zero / (mat.length * mat[0].length));
5539
}
5640
}

0 commit comments

Comments
 (0)