Skip to content

Commit c2612af

Browse files
Merge branch 'master' into master
2 parents bcde414 + ee6cd64 commit c2612af

File tree

9 files changed

+739
-13
lines changed

9 files changed

+739
-13
lines changed

DIRECTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* [AES](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AES.java)
4343
* [AESEncryption](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AESEncryption.java)
4444
* [AffineCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AffineCipher.java)
45+
* [Autokey](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/Autokey.java)
4546
* [Blowfish](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/Blowfish.java)
4647
* [Caesar](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/Caesar.java)
4748
* [ColumnarTranspositionCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/ColumnarTranspositionCipher.java)
@@ -203,6 +204,7 @@
203204
* [SameTreesCheck](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/SameTreesCheck.java)
204205
* [SegmentTree](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/SegmentTree.java)
205206
* [SplayTree](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/SplayTree.java)
207+
* [Treap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/Treap.java)
206208
* [TreeRandomNode](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/TreeRandomNode.java)
207209
* [TrieImp](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/TrieImp.java)
208210
* [VerticalOrderTraversal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/trees/VerticalOrderTraversal.java)
@@ -457,6 +459,7 @@
457459
* [GenerateSubsets](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java)
458460
* scheduling
459461
* [FCFSScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java)
462+
* [MLFQScheduler](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/MLFQScheduler.java)
460463
* [PreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java)
461464
* [RRScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/RRScheduling.java)
462465
* [SJFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SJFScheduling.java)
@@ -618,6 +621,7 @@
618621
* ciphers
619622
* a5
620623
* [LFSRTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/a5/LFSRTest.java)
624+
* [AutokeyTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/AutokeyTest.java)
621625
* [BlowfishTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/BlowfishTest.java)
622626
* [CaesarTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/CaesarTest.java)
623627
* [DESTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/DESTest.java)
@@ -728,6 +732,7 @@
728732
* [PreOrderTraversalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/PreOrderTraversalTest.java)
729733
* [SameTreesCheckTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/SameTreesCheckTest.java)
730734
* [SplayTreeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/SplayTreeTest.java)
735+
* [TreapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/TreapTest.java)
731736
* [TreeTestUtils](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/TreeTestUtils.java)
732737
* [TrieImpTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/TrieImpTest.java)
733738
* [VerticalOrderTraversalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/VerticalOrderTraversalTest.java)
@@ -911,6 +916,7 @@
911916
* [GenerateSubsetsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java)
912917
* scheduling
913918
* [FCFSSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/FCFSSchedulingTest.java)
919+
* [MLFQSchedulerTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/MLFQSchedulerTest.java)
914920
* [PreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java)
915921
* [RRSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RRSchedulingTest.java)
916922
* [SJFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SJFSchedulingTest.java)

src/main/java/com/thealgorithms/bitmanipulation/CountSetBits.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,32 @@ public long countSetBits(long num) {
4848
}
4949
return cnt;
5050
}
51+
52+
/**
53+
* This approach takes O(1) running time to count the set bits, but requires a pre-processing.
54+
*
55+
* So, we divide our 32-bit input into 8-bit chunks, with four chunks. We have 8 bits in each chunk.
56+
*
57+
* Then the range is from 0-255 (0 to 2^7).
58+
* So, we may need to count set bits from 0 to 255 in individual chunks.
59+
*
60+
* @param num takes a long number
61+
* @return the count of set bits in the binary equivalent
62+
*/
63+
public int lookupApproach(int num) {
64+
int[] table = new int[256];
65+
table[0] = 0;
66+
67+
for (int i = 1; i < 256; i++) {
68+
table[i] = (i & 1) + table[i >> 1]; // i >> 1 equals to i/2
69+
}
70+
71+
int res = 0;
72+
for (int i = 0; i < 4; i++) {
73+
res += table[num & 0xff];
74+
num >>= 8;
75+
}
76+
77+
return res;
78+
}
5179
}

src/main/java/com/thealgorithms/ciphers/a5/CompositeLFSR.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,33 @@
55
import java.util.Map;
66
import java.util.TreeMap;
77

