Array.hx 7.4 KB

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