Array.hx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Copyright (C)2005-2019 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. import haxe.iterators.ArrayKeyValueIterator;
  23. @:coreApi
  24. class Array<T> {
  25. public var length(default, null):Int;
  26. public function new():Void {
  27. untyped _hx_tab_array(this, 0);
  28. }
  29. public function concat(a:Array<T>):Array<T> {
  30. var ret = this.copy();
  31. for (i in a)
  32. ret.push(i);
  33. return ret;
  34. }
  35. public function join(sep:String):String {
  36. var tbl:lua.Table<Int, String> = lua.Table.create();
  37. for (i in iterator()) {
  38. lua.Table.insert(tbl, Std.string(i));
  39. }
  40. return lua.Table.concat(tbl, sep);
  41. }
  42. public function pop():Null<T> {
  43. if (length == 0)
  44. return null;
  45. var ret = this[length - 1];
  46. this[length - 1] = null;
  47. length--;
  48. return ret;
  49. }
  50. public function push(x:T):Int {
  51. this[this.length] = x;
  52. return length;
  53. }
  54. public function reverse():Void {
  55. var tmp:T;
  56. var i = 0;
  57. while (i < Std.int(this.length / 2)) {
  58. tmp = this[i];
  59. this[i] = this[this.length - i - 1];
  60. this[this.length - i - 1] = tmp;
  61. i++;
  62. }
  63. }
  64. public function shift():Null<T> {
  65. if (this.length == 0)
  66. return null;
  67. var ret = this[0];
  68. if (this.length == 1) {
  69. this[0] = null;
  70. } else if (this.length > 1) {
  71. this[0] = this[1];
  72. lua.Table.remove(untyped this, 1);
  73. }
  74. this.length -= 1;
  75. return ret;
  76. }
  77. public function slice(pos:Int, ?end:Int):Array<T> {
  78. if (end == null || end > length)
  79. end = length;
  80. else if (end < 0)
  81. end = (length - (-end % length)) % length; // negative pos needs to be wrapped from the end, and mod according to array length
  82. if (pos < 0)
  83. pos = (length - (-pos % length)) % length; // and here
  84. if (pos > end || pos > length)
  85. return [];
  86. var ret = [];
  87. for (i in pos...end) {
  88. ret.push(this[i]);
  89. }
  90. return ret;
  91. }
  92. // TODO: copied from neko Array.sort, move to general util library?
  93. public function sort(f:T->T->Int):Void {
  94. var i = 0;
  95. var l = this.length;
  96. while (i < l) {
  97. var swap = false;
  98. var j = 0;
  99. var max = l - i - 1;
  100. while (j < max) {
  101. if (f(this[j], this[j + 1]) > 0) {
  102. var tmp = this[j + 1];
  103. this[j + 1] = this[j];
  104. this[j] = tmp;
  105. swap = true;
  106. }
  107. j += 1;
  108. }
  109. if (!swap)
  110. break;
  111. i += 1;
  112. }
  113. }
  114. public function splice(pos:Int, len:Int):Array<T> {
  115. if (len < 0 || pos > length)
  116. return [];
  117. else if (pos < 0)
  118. pos = length - (-pos % length);
  119. len = cast Math.min(len, this.length - pos);
  120. var ret = [];
  121. for (i in pos...(pos + len)) {
  122. ret.push(this[i]);
  123. this[i] = this[i + len];
  124. }
  125. for (i in (pos + len)...length) {
  126. this[i] = this[i + len];
  127. }
  128. this.length -= len;
  129. return ret;
  130. }
  131. public function toString():String {
  132. var tbl:lua.Table<Int, String> = lua.Table.create();
  133. lua.Table.insert(tbl, '[');
  134. lua.Table.insert(tbl, join(","));
  135. lua.Table.insert(tbl, ']');
  136. return lua.Table.concat(tbl, "");
  137. }
  138. public function unshift(x:T):Void {
  139. var len = length;
  140. for (i in 0...len)
  141. this[len - i] = this[len - i - 1];
  142. this[0] = x;
  143. }
  144. public inline function insert(pos:Int, x:T):Void {
  145. if (pos > length)
  146. pos = length;
  147. if (pos < 0) {
  148. pos = (length + pos);
  149. if (pos < 0)
  150. pos = 0;
  151. }
  152. var cur_len = length;
  153. while (cur_len > pos) {
  154. this[cur_len] = this[cur_len - 1];
  155. cur_len -= 1;
  156. }
  157. this[pos] = x;
  158. }
  159. public function remove(x:T):Bool {
  160. for (i in 0...length) {
  161. if (this[i] == x) {
  162. for (j in i...length - 1) {
  163. this[j] = this[j + 1];
  164. }
  165. // We need to decrement the length variable, and set its
  166. // value to null to avoid hanging on to a reference in the
  167. // underlying lua table.
  168. this[length - 1] = null;
  169. // Do this in two steps to avoid re-updating the __index metamethod
  170. length--;
  171. return true;
  172. }
  173. }
  174. return false;
  175. }
  176. public function contains(x:T):Bool {
  177. for (i in 0...length) {
  178. if (this[i] == x)
  179. return true;
  180. }
  181. return false;
  182. }
  183. public function indexOf(x:T, ?fromIndex:Int):Int {
  184. var end = length;
  185. if (fromIndex == null)
  186. fromIndex = 0;
  187. else if (fromIndex < 0) {
  188. fromIndex = length + fromIndex;
  189. if (fromIndex < 0)
  190. fromIndex = 0;
  191. }
  192. for (i in fromIndex...end) {
  193. if (x == this[i])
  194. return i;
  195. }
  196. return -1;
  197. }
  198. public function lastIndexOf(x:T, ?fromIndex:Int):Int {
  199. if (fromIndex == null || fromIndex >= length)
  200. fromIndex = length - 1;
  201. else if (fromIndex < 0) {
  202. fromIndex = length + fromIndex;
  203. if (fromIndex < 0)
  204. return -1;
  205. }
  206. var i = fromIndex;
  207. while (i >= 0) {
  208. if (this[i] == x)
  209. return i;
  210. else
  211. i--;
  212. }
  213. return -1;
  214. }
  215. public inline function copy():Array<T> {
  216. return [for (i in this) i];
  217. }
  218. public inline function map<S>(f:T->S):Array<S> {
  219. return [for (i in this) f(i)];
  220. }
  221. public inline function filter(f:T->Bool):Array<T> {
  222. return [for (i in this) if (f(i)) i];
  223. }
  224. public inline function iterator():haxe.iterators.ArrayIterator<T> {
  225. return new haxe.iterators.ArrayIterator(this);
  226. }
  227. public inline function keyValueIterator():ArrayKeyValueIterator<T> {
  228. return new ArrayKeyValueIterator(this);
  229. }
  230. public function resize(len:Int):Void {
  231. if (length < len) {
  232. this.length = len;
  233. } else if (length > len) {
  234. for (i in len...length) {
  235. this[i] = null;
  236. }
  237. this.length = len;
  238. }
  239. }
  240. }