Array.hx 5.7 KB

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