Array.hx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. @:coreApi
  23. class Array<T> {
  24. public var length(default,null) : Int;
  25. public function new() : Void {
  26. lua.Boot.defArray(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 (pos + len > this.length || len < 0) 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. (untyped this).splice(pos,0,x);
  112. }
  113. public function remove( x : T ) : Bool {
  114. for (i in 0...this.length){
  115. var a = this[i];
  116. if (a == x){
  117. if (i == 0){
  118. if (length == 1){
  119. this[0] = null;
  120. } else {
  121. this[0] = this[1];
  122. lua.Table.remove(cast this, 1);
  123. }
  124. } else {
  125. lua.Table.remove(cast this, i);
  126. }
  127. this.length-=1;
  128. return true;
  129. }
  130. }
  131. return false;
  132. }
  133. public function indexOf( x : T, ?fromIndex:Int ) : Int {
  134. if (fromIndex == null) fromIndex = 0;
  135. for (i in fromIndex...length){
  136. if (x == this[i]) return i;
  137. }
  138. return -1;
  139. }
  140. public function lastIndexOf( x : T, ?fromIndex:Int ) : Int {
  141. if (fromIndex == null) fromIndex = 0;
  142. var i = length;
  143. while(i-- > fromIndex){
  144. if (this[i] == x) return i;
  145. }
  146. return -1;
  147. }
  148. public inline function copy() : Array<T> {
  149. return [for (i in this) i];
  150. }
  151. public function map<S>(f:T->S):Array<S> {
  152. return [for (i in this) f(i)];
  153. }
  154. public function filter(f:T->Bool):Array<T> {
  155. return [for (i in this) if (f(i)) i];
  156. }
  157. public inline function iterator() : Iterator<T> {
  158. var cur_length = 0;
  159. return {
  160. hasNext : function() return cur_length < length,
  161. next : function() return this[cur_length++]
  162. }
  163. }
  164. }