File tree Expand file tree Collapse file tree 8 files changed +61
-1
lines changed
out/production/Algorithms/ws/codelogic Expand file tree Collapse file tree 8 files changed +61
-1
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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 )){
Original file line number Diff line number Diff line change 44import org .junit .runners .Suite ;
55import ws .codelogic .algorithms .Stack .ArrayStackTest ;
66import ws .codelogic .algorithms .Stack .LinkStackTest ;
7+ import ws .codelogic .algorithms .SymbolTable .SymbolTableTest ;
78import ws .codelogic .algorithms .arithmetic .evaluation .DijkstrasTwoStackTest ;
89import ws .codelogic .algorithms .priorityqueue .PriorityQueueTest ;
910import ws .codelogic .algorithms .search .SearchTest ;
1516
1617@ RunWith (Suite .class )
1718@ Suite .SuiteClasses ({
19+ SymbolTableTest .class ,
1820 PriorityQueueTest .class ,
1921 Dijkstras3WayPartitionTest .class ,
2022 QuickSelectTest .class ,
You can’t perform that action at this time.
0 commit comments