Array.hx 5.8 KB

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