Array.hx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (C)2005-2016 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. @:coreApi
  23. class Array<T> {
  24. public var length(default,null) : Int;
  25. public function new() : Void {
  26. lua.Lib.defArray(cast this,0);
  27. }
  28. public function concat( a : Array<T> ) : Array<T> {
  29. var ret = this.copy();
  30. for (i in a) ret.push(i);
  31. return ret;
  32. }
  33. public function join( sep : String ) : String {
  34. var sb = new StringBuf();
  35. var first = true;
  36. for (i in iterator()){
  37. if (first) first = false;
  38. else sb.add(sep);
  39. sb.add(Std.string(i));
  40. }
  41. return sb.toString();
  42. }
  43. public function pop() : Null<T> {
  44. return this.length == 0 ? null : this[this.length-- -1];
  45. }
  46. public function push(x : T) : Int {
  47. this[this.length++] = x;
  48. return this.length;
  49. }
  50. public function reverse() : Void {
  51. var tmp:T;
  52. var i = 0;
  53. while(i < Std.int(this.length/2)){
  54. tmp = this[i];
  55. this[i] = this[this.length-i-1];
  56. this[this.length-i-1] = tmp;
  57. i++;
  58. }
  59. }
  60. public function shift() : Null<T> {
  61. if (this.length == 0) return null;
  62. var ret = this[0];
  63. for (i in 0...length){
  64. this[i] = this[i+1];
  65. }
  66. this.length-=1;
  67. return ret;
  68. }
  69. public function slice( pos : Int, ?end : Int ) : Array<T> {
  70. if (end == null || end > length) end = length;
  71. else if (end < 0) end = (length-(-end % length)) % length; // negative pos needs to be wrapped from the end, and mod according to array length
  72. if (pos < 0) pos = (length -(-pos % length)) % length; // and here
  73. if (pos > end || pos > length) return [];
  74. var ret = [];
  75. for (i in pos...end){
  76. ret.push(this[i]);
  77. }
  78. return ret;
  79. }
  80. public function sort( f : T -> T -> Int ) : Void {
  81. return haxe.ds.ArraySort.sort(this,f);
  82. }
  83. public function splice( pos : Int, len : Int ) : Array<T> {
  84. if (len < 0 || pos > length) return [];
  85. else if (pos < 0) pos = length -(-pos % length);
  86. len = cast Math.min(len,this.length-pos);
  87. var ret = [];
  88. for (i in pos...(pos+len)){
  89. ret.push(this[i]);
  90. this[i] = this[i+len];
  91. }
  92. for (i in (pos+len)...length){
  93. this[i] = this[i+len];
  94. }
  95. this.length-= len;
  96. return ret;
  97. }
  98. public function toString() : String {
  99. var sb = new StringBuf();
  100. sb.add("[");
  101. sb.add(join(","));
  102. sb.add("]");
  103. return sb.toString();
  104. }
  105. public function unshift( x : T ) : Void {
  106. var len = length;
  107. for (i in 0...len) this[len - i] = this[len - i - 1];
  108. this[0] = x;
  109. }
  110. public inline function insert( pos : Int, x : T ) : Void {
  111. if (pos > length) pos = length;
  112. if (pos < 0) {
  113. pos = (length + pos);
  114. if (pos < 0) pos = 0;
  115. }
  116. var cur_len = length;
  117. while (cur_len > pos){
  118. this[cur_len] = this[cur_len-1];
  119. cur_len -=1;
  120. }
  121. this[pos] = x;
  122. }
  123. public function remove( x : T ) : Bool {
  124. for (i in 0...length){
  125. if (this[i] == x){
  126. for (j in i...length-1){
  127. this[j] = this[j+1];
  128. }
  129. // We need to decrement the length variable, and set its
  130. // value to null to avoid hanging on to a reference in the
  131. // underlying lua table.
  132. this[length-1] = null;
  133. // Do this in two steps to avoid re-updating the __index metamethod
  134. length--;
  135. return true;
  136. }
  137. }
  138. return false;
  139. }
  140. public function indexOf( x : T, ?fromIndex:Int ) : Int {
  141. var end = length;
  142. if (fromIndex == null) fromIndex = 0;
  143. else if (fromIndex < 0 ) {
  144. fromIndex = length + fromIndex;
  145. if (fromIndex < 0) fromIndex = 0;
  146. }
  147. for (i in fromIndex...end){
  148. if (x == this[i]) return i;
  149. }
  150. return -1;
  151. }
  152. public function lastIndexOf( x : T, ?fromIndex:Int ) : Int {
  153. if (fromIndex == null || fromIndex >= length ) fromIndex = length-1;
  154. else if (fromIndex < 0) {
  155. fromIndex = length + fromIndex;
  156. if (fromIndex < 0) return -1;
  157. }
  158. var i = fromIndex;
  159. while(i >= 0){
  160. if (this[i] == x) return i;
  161. else i--;
  162. }
  163. return -1;
  164. }
  165. public inline function copy() : Array<T> {
  166. return [for (i in this) i];
  167. }
  168. public function map<S>(f:T->S):Array<S> {
  169. return [for (i in this) f(i)];
  170. }
  171. public function filter(f:T->Bool):Array<T> {
  172. return [for (i in this) if (f(i)) i];
  173. }
  174. public inline function iterator() : Iterator<T> {
  175. var cur_length = 0;
  176. return {
  177. hasNext : function() return cur_length < length,
  178. next : function() return this[cur_length++]
  179. }
  180. }
  181. private static function __init__() : Void{
  182. // table-to-array helper
  183. haxe.macro.Compiler.includeFile("lua/_lua/_hx_tab_array.lua");
  184. }
  185. }