Skip to content

Commit 37d7f3d

Browse files
committed
feat: 체이닝을 사용한 테이블 구현
1 parent 2467608 commit 37d7f3d

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package table.chainedtable;
2+
3+
import list.DummyLinkedList;
4+
import list.List;
5+
import table.HashFunction;
6+
import table.Table;
7+
8+
public class ChainedHashTable<K, V> implements Table<K, V> {
9+
10+
private final List<ChainedSlot<K, V>>[] slots;
11+
private final HashFunction<K> hashFunction;
12+
13+
public ChainedHashTable(HashFunction<K> hashFunction) {
14+
this(100, hashFunction);
15+
}
16+
17+
public ChainedHashTable(int slotSize, HashFunction<K> hashFunction) {
18+
this.slots = new List[slotSize];
19+
this.hashFunction = hashFunction;
20+
}
21+
22+
@Override
23+
public void insert(K key, V value) {
24+
if (search(key) != null) {
25+
System.out.println("키 중복");
26+
return;
27+
}
28+
29+
List<ChainedSlot<K, V>> slotList = this.slots[hashFunction.hashFunction(key)];
30+
ChainedSlot<K, V> slot = new ChainedSlot<>(key, value);
31+
if (slotList == null) {
32+
this.slots[hashFunction.hashFunction(key)] = new DummyLinkedList<>();
33+
slotList = this.slots[hashFunction.hashFunction(key)];
34+
}
35+
slotList.insert(slot);
36+
}
37+
38+
@Override
39+
public V search(K key) {
40+
List<ChainedSlot<K, V>> slotList = this.slots[hashFunction.hashFunction(key)];
41+
if (slotList == null || slotList.isEmpty()) {
42+
return null;
43+
}
44+
45+
for (int i = 0; i < slotList.size(); i++) {
46+
ChainedSlot<K, V> slot = slotList.get(i);
47+
if (key.equals(slot.getKey())) {
48+
return slot.getValue();
49+
}
50+
}
51+
return null;
52+
}
53+
54+
@Override
55+
public V delete(K key) {
56+
List<ChainedSlot<K, V>> slotList = this.slots[hashFunction.hashFunction(key)];
57+
if (slotList == null || slotList.isEmpty()) {
58+
return null;
59+
}
60+
61+
for (int i = 0; i < slotList.size(); i++) {
62+
if (key.equals(slotList.get(i).getKey())) {
63+
return slotList.remove(i).getValue();
64+
}
65+
}
66+
return null;
67+
}
68+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package table.chainedtable;
2+
3+
public class ChainedSlot<K, V> {
4+
5+
private final K key;
6+
private final V value;
7+
8+
public ChainedSlot(K key, V value) {
9+
this.key = key;
10+
this.value = value;
11+
}
12+
13+
public V getValue() {
14+
return this.value;
15+
}
16+
17+
public K getKey() {
18+
return this.key;
19+
}
20+
}

yoonexample/src/test/java/table/TableTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.assertj.core.util.Lists;
88
import org.junit.jupiter.api.BeforeEach;
99
import org.junit.jupiter.api.Test;
10+
import table.chainedtable.ChainedHashTable;
1011

1112
class TableTest {
1213

@@ -92,6 +93,21 @@ void setUp() {
9293
}
9394
}
9495

96+
@Test
97+
void 체이닝_테이블_중복_탐색_테스트() {
98+
hashFunction = key -> 0;
99+
table = new ChainedHashTable<>(hashFunction);
100+
for (Person person : persons) {
101+
table.insert(person.getId(), person);
102+
}
103+
104+
Person searchedPerson;
105+
for (Person targetPerson : persons) {
106+
searchedPerson = table.search(targetPerson.getId());
107+
assertThat(searchedPerson).isNotNull().isEqualToComparingFieldByField(targetPerson);
108+
}
109+
}
110+
95111
@Test
96112
void 체이닝_테이블_삭제_테스트() {
97113
table = new ChainedHashTable<>(hashFunction);

0 commit comments

Comments
 (0)