Skip to content

Commit fa500ba

Browse files
author
someone-1
committed
refactoring, added support for iteration
1 parent eda030f commit fa500ba

File tree

2 files changed

+49
-17
lines changed

2 files changed

+49
-17
lines changed

src/main/java/dataStructures/Hashmap.java renamed to src/main/java/dataStructures/FakeHashmap.java

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package dataStructures;
22

3+
import java.util.Collection;
4+
import java.util.HashSet;
5+
import java.util.Set;
36
import java.util.concurrent.locks.Lock;
47
import java.util.concurrent.locks.ReentrantReadWriteLock;
58

6-
public class Hashmap <K, V>{
9+
public class FakeHashmap <K, V> {
710

811
private int bucketCount = 16;
912
private Bucket<K, V>[] buckets = new Bucket[bucketCount];
@@ -18,7 +21,7 @@ public void insert(K key, V val){
1821
}else {
1922
bucket = buckets[ind];
2023
}
21-
bucket.updNode(node);
24+
bucket.updateNode(node);
2225
}
2326

2427
public V get(K key){
@@ -52,6 +55,19 @@ private int getIndex(K key){
5255
return hash%bucketCount;
5356
}
5457

58+
public Set<Node> entrySet(){
59+
Bucket<K, V> bucket;
60+
Set<Node> outputSet = new HashSet<>();
61+
for (int i=0 ; i<bucketCount ; i++){
62+
bucket = buckets[i];
63+
if(bucket != null) {
64+
Set<Node<K, V>> set = bucket.getAll();
65+
outputSet.addAll(set);
66+
}
67+
}
68+
return outputSet;
69+
}
70+
5571
}
5672

5773
class Bucket<K, V>{
@@ -65,7 +81,7 @@ class Bucket<K, V>{
6581
* adds new node if key is not present
6682
* updated value of node if key is present
6783
* */
68-
void updNode(Node<K, V> node){
84+
void updateNode(Node<K, V> node){
6985
wLock.lock();
7086
try {
7187
if (start == null) {
@@ -107,10 +123,25 @@ Node<K,V> getNode(K key){
107123
}
108124

109125

126+
Set<Node<K, V>> getAll(){
127+
Set<Node<K, V>> set = new HashSet<>();
128+
rLock.lock();
129+
try {
130+
Node<K, V> node = start;
131+
while (node != null){
132+
set.add(node);
133+
node = node.next;
134+
}
135+
return set;
136+
}finally {
137+
rLock.unlock();
138+
}
139+
}
140+
141+
110142
Node<K, V> delNode(K key){
111143
wLock.lock();
112144
try {
113-
114145
Node<K, V> node = getNode(key);
115146
if (node != null) {
116147
if (start == node) {
@@ -131,16 +162,3 @@ Node<K, V> delNode(K key){
131162
}
132163
}
133164
}
134-
135-
class Node<K, V>{
136-
Node<K, V> next;
137-
Node<K, V> prev;
138-
K key;
139-
V val;
140-
Node(K key, V val){
141-
this.val = val;
142-
this.key = key;
143-
next = null;
144-
prev = null;
145-
}
146-
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package dataStructures;
2+
3+
class Node<K, V>{
4+
Node<K, V> next;
5+
Node<K, V> prev;
6+
K key;
7+
V val;
8+
Node(K key, V val){
9+
this.val = val;
10+
this.key = key;
11+
next = null;
12+
prev = null;
13+
}
14+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy