1#include "tads.h" 2#include "lookup.h" 3 4#define show(x) showTab(tab, #@x, x) 5 6showTab(tab, caption, val) 7{ 8 "<<caption>> -> <<tab[val]>>\n"; 9} 10 11main(args) 12{ 13 local tab = new LookupTable(16, 32); 14 15 /* insert some elements */ 16 tab['hello'] = 1; 17 tab['goodbye'] = 2; 18 tab['asdf'] = 3; 19 tab[[1,2]] = 4; 20 tab[[1,3]] = 5; 21 tab[2.818] = 6; 22 tab[3.1415] = 7; 23 24 /* show the values */ 25 show('hello'); 26 show('goodbye'); 27 show('asdf'); 28 show('asdf2'); 29 show([1,2]); 30 show([1,3]); 31 show([2,3]); 32 show(2.818); 33 show(2.818000); 34 show(0.28180e1); 35 show(3.1415); 36 show(1.707); 37 38 /* change some values */ 39 tab['goodbye'] *= 100; 40 tab[[1,3]] *= 100; 41 42 /* show the changes */ 43 "\bafter changes:\n"; 44 show('hello'); 45 show('goodbye'); 46 show('asdf'); 47 show('asdf2'); 48 show([1,2]); 49 show([1,3]); 50 show([2,3]); 51 show(2.818); 52 show(3.1415); 53 show(1.707); 54 55 /* remove some keys */ 56 tab.removeElement('hello'); 57 tab.removeElement([1,2]); 58 59 /* show the changes */ 60 "\bafter removals:\n"; 61 show('hello'); 62 show('goodbye'); 63 show('asdf'); 64 show('asdf2'); 65 show([1,2]); 66 show([1,3]); 67 show([2,3]); 68 show(2.818); 69 show(3.1415); 70 show(1.707); 71 72 /* iterate through the table */ 73 "\bIterating:\n"; 74 foreach (local val in tab) 75 "\t<<val>>\n"; 76 77 /* iterate again, this time using the forEach method */ 78 "\bAgain, using tab.forEach:\n"; 79 tab.forEachAssoc({k,v: "\t[<<sayval(k)>>] = <<v>>\n"}); 80 81 /* try an iterator */ 82 "\bIterator:\n"; 83 for (local iter = tab.createIterator() ; iter.isNextAvailable() ; ) 84 { 85 local cur; 86 87 /* get the next element */ 88 cur = iter.getNext(); 89 90 /* show it, and the current key/value from the iterator */ 91 "\tgetNext() returned <<cur>>, cur key = 92 <<sayval(iter.getCurKey())>>, cur val = <<iter.getCurVal()>>\n"; 93 } 94} 95 96sayval(val) 97{ 98 if (dataType(val) == TypeList) 99 { 100 local first = true; 101 "["; 102 foreach (local x in val) 103 { 104 if (!first) 105 ","; 106 first = nil; 107 sayval(x); 108 } 109 "]"; 110 } 111 else 112 tadsSay(val); 113} 114 115