| 1 | #charset "us-ascii" |
| 2 | #pragma once |
| 3 | |
| 4 | /* |
| 5 | * Copyright (c) 2000, 2006 Michael J. Roberts |
| 6 | * |
| 7 | * This file is part of TADS 3. |
| 8 | * |
| 9 | * This header defines the Vector intrinsic class. |
| 10 | */ |
| 11 | |
| 12 | |
| 13 | /* include our base class definition */ |
| 14 | #include "systype.h" |
| 15 | |
| 16 | |
| 17 | /* |
| 18 | * The Vector intrinsic class provides a varying-length array type. |
| 19 | * Vectors can be expanded dynamically, and values within a vector can be |
| 20 | * changed. (In contrast, List is a constant type: a value within a list |
| 21 | * cannot be changed, and new values can't be added to a list. Any |
| 22 | * manipulation of a List results in a new, separate List object, leaving |
| 23 | * the original unchanged. Vector allows new values to be added and |
| 24 | * existing values to be changed in place, without creating a new object.) |
| 25 | */ |
| 26 | intrinsic class Vector 'vector/030005': Collection |
| 27 | { |
| 28 | /* |
| 29 | * Create a list with the same elements as the vector. If 'start' is |
| 30 | * specified, it's the index of the first element we store; we'll |
| 31 | * store elements starting at index 'start'. If 'cnt' is specified, |
| 32 | * it gives the maximum number of elements for the new list; by |
| 33 | * default, we'll store all of the elements from 'start' to the last |
| 34 | * element. |
| 35 | */ |
| 36 | toList(start?, cnt?); |
| 37 | |
| 38 | /* get the number of elements in the vector */ |
| 39 | length(); |
| 40 | |
| 41 | /* |
| 42 | * Copy from another vector or list. Elements are copied from the |
| 43 | * source vector or list starting at the element given by 'src_start', |
| 44 | * and are copied into 'self' starting at the index given by |
| 45 | * 'dst_start'. At most 'cnt' values are copied, but we stop when we |
| 46 | * reach the last element of either the source or destination values. |
| 47 | * If either index is negative, it counts from the end of the vector: |
| 48 | * -1 is the last element, -2 is the second to last, and so on. |
| 49 | */ |
| 50 | copyFrom(src, src_start, dst_start, cnt); |
| 51 | |
| 52 | /* |
| 53 | * Fill with a given value, starting at the given element (the first |
| 54 | * element if not specified), and running for the given number of |
| 55 | * elements (the remaining existing elements of the vector, if not |
| 56 | * specified). The vector is expanded if necessary. A negative |
| 57 | * starting index counts backwards from the last element. |
| 58 | */ |
| 59 | fillValue(val, start?, cnt?); |
| 60 | |
| 61 | /* |
| 62 | * Select a subset of the vector. Returns a new vector consisting |
| 63 | * only of the elements of this vector for which the callback function |
| 64 | * returns true. |
| 65 | */ |
| 66 | subset(func); |
| 67 | |
| 68 | /* |
| 69 | * Apply a callback function to each element of the vector. For each |
| 70 | * element of the vector, invokes the callback, and replaces the |
| 71 | * element with the return value of the callback. Modifies the vector |
| 72 | * in-place, and returns 'self'. |
| 73 | */ |
| 74 | applyAll(func); |
| 75 | |
| 76 | /* |
| 77 | * Find the first element for which the given condition is true. |
| 78 | * Apply the callback function (which encodes the condition to |
| 79 | * evaluate) to each element in turn, starting with the first. For |
| 80 | * each element, if the callback returns nil, proceed to the next |
| 81 | * element; otherwise, stop and return the index of the element. If |
| 82 | * the callback never returns true for any element, we'll return nil. |
| 83 | */ |
| 84 | indexWhich(cond); |
| 85 | |
| 86 | /* |
| 87 | * Invoke the callback func(val) on each element, in order from first |
| 88 | * to last. No return value. |
| 89 | */ |
| 90 | forEach(func); |
| 91 | |
| 92 | /* |
| 93 | * Invoke the callback func(index, val) on each element, in order from |
| 94 | * first to last. No return value. |
| 95 | */ |
| 96 | forEachAssoc(func); |
| 97 | |
| 98 | /* |
| 99 | * Apply the callback function to each element of this vector, and |
| 100 | * return a new vector consisting of the results. Effectively maps |
| 101 | * the vector to a new vector using the given function, leaving the |
| 102 | * original vector unchanged. |
| 103 | */ |
| 104 | mapAll(func); |
| 105 | |
| 106 | /* get the index of the first match for the given value */ |
| 107 | indexOf(val); |
| 108 | |
| 109 | /* |
| 110 | * Find the first element for which the given condition is true, and |
| 111 | * return the value of the element. |
| 112 | */ |
| 113 | valWhich(cond); |
| 114 | |
| 115 | /* find the last element with the given value, and return its index */ |
| 116 | lastIndexOf(val); |
| 117 | |
| 118 | /* |
| 119 | * Find the last element for which the condition is true, and return |
| 120 | * the index of the element. Applies the callback to each element in |
| 121 | * turn, starting with the last element and working backwards. For |
| 122 | * each element, if the callback returns nil, proceeds to the previous |
| 123 | * element; otherwise, stops and returns the index of the element. If |
| 124 | * the callback never returns true for any element, we'll return nil. |
| 125 | */ |
| 126 | lastIndexWhich(cond); |
| 127 | |
| 128 | /* |
| 129 | * Find the last element for which the condition is true, and return |
| 130 | * the value of the element |
| 131 | */ |
| 132 | lastValWhich(cond); |
| 133 | |
| 134 | /* count the number of elements with the given value */ |
| 135 | countOf(val); |
| 136 | |
| 137 | /* count the number of elements for which the callback returns true */ |
| 138 | countWhich(cond); |
| 139 | |
| 140 | /* create a new vector consisting of the unique elements of this vector */ |
| 141 | getUnique(); |
| 142 | |
| 143 | /* |
| 144 | * append the elements of the list or vector 'val' to the elements of |
| 145 | * this vector, then remove repeated elements in the result; returns a |
| 146 | * new vector with the unique elements of the combination |
| 147 | */ |
| 148 | appendUnique(val); |
| 149 | |
| 150 | /* |
| 151 | * Sort the vector in place; returns 'self'. If the 'descending' |
| 152 | * flag is provided and is not nil, we'll sort the vector in |
| 153 | * descending order rather than ascending order. |
| 154 | * |
| 155 | * If the 'comparisonFunction' argument is provided, it must be a |
| 156 | * callback function; the callback takes two arguments, and returns |
| 157 | * an integer less than zero if the first argument value is less |
| 158 | * than the second, zero if they're equal, and an integer greater |
| 159 | * than zero if the first is greater than the second. If no |
| 160 | * 'comparisonFunction' argument is provided, or it's provided and |
| 161 | * its value is nil, we'll simply compare the vector elements as |
| 162 | * ordinary values. The comparison function can be provided for |
| 163 | * caller-defined orderings, such as ordering a set of objects. |
| 164 | */ |
| 165 | sort(descending?, comparisonFunction?); |
| 166 | |
| 167 | /* |
| 168 | * Set the length - if this is shorter than the current length, |
| 169 | * existing items will be discarded; if it's longer, the newly added |
| 170 | * slots will be set to nil. Returns 'self'. |
| 171 | */ |
| 172 | setLength(newElementCount); |
| 173 | |
| 174 | /* |
| 175 | * Insert one or more elements at the given index. If the starting |
| 176 | * index is 1, the elements will be inserted before the first existing |
| 177 | * element. If the index is one higher than the number of elements, |
| 178 | * the elements will be inserted after all existing elements. A |
| 179 | * negative starting index counts from the end of the vector: -1 is the |
| 180 | * last element, -2 is the second to last, and so on. A zero starting |
| 181 | * index inserts after the last existing element. |
| 182 | * |
| 183 | * Note that a list value argument will simply be inserted as a single |
| 184 | * element. |
| 185 | * |
| 186 | * Returns 'self'. |
| 187 | */ |
| 188 | insertAt(startingIndex, val, ...); |
| 189 | |
| 190 | /* |
| 191 | * Delete the element at the given index, reducing the length of the |
| 192 | * vector by one element. If 'index' is negative, it counts from the |
| 193 | * end of the vector: -1 is the last element, -2 is the second to last, |
| 194 | * and so on. Returns 'self'. |
| 195 | */ |
| 196 | removeElementAt(index); |
| 197 | |
| 198 | /* |
| 199 | * Delete the range of elements starting at startingIndex and ending at |
| 200 | * endingIndex. The elements at the ends of the range are included in |
| 201 | * the deletion. If startingIndex == endingIndex, only one element is |
| 202 | * removed. If either index is negative, it counts backwards from the |
| 203 | * last element: -1 is the last element, -2 is the second to last, and |
| 204 | * so on. The length of the vector is reduced by the number of |
| 205 | * elements removed. Returns 'self'. |
| 206 | */ |
| 207 | removeRange(startingIndex, endingIndex); |
| 208 | |
| 209 | /* |
| 210 | * Append an element to the vector. This works just like insertAt() |
| 211 | * with a starting index one higher than the length of the vector. |
| 212 | * This has almost the same effect as the '+' operator, but treats a |
| 213 | * list value like any other value by simply inserting the list as a |
| 214 | * single new element (rather than appending each item in the list |
| 215 | * individually, as the '+' operator would). |
| 216 | */ |
| 217 | append(val); |
| 218 | |
| 219 | /* |
| 220 | * Prepend an element. This works like insertAt() with a starting |
| 221 | * index of 1. |
| 222 | */ |
| 223 | prepend(val); |
| 224 | |
| 225 | /* |
| 226 | * Append all elements from a list or vector. This works like |
| 227 | * append(val), except that if 'val' is a list or vector, the elements |
| 228 | * of 'val' will be appended individually, like the '+' operator does. |
| 229 | * The difference between this method and the '+' operator is that |
| 230 | * this method modifies this Vector by adding the new elements |
| 231 | * directly to the existing Vector object, whereas the '+' operator |
| 232 | * creates a new Vector to store the result. |
| 233 | */ |
| 234 | appendAll(val); |
| 235 | |
| 236 | /* |
| 237 | * Remove an element by value. Each element of the vector matching |
| 238 | * the given value is removed. The vector is modified in-place. The |
| 239 | * return value is 'self'. |
| 240 | */ |
| 241 | removeElement(val); |
| 242 | |
| 243 | /* |
| 244 | * Splice new values into the vector. Deletes the 'del' elements |
| 245 | * starting at 'idx', then inserts the extra arguments in their place. |
| 246 | * Updates the vector in place. To insert items without deleting |
| 247 | * anything, pass 0 for 'del'. To delete items without inserting |
| 248 | * anything, omit any additional arguments. Returns 'self'. |
| 249 | */ |
| 250 | splice(idx, del, ...); |
| 251 | |
| 252 | /* |
| 253 | * Combine the vector elements into a string. This converts each |
| 254 | * element into a string value using the usual default conversions (or |
| 255 | * throws an error if string conversion isn't possible), then |
| 256 | * concatenates the values together and returns the result. If |
| 257 | * 'separator' is provided, it's a string that's interposed between |
| 258 | * elements; if this is omitted, the elements are concatenated together |
| 259 | * with no extra characters in between. |
| 260 | */ |
| 261 | join(sep?); |
| 262 | |
| 263 | /* |
| 264 | * Class method: Generate a new Vector. 'func' is a callback function, |
| 265 | * which can take zero or one argument. 'n' is the number of elements |
| 266 | * for the new list. For each element, 'func' is invoked as func() if |
| 267 | * it takes no arguments, or func(idx) if it takes one argument, where |
| 268 | * 'idx' is the index of the element being generated. The return value |
| 269 | * of the call to 'func' is stored as the list element. The method |
| 270 | * returns the resulting list. For example, a vector of the first ten |
| 271 | * even positive integers: Vector.generate({i: i*2}, 10). |
| 272 | */ |
| 273 | static generate(func, n); |
| 274 | |
| 275 | /* |
| 276 | * Get the index of the element with the minimum value. If 'func' is |
| 277 | * missing, this simply returns the index of the element with the |
| 278 | * smallest value, comparing the element values as though using the '>' |
| 279 | * and '<' operators. If 'func' is specified, it must be a function; |
| 280 | * it's called as func(x) for each element's value, and the result of |
| 281 | * the overall call is the index of the element for which func(x) |
| 282 | * returns the smallest value. For example, if you have a vector v |
| 283 | * containing string elements, v.indexOfMin({x: x.length()}) returns |
| 284 | * the index of the shortest string. |
| 285 | */ |
| 286 | indexOfMin(func?); |
| 287 | |
| 288 | /* |
| 289 | * Get the minimum element value. If 'func' is missing, this simply |
| 290 | * returns the smallest element value. If 'func' is specified, it must |
| 291 | * be a function; it's called as func(x) for each element value x, and |
| 292 | * the result of the overall method call is the element value x that |
| 293 | * minimizes func(x). For example, if v is a vector containing string |
| 294 | * elements, v.minVal({x: x.length()}) returns the shortest string. |
| 295 | */ |
| 296 | minVal(func?); |
| 297 | |
| 298 | /* |
| 299 | * Get the index of the element the maximum value. If 'func' is |
| 300 | * missing, this simply returns the index of the element with the |
| 301 | * largest value, comparing the element values as though using the '>' |
| 302 | * and '<' operators. If 'func' is specified, it must be a function; |
| 303 | * it's called as func(x) for each element value x, and the result of |
| 304 | * the overall call is the index of the element for which func(x) |
| 305 | * returns the greatest value. For example, if you have a vector v |
| 306 | * containing string elements, v.indexOfMax({x: x.length()}) returns |
| 307 | * the index of the longest string. |
| 308 | */ |
| 309 | indexOfMax(func?); |
| 310 | |
| 311 | /* |
| 312 | * Get the maximum element value. If 'func' is missing, this returns |
| 313 | * the largest element value. If 'func' is specified, it must be a |
| 314 | * function; it's called as func(x) for each element value x, and the |
| 315 | * result of the overall method call is the element value x that |
| 316 | * maximizes func(x). For example, if v is a vector containing string |
| 317 | * elements, v.minVal({x: x.length()}) returns the longest string. |
| 318 | */ |
| 319 | maxVal(func?); |
| 320 | } |
| 321 |