Array.hx 5.7 KB

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