Array.hx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. An Array is a storage for values. You can access it using indexes or
  24. with its API.
  25. @see https://haxe.org/manual/std-Array.html
  26. @see https://haxe.org/manual/lf-array-comprehension.html
  27. **/
  28. extern class Array<T> {
  29. /**
  30. The length of `this` Array.
  31. **/
  32. var length(default, null):Int;
  33. /**
  34. Creates a new Array.
  35. **/
  36. function new():Void;
  37. /**
  38. Returns a new Array by appending the elements of `a` to the elements of
  39. `this` Array.
  40. This operation does not modify `this` Array.
  41. If `a` is the empty Array `[]`, a copy of `this` Array is returned.
  42. The length of the returned Array is equal to the sum of `this.length`
  43. and `a.length`.
  44. If `a` is `null`, the result is unspecified.
  45. **/
  46. function concat(a:Array<T>):Array<T>;
  47. /**
  48. Returns a string representation of `this` Array, with `sep` separating
  49. each element.
  50. The result of this operation is equal to `Std.string(this[0]) + sep +
  51. Std.string(this[1]) + sep + ... + sep + Std.string(this[this.length-1])`
  52. If `this` is the empty Array `[]`, the result is the empty String `""`.
  53. If `this` has exactly one element, the result is equal to a call to
  54. `Std.string(this[0])`.
  55. If `sep` is null, the result is unspecified.
  56. **/
  57. function join(sep:String):String;
  58. /**
  59. Removes the last element of `this` Array and returns it.
  60. This operation modifies `this` Array in place.
  61. If `this` has at least one element, `this.length` will decrease by 1.
  62. If `this` is the empty Array `[]`, null is returned and the length
  63. remains 0.
  64. **/
  65. function pop():Null<T>;
  66. /**
  67. Adds the element `x` at the end of `this` Array and returns the new
  68. length of `this` Array.
  69. This operation modifies `this` Array in place.
  70. `this.length` increases by 1.
  71. **/
  72. function push(x:T):Int;
  73. /**
  74. Reverse the order of elements of `this` Array.
  75. This operation modifies `this` Array in place.
  76. If `this.length < 2`, `this` remains unchanged.
  77. **/
  78. function reverse():Void;
  79. /**
  80. Removes the first element of `this` Array and returns it.
  81. This operation modifies `this` Array in place.
  82. If `this` has at least one element, `this`.length and the index of each
  83. remaining element is decreased by 1.
  84. If `this` is the empty Array `[]`, `null` is returned and the length
  85. remains 0.
  86. **/
  87. function shift():Null<T>;
  88. /**
  89. Creates a shallow copy of the range of `this` Array, starting at and
  90. including `pos`, up to but not including `end`.
  91. This operation does not modify `this` Array.
  92. The elements are not copied and retain their identity.
  93. If `end` is omitted or exceeds `this.length`, it defaults to the end of
  94. `this` Array.
  95. If `pos` or `end` are negative, their offsets are calculated from the
  96. end of `this` Array by `this.length + pos` and `this.length + end`
  97. respectively. If this yields a negative value, 0 is used instead.
  98. If `pos` exceeds `this.length` or if `end` is less than or equals
  99. `pos`, the result is `[]`.
  100. **/
  101. function slice(pos:Int, ?end:Int):Array<T>;
  102. /**
  103. Sorts `this` Array according to the comparison function `f`, where
  104. `f(x,y)` returns 0 if x == y, a positive Int if x > y and a
  105. negative Int if x < y.
  106. This operation modifies `this` Array in place.
  107. The sort operation is not guaranteed to be stable, which means that the
  108. order of equal elements may not be retained. For a stable Array sorting
  109. algorithm, `haxe.ds.ArraySort.sort()` can be used instead.
  110. If `f` is null, the result is unspecified.
  111. **/
  112. function sort(f:T->T->Int):Void;
  113. /**
  114. Removes `len` elements from `this` Array, starting at and including
  115. `pos`, an returns them.
  116. This operation modifies `this` Array in place.
  117. If `len` is < 0 or `pos` exceeds `this`.length, an empty Array [] is
  118. returned and `this` Array is unchanged.
  119. If `pos` is negative, its value is calculated from the end of `this`
  120. Array by `this.length + pos`. If this yields a negative value, 0 is
  121. used instead.
  122. If the sum of the resulting values for `len` and `pos` exceed
  123. `this.length`, this operation will affect the elements from `pos` to the
  124. end of `this` Array.
  125. The length of the returned Array is equal to the new length of `this`
  126. Array subtracted from the original length of `this` Array. In other
  127. words, each element of the original `this` Array either remains in
  128. `this` Array or becomes an element of the returned Array.
  129. **/
  130. function splice(pos:Int, len:Int):Array<T>;
  131. /**
  132. Returns a string representation of `this` Array.
  133. The result will include the individual elements' String representations
  134. separated by comma. The enclosing [ ] may be missing on some platforms,
  135. use `Std.string()` to get a String representation that is consistent
  136. across platforms.
  137. **/
  138. function toString():String;
  139. /**
  140. Adds the element `x` at the start of `this` Array.
  141. This operation modifies `this` Array in place.
  142. `this.length` and the index of each Array element increases by 1.
  143. **/
  144. function unshift(x:T):Void;
  145. /**
  146. Inserts the element `x` at the position `pos`.
  147. This operation modifies `this` Array in place.
  148. The offset is calculated like so:
  149. - If `pos` exceeds `this.length`, the offset is `this.length`.
  150. - If `pos` is negative, the offset is calculated from the end of `this`
  151. Array, i.e. `this.length + pos`. If this yields a negative value, the
  152. offset is 0.
  153. - Otherwise, the offset is `pos`.
  154. If the resulting offset does not exceed `this.length`, all elements from
  155. and including that offset to the end of `this` Array are moved one index
  156. ahead.
  157. **/
  158. function insert(pos:Int, x:T):Void;
  159. /**
  160. Removes the first occurrence of `x` in `this` Array.
  161. This operation modifies `this` Array in place.
  162. If `x` is found by checking standard equality, it is removed from `this`
  163. Array and all following elements are reindexed accordingly. The function
  164. then returns true.
  165. If `x` is not found, `this` Array is not changed and the function
  166. returns false.
  167. **/
  168. function remove(x:T):Bool;
  169. /**
  170. Returns position of the first occurrence of `x` in `this` Array, searching front to back.
  171. If `x` is found by checking standard equality, the function returns its index.
  172. If `x` is not found, the function returns -1.
  173. If `fromIndex` is specified, it will be used as the starting index to search from,
  174. otherwise search starts with zero index. If it is negative, it will be taken as the
  175. offset from the end of `this` Array to compute the starting index. If given or computed
  176. starting index is less than 0, the whole array will be searched, if it is greater than
  177. or equal to the length of `this` Array, the function returns -1.
  178. **/
  179. function indexOf(x:T, ?fromIndex:Int):Int;
  180. /**
  181. Returns position of the last occurrence of `x` in `this` Array, searching back to front.
  182. If `x` is found by checking standard equality, the function returns its index.
  183. If `x` is not found, the function returns -1.
  184. If `fromIndex` is specified, it will be used as the starting index to search from,
  185. otherwise search starts with the last element index. If it is negative, it will be
  186. taken as the offset from the end of `this` Array to compute the starting index. If
  187. given or computed starting index is greater than or equal to the length of `this` Array,
  188. the whole array will be searched, if it is less than 0, the function returns -1.
  189. **/
  190. function lastIndexOf(x:T, ?fromIndex:Int):Int;
  191. /**
  192. Returns a shallow copy of `this` Array.
  193. The elements are not copied and retain their identity, so
  194. `a[i] == a.copy()[i]` is true for any valid `i`. However,
  195. `a == a.copy()` is always false.
  196. **/
  197. function copy():Array<T>;
  198. /**
  199. Returns an iterator of the Array values.
  200. **/
  201. @:runtime inline function iterator():haxe.iterators.ArrayIterator<T> {
  202. return new haxe.iterators.ArrayIterator(this);
  203. }
  204. /**
  205. Creates a new Array by applying function `f` to all elements of `this`.
  206. The order of elements is preserved.
  207. If `f` is null, the result is unspecified.
  208. **/
  209. @:runtime inline function map<S>(f:T->S):Array<S> {
  210. #if (cpp && !cppia)
  211. var result = cpp.NativeArray.create(length);
  212. for (i in 0...length) cpp.NativeArray.unsafeSet(result, i, f(cpp.NativeArray.unsafeGet(this, i)));
  213. return result;
  214. #else
  215. return [for (v in this) f(v)];
  216. #end
  217. }
  218. /**
  219. Returns an Array containing those elements of `this` for which `f`
  220. returned true.
  221. The individual elements are not duplicated and retain their identity.
  222. If `f` is null, the result is unspecified.
  223. **/
  224. @:runtime inline function filter(f:T->Bool):Array<T> {
  225. return [for (v in this) if (f(v)) v];
  226. }
  227. /**
  228. Set the length of the Array.
  229. If `len` is shorter than the array's current size, the last
  230. `length - len` elements will be removed. If `len` is longer, the Array
  231. will be extended, with new elements set to a target-specific default
  232. value:
  233. - always null on dynamic targets
  234. - 0, 0.0 or false for Int, Float and Bool respectively on static targets
  235. - null for other types on static targets
  236. **/
  237. function resize(len:Int):Void;
  238. }