Array.hx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 1...length){
  64. this[i] = this[i+1];
  65. }
  66. return ret;
  67. }
  68. public function slice( pos : Int, ?end : Int ) : Array<T> {
  69. if (end == null || end > length) end = length;
  70. else if (end < 0) end = length-(-end % length);
  71. if (pos < 0) pos = length -(-pos % length);
  72. if (pos > end || pos > length) return [];
  73. var ret = [];
  74. for (i in pos...end){
  75. ret.push(this[i]);
  76. }
  77. return ret;
  78. }
  79. public function sort( f : T -> T -> Int ) : Void {
  80. return haxe.ds.ArraySort.sort(this,f);
  81. }
  82. public function splice( pos : Int, len : Int ) : Array<T> {
  83. if (pos + len > this.length || len < 0) return [];
  84. else if (pos < 0) pos = length -(-pos % length);
  85. len = cast Math.min(len,this.length-pos);
  86. var ret = [];
  87. for (i in pos...(pos+len)){
  88. ret.push(this[i]);
  89. this[i] = this[i+len];
  90. }
  91. for (i in (pos+len)...length){
  92. this[i] = this[i+len];
  93. }
  94. this.length-= len;
  95. return ret;
  96. }
  97. public function toString() : String {
  98. var sb = new StringBuf();
  99. sb.add("[");
  100. sb.add(join(","));
  101. sb.add("]");
  102. return sb.toString();
  103. }
  104. public function unshift( x : T ) : Void {
  105. var len = length;
  106. for (i in 0...len) this[len - i] = this[len - i - 1];
  107. this[0] = x;
  108. }
  109. public inline function insert( pos : Int, x : T ) : Void {
  110. (untyped this).splice(pos,0,x);
  111. }
  112. public function remove( x : T ) : Bool {
  113. for (i in 0...this.length){
  114. var a = this[i];
  115. if (a == x){
  116. if (i == 0){
  117. if (length == 1){
  118. this[0] = null;
  119. } else {
  120. this[0] = this[1];
  121. lua.Table.remove(cast this, 1);
  122. }
  123. } else {
  124. lua.Table.remove(cast this, i);
  125. }
  126. this.length-=1;
  127. return true;
  128. }
  129. }
  130. return false;
  131. }
  132. public function indexOf( x : T, ?fromIndex:Int ) : Int {
  133. if (fromIndex == null) fromIndex = 0;
  134. for (i in fromIndex...length){
  135. if (x == this[i]) return i;
  136. }
  137. return -1;
  138. }
  139. public function lastIndexOf( x : T, ?fromIndex:Int ) : Int {
  140. if (fromIndex == null) fromIndex = 0;
  141. var i = length;
  142. while(i-- > fromIndex){
  143. if (this[i] == x) return i;
  144. }
  145. return -1;
  146. }
  147. public inline function copy() : Array<T> {
  148. return [for (i in this) i];
  149. }
  150. public function map<S>(f:T->S):Array<S> {
  151. return [for (i in this) f(i)];
  152. }
  153. public function filter(f:T->Bool):Array<T> {
  154. return [for (i in this) if (f(i)) i];
  155. }
  156. public inline function iterator() : Iterator<T> {
  157. var cur_length = 0;
  158. return {
  159. hasNext : function() return cur_length < length,
  160. next : function() return untyped this[cur_length++]
  161. }
  162. }
  163. }