-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashTable.java
126 lines (124 loc) · 3.99 KB
/
HashTable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import java.io.*;
import java.util.Objects;
import java.util.OptionalInt;
public class HashTable {
static class Pair {
public Integer key;
public Integer value;
public Pair(Integer key, Integer value){
this.key = key;
this.value = value;
}
}
static class Node{
public Pair value;
public Node next;
public Node(Pair value, Node next){
this.next = next;
this.value = value;
}
}
private static final Node[] pairs = new Node[131072];
public static void main(String[] args) throws IOException {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out))) {
int operationsNumber = Integer.parseInt(reader.readLine());
for (int i = 0; i < operationsNumber; i++) {
String command = reader.readLine();
doCommand(command, writer);
}
}
}
public static void doCommand(String command, BufferedWriter writer) throws IOException{
String[] commandValues = command.split(" ");
OptionalInt result;
switch (commandValues[0]){
case("get"):
result = get(Integer.parseInt(commandValues[1]));
print(writer, result);
break;
case("delete"):
result = delete(Integer.parseInt(commandValues[1]));
print(writer, result);
break;
default:
put(Integer.parseInt(commandValues[1]), Integer.parseInt(commandValues[2]));
}
}
private static void print(BufferedWriter writer, OptionalInt result) throws IOException {
if(result.isPresent()){
writer.write(Integer.toString(result.getAsInt()));
} else{
writer.write("None");
}
writer.newLine();
}
private static int getSize(int operations){
int n = 1;
while(operations > 2){
operations = operations / 2;
n++;
}
return n;
}
private static OptionalInt get(Integer key){
int bucket = getBucket(key);
Node search = getNode(key, bucket);
if(search == null){
return OptionalInt.empty();
}
return OptionalInt.of(search.value.value);
}
private static Node getNode(Integer key, int bucket){
if(pairs[bucket] == null){
return null;
}
Node search = pairs[bucket];
while(search != null){
if(Objects.equals(search.value.key, key)){
return search;
}
search = search.next;
}
return null;
}
private static OptionalInt delete(Integer key){
int bucket = getBucket(key);;
if(pairs[bucket] == null){
return OptionalInt.empty();
}
Node search = pairs[bucket];
Node prev = null;
while(search != null){
if(Objects.equals(search.value.key, key)){
OptionalInt result = OptionalInt.of(search.value.value);
if(prev == null){
pairs[bucket] = search.next;
} else {
prev.next = search.next;
}
return result;
}
prev = search;
search = search.next;
}
return OptionalInt.empty();
}
private static void put(Integer key, Integer value){
int bucket = getBucket(key);
Node search = getNode(key, bucket);
if(search != null){
search.value.value = value;
return;
}
Pair pair = new Pair(key, value);
Node node = new Node(pair, null);
if(pairs[bucket] != null){
node.next = pairs[bucket];
}
pairs[bucket] = node;
}
private static int getBucket(Integer key){
return (pairs.length - 1) & key;
}
}