Array.hx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 function copy():Array<T> {
  37. return wrap(arr);
  38. }
  39. public inline function filter(f:T->Bool):Array<T> {
  40. var result = Syntax.arrayDecl();
  41. for(i in 0...length) {
  42. if(f(arr[i])) {
  43. result.push(arr[i]);
  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('strval', 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. for(i in 0...length) {
  94. result.push(f(arr[i]));
  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. for (i in 0...length) {
  108. if (arr[i] == x) {
  109. Global.array_splice(arr, i, 1);
  110. length--;
  111. return true;
  112. }
  113. }
  114. return false;
  115. }
  116. public inline function reverse():Void {
  117. arr = Global.array_reverse(arr);
  118. }
  119. public inline function shift():Null<T> {
  120. if (length > 0) length--;
  121. return Global.array_shift(arr);
  122. }
  123. public function slice(pos:Int, ?end:Int):Array<T> {
  124. if (pos < 0) pos += length;
  125. if (pos < 0) pos = 0;
  126. if (end == null) {
  127. return wrap(Global.array_slice(arr, pos));
  128. } else {
  129. if (end < 0) end += length;
  130. if (end <= pos) {
  131. return [];
  132. } else {
  133. return wrap(Global.array_slice(arr, pos, end - pos));
  134. }
  135. }
  136. }
  137. public inline function sort(f:T->T->Int):Void {
  138. arr.usort(f);
  139. }
  140. public function splice(pos:Int, len:Int):Array<T> {
  141. if (len < 0) return [];
  142. var result = wrap(Global.array_splice(arr, pos, len));
  143. length -= result.length;
  144. return result;
  145. }
  146. public inline function unshift(x:T):Void {
  147. length = Global.array_unshift(arr, x);
  148. }
  149. public function toString():String {
  150. var strings = Syntax.arrayDecl();
  151. Syntax.foreach(arr, function(index:Int, value:T) {
  152. strings[index] = Boot.stringify(value);
  153. });
  154. return '[' + Global.implode(',', strings) + ']';
  155. }
  156. @:noCompletion
  157. function offsetExists( offset:Int ) : Bool {
  158. return offset < length;
  159. }
  160. @:noCompletion
  161. function offsetGet( offset:Int ) : Ref<T> {
  162. try {
  163. return arr[offset];
  164. } catch(e:Dynamic) {
  165. return null;
  166. }
  167. }
  168. @:noCompletion
  169. function offsetSet( offset:Int, value:T ) : Void {
  170. if (length <= offset) {
  171. arr = Global.array_merge(arr, Global.array_fill(0, offset + 1 - length, null));
  172. length = offset + 1;
  173. }
  174. arr[offset] = value;
  175. }
  176. @:noCompletion
  177. function offsetUnset( offset:Int ) : Void {
  178. if (offset >= 0 && offset < length ) {
  179. Global.array_splice(arr, offset, 1);
  180. --length;
  181. }
  182. }
  183. static function wrap<T>(arr:NativeIndexedArray<T>):Array<T> {
  184. var a = new Array();
  185. a.arr = arr;
  186. a.length = Global.count(arr);
  187. return a;
  188. }
  189. }
  190. private class ArrayIterator<T> {
  191. var idx:Int;
  192. var arr:Array<T>;
  193. public inline function new(arr:Array<T>) {
  194. this.arr = arr;
  195. idx = 0;
  196. }
  197. public inline function hasNext():Bool {
  198. return idx < arr.length;
  199. }
  200. public inline function next():T {
  201. return arr[idx++];
  202. }
  203. @:keep
  204. @:phpMagic
  205. function __get(method:String) {
  206. return switch(method) {
  207. case 'hasNext', 'next': Boot.closure(this, method);
  208. case _: null;
  209. }
  210. }
  211. }
  212. /**
  213. This one is required for `Array`
  214. **/
  215. @:native('ArrayAccess')
  216. private extern interface ArrayAccess<K,V> {
  217. private function offsetExists( offset:K ) : Bool;
  218. private function offsetGet( offset:K ) : V;
  219. private function offsetSet( offset:K, value:V ) : Void;
  220. private function offsetUnset( offset:K ) : Void;
  221. }