Skip to content

Commit 37991d3

Browse files
author
codelogicws
committed
SymbolTable
starting to implement it.
1 parent b5f072b commit 37991d3

File tree

8 files changed

+61
-1
lines changed

8 files changed

+61
-1
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
59 Bytes
Binary file not shown.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package ws.codelogic.algorithms.SymbolTable;
2+
3+
public class SymbolTable <T>{
4+
5+
Comparable[] keys;
6+
T[] values;
7+
int currentIndex = -1;
8+
9+
public SymbolTable(T[] emptyValuesArray){
10+
keys = new Comparable[emptyValuesArray.length];
11+
values = emptyValuesArray;
12+
}
13+
14+
public void add(Comparable key, T value){
15+
currentIndex ++;
16+
keys[currentIndex] = key;
17+
values[currentIndex] = value;
18+
}
19+
20+
public T get(Comparable key){
21+
for(int i=0;i<keys.length;i++){
22+
if(key == keys[i]){
23+
return values[i];
24+
}
25+
}
26+
return null;
27+
}
28+
29+
public boolean contains(Comparable key){
30+
return get(key) != null;
31+
}
32+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ws.codelogic.algorithms.SymbolTable;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import static org.junit.Assert.*;
6+
7+
public class SymbolTableTest {
8+
9+
private SymbolTable<String> symbolTable;
10+
11+
@Before
12+
public void setUp() {
13+
String[] emptyArray = new String[10];
14+
symbolTable = new SymbolTable<String>(emptyArray);
15+
symbolTable.add(1, "foo");
16+
symbolTable.add(2, "bar");
17+
symbolTable.add(3, "big");
18+
symbolTable.add(4, "boy");
19+
}
20+
21+
@Test
22+
public void getElementThatWasStored() {
23+
String returned = symbolTable.get(4);
24+
assertEquals("boy", returned);
25+
}
26+
}

src/ws/codelogic/algorithms/priorityqueue/BinaryTree.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public Comparable remove(){
6969
private void sink(int index){
7070
while(2*index <= indexOfLastElement){
7171
int child = 2*index;
72-
if(child <= indexOfLastElement && less(child, child + 1)){
72+
if(child < indexOfLastElement && less(child, child + 1)){
7373
child++;
7474
}
7575
if(less(child, index)){

src/ws/codelogic/test/TestSuite.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.junit.runners.Suite;
55
import ws.codelogic.algorithms.Stack.ArrayStackTest;
66
import ws.codelogic.algorithms.Stack.LinkStackTest;
7+
import ws.codelogic.algorithms.SymbolTable.SymbolTableTest;
78
import ws.codelogic.algorithms.arithmetic.evaluation.DijkstrasTwoStackTest;
89
import ws.codelogic.algorithms.priorityqueue.PriorityQueueTest;
910
import ws.codelogic.algorithms.search.SearchTest;
@@ -15,6 +16,7 @@
1516

1617
@RunWith(Suite.class)
1718
@Suite.SuiteClasses({
19+
SymbolTableTest.class,
1820
PriorityQueueTest.class,
1921
Dijkstras3WayPartitionTest.class,
2022
QuickSelectTest.class,

0 commit comments

Comments
 (0)