Array.hx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 [a] 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 will increase 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 robust: Equal elements will retain their order.
  107. If [f] is null, the result is unspecified.
  108. **/
  109. function sort( f : T -> T -> Int ) : Void;
  110. /**
  111. Removes [len] elements from [this] Array, starting at and including
  112. [pos], an returns them.
  113. This operation modifies [this] Array in place.
  114. If [len] is < 0 or [pos] exceeds [this].length, the result is the empty
  115. Array [].
  116. If [pos] is negative, its value is calculated from the end of [this]
  117. Array by [this].length + [pos]. If this yields a negative value, 0 is
  118. used instead.
  119. If the sum of the resulting values for [len] and [pos] exceed
  120. [this].length, this operation will affect the elements from [pos] to the
  121. end of [this] Array.
  122. The length of the returned Array is equal to the new length of [this]
  123. Array subtracted from the original length of [this] Array. In other
  124. words, each element of the original [this] Array either remains in
  125. [this] Array or becomes an element of the returned Array.
  126. **/
  127. function splice( pos : Int, len : Int ) : Array<T>;
  128. /**
  129. Returns a string representation of [this] Array.
  130. The result will include the individual elements' String representations
  131. separated by comma. The enclosing [ ] may be missing on some platforms,
  132. use Std.string() to get a String representation that is consistent
  133. across platforms.
  134. **/
  135. function toString() : String;
  136. /**
  137. Adds the element [x] at the start of [this] Array.
  138. This operation modifies [this] Array in place.
  139. [this].length and the index of each Array element increases by 1.
  140. **/
  141. function unshift( x : T ) : Void;
  142. /**
  143. Inserts the element [x] at the position [pos].
  144. This operation modifies [this] Array in place.
  145. The offset is calculated like so:
  146. - If [pos] exceeds [this].length, the offset is [this].length.
  147. - If [pos] is negative, the offset is calculated from the end of [this]
  148. Array, i.e. [this].length + [pos]. If this yields a negative value,
  149. the offset is 0.
  150. - Otherwise, the offset is [pos].
  151. If the resulting offset does not exceed [this].length, all elements from
  152. and including that offset to the end of [this] Array are moved one index
  153. ahead.
  154. **/
  155. function insert( pos : Int, x : T ) : Void;
  156. /**
  157. Removes the first occurence of [x] in [this] Array.
  158. This operation modifies [this] Array in place.
  159. If [x] is found by checking standard equality, it is removed from [this]
  160. Array and all following elements are reindexed acoordingly. The function
  161. then returns true.
  162. If [x] is not found, [this] Array is not changed and the function
  163. returns false.
  164. **/
  165. function remove( x : T ) : Bool;
  166. /**
  167. Returns a shallow copy of [this] Array.
  168. The elements are not copied and retain their identity, so
  169. a[i] == a.copy()[i] is true for any valid i. However, a == a.copy() is
  170. always false.
  171. **/
  172. function copy() : Array<T>;
  173. /**
  174. Returns an iterator of the Array values.
  175. **/
  176. function iterator() : Iterator<T>;
  177. }