| | 1 | #charset "us-ascii" |
| | 2 | |
| | 3 | /* |
| | 4 | * Copyright (c) 2000, 2006 Michael J. Roberts |
| | 5 | * |
| | 6 | * This file is part of TADS 3. |
| | 7 | * |
| | 8 | * This header defines the Vector intrinsic class. |
| | 9 | */ |
| | 10 | |
| | 11 | #ifndef _VECTOR_H_ |
| | 12 | #define _VECTOR_H_ |
| | 13 | |
| | 14 | /* include our base class definition */ |
| | 15 | #include "systype.h" |
| | 16 | |
| | 17 | |
| | 18 | /* |
| | 19 | * The Vector intrinsic class provides a varying-length array type. |
| | 20 | * Vectors can be expanded dynamically, and values within a vector can be |
| | 21 | * changed. (In contrast, List is a constant type: a value within a list |
| | 22 | * cannot be changed, and new values can't be added to a list. Any |
| | 23 | * manipulation of a List results in a new, separate List object, leaving |
| | 24 | * the original unchanged. Vector allows new values to be added and |
| | 25 | * existing values to be changed in place, without creating a new object.) |
| | 26 | */ |
| | 27 | intrinsic class Vector 'vector/030004': Collection |
| | 28 | { |
| | 29 | /* |
| | 30 | * Create a list with the same elements as the vector. If 'start' is |
| | 31 | * specified, it's the index of the first element we store; we'll |
| | 32 | * store elements starting at index 'start'. If 'cnt' is specified, |
| | 33 | * it gives the maximum number of elements for the new list; by |
| | 34 | * default, we'll store all of the elements from 'start' to the last |
| | 35 | * element. |
| | 36 | */ |
| | 37 | toList(start?, cnt?); |
| | 38 | |
| | 39 | /* get the number of elements in the vector */ |
| | 40 | length(); |
| | 41 | |
| | 42 | /* |
| | 43 | * Copy from another vector or list. Elements are copied from the |
| | 44 | * source vector or list starting at the element given by 'src_start', |
| | 45 | * and are copied into 'self' starting at the index given by |
| | 46 | * 'dst_start'. At most 'cnt' values are copied, but we stop when we |
| | 47 | * reach the last element of either the source or destination values. |
| | 48 | */ |
| | 49 | copyFrom(src, src_start, dst_start, cnt); |
| | 50 | |
| | 51 | /* |
| | 52 | * Fill with a given value, starting at the given element (the first |
| | 53 | * element if not specified), and running for the given number of |
| | 54 | * elements (the remaining existing elements of the vector, if not |
| | 55 | * specified). The vector is expanded if necessary. |
| | 56 | */ |
| | 57 | fillValue(val, start?, cnt?); |
| | 58 | |
| | 59 | /* |
| | 60 | * Select a subset of the vector. Returns a new vector consisting |
| | 61 | * only of the elements of this vector for which the callback function |
| | 62 | * returns true. |
| | 63 | */ |
| | 64 | subset(func); |
| | 65 | |
| | 66 | /* |
| | 67 | * Apply a callback function to each element of the vector. For each |
| | 68 | * element of the vector, invokes the callback, and replaces the |
| | 69 | * element with the return value of the callback. Modifies the vector |
| | 70 | * in-place, and returns 'self'. |
| | 71 | */ |
| | 72 | applyAll(func); |
| | 73 | |
| | 74 | /* |
| | 75 | * Find the first element for which the given condition is true. |
| | 76 | * Apply the callback function (which encodes the condition to |
| | 77 | * evaluate) to each element in turn, starting with the first. For |
| | 78 | * each element, if the callback returns nil, proceed to the next |
| | 79 | * element; otherwise, stop and return the index of the element. If |
| | 80 | * the callback never returns true for any element, we'll return nil. |
| | 81 | */ |
| | 82 | indexWhich(cond); |
| | 83 | |
| | 84 | /* |
| | 85 | * Invoke the callback func(val) on each element, in order from first |
| | 86 | * to last. No return value. |
| | 87 | */ |
| | 88 | forEach(func); |
| | 89 | |
| | 90 | /* |
| | 91 | * Invoke the callback func(index, val) on each element, in order from |
| | 92 | * first to last. No return value. |
| | 93 | */ |
| | 94 | forEachAssoc(func); |
| | 95 | |
| | 96 | /* |
| | 97 | * Apply the callback function to each element of this vector, and |
| | 98 | * return a new vector consisting of the results. Effectively maps |
| | 99 | * the vector to a new vector using the given function, leaving the |
| | 100 | * original vector unchanged. |
| | 101 | */ |
| | 102 | mapAll(func); |
| | 103 | |
| | 104 | /* get the index of the first match for the given value */ |
| | 105 | indexOf(val); |
| | 106 | |
| | 107 | /* |
| | 108 | * Find the first element for which the given condition is true, and |
| | 109 | * return the value of the element. |
| | 110 | */ |
| | 111 | valWhich(cond); |
| | 112 | |
| | 113 | /* find the last element with the given value, and return its index */ |
| | 114 | lastIndexOf(val); |
| | 115 | |
| | 116 | /* |
| | 117 | * Find the last element for which the condition is true, and return |
| | 118 | * the index of the element. Applies the callback to each element in |
| | 119 | * turn, starting with the last element and working backwards. For |
| | 120 | * each element, if the callback returns nil, proceeds to the previous |
| | 121 | * element; otherwise, stops and returns the index of the element. If |
| | 122 | * the callback never returns true for any element, we'll return nil. |
| | 123 | */ |
| | 124 | lastIndexWhich(cond); |
| | 125 | |
| | 126 | /* |
| | 127 | * Find the last element for which the condition is true, and return |
| | 128 | * the value of the element |
| | 129 | */ |
| | 130 | lastValWhich(cond); |
| | 131 | |
| | 132 | /* count the number of elements with the given value */ |
| | 133 | countOf(val); |
| | 134 | |
| | 135 | /* count the number of elements for which the callback returns true */ |
| | 136 | countWhich(cond); |
| | 137 | |
| | 138 | /* create a new vector consisting of the unique elements of this vector */ |
| | 139 | getUnique(); |
| | 140 | |
| | 141 | /* |
| | 142 | * append the elements of the list or vector 'val' to the elements of |
| | 143 | * this vector, then remove repeated elements in the result; returns a |
| | 144 | * new vector with the unique elements of the combination |
| | 145 | */ |
| | 146 | appendUnique(val); |
| | 147 | |
| | 148 | /* |
| | 149 | * Sort the vector in place; returns 'self'. If the 'descending' |
| | 150 | * flag is provided and is not nil, we'll sort the vector in |
| | 151 | * descending order rather than ascending order. |
| | 152 | * |
| | 153 | * If the 'comparisonFunction' argument is provided, it must be a |
| | 154 | * callback function; the callback takes two arguments, and returns |
| | 155 | * an integer less than zero if the first argument value is less |
| | 156 | * than the second, zero if they're equal, and an integer greater |
| | 157 | * than zero if the first is greater than the second. If no |
| | 158 | * 'comparisonFunction' argument is provided, or it's provided and |
| | 159 | * its value is nil, we'll simply compare the vector elements as |
| | 160 | * ordinary values. The comparison function can be provided for |
| | 161 | * caller-defined orderings, such as ordering a set of objects. |
| | 162 | */ |
| | 163 | sort(descending?, comparisonFunction?); |
| | 164 | |
| | 165 | /* |
| | 166 | * Set the length - if this is shorter than the current length, |
| | 167 | * existing items will be discarded; if it's longer, the newly added |
| | 168 | * slots will be set to nil. Returns 'self'. |
| | 169 | */ |
| | 170 | setLength(newElementCount); |
| | 171 | |
| | 172 | /* |
| | 173 | * Insert one or more elements at the given index. If the index is |
| | 174 | * 1, the elements will be inserted before the first existing |
| | 175 | * element. If the index is one higher than the number of elements, |
| | 176 | * the elements will be inserted after all existing elements. |
| | 177 | * |
| | 178 | * Note that a list value argument will simply be inserted as a |
| | 179 | * single element. |
| | 180 | * |
| | 181 | * Returns 'self'. |
| | 182 | */ |
| | 183 | insertAt(startingIndex, val, ...); |
| | 184 | |
| | 185 | /* |
| | 186 | * Delete the element at the given index, reducing the length of the |
| | 187 | * vector by one element. Returns 'self'. |
| | 188 | */ |
| | 189 | removeElementAt(index); |
| | 190 | |
| | 191 | /* |
| | 192 | * Delete the range of elements starting at startingIndex and ending |
| | 193 | * at endingIndex. The elements at the ends of the range are |
| | 194 | * included in the deletion. If startingIndex == endingIndex, only |
| | 195 | * one element is removed. Reduces the length of the vector by the |
| | 196 | * number of elements removed. Returns 'self'. |
| | 197 | */ |
| | 198 | removeRange(startingIndex, endingIndex); |
| | 199 | |
| | 200 | /* |
| | 201 | * Append an element to the vector. This works just like insertAt() |
| | 202 | * with a starting index one higher than the length of the vector. |
| | 203 | * This has almost the same effect as the '+' operator, but treats a |
| | 204 | * list value like any other value by simply inserting the list as a |
| | 205 | * single new element (rather than appending each item in the list |
| | 206 | * individually, as the '+' operator would). |
| | 207 | */ |
| | 208 | append(val); |
| | 209 | |
| | 210 | /* |
| | 211 | * Prepend an element. This works like insertAt() with a starting |
| | 212 | * index of 1. |
| | 213 | */ |
| | 214 | prepend(val); |
| | 215 | |
| | 216 | /* |
| | 217 | * Append all elements from a list or vector. This works like |
| | 218 | * append(val), except that if 'val' is a list or vector, the elements |
| | 219 | * of 'val' will be appended individually, like the '+' operator does. |
| | 220 | * The difference between this method and the '+' operator is that |
| | 221 | * this method modifies this Vector by adding the new elements |
| | 222 | * directly to the existing Vector object, whereas the '+' operator |
| | 223 | * creates a new Vector to store the result. |
| | 224 | */ |
| | 225 | appendAll(val); |
| | 226 | |
| | 227 | /* |
| | 228 | * Remove an element by value. Each element of the vector matching |
| | 229 | * the given value is removed. The vector is modified in-place. The |
| | 230 | * return value is 'self'. |
| | 231 | */ |
| | 232 | removeElement(val); |
| | 233 | } |
| | 234 | |
| | 235 | #endif /* _VECTOR_H_ */ |
| | 236 | |