-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArray.cpp
More file actions
1279 lines (1116 loc) · 37.7 KB
/
Array.cpp
File metadata and controls
1279 lines (1116 loc) · 37.7 KB
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <hxcpp.h>
#include <vector>
#include <cpp/Pointer.h>
#ifdef HXCPP_TELEMETRY
extern void __hxt_new_array(void* obj, int size);
#endif
using namespace hx;
// -------- ArrayBase -------------------------------------
namespace hx
{
Array<Dynamic> ArrayBase::__new(int inSize,int inReserve)
{ return Array<Dynamic>(new Array_obj<Dynamic>(inSize,inReserve)); }
ArrayBase::ArrayBase(int inSize,int inReserve,int inElementSize,bool inAtomic)
{
length = inSize;
int alloc = inSize < inReserve ? inReserve : inSize;
if (alloc)
{
mBase = (char *)hx::InternalNew(alloc * inElementSize,false);
HX_OBJ_WB_GET(this,mBase);
#ifdef HXCPP_TELEMETRY
__hxt_new_array(mBase, alloc * inElementSize);
#endif
}
else
mBase = 0;
mAlloc = alloc;
mArrayConvertId = inAtomic ? inElementSize :
inElementSize==sizeof(String) ? aciStringArray : aciObjectArray;
}
void ArrayBase::reserve(int inSize) const
{
if (mAlloc<inSize)
{
int elemSize = GetElementSize();
int bytes = inSize * elemSize;
if (mBase)
{
bool wasUnamanaged = mAlloc<0;
if (wasUnamanaged)
{
char *base=(char *)hx::InternalNew(bytes,false);
memcpy(base,mBase,length*elemSize);
mBase = base;
}
else
mBase = (char *)hx::InternalRealloc(length*elemSize,mBase, bytes );
}
else
{
mBase = (char *)hx::InternalNew(bytes,false);
#ifdef HXCPP_TELEMETRY
__hxt_new_array(mBase, bytes);
#endif
}
mAlloc = inSize;
HX_OBJ_WB_GET(const_cast<ArrayBase *>(this),mBase);
}
}
void ArrayBase::Realloc(int inSize) const
{
// Try to detect "push" case vs resizing to big array size explicitly by looking at gap
bool pushCase = (inSize<=mAlloc + 16);
if (!pushCase)
{
reserve(inSize);
}
else if (pushCase)
{
int newAlloc = inSize;
unsigned int elemSize = GetElementSize();
unsigned int minBytes = inSize*elemSize + 8;
unsigned int roundup = 64;
while(roundup<minBytes)
roundup<<=1;
if (roundup>64)
{
int half = 3*(roundup>>2);
if (minBytes<half)
roundup = half;
}
unsigned int bytes = roundup-8;
if (mBase)
{
bool wasUnamanaged = mAlloc<0;
if (wasUnamanaged)
{
char *base=(char *)hx::InternalNew(bytes,false);
memcpy(base,mBase,length*elemSize);
mBase = base;
}
else
{
mBase = (char *)hx::InternalRealloc(length*elemSize,mBase, bytes, true);
bytes = hx::ObjectSizeSafe(mBase);
}
}
else
{
mBase = (char *)hx::InternalNew(bytes,false);
#ifdef HXCPP_TELEMETRY
__hxt_new_array(mBase, bytes);
#endif
}
mAlloc = bytes/elemSize;
HX_OBJ_WB_GET(const_cast<ArrayBase *>(this),mBase);
}
}
// Set numeric values to 0, pointers to null, bools to false
void ArrayBase::zero(Dynamic inFirst, Dynamic inCount)
{
int first = inFirst==null() ? 0 : inFirst->__ToInt();
if (first<0)
first+=length;
if (first<0 || first>=length)
return;
int count = length;
if (inCount!=null())
count = inCount->__ToInt();
if (count<0)
count += length;
if (count<0)
return;
if (first+count > length)
count = length - first;
int size = GetElementSize();
memset(mBase + first*size, 0, count*size);
}
int ArrayBase::Memcmp(ArrayBase *inOther)
{
int bytesA = length * GetElementSize();
int bytesB = inOther->length * inOther->GetElementSize();
int common = bytesA<bytesB ? bytesA : bytesB;
int result = memcmp(mBase, inOther->mBase, common);
if (result)
return result;
return bytesA - bytesB;
}
void ArrayBase::Blit(int inDestElement, ArrayBase *inSourceArray, int inSourceElement, int inElementCount)
{
int srcSize = inSourceArray->GetElementSize();
int srcElems = inSourceArray->length;
if (inDestElement<0 || inSourceElement<0 || inSourceElement+inElementCount>srcElems)
hx::Throw( HX_CSTRING("blit out of bounds") );
if (srcSize!=GetElementSize())
hx::Throw( HX_CSTRING("blit array mismatch") );
int newSize = inDestElement + inElementCount;
if (newSize>length)
resize(newSize);
const char *src = inSourceArray->mBase + inSourceElement*srcSize;
char *dest = mBase + inDestElement*srcSize;
int len = inElementCount*srcSize;
if (src+len < dest || dest+len<src)
memcpy(dest,src,len);
else
memmove(dest,src,len);
HX_OBJ_WB_PESSIMISTIC_GET(this);
}
int ArrayBase::__Compare(const hx::Object *inRHS) const
{
if (inRHS==this)
return 0;
if (inRHS->__GetType()!=vtArray)
return -1;
ArrayCommon *common = (ArrayCommon *)inRHS;
hx::Object *implementation = common->__GetRealObject();
return implementation<this ? -1 : implementation!=this;
}
String ArrayBase::__ToString() const { return HX_CSTRING("Array"); }
String ArrayBase::toString()
{
// Byte-array (not bool!)
if (IsByteArray())
{
return String( (const char *) mBase, length);
}
return HX_CSTRING("[") + __join(HX_CSTRING(",")) + HX_CSTRING("]");
}
void ArrayBase::__SetSizeExact(int inSize)
{
if (inSize==0)
{
InternalReleaseMem(mBase);
mBase = 0;
mAlloc = length = 0;
}
else if (inSize!=length || inSize!=mAlloc)
{
int elemSize = GetElementSize();
int bytes = inSize * elemSize;
if (mBase)
{
bool wasUnamanaged = mAlloc<0;
if (wasUnamanaged)
{
char *base=(char *)(AllocAtomic() ? hx::NewGCPrivate(0,bytes) : hx::NewGCBytes(0,bytes));
memcpy(base,mBase,std::min(length,inSize)*elemSize);
mBase = base;
}
else
mBase = (char *)hx::InternalRealloc(length*elemSize,mBase, bytes );
}
else if (AllocAtomic())
{
mBase = (char *)hx::NewGCPrivate(0,bytes);
#ifdef HXCPP_TELEMETRY
__hxt_new_array(mBase, bytes);
#endif
}
else
{
mBase = (char *)hx::NewGCBytes(0,bytes);
#ifdef HXCPP_TELEMETRY
__hxt_new_array(mBase, bytes);
#endif
}
mAlloc = length = inSize;
HX_OBJ_WB_GET(this,mBase);
}
}
Dynamic ArrayBase::__unsafe_get(const Dynamic &i)
{
return __GetItem(i);
}
Dynamic ArrayBase::__unsafe_set(const Dynamic &i, const Dynamic &val)
{
return __SetItem(i,val);
}
void ArrayBase::Insert(int inPos)
{
if (inPos>=length)
resize(length+1);
else
{
resize(length+1);
int s = GetElementSize();
memmove(mBase + inPos*s + s, mBase+inPos*s, (length-inPos-1)*s );
}
}
void ArrayBase::Splice(ArrayBase *outResult,int inPos,int inLen)
{
if (inPos>=length)
{
return;
}
else if (inPos<0)
{
inPos += length;
if (inPos<0)
inPos =0;
}
if (inLen<=0)
return;
if (inPos+inLen>length)
inLen = length - inPos;
int s = GetElementSize();
if (outResult)
{
outResult->resize(inLen);
memcpy(outResult->mBase, mBase+inPos*s, s*inLen);
// todo - only needed if we have dirty pointer elements
HX_OBJ_WB_PESSIMISTIC_GET(outResult);
}
memmove(mBase+inPos*s, mBase + (inPos+inLen)*s, (length-(inPos+inLen))*s);
resize(length-inLen);
}
void ArrayBase::Slice(ArrayBase *outResult,int inPos,int inEnd)
{
if (inPos<0)
{
inPos += length;
if (inPos<0)
inPos =0;
}
if (inEnd<0)
inEnd += length;
if (inEnd>length)
inEnd = length;
int n = inEnd - inPos;
if (n<=0)
outResult->resize(0);
else
{
outResult->resize(n);
int s = GetElementSize();
memcpy(outResult->mBase, mBase+inPos*s, n*s);
// todo - only needed if we have dirty pointer elements
HX_OBJ_WB_PESSIMISTIC_GET(outResult);
}
}
void ArrayBase::RemoveElement(int inPos)
{
if (inPos<length)
{
int s = GetElementSize();
memmove(mBase + inPos*s, mBase+inPos*s + s, (length-inPos-1)*s );
resize(length-1);
}
}
void ArrayBase::Concat(ArrayBase *outResult,const char *inSecond,int inLen)
{
char *ptr = outResult->GetBase();
int n = length * GetElementSize();
memcpy(ptr,mBase,n);
ptr += n;
memcpy(ptr,inSecond,inLen*GetElementSize());
HX_OBJ_WB_PESSIMISTIC_GET(this);
}
String ArrayBase::joinArray(Array_obj<String> *inArray, String inSeparator)
{
int length = inArray->length;
if (length==0)
return HX_CSTRING("");
int len = 0;
bool isWChar = false;
for(int i=0;i<length;i++)
{
String strI = inArray->__unsafe_get(i);
if (strI.raw_ptr())
{
len += strI.length;
#ifdef HX_SMART_STRINGS
if (strI.isUTF16Encoded())
isWChar = true;
#endif
}
else
len += 4;
}
len += (length-1) * inSeparator.length;
#ifdef HX_SMART_STRINGS
bool sepIsWide = inSeparator.isUTF16Encoded();
if (isWChar || sepIsWide)
{
char16_t *buf = String::allocChar16Ptr(len);
int pos = 0;
bool separated = inSeparator.length>0;
for(int i=0;i<length;i++)
{
String strI = inArray->__unsafe_get(i);
if (!strI.raw_ptr())
{
memcpy(buf+pos,u"null",8);
pos+=4;
}
else if(strI.length==0)
{
// ignore
}
else if (strI.isUTF16Encoded())
{
memcpy(buf+pos,strI.raw_wptr(),strI.length*sizeof(char16_t));
pos += strI.length;
}
else
{
const char *ptr = strI.raw_ptr();
for(int c=0;c<strI.length;c++)
buf[pos++] = ptr[c];
}
if (separated && (i+1<length) )
{
if (sepIsWide)
{
memcpy(buf+pos,inSeparator.raw_ptr(),inSeparator.length*sizeof(char16_t));
pos += inSeparator.length;
}
else
{
const char *ptr = inSeparator.raw_ptr();
for(int c=0;c<inSeparator.length;c++)
buf[pos++] = ptr[c];
}
}
}
buf[len] = '\0';
String result(buf,len);
return result;
}
#endif
{
char *buf = hx::NewString(len);
int pos = 0;
bool separated = inSeparator.length>0;
for(int i=0;i<length;i++)
{
String strI = inArray->__unsafe_get(i);
if (!strI.raw_ptr())
{
memcpy(buf+pos,"null",4);
pos+=4;
}
else
{
memcpy(buf+pos,strI.raw_ptr(),strI.length*sizeof(char));
pos += strI.length;
}
if (separated && (i+1<length) )
{
memcpy(buf+pos,inSeparator.raw_ptr(),inSeparator.length*sizeof(char));
pos += inSeparator.length;
}
}
buf[len] = '\0';
return String(buf,len);
}
}
int _hx_toString_depth = 0;
String ArrayBase::joinArray(ArrayBase *inBase, String inSeparator)
{
if (_hx_toString_depth >= 5)
return HX_CSTRING("...");
int length = inBase->length;
if (length==0)
return HX_CSTRING("");
Array<String> stringArray = Array_obj<String>::__new(length, length);
_hx_toString_depth++;
try
{
for(int i=0;i<length;i++)
stringArray->__unsafe_set(i, inBase->ItemString(i));
_hx_toString_depth--;
}
catch (...)
{
_hx_toString_depth--;
throw;
}
return stringArray->join(inSeparator);
}
void ArrayBase::safeSort(DynamicSorterFunc inSorter, bool inIsString)
{
if (inIsString)
hx::SafeSorter<String>::Sort((String *)mBase, length,inSorter);
else
hx::SafeSorter<Dynamic>::Sort((Dynamic *)mBase, length,inSorter);
}
#if (HXCPP_API_LEVEL<500)
#define ARRAY_RUN_FUNC(ret,func,dynamic_arg_list,arg_list) \
Dynamic __run(dynamic_arg_list) \
{ \
ret mThis->__##func(arg_list); return Dynamic(); \
}
#define DEFINE_ARRAY_FUNC(ret,func,array_list,run_func,ARG_C) \
struct ArrayBase_##func : public hx::Object \
{ \
HX_IS_INSTANCE_OF enum { _hx_ClassId = hx::clsIdClosure }; \
bool __IsFunction() const { return true; } \
ArrayBase *mThis; \
ArrayBase_##func(ArrayBase *inThis) : mThis(inThis) { } \
String toString() const{ return HX_CSTRING(#func) ; } \
String __ToString() const{ return HX_CSTRING(#func) ; } \
int __GetType() const { return vtFunction; } \
void *__GetHandle() const { return mThis; } \
int __ArgCount() const { return ARG_C; } \
void __Mark(hx::MarkContext *__inCtx) { HX_MARK_MEMBER(mThis); } \
ARRAY_VISIT_FUNC \
Dynamic __Run(const Array<Dynamic> &inArgs) \
{ \
ret mThis->__##func(array_list); return Dynamic(); \
} \
run_func \
int __Compare(const hx::Object *inRHS) const \
{ \
if (!dynamic_cast<const ArrayBase_##func *>(inRHS)) return -1; \
return (mThis==inRHS->__GetHandle() ? 0 : -1); \
} \
}; \
Dynamic ArrayBase::func##_dyn() { return new ArrayBase_##func(this); }
#define DEFINE_ARRAY_FUNC0(ret,func) DEFINE_ARRAY_FUNC(ret,func,HX_ARR_LIST0,ARRAY_RUN_FUNC(ret,func,HX_DYNAMIC_ARG_LIST0,HX_ARG_LIST0),0)
#define DEFINE_ARRAY_FUNC1(ret,func) DEFINE_ARRAY_FUNC(ret,func,HX_ARR_LIST1,ARRAY_RUN_FUNC(ret,func,HX_DYNAMIC_ARG_LIST1,HX_ARG_LIST1),1)
#define DEFINE_ARRAY_FUNC2(ret,func) DEFINE_ARRAY_FUNC(ret,func,HX_ARR_LIST2,ARRAY_RUN_FUNC(ret,func,HX_DYNAMIC_ARG_LIST2,HX_ARG_LIST2),2)
#define DEFINE_ARRAY_FUNC3(ret,func) DEFINE_ARRAY_FUNC(ret,func,HX_ARR_LIST3,ARRAY_RUN_FUNC(ret,func,HX_DYNAMIC_ARG_LIST3,HX_ARG_LIST3),3)
#define DEFINE_ARRAY_FUNC4(ret,func) DEFINE_ARRAY_FUNC(ret,func,HX_ARR_LIST4,ARRAY_RUN_FUNC(ret,func,HX_DYNAMIC_ARG_LIST4,HX_ARG_LIST4),4)
DEFINE_ARRAY_FUNC1(,__SetSize);
DEFINE_ARRAY_FUNC1(,__SetSizeExact);
DEFINE_ARRAY_FUNC2(,insert);
DEFINE_ARRAY_FUNC0(,reverse);
DEFINE_ARRAY_FUNC1(,sort);
DEFINE_ARRAY_FUNC1(,unshift);
DEFINE_ARRAY_FUNC4(,blit);
DEFINE_ARRAY_FUNC2(,zero);
DEFINE_ARRAY_FUNC1(,resize);
DEFINE_ARRAY_FUNC1(return,concat);
DEFINE_ARRAY_FUNC0(return,iterator);
DEFINE_ARRAY_FUNC0(return,keyValueIterator);
DEFINE_ARRAY_FUNC1(return,join);
DEFINE_ARRAY_FUNC0(return,pop);
DEFINE_ARRAY_FUNC0(return,copy);
DEFINE_ARRAY_FUNC1(return,push);
DEFINE_ARRAY_FUNC1(return,contains);
DEFINE_ARRAY_FUNC1(return,remove);
DEFINE_ARRAY_FUNC1(return,removeAt);
DEFINE_ARRAY_FUNC2(return,indexOf);
DEFINE_ARRAY_FUNC2(return,lastIndexOf);
DEFINE_ARRAY_FUNC0(return,shift);
DEFINE_ARRAY_FUNC2(return,slice);
DEFINE_ARRAY_FUNC2(return,splice);
DEFINE_ARRAY_FUNC0(return,toString);
DEFINE_ARRAY_FUNC1(return,map);
DEFINE_ARRAY_FUNC1(return,filter);
DEFINE_ARRAY_FUNC1(return,__unsafe_get);
DEFINE_ARRAY_FUNC2(return,__unsafe_set);
DEFINE_ARRAY_FUNC1(return,memcmp);
hx::Val ArrayBase::__Field(const String& inString, hx::PropertyAccess inCallProp)
{
if (inString==HX_CSTRING("length")) return (int)size();
if (inString==HX_CSTRING("concat")) return concat_dyn();
if (inString==HX_CSTRING("insert")) return insert_dyn();
if (inString==HX_CSTRING("copy")) return copy_dyn();
if (inString==HX_CSTRING("iterator")) return iterator_dyn();
if (inString==HX_CSTRING("keyValueIterator")) return keyValueIterator_dyn();
if (inString==HX_CSTRING("join")) return join_dyn();
if (inString==HX_CSTRING("pop")) return pop_dyn();
if (inString==HX_CSTRING("push")) return push_dyn();
if (inString==HX_CSTRING("contains")) return contains_dyn();
if (inString==HX_CSTRING("remove")) return remove_dyn();
if (inString==HX_CSTRING("removeAt")) return removeAt_dyn();
if (inString==HX_CSTRING("indexOf")) return indexOf_dyn();
if (inString==HX_CSTRING("lastIndexOf")) return lastIndexOf_dyn();
if (inString==HX_CSTRING("reverse")) return reverse_dyn();
if (inString==HX_CSTRING("shift")) return shift_dyn();
if (inString==HX_CSTRING("splice")) return splice_dyn();
if (inString==HX_CSTRING("slice")) return slice_dyn();
if (inString==HX_CSTRING("sort")) return sort_dyn();
if (inString==HX_CSTRING("toString")) return toString_dyn();
if (inString==HX_CSTRING("unshift")) return unshift_dyn();
if (inString==HX_CSTRING("filter")) return filter_dyn();
if (inString==HX_CSTRING("map")) return map_dyn();
if (inString==HX_CSTRING("__SetSize")) return __SetSize_dyn();
if (inString==HX_CSTRING("__SetSizeExact")) return __SetSizeExact_dyn();
if (inString==HX_CSTRING("__unsafe_get")) return __unsafe_get_dyn();
if (inString==HX_CSTRING("__unsafe_set")) return __unsafe_set_dyn();
if (inString==HX_CSTRING("blit")) return blit_dyn();
if (inString==HX_CSTRING("zero")) return zero_dyn();
if (inString==HX_CSTRING("memcmp")) return memcmp_dyn();
if (inString==HX_CSTRING("_hx_storeType")) return (int)getStoreType();
if (inString==HX_CSTRING("_hx_elementSize")) return (int)GetElementSize();
if (inString==HX_CSTRING("_hx_pointer")) return cpp::CreateDynamicPointer((void *)mBase);
if (inString==HX_CSTRING("resize")) return resize_dyn();
return null();
}
#endif
#if (HXCPP_API_LEVEL>=500)
hx::Val ArrayBase::__pointerToBase()
{
return cpp::CreateDynamicPointer(mBase);
}
#endif
static String sArrayFields[] = {
HX_CSTRING("length"),
HX_CSTRING("concat"),
HX_CSTRING("insert"),
HX_CSTRING("iterator"),
HX_CSTRING("keyValueIterator"),
HX_CSTRING("join"),
HX_CSTRING("copy"),
HX_CSTRING("pop"),
HX_CSTRING("push"),
HX_CSTRING("contains"),
HX_CSTRING("remove"),
HX_CSTRING("removeAt"),
HX_CSTRING("indexOf"),
HX_CSTRING("lastIndexOf"),
HX_CSTRING("reverse"),
HX_CSTRING("shift"),
HX_CSTRING("slice"),
HX_CSTRING("splice"),
HX_CSTRING("sort"),
HX_CSTRING("toString"),
HX_CSTRING("unshift"),
HX_CSTRING("filter"),
HX_CSTRING("map"),
HX_CSTRING("resize"),
String(null())
};
// TODO;
hx::Class ArrayBase::__mClass;
Dynamic ArrayCreateEmpty() { return new Array<Dynamic>(0,0); }
Dynamic ArrayCreateArgs(DynamicArray inArgs)
{
return inArgs->__copy();
}
static bool ArrayCanCast(hx::Object *inInstance)
{
return inInstance->__GetClass().mPtr == ArrayBase::__mClass.mPtr;
}
void ArrayBase::__boot()
{
Static(__mClass) = hx::_hx_RegisterClass(HX_CSTRING("Array"),ArrayCanCast,sNone,sArrayFields,
ArrayCreateEmpty,ArrayCreateArgs,0,0);
}
bool DynamicEq(const Dynamic &a, const Dynamic &b)
{
// ? return hx::IsInstanceEq(a,b);
return hx::IsEq(a,b);
}
// -------- ArrayIterator -------------------------------------
} // End namespace hx
namespace cpp
{
#if (HXCPP_API_LEVEL>=500)
::hx::Callable<bool()> IteratorBase::hasNext_dyn()
{
struct _hx_iterator_hasNext final : public ::hx::AutoCallable_obj<bool()>
{
::hx::ObjectPtr<IteratorBase> __this;
_hx_iterator_hasNext(IteratorBase* ptr) : __this(ptr)
{
HX_OBJ_WB_NEW_MARKED_OBJECT(this);
}
bool HX_LOCAL_RUN() final override
{
return __this->hasNext();
}
void* __GetHandle() const override
{
return __this.GetPtr();
}
void __Mark(hx::MarkContext* __inCtx) final override
{
HX_MARK_MEMBER(__this);
}
#ifdef HXCPP_VISIT_ALLOCS
void __Visit(hx::VisitContext* __inCtx)
{
HX_VISIT_MEMBER(__this);
}
#endif
};
return new _hx_iterator_hasNext(this);
}
::hx::Callable<::Dynamic()> IteratorBase::next_dyn()
{
struct _hx_iterator_next : public ::hx::AutoCallable_obj<::Dynamic()>
{
::hx::ObjectPtr<IteratorBase> __this;
_hx_iterator_next(IteratorBase* ptr) : __this(ptr)
{
HX_OBJ_WB_NEW_MARKED_OBJECT(this);
}
::Dynamic HX_LOCAL_RUN() final override
{
return __this->_dynamicNext();
}
void* __GetHandle() const override
{
return __this.GetPtr();
}
void __Mark(hx::MarkContext* __inCtx) final override
{
HX_MARK_MEMBER(__this);
}
#ifdef HXCPP_VISIT_ALLOCS
void __Visit(hx::VisitContext* __inCtx)
{
HX_VISIT_MEMBER(__this);
}
#endif
};
return new _hx_iterator_next(this);
}
hx::Val IteratorBase::__Field(const String &inString, hx::PropertyAccess inCallProp)
{
if (inString==HX_CSTRING("hasNext")) return hasNext_dyn();
if (inString==HX_CSTRING("next")) return next_dyn();
return null();
}
#else
HX_DEFINE_DYNAMIC_FUNC0(IteratorBase, hasNext, return)
HX_DEFINE_DYNAMIC_FUNC0(IteratorBase, _dynamicNext, return)
Dynamic IteratorBase::next_dyn()
{
return hx::CreateMemberFunction0("next", this, __IteratorBase_dynamicNext);
}
hx::Val IteratorBase::__Field(const String & inString, hx::PropertyAccess inCallProp)
{
if (inString == HX_CSTRING("hasNext")) return hasNext_dyn();
if (inString == HX_CSTRING("next")) return _dynamicNext_dyn();
return null();
}
#endif
}
#ifdef HX_VARRAY_DEFINED
// -------- VirtualArray -------------------------------------
namespace cpp
{
#if (HXCPP_API_LEVEL>=500)
#define HX_VARRAY_ARG_LIST0
#define HX_VARRAY_ARG_LIST1(arg0) arg0
#define HX_VARRAY_ARG_LIST2(arg0, arg1) arg0, arg1
#define HX_VARRAY_ARG_LIST3(arg0, arg1, arg2) arg0, arg1, arg2
#define HX_VARRAY_ARG_LIST4(arg0, arg1, arg2, arg3) arg0, arg1, arg2, arg3
#define HX_VARRAY_FUNC_LIST0
#define HX_VARRAY_FUNC_LIST1(arg0) arg0 inArg0
#define HX_VARRAY_FUNC_LIST2(arg0, arg1) arg0 inArg0, arg1 inArg1
#define HX_VARRAY_FUNC_LIST3(arg0, arg1, arg2) arg0 inArg0, arg1 inArg1, arg2 inArg2
#define HX_VARRAY_FUNC_LIST4(arg0, arg1, arg2, arg3) arg0 inArg0, arg1 inArg1, arg2 inArg2, arg3 inArg3
#define HX_VARRAY_FUNC(ret, value, name, args_list, func_list, args_call) \
::hx::Callable<value(args_list)> VirtualArray_obj::name##_dyn() \
{ \
struct _hx_virtualarray_##name final : public ::hx::AutoCallable_obj<value(args_list)> \
{ \
VirtualArray mThis; \
_hx_virtualarray_##name(::cpp::VirtualArray inThis) : mThis(inThis) \
{ \
HX_OBJ_WB_NEW_MARKED_OBJECT(this); \
} \
value _hx_run(func_list) override \
{ \
ret mThis->name(args_call); \
} \
void* __GetHandle() const override { return mThis.GetPtr(); } \
void __Mark(hx::MarkContext *__inCtx) { HX_MARK_MEMBER(mThis); } \
ARRAY_VISIT_FUNC \
int __Compare(const ::hx::Object* inRhs) const override \
{ \
auto casted = dynamic_cast<const _hx_virtualarray_##name *>(inRhs); \
if (!casted) return 1; \
if (mThis != casted->mThis) return -1; \
return 0; \
} \
}; \
return new _hx_virtualarray_##name(this); \
}
HX_VARRAY_FUNC(return, ::cpp::VirtualArray, concat, HX_VARRAY_ARG_LIST1(::cpp::VirtualArray), HX_VARRAY_FUNC_LIST1(::cpp::VirtualArray), HX_ARG_LIST1);
HX_VARRAY_FUNC(return, ::cpp::VirtualArray, copy, HX_VARRAY_ARG_LIST0, HX_VARRAY_FUNC_LIST0, HX_ARG_LIST0);
HX_VARRAY_FUNC(, void, insert, HX_VARRAY_ARG_LIST2(int, ::Dynamic), HX_VARRAY_FUNC_LIST2(int, ::Dynamic), HX_ARG_LIST2);
HX_VARRAY_FUNC(return, ::Dynamic, iterator, HX_VARRAY_ARG_LIST0, HX_VARRAY_FUNC_LIST0, HX_ARG_LIST0);
HX_VARRAY_FUNC(return, ::Dynamic, keyValueIterator, HX_VARRAY_ARG_LIST0, HX_VARRAY_FUNC_LIST0, HX_ARG_LIST0);
HX_VARRAY_FUNC(return, ::String, join, HX_VARRAY_ARG_LIST1(::String), HX_VARRAY_FUNC_LIST1(::String), HX_ARG_LIST1);
HX_VARRAY_FUNC(return, ::Dynamic, pop, HX_VARRAY_ARG_LIST0, HX_VARRAY_FUNC_LIST0, HX_ARG_LIST0);
HX_VARRAY_FUNC(return, bool, contains, HX_VARRAY_ARG_LIST1(::Dynamic), HX_VARRAY_FUNC_LIST1(::Dynamic), HX_ARG_LIST1);
HX_VARRAY_FUNC(return, bool, remove, HX_VARRAY_ARG_LIST1(::Dynamic), HX_VARRAY_FUNC_LIST1(::Dynamic), HX_ARG_LIST1);
HX_VARRAY_FUNC(return, bool, removeAt, HX_VARRAY_ARG_LIST1(int), HX_VARRAY_FUNC_LIST1(int), HX_ARG_LIST1);
HX_VARRAY_FUNC(return, int, indexOf, HX_VARRAY_ARG_LIST2(::Dynamic, ::Dynamic), HX_VARRAY_FUNC_LIST2(::Dynamic, ::Dynamic), HX_ARG_LIST2);
HX_VARRAY_FUNC(return, int, lastIndexOf, HX_VARRAY_ARG_LIST2(::Dynamic, ::Dynamic), HX_VARRAY_FUNC_LIST2(::Dynamic, ::Dynamic), HX_ARG_LIST2);
HX_VARRAY_FUNC(, void, reverse, HX_VARRAY_ARG_LIST0, HX_VARRAY_FUNC_LIST0, HX_ARG_LIST0);
HX_VARRAY_FUNC(return, ::Dynamic, shift, HX_VARRAY_ARG_LIST0, HX_VARRAY_FUNC_LIST0, HX_ARG_LIST0);
HX_VARRAY_FUNC(return, ::cpp::VirtualArray, slice, HX_VARRAY_ARG_LIST2(int, ::Dynamic), HX_VARRAY_FUNC_LIST2(int, ::Dynamic), HX_ARG_LIST2);
HX_VARRAY_FUNC(return, ::cpp::VirtualArray, splice, HX_VARRAY_ARG_LIST2(int, int), HX_VARRAY_FUNC_LIST2(int, int), HX_ARG_LIST2);
HX_VARRAY_FUNC(return, void, sort, HX_VARRAY_ARG_LIST1(hx::ArrayBase::DynamicSorterFunc), HX_VARRAY_FUNC_LIST1(hx::ArrayBase::DynamicSorterFunc), HX_ARG_LIST1);
HX_VARRAY_FUNC(return, ::String, toString, HX_VARRAY_ARG_LIST0, HX_VARRAY_FUNC_LIST0, HX_ARG_LIST0);
HX_VARRAY_FUNC(, void, unshift, HX_VARRAY_ARG_LIST1(::Dynamic), HX_VARRAY_FUNC_LIST1(::Dynamic), HX_ARG_LIST1);
HX_VARRAY_FUNC(return, ::cpp::VirtualArray, map, HX_VARRAY_ARG_LIST1(hx::ArrayBase::DynamicMappingFunc), HX_VARRAY_FUNC_LIST1(hx::ArrayBase::DynamicMappingFunc), HX_ARG_LIST1);
HX_VARRAY_FUNC(return, ::cpp::VirtualArray, filter, HX_VARRAY_ARG_LIST1(hx::ArrayBase::DynamicFilterFunc), HX_VARRAY_FUNC_LIST1(hx::ArrayBase::DynamicFilterFunc), HX_ARG_LIST1);
HX_VARRAY_FUNC(, void, __SetSize, HX_VARRAY_ARG_LIST1(int), HX_VARRAY_FUNC_LIST1(int), HX_ARG_LIST1);
HX_VARRAY_FUNC(, void, __SetSizeExact, HX_VARRAY_ARG_LIST1(int), HX_VARRAY_FUNC_LIST1(int), HX_ARG_LIST1);
HX_VARRAY_FUNC(return, ::Dynamic, __unsafe_get, HX_VARRAY_ARG_LIST1(::Dynamic), HX_VARRAY_FUNC_LIST1(::Dynamic), HX_ARG_LIST1);
HX_VARRAY_FUNC(return, ::Dynamic, __unsafe_set, HX_VARRAY_ARG_LIST2(::Dynamic, ::Dynamic), HX_VARRAY_FUNC_LIST2(::Dynamic, ::Dynamic), HX_ARG_LIST2);
HX_VARRAY_FUNC(, void, blit, HX_VARRAY_ARG_LIST4(int, ::cpp::VirtualArray, int, int), HX_VARRAY_FUNC_LIST4(int, ::cpp::VirtualArray, int, int), HX_ARG_LIST4);
HX_VARRAY_FUNC(, void, zero, HX_VARRAY_ARG_LIST2(::Dynamic, ::Dynamic), HX_VARRAY_FUNC_LIST2(::Dynamic, ::Dynamic), HX_ARG_LIST2);
HX_VARRAY_FUNC(, void, memcmp, HX_VARRAY_ARG_LIST1(::cpp::VirtualArray), HX_VARRAY_FUNC_LIST1(::cpp::VirtualArray), HX_ARG_LIST1);
HX_VARRAY_FUNC(, void, resize, HX_VARRAY_ARG_LIST1(int), HX_VARRAY_FUNC_LIST1(int), HX_ARG_LIST1);
#else
#define VARRAY_RUN_FUNC(ret,func,dynamic_arg_list,arg_list) \
Dynamic __run(dynamic_arg_list) \
{ \
ret mThis->func(arg_list); return Dynamic(); \
}
#define DEFINE_VARRAY_FUNC(ret, func,array_list,run_func,ARG_C) \
struct VirtualArray_##func : public hx::Object \
{ \
HX_IS_INSTANCE_OF enum { _hx_ClassId = hx::clsIdClosure }; \
bool __IsFunction() const { return true; } \
VirtualArray mThis; \
VirtualArray_##func(VirtualArray inThis) : mThis(inThis) { \
HX_OBJ_WB_NEW_MARKED_OBJECT(this); \
} \
String toString() const{ return HX_CSTRING(#func) ; } \
String __ToString() const{ return HX_CSTRING(#func) ; } \
int __GetType() const { return vtFunction; } \
void *__GetHandle() const { return mThis.mPtr; } \
int __ArgCount() const { return ARG_C; } \
void __Mark(hx::MarkContext *__inCtx) { HX_MARK_MEMBER(mThis); } \
ARRAY_VISIT_FUNC \
run_func \
Dynamic __Run(const Array<Dynamic> &inArgs) \
{ \
ret mThis->func(array_list); return Dynamic(); \
} \
}; \
Dynamic VirtualArray_obj::func##_dyn() { return new VirtualArray_##func(this); }
#define DEFINE_VARRAY_FUNC0(ret,func) DEFINE_VARRAY_FUNC(ret,func,HX_ARR_LIST0,VARRAY_RUN_FUNC(ret,func,HX_DYNAMIC_ARG_LIST0,HX_ARG_LIST0),0)
#define DEFINE_VARRAY_FUNC1(ret,func) DEFINE_VARRAY_FUNC(ret,func,HX_ARR_LIST1,VARRAY_RUN_FUNC(ret,func,HX_DYNAMIC_ARG_LIST1,HX_ARG_LIST1),1)
#define DEFINE_VARRAY_FUNC2(ret,func) DEFINE_VARRAY_FUNC(ret,func,HX_ARR_LIST2,VARRAY_RUN_FUNC(ret,func,HX_DYNAMIC_ARG_LIST2,HX_ARG_LIST2),2)
#define DEFINE_VARRAY_FUNC3(ret,func) DEFINE_VARRAY_FUNC(ret,func,HX_ARR_LIST3,VARRAY_RUN_FUNC(ret,func,HX_DYNAMIC_ARG_LIST3,HX_ARG_LIST3),3)
#define DEFINE_VARRAY_FUNC4(ret,func) DEFINE_VARRAY_FUNC(ret,func,HX_ARR_LIST4,VARRAY_RUN_FUNC(ret,func,HX_DYNAMIC_ARG_LIST4,HX_ARG_LIST4),4)
DEFINE_VARRAY_FUNC1(return,concat);
DEFINE_VARRAY_FUNC2(,insert);
DEFINE_VARRAY_FUNC0(return,iterator);
DEFINE_VARRAY_FUNC0(return,keyValueIterator);
DEFINE_VARRAY_FUNC1(return,join);
DEFINE_VARRAY_FUNC0(return,pop);
DEFINE_VARRAY_FUNC0(return,copy);
DEFINE_VARRAY_FUNC1(return,push);
DEFINE_VARRAY_FUNC1(return,contains);
DEFINE_VARRAY_FUNC1(return,remove);
DEFINE_VARRAY_FUNC1(return,removeAt);
DEFINE_VARRAY_FUNC2(return,indexOf);
DEFINE_VARRAY_FUNC2(return,lastIndexOf);
DEFINE_VARRAY_FUNC0(,reverse);
DEFINE_VARRAY_FUNC0(return,shift);
DEFINE_VARRAY_FUNC2(return,slice);
DEFINE_VARRAY_FUNC2(return,splice);
DEFINE_VARRAY_FUNC1(,sort);
DEFINE_VARRAY_FUNC0(return,toString);
DEFINE_VARRAY_FUNC1(,unshift);
DEFINE_VARRAY_FUNC1(return,map);
DEFINE_VARRAY_FUNC1(return,filter);
DEFINE_VARRAY_FUNC1(,__SetSize);
DEFINE_VARRAY_FUNC1(,__SetSizeExact);
DEFINE_VARRAY_FUNC2(,zero);
DEFINE_VARRAY_FUNC1(,memcmp);
DEFINE_VARRAY_FUNC1(return,__unsafe_get);
DEFINE_VARRAY_FUNC2(return,__unsafe_set);
DEFINE_VARRAY_FUNC4(,blit);
DEFINE_VARRAY_FUNC1(,resize);
#endif
int VirtualArray_obj::__Compare(const hx::Object *inRHS) const
{
if (inRHS->__GetType()!=vtArray)
return -1;
ArrayCommon *common = (ArrayCommon *)inRHS;
hx::Object *a = const_cast<VirtualArray_obj *>(this)->__GetRealObject();
hx::Object *b = common->__GetRealObject();
return a<b ? -1 : a>b;
}
Dynamic VirtualArray_obj::__GetItem(int inIndex) const
{
checkBase();
if (store==hx::arrayEmpty || inIndex<0 || inIndex>=get_length()) return
null();
return base->__GetItem(inIndex);
}
Dynamic VirtualArray_obj::__SetItem(int inIndex,Dynamic inValue)
{
checkBase();
if (store!=hx::arrayFixed)
{
if (inIndex>(store==hx::arrayEmpty ? 0 : (int)base->length) )
EnsureObjectStorage();
else
EnsureStorage(inValue);
}
base->__SetItem(inIndex,inValue);
return inValue;
}
hx::Val VirtualArray_obj::__Field(const String &inString, hx::PropertyAccess inCallProp)
{
if (inString==HX_CSTRING("length")) return (int)get_length();
if (inString==HX_CSTRING("concat")) return concat_dyn();
if (inString==HX_CSTRING("insert")) return insert_dyn();
if (inString==HX_CSTRING("copy")) return copy_dyn();
if (inString==HX_CSTRING("iterator")) return iterator_dyn();
if (inString==HX_CSTRING("keyValueIterator")) return keyValueIterator_dyn();
if (inString==HX_CSTRING("join")) return join_dyn();
if (inString==HX_CSTRING("pop")) return pop_dyn();
#if (HXCPP_API_LEVEL>=500)
if (inString==HX_CSTRING("push")) return push_dyn<::Dynamic>();
#else
if (inString == HX_CSTRING("push")) return push_dyn();
#endif
if (inString==HX_CSTRING("contains")) return contains_dyn();
if (inString==HX_CSTRING("remove")) return remove_dyn();
if (inString==HX_CSTRING("removeAt")) return removeAt_dyn();
if (inString==HX_CSTRING("indexOf")) return indexOf_dyn();
if (inString==HX_CSTRING("lastIndexOf")) return lastIndexOf_dyn();
if (inString==HX_CSTRING("reverse")) return reverse_dyn();
if (inString==HX_CSTRING("shift")) return shift_dyn();
if (inString==HX_CSTRING("splice")) return splice_dyn();
if (inString==HX_CSTRING("slice")) return slice_dyn();
if (inString==HX_CSTRING("sort")) return sort_dyn();
if (inString==HX_CSTRING("toString")) return toString_dyn();
if (inString==HX_CSTRING("unshift")) return unshift_dyn();
if (inString==HX_CSTRING("filter")) return filter_dyn();