Array.hx 6.2 KB

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