Array.hx 5.8 KB

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