| | 1 | #include "tads.h" |
| | 2 | #include "t3.h" |
| | 3 | #include "vector.h" |
| | 4 | |
| | 5 | preinit() |
| | 6 | { |
| | 7 | } |
| | 8 | |
| | 9 | main(args) |
| | 10 | { |
| | 11 | local x, y, z; |
| | 12 | local fib0, fib1; |
| | 13 | |
| | 14 | x = new Vector(7, 7).fillValue('A'); |
| | 15 | "create and fill:\n"; showVector(x); |
| | 16 | |
| | 17 | x = new Vector(0, [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]); |
| | 18 | "initially: x = \n"; showVector(x); |
| | 19 | "\b"; |
| | 20 | |
| | 21 | y = x; |
| | 22 | y[3] = 100; |
| | 23 | "after modifying through reference: y = \n"; showVector(y); |
| | 24 | "x = \n"; showVector(x); |
| | 25 | "\b"; |
| | 26 | |
| | 27 | y = x.toList(); |
| | 28 | "toList:\n"; showList(y); |
| | 29 | "\b"; |
| | 30 | |
| | 31 | y = x.toList(5); |
| | 32 | "toList(5):\n"; showList(y); |
| | 33 | "\b"; |
| | 34 | |
| | 35 | y = x.toList(17); |
| | 36 | "toList(17):\n"; showList(y); |
| | 37 | "\b"; |
| | 38 | |
| | 39 | y = x.toList(15, 17); |
| | 40 | "toList(15, 17):\n"; showList(y); |
| | 41 | "\b"; |
| | 42 | |
| | 43 | y = x.toList(3, 5); |
| | 44 | "toList(3, 5):\n"; showList(y); |
| | 45 | "\b"; |
| | 46 | |
| | 47 | y = x.toList(10, 8); |
| | 48 | "toList(10, 8):\n"; showList(y); |
| | 49 | "\b"; |
| | 50 | |
| | 51 | y = new Vector(0, x); |
| | 52 | "direct copy:\n"; showVector(y); |
| | 53 | "\b"; |
| | 54 | |
| | 55 | y.copyFrom(['a', 'b', 'c', 'd'], 1, 3, 7); |
| | 56 | "copyFrom 1,3,7:\n"; showVector(y); |
| | 57 | "\b"; |
| | 58 | |
| | 59 | y = new Vector(0, x); |
| | 60 | y.copyFrom(['a', 'b', 'c', 'd'], 1, 7, 7); |
| | 61 | "copyFrom 1,7,7:\n"; showVector(y); |
| | 62 | "\b"; |
| | 63 | |
| | 64 | y = new Vector(0, x); |
| | 65 | y.copyFrom(['a', 'b', 'c', 'd'], 1, 4, 3); |
| | 66 | "copyFrom 1,4,3:\n"; showVector(y); |
| | 67 | "\b"; |
| | 68 | |
| | 69 | y = new Vector(0, x); |
| | 70 | y.copyFrom(['a', 'b', 'c', 'd'], 3, 5, 7); |
| | 71 | "copyFrom 3,5,7:\n"; showVector(y); |
| | 72 | "\b"; |
| | 73 | |
| | 74 | y = new Vector(10, 10); |
| | 75 | y.fillValue('x'); |
| | 76 | "fillValue 'x':\n"; showVector(y); |
| | 77 | "\b"; |
| | 78 | |
| | 79 | y = new Vector(10, 10); |
| | 80 | y.fillValue(123, 3); |
| | 81 | "fillValue 123, 3:\n"; showVector(y); |
| | 82 | "\b"; |
| | 83 | |
| | 84 | y = new Vector(10, 10); |
| | 85 | y.fillValue('abc', 3, 4); |
| | 86 | "fillValue 'abc', 3, 4:\n"; showVector(y); |
| | 87 | "\b"; |
| | 88 | |
| | 89 | fib0 = 1; |
| | 90 | fib1 = 1; |
| | 91 | y = new Vector(20, 20).applyAll(new function(x) |
| | 92 | { local ret = fib0; fib0 = fib1; fib1 += ret; return ret; }); |
| | 93 | "applyAll fibonacci:\n"; showVector(y); |
| | 94 | "\b"; |
| | 95 | |
| | 96 | "Subset >= 10:\n"; showVector(y.subset({x: x >= 10})); |
| | 97 | "\b"; |
| | 98 | |
| | 99 | "find where x > 10: <<y.indexWhich({x: x > 10})>>\n"; |
| | 100 | "find where x > 100: <<y.indexWhich({x: x > 100})>>\n"; |
| | 101 | "find where x > 10000: <<y.indexWhich({x: x > 10000})>>\n"; |
| | 102 | "\b"; |
| | 103 | |
| | 104 | x = y.mapAll({x: x + 1}); |
| | 105 | "mapAll plus one:\n"; showVector(x); |
| | 106 | "mapAll == orig? <<x == y ? 'yes' : 'no'>>\n"; |
| | 107 | "mapAll original:\n"; showVector(y); |
| | 108 | "\b"; |
| | 109 | |
| | 110 | "indexOf 1: <<y.indexOf(1)>>\n"; |
| | 111 | "indexOf 54: <<y.indexOf(54)>>\n"; |
| | 112 | "indexOf 55: <<y.indexOf(55)>>\n"; |
| | 113 | "indexOf 6765: <<y.indexOf(6765)>>\n"; |
| | 114 | "\b"; |
| | 115 | |
| | 116 | "lastIndexOf 1: <<y.lastIndexOf(1)>>\n"; |
| | 117 | "lastIndexOf 54: <<y.lastIndexOf(54)>>\n"; |
| | 118 | "lastIndexOf 55: <<y.lastIndexOf(55)>>\n"; |
| | 119 | "lastIndexOf 6765: <<y.lastIndexOf(6765)>>\n"; |
| | 120 | "\b"; |
| | 121 | |
| | 122 | "lastIndexWhich(x < 1): <<y.lastIndexWhich({x: x < 1})>>\n"; |
| | 123 | "lastIndexWhich(x < 2): <<y.lastIndexWhich({x: x < 2})>>\n"; |
| | 124 | "lastIndexWhich(x < 10): <<y.lastIndexWhich({x: x < 10})>>\n"; |
| | 125 | "lastIndexWhich(x < 100): <<y.lastIndexWhich({x: x < 100})>>\n"; |
| | 126 | "lastIndexWhich(x < 10000): <<y.lastIndexWhich({x: x < 10000})>>\n"; |
| | 127 | "\b"; |
| | 128 | |
| | 129 | "lastValWhich(x < 1): <<y.lastValWhich({x: x < 1})>>\n"; |
| | 130 | "lastValWhich(x < 2): <<y.lastValWhich({x: x < 2})>>\n"; |
| | 131 | "lastValWhich(x < 10): <<y.lastValWhich({x: x < 10})>>\n"; |
| | 132 | "lastValWhich(x < 100): <<y.lastValWhich({x: x < 100})>>\n"; |
| | 133 | "lastValWhich(x < 10000): <<y.lastValWhich({x: x < 10000})>>\n"; |
| | 134 | "\b"; |
| | 135 | |
| | 136 | "countOf 0: <<y.countOf(0)>>\n"; |
| | 137 | "countOf 1: <<y.countOf(1)>>\n"; |
| | 138 | "countOf 55: <<y.countOf(55)>>\n"; |
| | 139 | "\b"; |
| | 140 | |
| | 141 | "countWhich x>10: <<y.countWhich({x: x > 10})>>\n"; |
| | 142 | "countWhich x>100: <<y.countWhich({x: x > 100})>>\n"; |
| | 143 | "countWhich x>1000: <<y.countWhich({x: x > 1000})>>\n"; |
| | 144 | "countWhich x>10000: <<y.countWhich({x: x > 10000})>>\n"; |
| | 145 | "\b"; |
| | 146 | |
| | 147 | x = new Vector(0, [2, 4, 6, 8, 10, 12]); |
| | 148 | "initially: x = \n"; showVector(x); |
| | 149 | "getUnique:\n"; showVector(x.getUnique()); |
| | 150 | |
| | 151 | x = new Vector(0, [9, 1, 9, 2, 9, 3, 9, 2, 9, 1, 9]); |
| | 152 | "initially: x = \n"; showVector(x); |
| | 153 | "getUnique:\n"; showVector(x.getUnique()); |
| | 154 | |
| | 155 | "\b"; |
| | 156 | |
| | 157 | x = new Vector(0, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); |
| | 158 | y = new Vector(0, [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]); |
| | 159 | "appendUnique:\n"; showVector(x.appendUnique(y)); |
| | 160 | |
| | 161 | x = new Vector(0, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); |
| | 162 | y = new Vector(0, [11, 12, 13, 14, 15]); |
| | 163 | "appendUnique:\n"; showVector(x.appendUnique(y)); |
| | 164 | |
| | 165 | x = new Vector(0, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); |
| | 166 | y = new Vector(0, [2, 4, 6, 8, 10]); |
| | 167 | "appendUnique:\n"; showVector(x.appendUnique(y)); |
| | 168 | |
| | 169 | x = new Vector(0, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); |
| | 170 | "appendUnique - list:\n"; |
| | 171 | "appendUnique:\n"; showVector(x.appendUnique([5, 10, 15, 20])); |
| | 172 | |
| | 173 | "\b"; |
| | 174 | |
| | 175 | y = new Vector(0, ['hello', 'there', 'how', 'are', 'you', 'today']); |
| | 176 | "sort:\n"; showVector(y.sort()); |
| | 177 | |
| | 178 | "sort descending:\n"; showVector(y.sort(true)); |
| | 179 | |
| | 180 | "\b"; |
| | 181 | |
| | 182 | x = new Vector(0, [1, 1, 2, 3, 5, 8, 13]); |
| | 183 | y = [101, 102, 103]; |
| | 184 | z = x + y; |
| | 185 | "adding list to vector:\n"; showVector(z); |
| | 186 | |
| | 187 | y = new Vector(5); |
| | 188 | y += [987, 654, 321]; |
| | 189 | z = x + y; |
| | 190 | "adding vector to vector:\n"; showVector(z); |
| | 191 | |
| | 192 | y = new Vector(0, [111, 222, 333]); |
| | 193 | z = x + y; |
| | 194 | "adding vector to vector:\n"; showVector(z); |
| | 195 | |
| | 196 | y = [2, 3, 5, 8]; |
| | 197 | z = x - y; |
| | 198 | "subtracting list from vector:\n"; showVector(z); |
| | 199 | |
| | 200 | y = new Vector(5); |
| | 201 | y += [1, 13]; |
| | 202 | z = x - y; |
| | 203 | "subtracting vector from vector:\n"; showVector(z); |
| | 204 | |
| | 205 | y = new Vector(0, [1, 3, 5]); |
| | 206 | z = x - y; |
| | 207 | "subtracting vector from vector:\n"; showVector(z); |
| | 208 | } |
| | 209 | |
| | 210 | showVector(x) |
| | 211 | { |
| | 212 | for (local i = 1 ; i <= x.length() ; ++i) |
| | 213 | "\t[<<i>>] = <<showVal(x[i])>>\n"; |
| | 214 | } |
| | 215 | |
| | 216 | showList(x) |
| | 217 | { |
| | 218 | for (local i = 1 ; i <= x.length() ; ++i) |
| | 219 | "\t[<<i>>] = <<showVal(x[i])>>\n"; |
| | 220 | } |
| | 221 | |
| | 222 | showVal(x) |
| | 223 | { |
| | 224 | if (x == nil) |
| | 225 | "nil"; |
| | 226 | else |
| | 227 | tadsSay(x); |
| | 228 | } |
| | 229 | |