Array.hx 9.0 KB

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