|
| 1 | +/* |
| 2 | + * Copyright (C)2005-2019 Haxe Foundation |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | + * copy of this software and associated documentation files (the "Software"), |
| 6 | + * to deal in the Software without restriction, including without limitation |
| 7 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | + * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | + * Software is furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 | + * DEALINGS IN THE SOFTWARE. |
| 21 | + */ |
| 22 | + |
| 23 | +/** |
| 24 | + An Array is a storage for values. You can access it using indexes or |
| 25 | + with its API. |
| 26 | +
|
| 27 | + @see https://haxe.org/manual/std-Array.html |
| 28 | + @see https://haxe.org/manual/lf-array-comprehension.html |
| 29 | +**/ |
| 30 | + |
| 31 | +import haxe.iterators.ArrayKeyValueIterator; |
| 32 | + |
| 33 | +extern class Array<T> { |
| 34 | + /** |
| 35 | + The length of `this` Array. |
| 36 | + **/ |
| 37 | + var length(default, null):Int; |
| 38 | + |
| 39 | + /** |
| 40 | + Creates a new Array. |
| 41 | + **/ |
| 42 | + function new():Void; |
| 43 | + |
| 44 | + /** |
| 45 | + Returns a new Array by appending the elements of `a` to the elements of |
| 46 | + `this` Array. |
| 47 | +
|
| 48 | + This operation does not modify `this` Array. |
| 49 | +
|
| 50 | + If `a` is the empty Array `[]`, a copy of `this` Array is returned. |
| 51 | +
|
| 52 | + The length of the returned Array is equal to the sum of `this.length` |
| 53 | + and `a.length`. |
| 54 | +
|
| 55 | + If `a` is `null`, the result is unspecified. |
| 56 | + **/ |
| 57 | + function concat(a:Array<T>):Array<T>; |
| 58 | + |
| 59 | + /** |
| 60 | + Returns a string representation of `this` Array, with `sep` separating |
| 61 | + each element. |
| 62 | +
|
| 63 | + The result of this operation is equal to `Std.string(this[0]) + sep + |
| 64 | + Std.string(this[1]) + sep + ... + sep + Std.string(this[this.length-1])` |
| 65 | +
|
| 66 | + If `this` is the empty Array `[]`, the result is the empty String `""`. |
| 67 | + If `this` has exactly one element, the result is equal to a call to |
| 68 | + `Std.string(this[0])`. |
| 69 | +
|
| 70 | + If `sep` is null, the result is unspecified. |
| 71 | + **/ |
| 72 | + function join(sep:String):String; |
| 73 | + |
| 74 | + /** |
| 75 | + Removes the last element of `this` Array and returns it. |
| 76 | +
|
| 77 | + This operation modifies `this` Array in place. |
| 78 | +
|
| 79 | + If `this` has at least one element, `this.length` will decrease by 1. |
| 80 | +
|
| 81 | + If `this` is the empty Array `[]`, null is returned and the length |
| 82 | + remains 0. |
| 83 | + **/ |
| 84 | + function pop():Null<T>; |
| 85 | + |
| 86 | + /** |
| 87 | + Adds the element `x` at the end of `this` Array and returns the new |
| 88 | + length of `this` Array. |
| 89 | +
|
| 90 | + This operation modifies `this` Array in place. |
| 91 | +
|
| 92 | + `this.length` increases by 1. |
| 93 | + **/ |
| 94 | + function push(x:T):Int; |
| 95 | + |
| 96 | + /** |
| 97 | + Reverse the order of elements of `this` Array. |
| 98 | +
|
| 99 | + This operation modifies `this` Array in place. |
| 100 | +
|
| 101 | + If `this.length < 2`, `this` remains unchanged. |
| 102 | + **/ |
| 103 | + function reverse():Void; |
| 104 | + |
| 105 | + /** |
| 106 | + Removes the first element of `this` Array and returns it. |
| 107 | +
|
| 108 | + This operation modifies `this` Array in place. |
| 109 | +
|
| 110 | + If `this` has at least one element, `this`.length and the index of each |
| 111 | + remaining element is decreased by 1. |
| 112 | +
|
| 113 | + If `this` is the empty Array `[]`, `null` is returned and the length |
| 114 | + remains 0. |
| 115 | + **/ |
| 116 | + function shift():Null<T>; |
| 117 | + |
| 118 | + /** |
| 119 | + Creates a shallow copy of the range of `this` Array, starting at and |
| 120 | + including `pos`, up to but not including `end`. |
| 121 | +
|
| 122 | + This operation does not modify `this` Array. |
| 123 | +
|
| 124 | + The elements are not copied and retain their identity. |
| 125 | +
|
| 126 | + If `end` is omitted or exceeds `this.length`, it defaults to the end of |
| 127 | + `this` Array. |
| 128 | +
|
| 129 | + If `pos` or `end` are negative, their offsets are calculated from the |
| 130 | + end of `this` Array by `this.length + pos` and `this.length + end` |
| 131 | + respectively. If this yields a negative value, 0 is used instead. |
| 132 | +
|
| 133 | + If `pos` exceeds `this.length` or if `end` is less than or equals |
| 134 | + `pos`, the result is `[]`. |
| 135 | + **/ |
| 136 | + function slice(pos:Int, ?end:Int):Array<T>; |
| 137 | + |
| 138 | + /** |
| 139 | + Sorts `this` Array according to the comparison function `f`, where |
| 140 | + `f(x,y)` returns 0 if x == y, a positive Int if x > y and a |
| 141 | + negative Int if x < y. |
| 142 | +
|
| 143 | + This operation modifies `this` Array in place. |
| 144 | +
|
| 145 | + The sort operation is not guaranteed to be stable, which means that the |
| 146 | + order of equal elements may not be retained. For a stable Array sorting |
| 147 | + algorithm, `haxe.ds.ArraySort.sort()` can be used instead. |
| 148 | +
|
| 149 | + If `f` is null, the result is unspecified. |
| 150 | + **/ |
| 151 | + function sort(f:T->T->Int):Void; |
| 152 | + |
| 153 | + /** |
| 154 | + Removes `len` elements from `this` Array, starting at and including |
| 155 | + `pos`, an returns them. |
| 156 | +
|
| 157 | + This operation modifies `this` Array in place. |
| 158 | +
|
| 159 | + If `len` is < 0 or `pos` exceeds `this`.length, an empty Array [] is |
| 160 | + returned and `this` Array is unchanged. |
| 161 | +
|
| 162 | + If `pos` is negative, its value is calculated from the end of `this` |
| 163 | + Array by `this.length + pos`. If this yields a negative value, 0 is |
| 164 | + used instead. |
| 165 | +
|
| 166 | + If the sum of the resulting values for `len` and `pos` exceed |
| 167 | + `this.length`, this operation will affect the elements from `pos` to the |
| 168 | + end of `this` Array. |
| 169 | +
|
| 170 | + The length of the returned Array is equal to the new length of `this` |
| 171 | + Array subtracted from the original length of `this` Array. In other |
| 172 | + words, each element of the original `this` Array either remains in |
| 173 | + `this` Array or becomes an element of the returned Array. |
| 174 | + **/ |
| 175 | + function splice(pos:Int, len:Int):Array<T>; |
| 176 | + |
| 177 | + /** |
| 178 | + Returns a string representation of `this` Array. |
| 179 | +
|
| 180 | + The result will include the individual elements' String representations |
| 181 | + separated by comma. The enclosing [ ] may be missing on some platforms, |
| 182 | + use `Std.string()` to get a String representation that is consistent |
| 183 | + across platforms. |
| 184 | + **/ |
| 185 | + function toString():String; |
| 186 | + |
| 187 | + /** |
| 188 | + Adds the element `x` at the start of `this` Array. |
| 189 | +
|
| 190 | + This operation modifies `this` Array in place. |
| 191 | +
|
| 192 | + `this.length` and the index of each Array element increases by 1. |
| 193 | + **/ |
| 194 | + function unshift(x:T):Void; |
| 195 | + |
| 196 | + /** |
| 197 | + Inserts the element `x` at the position `pos`. |
| 198 | +
|
| 199 | + This operation modifies `this` Array in place. |
| 200 | +
|
| 201 | + The offset is calculated like so: |
| 202 | +
|
| 203 | + - If `pos` exceeds `this.length`, the offset is `this.length`. |
| 204 | + - If `pos` is negative, the offset is calculated from the end of `this` |
| 205 | + Array, i.e. `this.length + pos`. If this yields a negative value, the |
| 206 | + offset is 0. |
| 207 | + - Otherwise, the offset is `pos`. |
| 208 | +
|
| 209 | + If the resulting offset does not exceed `this.length`, all elements from |
| 210 | + and including that offset to the end of `this` Array are moved one index |
| 211 | + ahead. |
| 212 | + **/ |
| 213 | + function insert(pos:Int, x:T):Void; |
| 214 | + |
| 215 | + /** |
| 216 | + Removes the first occurrence of `x` in `this` Array. |
| 217 | +
|
| 218 | + This operation modifies `this` Array in place. |
| 219 | +
|
| 220 | + If `x` is found by checking standard equality, it is removed from `this` |
| 221 | + Array and all following elements are reindexed accordingly. The function |
| 222 | + then returns true. |
| 223 | +
|
| 224 | + If `x` is not found, `this` Array is not changed and the function |
| 225 | + returns false. |
| 226 | + **/ |
| 227 | + function remove(x:T):Bool; |
| 228 | + |
| 229 | + |
| 230 | + /** |
| 231 | + Returns whether `this` Array contains `x`. |
| 232 | +
|
| 233 | + If `x` is found by checking standard equality, the function returns `true`, otherwise |
| 234 | + the function returns `false`. |
| 235 | + **/ |
| 236 | + @:pure function contains( x : T ) : Bool; |
| 237 | + |
| 238 | + /** |
| 239 | + Returns position of the first occurrence of `x` in `this` Array, searching front to back. |
| 240 | +
|
| 241 | + If `x` is found by checking standard equality, the function returns its index. |
| 242 | +
|
| 243 | + If `x` is not found, the function returns -1. |
| 244 | +
|
| 245 | + If `fromIndex` is specified, it will be used as the starting index to search from, |
| 246 | + otherwise search starts with zero index. If it is negative, it will be taken as the |
| 247 | + offset from the end of `this` Array to compute the starting index. If given or computed |
| 248 | + starting index is less than 0, the whole array will be searched, if it is greater than |
| 249 | + or equal to the length of `this` Array, the function returns -1. |
| 250 | + **/ |
| 251 | + function indexOf(x:T, ?fromIndex:Int):Int; |
| 252 | + |
| 253 | + /** |
| 254 | + Returns position of the last occurrence of `x` in `this` Array, searching back to front. |
| 255 | +
|
| 256 | + If `x` is found by checking standard equality, the function returns its index. |
| 257 | +
|
| 258 | + If `x` is not found, the function returns -1. |
| 259 | +
|
| 260 | + If `fromIndex` is specified, it will be used as the starting index to search from, |
| 261 | + otherwise search starts with the last element index. If it is negative, it will be |
| 262 | + taken as the offset from the end of `this` Array to compute the starting index. If |
| 263 | + given or computed starting index is greater than or equal to the length of `this` Array, |
| 264 | + the whole array will be searched, if it is less than 0, the function returns -1. |
| 265 | + **/ |
| 266 | + function lastIndexOf(x:T, ?fromIndex:Int):Int; |
| 267 | + |
| 268 | + /** |
| 269 | + Returns a shallow copy of `this` Array. |
| 270 | +
|
| 271 | + The elements are not copied and retain their identity, so |
| 272 | + `a[i] == a.copy()[i]` is true for any valid `i`. However, |
| 273 | + `a == a.copy()` is always false. |
| 274 | + **/ |
| 275 | + function copy():Array<T>; |
| 276 | + |
| 277 | + /** |
| 278 | + Returns an iterator of the Array values. |
| 279 | + **/ |
| 280 | + @:runtime inline function iterator():haxe.iterators.ArrayIterator<T> { |
| 281 | + return new haxe.iterators.ArrayIterator(this); |
| 282 | + } |
| 283 | + |
| 284 | + /** |
| 285 | + Returns an iterator of the Array indices and values. |
| 286 | + **/ |
| 287 | + @:pure @:runtime public inline function keyValueIterator() : ArrayKeyValueIterator<T> { |
| 288 | + return new ArrayKeyValueIterator(this); |
| 289 | + } |
| 290 | + |
| 291 | + /** |
| 292 | + Creates a new Array by applying function `f` to all elements of `this`. |
| 293 | +
|
| 294 | + The order of elements is preserved. |
| 295 | +
|
| 296 | + If `f` is null, the result is unspecified. |
| 297 | + **/ |
| 298 | + @:runtime inline function map<S>(f:T->S):Array<S> { |
| 299 | + #if (cpp && !cppia) |
| 300 | + var result = cpp.NativeArray.create(length); |
| 301 | + for (i in 0...length) cpp.NativeArray.unsafeSet(result, i, f(cpp.NativeArray.unsafeGet(this, i))); |
| 302 | + return result; |
| 303 | + #else |
| 304 | + return [for (v in this) f(v)]; |
| 305 | + #end |
| 306 | + } |
| 307 | + |
| 308 | + /** |
| 309 | + Returns an Array containing those elements of `this` for which `f` |
| 310 | + returned true. |
| 311 | +
|
| 312 | + The individual elements are not duplicated and retain their identity. |
| 313 | +
|
| 314 | + If `f` is null, the result is unspecified. |
| 315 | + **/ |
| 316 | + @:runtime inline function filter(f:T->Bool):Array<T> { |
| 317 | + return [for (v in this) if (f(v)) v]; |
| 318 | + } |
| 319 | + |
| 320 | + /** |
| 321 | + Set the length of the Array. |
| 322 | +
|
| 323 | + If `len` is shorter than the array's current size, the last |
| 324 | + `length - len` elements will be removed. If `len` is longer, the Array |
| 325 | + will be extended, with new elements set to a target-specific default |
| 326 | + value: |
| 327 | +
|
| 328 | + - always null on dynamic targets |
| 329 | + - 0, 0.0 or false for Int, Float and Bool respectively on static targets |
| 330 | + - null for other types on static targets |
| 331 | + **/ |
| 332 | + function resize(len:Int):Void; |
| 333 | +} |
0 commit comments