Skip to content

Commit a749a1d

Browse files
authored
Merge pull request #44 from Data-Structure-Study/yoonexample
ChainedHashTable 구현 완료
2 parents e92a1a4 + 37d7f3d commit a749a1d

File tree

3 files changed

+147
-1
lines changed

3 files changed

+147
-1
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: 59 additions & 1 deletion
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

@@ -24,11 +25,11 @@ void setUp() {
2425
new Person(20202457, "알렉스", "수원"),
2526
new Person(20201390, "에버", "서울어디였는데...")
2627
);
27-
table = new SimpleTable<>(hashFunction);
2828
}
2929

3030
@Test
3131
void 간단한_테이블_입력_테스트() {
32+
table = new SimpleTable<>(hashFunction);
3233
try {
3334
for (Person person : persons) {
3435
table.insert(person.getId(), person);
@@ -40,6 +41,7 @@ void setUp() {
4041

4142
@Test
4243
void 간단한_테이블_탐색_테스트() {
44+
table = new SimpleTable<>(hashFunction);
4345
for (Person person : persons) {
4446
table.insert(person.getId(), person);
4547
}
@@ -53,6 +55,62 @@ void setUp() {
5355

5456
@Test
5557
void 간단한_테이블_삭제_테스트() {
58+
table = new SimpleTable<>(hashFunction);
59+
for (Person person : persons) {
60+
table.insert(person.getId(), person);
61+
}
62+
63+
Person searchedPerson;
64+
for (Person targetPerson : persons) {
65+
searchedPerson = table.delete(targetPerson.getId());
66+
assertThat(searchedPerson).isNotNull().isEqualToComparingFieldByField(targetPerson);
67+
}
68+
}
69+
70+
@Test
71+
void 체이닝_테이블_입력_테스트() {
72+
table = new ChainedHashTable<>(hashFunction);
73+
try {
74+
for (Person person : persons) {
75+
table.insert(person.getId(), person);
76+
}
77+
} catch (Exception e) {
78+
fail("입력 테스트 실패");
79+
}
80+
}
81+
82+
@Test
83+
void 체이닝_테이블_탐색_테스트() {
84+
table = new ChainedHashTable<>(hashFunction);
85+
for (Person person : persons) {
86+
table.insert(person.getId(), person);
87+
}
88+
89+
Person searchedPerson;
90+
for (Person targetPerson : persons) {
91+
searchedPerson = table.search(targetPerson.getId());
92+
assertThat(searchedPerson).isNotNull().isEqualToComparingFieldByField(targetPerson);
93+
}
94+
}
95+
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+
111+
@Test
112+
void 체이닝_테이블_삭제_테스트() {
113+
table = new ChainedHashTable<>(hashFunction);
56114
for (Person person : persons) {
57115
table.insert(person.getId(), person);
58116
}

0 commit comments

Comments
 (0)