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 php.*;
  23. using php.Global;
  24. @:coreApi
  25. final class Array<T> implements ArrayAccess<Int,T> {
  26. public var length(default, null):Int;
  27. var arr:NativeIndexedArray<T>;
  28. public function new() {
  29. arr = new NativeIndexedArray<T>();
  30. length = 0;
  31. }
  32. public function concat(a:Array<T>):Array<T> {
  33. return wrap(Global.array_merge(arr, a.arr));
  34. }
  35. public inline function copy():Array<T> {
  36. return Syntax.clone(this);
  37. }
  38. public inline function filter(f:T->Bool):Array<T> {
  39. var result = Syntax.arrayDecl();
  40. var i = 0;
  41. while(i < length) {
  42. if(f(arr[i])) {
  43. result.push(arr[i]);
  44. }
  45. i++;
  46. }
  47. return wrap(result);
  48. }
  49. public function indexOf(x:T, ?fromIndex:Int):Int {
  50. if (fromIndex == null && !Boot.isHxClosure(x) && !Boot.isNumber(x)) {
  51. var index = Global.array_search(x, arr, true);
  52. if (index == false) {
  53. return -1;
  54. } else {
  55. return index;
  56. }
  57. }
  58. if (fromIndex == null) {
  59. fromIndex = 0;
  60. } else {
  61. if (fromIndex < 0) fromIndex += length;
  62. if (fromIndex < 0) fromIndex = 0;
  63. }
  64. while (fromIndex < length) {
  65. if (arr[fromIndex] == x)
  66. return fromIndex;
  67. fromIndex++;
  68. }
  69. return -1;
  70. }
  71. public function insert(pos:Int, x:T):Void {
  72. length++;
  73. Global.array_splice(arr, pos, 0, Syntax.arrayDecl(x));
  74. }
  75. @:keep
  76. public function iterator() : Iterator<T> {
  77. return new ArrayIterator(this);
  78. }
  79. public function join(sep:String):String {
  80. return Global.implode(sep, Global.array_map(Syntax.nativeClassName(Boot) + '::stringify', arr));
  81. }
  82. public function lastIndexOf(x:T, ?fromIndex:Int):Int {
  83. if (fromIndex == null || fromIndex >= length) fromIndex = length - 1;
  84. if (fromIndex < 0) fromIndex += length;
  85. while (fromIndex >= 0) {
  86. if (arr[fromIndex] == x)
  87. return fromIndex;
  88. fromIndex--;
  89. }
  90. return -1;
  91. }
  92. public inline function map<S>(f:T->S):Array<S> {
  93. var result = Syntax.arrayDecl();
  94. var i = 0;
  95. while(i < length) {
  96. result.push(f(arr[i]));
  97. i++;
  98. }
  99. return wrap(result);
  100. }
  101. public inline function pop():Null<T> {
  102. if (length > 0) length--;
  103. return Global.array_pop(arr);
  104. }
  105. public inline function push(x:T):Int {
  106. arr[length] = x;
  107. return ++length;
  108. }
  109. public function remove(x:T):Bool {
  110. var result = false;
  111. Syntax.foreach(arr, function(index:Int, value:T) {
  112. if (value == x) {
  113. Global.array_splice(arr, index, 1);
  114. length--;
  115. result = true;
  116. Syntax.code('break');
  117. }
  118. });
  119. return result;
  120. }
  121. public inline function reverse():Void {
  122. arr = Global.array_reverse(arr);
  123. }
  124. public inline function shift():Null<T> {
  125. if (length > 0) length--;
  126. return Global.array_shift(arr);
  127. }
  128. public function slice(pos:Int, ?end:Int):Array<T> {
  129. if (pos < 0) pos += length;
  130. if (pos < 0) pos = 0;
  131. if (end == null) {
  132. return wrap(Global.array_slice(arr, pos));
  133. } else {
  134. if (end < 0) end += length;
  135. if (end <= pos) {
  136. return [];
  137. } else {
  138. return wrap(Global.array_slice(arr, pos, end - pos));
  139. }
  140. }
  141. }
  142. public inline function sort(f:T->T->Int):Void {
  143. arr.usort(f);
  144. }
  145. public function splice(pos:Int, len:Int):Array<T> {
  146. if (len < 0) return [];
  147. var result = wrap(Global.array_splice(arr, pos, len));
  148. length -= result.length;
  149. return result;
  150. }
  151. public inline function unshift(x:T):Void {
  152. length = Global.array_unshift(arr, x);
  153. }
  154. public function toString():String {
  155. return inline Boot.stringifyNativeIndexedArray(arr);
  156. }
  157. public function resize( len:Int ) : Void {
  158. if (length < len) {
  159. arr = Global.array_pad(arr, len, null);
  160. } else if (length > len) {
  161. Global.array_splice(arr, len, length - len);
  162. }
  163. length = len;
  164. }
  165. @:noCompletion
  166. function offsetExists( offset:Int ) : Bool {
  167. return offset < length;
  168. }
  169. @:noCompletion
  170. function offsetGet( offset:Int ) : Ref<T> {
  171. try {
  172. return arr[offset];
  173. } catch(e:Dynamic) {
  174. return null;
  175. }
  176. }
  177. @:noCompletion
  178. function offsetSet( offset:Int, value:T ) : Void {
  179. if (length <= offset) {
  180. if(length < offset) {
  181. arr = Global.array_pad(arr, offset + 1, null);
  182. }
  183. length = offset + 1;
  184. }
  185. arr[offset] = value;
  186. }
  187. @:noCompletion
  188. function offsetUnset( offset:Int ) : Void {
  189. if (offset >= 0 && offset < length ) {
  190. Global.array_splice(arr, offset, 1);
  191. --length;
  192. }
  193. }
  194. static function wrap<T>(arr:NativeIndexedArray<T>):Array<T> {
  195. var a = new Array();
  196. a.arr = arr;
  197. a.length = Global.count(arr);
  198. return a;
  199. }
  200. }
  201. private class ArrayIterator<T> {
  202. var idx:Int;
  203. var arr:Array<T>;
  204. public inline function new(arr:Array<T>) {
  205. this.arr = arr;
  206. idx = 0;
  207. }
  208. public inline function hasNext():Bool {
  209. return idx < arr.length;
  210. }
  211. public inline function next():T {
  212. return arr[idx++];
  213. }
  214. @:keep
  215. @:phpMagic
  216. function __get(method:String) {
  217. return switch(method) {
  218. case 'hasNext', 'next': Boot.closure(this, method);
  219. case _: null;
  220. }
  221. }
  222. }
  223. /**
  224. This one is required for `Array`
  225. **/
  226. @:native('ArrayAccess')
  227. private extern interface ArrayAccess<K,V> {
  228. private function offsetExists( offset:K ) : Bool;
  229. private function offsetGet( offset:K ) : V;
  230. private function offsetSet( offset:K, value:V ) : Void;
  231. private function offsetUnset( offset:K ) : Void;
  232. }