8+
/**
9+
* The CompositeLFSR class represents a composite implementation of
10+
* Linear Feedback Shift Registers (LFSRs) for cryptographic purposes.
11+
*
12+
* <p>
13+
* This abstract class manages a collection of LFSR instances and
14+
* provides a mechanism for irregular clocking based on the
15+
* majority bit among the registers. It implements the BaseLFSR
16+
* interface, requiring subclasses to define specific LFSR behaviors.
17+
* </p>
18+
*/
819
public abstract class CompositeLFSR implements BaseLFSR {
920

1021
protected final List<LFSR> registers = new ArrayList<>();
1122

1223
/**
13-
* Implements irregular clocking using the clock bit for each register
14-
* @return the registers discarded bit xored value
24+
* Performs a clocking operation on the composite LFSR.
25+
*
26+
* <p>
27+
* This method determines the majority bit across all registers and
28+
* clocks each register based on its clock bit. If a register's
29+
* clock bit matches the majority bit, it is clocked (shifted).
30+
* The method also computes and returns the XOR of the last bits
31+
* of all registers.
32+
* </p>
33+
*
34+
* @return the XOR value of the last bits of all registers.
1535
*/
1636
@Override
1737
public boolean clock() {
@@ -26,6 +46,18 @@ public boolean clock() {
2646
return result;
2747
}
2848

49+
/**
50+
* Calculates the majority bit among all registers.
51+
*
52+
* <p>
53+
* This private method counts the number of true and false clock bits
54+
* across all LFSR registers. It returns true if the count of true
55+
* bits is greater than or equal to the count of false bits; otherwise,
56+
* it returns false.
57+
* </p>
58+
*
59+
* @return true if the majority clock bits are true; false otherwise.
60+
*/
2961
private boolean getMajorityBit() {
3062
Map<Boolean, Integer> bitCount = new TreeMap<>();
3163
bitCount.put(Boolean.FALSE, 0);

src/main/java/com/thealgorithms/datastructures/caches/LFUCache.java

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,41 @@
44
import java.util.Map;
55

66
/**
7-
* Java program for LFU Cache (https://en.wikipedia.org/wiki/Least_frequently_used)
7+
* The {@code LFUCache} class implements a Least Frequently Used (LFU) cache.
8+
* An LFU cache evicts the least frequently used item when the cache reaches its capacity.
9+
* It keeps track of how many times each item is used and maintains a doubly linked list
10+
* for efficient addition and removal of items based on their frequency of use.
11+
*
12+
* @param <K> The type of keys maintained by this cache.
13+
* @param <V> The type of mapped values.
14+
*
15+
* <p>
16+
* Reference: <a href="https://en.wikipedia.org/wiki/Least_frequently_used">LFU Cache - Wikipedia</a>
17+
* </p>
18+
*
819
* @author Akshay Dubey (https://github.com/itsAkshayDubey)
920
*/
1021
public class LFUCache<K, V> {
1122

23+
/**
24+
* The {@code Node} class represents an element in the LFU cache.
25+
* Each node contains a key, a value, and a frequency count.
26+
* It also has pointers to the previous and next nodes in the doubly linked list.
27+
*/
1228
private class Node {
1329
private final K key;
1430
private V value;
1531
private int frequency;
1632
private Node previous;
1733
private Node next;
1834

35+
/**
36+
* Constructs a new {@code Node} with the specified key, value, and frequency.
37+
*
38+
* @param key The key associated with this node.
39+
* @param value The value stored in this node.
40+
* @param frequency The frequency of usage of this node.
41+
*/
1942
Node(K key, V value, int frequency) {
2043
this.key = key;
2144
this.value = value;
@@ -29,10 +52,19 @@ private class Node {
2952
private final int capacity;
3053
private static final int DEFAULT_CAPACITY = 100;
3154

55+
/**
56+
* Constructs an LFU cache with the default capacity.
57+
*/
3258
public LFUCache() {
3359
this(DEFAULT_CAPACITY);
3460
}
3561

62+
/**
63+
* Constructs an LFU cache with the specified capacity.
64+
*
65+
* @param capacity The maximum number of items that the cache can hold.
66+
* @throws IllegalArgumentException if the specified capacity is less than or equal to zero.
67+
*/
3668
public LFUCache(int capacity) {
3769
if (capacity <= 0) {
3870
throw new IllegalArgumentException("Capacity must be greater than zero.");
@@ -42,10 +74,12 @@ public LFUCache(int capacity) {
4274
}
4375

4476
/**
45-
* Retrieves the value for the given key from the cache. Increases the frequency of the node.
77+
* Retrieves the value associated with the given key from the cache.
78+
* If the key exists, the node's frequency is increased and the node is repositioned
79+
* in the linked list based on its updated frequency.
4680
*
47-
* @param key The key to look up.
48-
* @return The value associated with the key, or null if the key is not present.
81+
* @param key The key whose associated value is to be returned.
82+
* @return The value associated with the key, or {@code null} if the key is not present in the cache.
4983
*/
5084
public V get(K key) {
5185
Node node = cache.get(key);
@@ -59,10 +93,12 @@ public V get(K key) {
5993
}
6094

6195
/**
62-
* Adds or updates a key-value pair in the cache. If the cache is full, the least frequently used item is evicted.
96+
* Inserts or updates a key-value pair in the cache.
97+
* If the key already exists, the value is updated and its frequency is incremented.
98+
* If the cache is full, the least frequently used item is removed before inserting the new item.
6399
*
64-
* @param key The key to insert or update.
65-
* @param value The value to insert or update.
100+
* @param key The key associated with the value to be inserted or updated.
101+
* @param value The value to be inserted or updated.
66102
*/
67103
public void put(K key, V value) {
68104
if (cache.containsKey(key)) {
@@ -73,7 +109,7 @@ public void put(K key, V value) {
73109
addNodeWithUpdatedFrequency(node);
74110
} else {
75111
if (cache.size() >= capacity) {
76-
cache.remove(this.head.key);
112+
cache.remove(this.head.key); // Evict least frequently used item
77113
removeNode(head);
78114
}
79115
Node node = new Node(key, value, 1);
@@ -84,8 +120,9 @@ public void put(K key, V value) {
84120

85121
/**
86122
* Adds a node to the linked list in the correct position based on its frequency.
123+
* The linked list is ordered by frequency, with the least frequently used node at the head.
87124
*
88-
* @param node The node to add.
125+
* @param node The node to be inserted into the list.
89126
*/
90127
private void addNodeWithUpdatedFrequency(Node node) {
91128
if (tail != null && head != null) {
@@ -122,9 +159,10 @@ private void addNodeWithUpdatedFrequency(Node node) {
122159
}
123160

124161
/**
125-
* Removes a node from the linked list.
162+
* Removes a node from the doubly linked list.
163+
* This method ensures that the pointers of neighboring nodes are properly updated.
126164
*
127-
* @param node The node to remove.
165+
* @param node The node to be removed from the list.
128166
*/
129167
private void removeNode(Node node) {
130168
if (node.previous != null) {

0 commit comments

Comments
 (0)