Array.hx 5.6 KB

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