1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
import php.*;
import php.ArrayIterator as NativeArrayIterator;
import haxe.iterators.ArrayKeyValueIterator;
using php.Global;
@:coreApi
final class Array<T> implements ArrayAccess<Int, T> implements IteratorAggregate<T> implements Countable implements JsonSerializable<NativeIndexedArray<T>> {
public var length(default, null):Int;
var arr:NativeIndexedArray<T>;
public function new() {
arr = new NativeIndexedArray<T>();
length = 0;
}
public function concat(a:Array<T>):Array<T> {
return wrap(Global.array_merge(arr, a.arr));
}
public inline function copy():Array<T> {
return Syntax.clone(this);
}
public inline function filter(f:T->Bool):Array<T> {
var result = Syntax.arrayDecl();
for(item in arr) {
if (f(item)) {
result.push(item);
}
}
return wrap(result);
}
public inline function contains(x:T):Bool {
return indexOf(x) != -1;
}
public function indexOf(x:T, ?fromIndex:Int):Int {
if (fromIndex == null && !Boot.isHxClosure(x) && !Boot.isNumber(x)) {
var index = Global.array_search(x, arr, true);
if (index == false) {
return -1;
} else {
return index;
}
}
if (fromIndex == null) {
fromIndex = 0;
} else {
if (fromIndex < 0)
fromIndex += length;
if (fromIndex < 0)
fromIndex = 0;
}
while (fromIndex < length) {
if (arr[fromIndex] == x)
return fromIndex;
fromIndex++;
}
return -1;
}
public function insert(pos:Int, x:T):Void {
length++;
Global.array_splice(arr, pos, 0, Syntax.arrayDecl(x));
}
@:ifFeature("dynamic_read.iterator", "anon_optional_read.iterator", "anon_read.iterator")
public inline function iterator():haxe.iterators.ArrayIterator<T> {
return new haxe.iterators.ArrayIterator(this);
}
@:keep
public inline function keyValueIterator():ArrayKeyValueIterator<T> {
return new ArrayKeyValueIterator(this);
}
public function join(sep:String):String {
return Global.implode(sep, Global.array_map(Syntax.nativeClassName(Boot) + '::stringify', arr));
}
public function lastIndexOf(x:T, ?fromIndex:Int):Int {
if (fromIndex == null || fromIndex >= length)
fromIndex = length - 1;
if (fromIndex < 0)
fromIndex += length;
while (fromIndex >= 0) {
if (arr[fromIndex] == x)
return fromIndex;
fromIndex--;
}
return -1;
}
public inline function map<S>(f:T->S):Array<S> {
var result = Syntax.arrayDecl();
for(item in arr) {
result.push(f(item));
}
return wrap(result);
}
public inline function pop():Null<T> {
if (length > 0)
length--;
return Global.array_pop(arr);
}
public inline function push(x:T):Int {
arr[length++] = x;
return length;
}
public function remove(x:T):Bool {
var result = false;
for(index in 0...length) {
if (arr[index] == x) {
Global.array_splice(arr, index, 1);
length--;
result = true;
break;
}
}
return result;
}
public inline function reverse():Void {
arr = Global.array_reverse(arr);
}
public inline function shift():Null<T> {
if (length > 0)
length--;
return Global.array_shift(arr);
}
public function slice(pos:Int, ?end:Int):Array<T> {
if (pos < 0)
pos += length;
if (pos < 0)
pos = 0;
if (end == null) {
return wrap(Global.array_slice(arr, pos));
} else {
if (end < 0)
end += length;
if (end <= pos) {
return [];
} else {
return wrap(Global.array_slice(arr, pos, end - pos));
}
}
}
public inline function sort(f:T->T->Int):Void {
arr.usort(f);
}
public function splice(pos:Int, len:Int):Array<T> {
if (len < 0)
return [];
var result = wrap(Global.array_splice(arr, pos, len));
length -= result.length;
return result;
}
public inline function unshift(x:T):Void {
length = Global.array_unshift(arr, x);
}
public function toString():String {
return inline Boot.stringifyNativeIndexedArray(arr);
}
public function resize(len:Int):Void {
if (length < len) {
arr = Global.array_pad(arr, len, null);
} else if (length > len) {
Global.array_splice(arr, len, length - len);
}
length = len;
}
@:noCompletion @:keep
function offsetExists(offset:Int):Bool {
return offset < length;
}
@:noCompletion @:keep
function offsetGet(offset:Int):Ref<T> {
try {
return arr[offset];
} catch (e:Dynamic) {
return null;
}
}
@:noCompletion @:keep
function offsetSet(offset:Int, value:T):Void {
if (length <= offset) {
for(i in length...offset + 1) {
arr[i] = null;
}
length = offset + 1;
}
arr[offset] = value;
Syntax.code("return {0}", value);
}
@:noCompletion @:keep
function offsetUnset(offset:Int):Void {
if (offset >= 0 && offset < length) {
Global.array_splice(arr, offset, 1);
--length;
}
}
@:noCompletion @:keep
private function getIterator():Traversable {
return new NativeArrayIterator(arr);
}
@:noCompletion @:keep
@:native('count') //to not interfere with `Lambda.count`
private function _hx_count():Int {
return length;
}
@:noCompletion @:keep
function jsonSerialize():NativeIndexedArray<T> {
return arr;
}
static function wrap<T>(arr:NativeIndexedArray<T>):Array<T> {
var a = new Array();
a.arr = arr;
a.length = Global.count(arr);
return a;
}
}
/**
Following interfaces are required to make `Array` mimic native arrays for usage
from a 3rd party PHP code.
**/
@:native('ArrayAccess')
private extern interface ArrayAccess<K, V> {
private function offsetExists(offset:K):Bool;
private function offsetGet(offset:K):V;
private function offsetSet(offset:K, value:V):Void;
private function offsetUnset(offset:K):Void;
}
@:native('JsonSerializable')
private extern interface JsonSerializable<T> {
private function jsonSerialize():T;
}
@:native('IteratorAggregate')
private extern interface IteratorAggregate<T> extends Traversable {
private function getIterator():Traversable;
}
@:native('Countable')
private extern interface Countable {
@:native('count') //to not interfere with `Lambda.count`
private function _hx_count():Int;
}
|