File: Array.hx

package info (click to toggle)
haxe 1%3A3.2.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 23,464 kB
  • ctags: 9,612
  • sloc: ml: 83,200; ansic: 1,724; makefile: 473; java: 349; cs: 314; python: 250; sh: 43; cpp: 39; xml: 25
file content (483 lines) | stat: -rw-r--r-- 9,755 bytes parent folder | download
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
 * Copyright (C)2005-2012 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 cs.NativeArray;

#if core_api_serialize
@:meta(System.Serializable)
#end
@:final class Array<T> implements ArrayAccess<T> {

	public var length(default,null) : Int;

	private var __a:NativeArray<T>;

#if erase_generics
	inline private static function ofNative<X>(native:NativeArray<Dynamic>):Array<X>
	{
		return new Array(native);
	}
#else
	inline private static function ofNative<X>(native:NativeArray<X>):Array<X>
	{
		return new Array(native);
	}
#end

	inline private static function alloc<Y>(size:Int):Array<Y>
	{
		return new Array(new NativeArray(size));
	}

	@:overload public function new() : Void
	{
		this.length = 0;
		this.__a = new NativeArray(0);
	}

#if erase_generics
	@:overload private function new(native:NativeArray<Dynamic>)
	{
		this.length = native.Length;
		this.__a = untyped native;
	}
#else
	@:overload private function new(native:NativeArray<T>)
	{
		this.length = native.Length;
		this.__a = native;
	}
#end

	public function concat( a : Array<T> ) : Array<T>
	{
		var len = length + a.length;
		var retarr = new NativeArray(len);
		cs.system.Array.Copy(__a, 0, retarr, 0, length);
		cs.system.Array.Copy(a.__a, 0, retarr, length, a.length);

		return ofNative(retarr);
	}

	private function concatNative( a : NativeArray<T> ) : Void
	{
		var __a = __a;
		var len = length + a.Length;
		if (__a.Length >= len)
		{
			cs.system.Array.Copy(a, 0, __a, length, length);
		} else {
			var newarr = new NativeArray(len);
			cs.system.Array.Copy(__a, 0, newarr, 0, length);
			cs.system.Array.Copy(a, 0, newarr, length, a.Length);

			this.__a = newarr;
		}

		this.length = len;
	}

	public function indexOf( x : T, ?fromIndex:Int ) : Int
	{
		var len = length, i:Int = (fromIndex == null) ? 0 : fromIndex;
		if (i < 0)
		{
			i += len;
			if (i < 0) i = 0;
		}
		else if (i >= len)
		{
			return -1;
		}
		return cs.system.Array.IndexOf(__a, x, i, len - i);
	}

	public function lastIndexOf( x : T, ?fromIndex:Int ) : Int
	{
		var len = length, i:Int = (fromIndex == null) ? len - 1 : fromIndex;
		if (i >= len)
		{
			i = len - 1;
		}
		else if (i < 0)
		{
			i += len;
			if (i < 0) return -1;
		}
		return cs.system.Array.LastIndexOf(__a, x, i, i + 1);
	}

	public function join( sep : String ) : String
	{
		var buf = new StringBuf();
		var i = -1;

		var first = true;
		var length = length;
		while (++i < length)
		{
			if (first)
				first = false;
			else
				buf.add(sep);
			buf.add(__a[i]);
		}

		return buf.toString();
	}

	public function pop() : Null<T>
	{
		var __a = __a;
		var length = length;
		if (length > 0)
		{
			var val = __a[--length];
			__a[length] = null;
			this.length = length;

			return val;
		} else {
			return null;
		}
	}

	public function push(x : T) : Int
	{
		if (length >= __a.Length)
		{
			var newLen = (length << 1) + 1;
			var newarr = new NativeArray(newLen);
			__a.CopyTo(newarr, 0);

			this.__a = newarr;
		}

		__a[length] = x;
		return ++length;
	}

	public function reverse() : Void
	{
		var i = 0;
		var l = this.length;
		var a = this.__a;
		var half = l >> 1;
		l -= 1;
		while ( i < half )
		{
			var tmp = a[i];
			a[i] = a[l-i];
			a[l-i] = tmp;
			i += 1;
		}
	}

	public function shift() : Null<T>
	{
		var l = this.length;
		if( l == 0 )
			return null;

		var a = this.__a;
		var x = a[0];
		l -= 1;
		cs.system.Array.Copy(a, 1, a, 0, length-1);
		a[l] = null;
		this.length = l;

		return x;
	}

	public function slice( pos : Int, ?end : Int ) : Array<T>
	{
		if( pos < 0 ){
			pos = this.length + pos;
			if( pos < 0 )
				pos = 0;
		}
		if( end == null )
			end = this.length;
		else if( end < 0 )
			end = this.length + end;
		if( end > this.length )
			end = this.length;
		var len = end - pos;
		if ( len < 0 ) return new Array();

		var newarr = new NativeArray(len);
		cs.system.Array.Copy(__a, pos, newarr, 0, len);

		return ofNative(newarr);
	}

	public function sort( f : T -> T -> Int ) : Void
	{
		if (length == 0)
			return;
		quicksort(0, length - 1, f);
	}

	private function quicksort( lo : Int, hi : Int, f : T -> T -> Int ) : Void
	{
		var buf = __a;
		var i = lo, j = hi;
		var p = buf[(i + j) >> 1];
		while ( i <= j )
		{
			while ( i < hi && f(buf[i], p) < 0 ) i++;
			while ( j > lo && f(buf[j], p) > 0 ) j--;
			if ( i <= j )
			{
				var t = buf[i];
				buf[i++] = buf[j];
				buf[j--] = t;
			}
		}

		if( lo < j ) quicksort( lo, j, f );
		if( i < hi ) quicksort( i, hi, f );
	}

	public function splice( pos : Int, len : Int ) : Array<T>
	{
		if( len < 0 ) return new Array();
		if( pos < 0 ) {
			pos = this.length + pos;
			if( pos < 0 ) pos = 0;
		}
		if( pos > this.length ) {
			pos = 0;
			len = 0;
		} else if( pos + len > this.length ) {
			len = this.length - pos;
			if( len < 0 ) len = 0;
		}
		var a = this.__a;

		var ret = new NativeArray(len);
		cs.system.Array.Copy(a, pos, ret, 0, len);
		var ret = ofNative(ret);

		var end = pos + len;
		cs.system.Array.Copy(a, end, a, pos, this.length - end);
		this.length -= len;
		while( --len >= 0 )
			a[this.length + len] = null;
		return ret;
	}

	private function spliceVoid( pos : Int, len : Int ) : Void
	{
		if( len < 0 ) return;
		if( pos < 0 ) {
			pos = this.length + pos;
			if( pos < 0 ) pos = 0;
		}
		if( pos > this.length ) {
			pos = 0;
			len = 0;
		} else if( pos + len > this.length ) {
			len = this.length - pos;
			if( len < 0 ) len = 0;
		}
		var a = this.__a;

		var end = pos + len;
		cs.system.Array.Copy(a, end, a, pos, this.length - end);
		this.length -= len;
		while( --len >= 0 )
			a[this.length + len] = null;
	}

	public function toString() : String
	{
		var ret = new StringBuf();
		var a = __a;
		ret.add("[");
		var first = true;
		for (i in 0...length)
		{
			if (first)
				first = false;
			else
				ret.add(",");
			ret.add(a[i]);
		}

		ret.add("]");
		return ret.toString();
	}

	public function unshift( x : T ) : Void
	{
		var __a = __a;
		var length = length;
		if (length >= __a.Length)
		{
			var newLen = (length << 1) + 1;
			var newarr = new NativeArray(newLen);
			cs.system.Array.Copy(__a, 0, newarr, 1, length);

			this.__a = newarr;
		} else {
			cs.system.Array.Copy(__a, 0, __a, 1, length);
		}

		this.__a[0] = x;
		++this.length;
	}

	public function insert( pos : Int, x : T ) : Void
	{
		var l = this.length;
		if( pos < 0 ) {
			pos = l + pos;
			if( pos < 0 ) pos = 0;
		}
		if ( pos >= l ) {
			this.push(x);
			return;
		} else if (pos == 0) {
			this.unshift(x);
			return;
		}

		if (l >= __a.Length)
		{
			var newLen = (length << 1) + 1;
			var newarr = new NativeArray(newLen);
			cs.system.Array.Copy(__a, 0, newarr, 0, pos);
			newarr[pos] = x;
			cs.system.Array.Copy(__a, pos, newarr, pos + 1, l - pos);

			this.__a = newarr;
			++this.length;
		} else {
			var __a = __a;
			cs.system.Array.Copy(__a, pos, __a, pos + 1, l - pos);
			cs.system.Array.Copy(__a, 0, __a, 0, pos);
			__a[pos] = x;
			++this.length;
		}
	}

	public function remove( x : T ) : Bool
	{
		var __a = __a;
		var i = -1;
		var length = length;
		while (++i < length)
		{
			if (__a[i] == x)
			{
				cs.system.Array.Copy(__a, i + 1, __a, i, length - i - 1);
				__a[--this.length] = null;

				return true;
			}
		}

		return false;
	}

	public function map<S>( f : T -> S ) : Array<S> {
		var ret = [];
		for (elt in this)
			ret.push(f(elt));
		return ret;
	}

	public function filter( f : T -> Bool ) : Array<T> {
		var ret = [];
		for (elt in this)
			if (f(elt))
				ret.push(elt);
		return ret;
	}

	public function copy() : Array<T>
	{
		var len = length;
		var __a = __a;
		var newarr = new NativeArray(len);
		cs.system.Array.Copy(__a, 0, newarr, 0, len);
		return ofNative(newarr);
	}

	public inline function iterator() : Iterator<T>
	{
		return new ArrayIterator<T>(this);
	}

	private function __get(idx:Int):T
	{
		return if ((cast idx : UInt) >= length) null else __a[idx];
	}

	private function __set(idx:Int, v:T):T
	{
		var idx:UInt = idx;
		var __a = __a;
		if (idx >= __a.Length)
		{
			var len = idx + 1;
			if (idx == __a.Length)
				len = (idx << 1) + 1;
			var newArr = new NativeArray<T>(len);
			__a.CopyTo(newArr, 0);
			this.__a = __a = newArr;
		}

		if (idx >= length)
			this.length = idx + 1;

		return __a[idx] = v;
	}

	private inline function __unsafe_get(idx:Int):T
	{
		return __a[idx];
	}

	private inline function __unsafe_set(idx:Int, val:T):T
	{
		return __a[idx] = val;
	}
}

@:final
private class ArrayIterator<T>
{
	var arr:Array<T>;
	var len:Int;
	var i:Int;

	public inline function new(a:Array<T>)
	{
		arr = a;
		len = a.length;
		i = 0;
	}

	public inline function hasNext():Bool return i < len;
	public inline function next():T return arr[i++];
}