Array.hx 6.1 KB

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