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
|
-
|
|
|
-
|
|
|
!
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
!
-
|
|
|
!
-
|
|
|
!
-
|
|
!
-
|
|
!
-
|
|
|
!
-
|
|
|
!
-
|
!
-
-
|
-
|
!
-
-
|
-
|
!
!
-
-
-
-
|
|
|
|
-
-
!
|
-
-
!
-
!
!
-
|
|
!
|
|
|
|
!
| #ifndef MOD_VECTOR
#define MOD_VECTOR
#module vector
#define true 1
#define false 0
#defcfunc _size array arr
return length(arr)
#deffunc _tnew array arr,array toarr,int l
dimtype toarr,vartype(arr),l
return
#deffunc _new array arr ,int l
dim arr,l
return
#deffunc _sequence array toarr,int l,int t
dim toarr,abs(l-t)+1
if l<=t{
repeat t-l+1
toarr.cnt=l+cnt
loop
}else{
repeat l-t+1
toarr.cnt=l-cnt
loop
}
return
#deffunc _clear array arr
dim arr
return
#defcfunc _at array arr ,int l
return arr.l
#deffunc _put array arr ,int l,int t
arr.l=t
return
#defcfunc _isin array arr ,int l
return (l>=0&&l<_size(arr))
#defcfunc _limin array arr ,int l
return limit(l,0,_size(arr))
#deffunc _slice array arr,array toarr,int l,int t
dim toarr,abs(t)
if (l>=0&t>0){ repeat t
toarr.cnt=arr(l+cnt)
loop
}else:if(l>=0&t<0){ repeat -t
toarr.cnt=arr(l-(cnt))
loop
}else:if(l<0&t>0){ repeat t
toarr.cnt=arr(_size(arr)+l+cnt)
loop
}else:if(l<0&t<0){ repeat -t
toarr.cnt=arr(_size(arr)+l-(cnt))
loop
}
return
#deffunc _copy array arr,array toarr
_slice arr,toarr,0,_size(arr)
return
#deffunc _comp array arr,array toarr,array other
dim toarr,_size(arr)+_size(other)
repeat _size(arr)
toarr(cnt)=arr.cnt
loop
repeat _size(other)
toarr(cnt+_size(arr))=other.cnt
loop
return
#deffunc _times array arr,array toarr,int l,local _lcnt
dim toarr,_size(arr)*l
repeat l
_lcnt=cnt
repeat _size(arr)
toarr(_size(arr)*_lcnt+cnt)=arr.cnt
loop
loop
return
#defcfunc _top array arr
return arr.0
#defcfunc _bottom array arr
return arr(_size(arr)-1)
#deffunc _pop array arr,local _tmparr
_slice arr,_tmparr,0,_size(arr)-1
_copy _tmparr,arr
return
#deffunc _push array arr,int l
arr(_size(arr))=l
return
#deffunc _shift array arr,local _tmparr
_slice arr,_tmparr,1,_size(arr)-1
_copy _tmparr,arr
return
#deffunc _unshift array arr,int l
_insert arr,0,l
return
#deffunc _reverse array arr,array toarr
_slice arr,toarr,-1,-_size(arr)
return
#defcfunc _join array arr,str l,local _t
_t=""+arr.0
repeat _size(arr)-1
_t=_t+l+arr(cnt+1)
loop
return _t
#defcfunc _inspect array arr
return "["+_join(arr,",")+"]"
#deffunc _insert array arr,int l,int i,local _tmparr,local _l
dim _tmparr,_size(arr)+1
if l<0:_l=_size(arr)+l+1:else:_l=l
repeat _l
_tmparr.cnt=arr.cnt
loop
_tmparr._l=i
repeat _size(arr)-_l
_tmparr(_l+cnt+1)=arr(_l+cnt)
loop
_copy _tmparr,arr
return
#deffunc _del array arr,int l,local _tmparr,local _l
dim _tmparr,_size(arr)-1
if l<0:_l=_size(arr)+l:else:_l=l
repeat _l
_tmparr.cnt=arr.cnt
loop
repeat _size(arr)-_l-1
_tmparr(_l+cnt)=arr(_l+cnt+1)
loop
_copy _tmparr,arr
return
#deffunc _fill array arr,int l,int i
dim arr,l
repeat l
arr.cnt=i
loop
return
#deffunc _replace array arr,int l,int tol
repeat _size(arr)
if (arr(cnt)==l):arr(cnt)=tol
loop
return
#defcfunc _sum array arr,local _l
_l=0
repeat _size(arr)
_l+=arr(cnt)
loop
return _l
#deffunc _shuffle array arr,array toarr,local _l
dim toarr,_size(arr)
repeat _size(arr)
_l=rnd(_size(arr))
toarr(cnt)=arr(_l)
_del arr,_l
loop
return
#defcfunc _find array arr,int l
repeat _size(arr)
if (arr(cnt)==l):return cnt
loop
return -1
#defcfunc _bifind array arr,int l,local _dl,local _dr,local _k,local _c
_dr=_size(arr)
_dl=0
_k=0
repeat
_c=(_dr+_dl)/2
if (arr(_c)==l):_k=_c:break
if (arr(_c)>l):_dr=_c
if (arr(_c)<l):_dl=_c
if (_dr==_dl):return -1
loop
while _k>0
if (arr(_k-1)!=l):_break
_k--
wend
return _k
#defcfunc _count array arr,int l,local _i
_i=0
repeat _size(arr)
if (arr(cnt)==l):_i++
loop
return _i
#defcfunc _equal array arr,array other
if (_size(arr)!=_size(other)):return false
repeat _size(arr)
if (arr(cnt)!=other(cnt)):return false
loop
return true
#defcfunc _mismatch array arr,array other,local _i
if (_size(arr)<=_size(other)):_i=_size(arr):else:_i=_size(other)
repeat _i
if (arr(cnt)!=other(cnt)):return cnt
loop
return -1
#deffunc _iswap array arr,int r,int l,local _t
if (r==l):return
_t=arr.r
arr.r=arr.l
arr.l=_t
return
#defcfunc _any array arr
repeat _size(arr)
if (arr.cnt==true):return true
loop
return false
#defcfunc _all array arr
repeat _size(arr)
if (arr.cnt!=true):return false
loop
return true
#deffunc _set array arr,array toarr,local _t,local _f,local _i
dim toarr
_i=0
repeat _size(arr)
_t=arr.cnt
_f=0
repeat _size(toarr)
if (_t==toarr.cnt):_f=1:break
loop
if (_f==0):toarr._i=_t:_i++
loop
return
#defcfunc _countarr array arr,array other,local _t,local _i,local _f
if(_size(arr)<_size(other)):return 0
_i=0
repeat _size(arr)-_size(other)+1
_t=cnt
_f=0
repeat _size(other)
if (arr(_t+cnt)!=other(cnt)):_f=1:break
loop
if (_f==0):_i++
loop
return _i
#defcfunc _search array arr,array other,local _t,local _f
if(_size(arr)<_size(other)):return -1
repeat _size(arr)-_size(other)+1
_t=cnt
_f=0
repeat _size(other)
if (arr(_t+cnt)!=other(cnt)):_f=1:break
loop
if (_f==0):return _t
loop
return -1
#deffunc _rawstrtostr str st,array arr,local _i,local _t
sdim arr,2
_i=0
_t=st
repeat strlen(_t)
if (IsByte(peek(_t,cnt))){
wpoke arr._i,0,wpeek(_t,cnt)
_i++
continue cnt + 2
}
poke arr._i,0,peek(_t,cnt)
_i++
loop
return
#deffunc _rawstrtoint str st,array arr,local _i,local _t
dim arr,2
_i=0
_t=st
repeat strlen(_t)
if (IsByte(peek(_t,cnt))){
arr._i=wpeek(_t,cnt)
_i++
continue cnt + 2
}
arr._i=peek(_t,cnt)
_i++
loop
return
#deffunc _strtoint array arr,array toarr
dim toarr,_size(arr)
repeat _size(arr)
if (IsByte(peek(arr.cnt,0))){
toarr.cnt=wpeek(arr.cnt,0)
continue
}
toarr.cnt=peek(arr.cnt,0)
loop
return
#deffunc _inttostr array arr,array toarr
sdim toarr,2
repeat _size(arr)
if (IsByte(peek(arr.cnt,0))){
wpoke toarr.cnt,0,wpeek(arr.cnt,0)
continue
}
poke toarr.cnt,0,peek(arr.cnt,0)
loop
return
#deffunc _strtorawstr array arr,var _st,local _i
_st=""
_i=0
repeat _size(arr)
if (IsByte(peek(arr.cnt,0))){
wpoke _st,_i,wpeek(arr.cnt,0)
_i+=2
continue
}
poke _st,_i,peek(arr.cnt,0)
_i++
loop
return
#deffunc _inttorawstr array arr,var st,local _i
st=""
_i=0
repeat _size(arr)
if (IsByte(peek(arr.cnt))){
wpoke st,_i,arr.cnt
_i+=2
continue
}
poke st,_i,peek(arr.cnt)
_i++
loop
return
#defcfunc mpeek str st,local _t
_t=st
if (IsByte(peek(_t))){
return wpeek(_t)
}
return peek(_t)
#defcfunc local IsByte int p1
return ((p1>=129)&(p1<=159) )|( (p1>=224)&(p1<=252))
#define global _filter(%1,%2,%3) dim (%2):__f@vecter=0:foreach %1:_self=%1.cnt:if(%3){%2.__f@vecter==_self:__f@vecter++}:loop
#define global _map(%1,%2,%3) dim(%2):foreach %1:_self=%1.cnt:%2.cnt==%3:loop
#define global _zipWith(%1,%2,%3,%4) dim (%2):__f=length(%1):__o=length(%3):repeat (__f>__o)*__o+(__f<=__o)*__f:_self=%1.cnt:_other=%3.cnt:%2.cnt=%4:loop
#deffunc _zip array arr,array toarr,array other,local _l
if (_size(arr)<_size(toarr)):_l=_size(arr):else:_l=_size(toarr)
dim toarr,_l*2
repeat _l
toarr.(cnt*2)=arr.cnt
toarr.(cnt*2+1)=other.cnt
loop
return
#define global _bk(%1,%2) %2 %1,__t@vecter:_copy __t@vecter,%1
#define global _bk2(%1,%2,%3) %2 %1,__t@vecter,%3:_copy __t@vecter,%1
#define global _inject(%1,%2,%3,%4) _acc=%3:foreach %1:_self=%1.cnt:_acc=%4:loop:%2=_acc
#deffunc _baqetsort array arr,array toarr,local _tmparr,local _tc,local _cc
dim toarr,length(arr)
dim _tmparr,length(arr)
repeat length(arr)
_tmparr.(arr.cnt)+=1
loop
_tc=0
repeat length(_tmparr)
_cc=cnt
repeat _tmparr._cc
toarr._tc=_cc
_tc++
loop
loop
return
#global
#define STACKSIZE@mod_qsort 2048
#module mod_qsort
#define swap_ae@mod_qsort( %1, %2, %3 )\
tmp@mod_qsort = %1( %2 ) : %1( %2 ) = %1( %3 ) : %1( %3 ) = tmp@mod_qsort
#defcfunc compare_ae@mod_qsort array v1, int i1, int i2, int Mode
if ( vartype( v1 ) != 2 ) {
if ( Mode < 0 ) {
return v1( i1 ) > v1( i2 )
} else {
return v1( i1 ) < v1( i2 )
}
} else {
if ( Mode < 0 ) {
return ( v1( i1 ) ! v1( i2 ) ) > 0
} else {
return ( v1( i1 ) ! v1( i2 ) ) < 0
}
}
return 0
#deffunc qsort_init
dim FrstIndexStk, STACKSIZE
dim LastIndexStk, STACKSIZE
return
#deffunc qsort_core array v1, int FrstIndexArg, int LastIndexArg, int Mode,\
local StackLevel, local FrstIndex, local LastIndex, local PivtCount, local TrueCount
StackLevel = 0
FrstIndexStk( 0 ) = FrstIndexArg
LastIndexStk( 0 ) = LastIndexArg
repeat:if ( StackLevel < 0 ) {break }
FrstIndex = FrstIndexStk( StackLevel )
LastIndex = LastIndexStk( StackLevel )
if ( FrstIndex >= LastIndex ) {
} else : if ( LastIndex - FrstIndex == 1 ) {
if ( compare_ae@mod_qsort( v1, LastIndex, FrstIndex, Mode ) ) {
swap_ae@mod_qsort v1, FrstIndex, LastIndex } } else {
swap_ae@mod_qsort v1, FrstIndex, ( FrstIndex + LastIndex ) / 2 PivtCount = 1 TrueCount = 0 repeat LastIndex - FrstIndex, FrstIndex + 1
if ( compare_ae@mod_qsort( v1, cnt, FrstIndex, Mode ) ) {
if ( cnt != PivtCount + TrueCount ) {
swap_ae@mod_qsort v1, FrstIndex + PivtCount + TrueCount, cnt}
TrueCount++
} else : if ( compare_ae@mod_qsort( v1, FrstIndex, cnt, Mode ) == 0 ) {
if ( cnt != PivtCount + TrueCount ) {
swap_ae@mod_qsort v1, FrstIndex + PivtCount + TrueCount, cnt}
if ( TrueCount > 0 ) {
swap_ae@mod_qsort v1, FrstIndex + PivtCount, FrstIndex + PivtCount + TrueCount }
PivtCount++ }:loop
if ( TrueCount > 0 ) {
repeat PivtCount
swap_ae@mod_qsort v1, FrstIndex + cnt, FrstIndex + PivtCount + TrueCount - 1 - cnt
loop}
FrstIndexStk( StackLevel + 1 ) = FrstIndex
LastIndexStk( StackLevel + 1 ) = FrstIndex + TrueCount - 1
FrstIndexStk( StackLevel ) = FrstIndex + PivtCount + TrueCount
LastIndexStk( StackLevel ) = LastIndex
StackLevel += 2}
StackLevel--:loop
return
#deffunc _sort array v1
qsort_core v1, 0, length( v1 ) - 1, 0
return
#deffunc _sort_down array v1
qsort_core v1, 0, length( v1 ) - 1, -1
return
#global
qsort_init
#if 1
out=""
arr=3,5,7,1,8
out+="_size:"+ _size(arr) +"\n"out+="_inspect:"+ _inspect(arr) +"\n"out+="_join:"+ _join(arr,"~") +"\n"
_clear arrout+="_clear:"+ _inspect(arr) +"\n"_new arr,5out+="_arr:"+ _inspect(arr) +"\n"out+="_at:"+ _at(arr,3) +"\n"_put arr,2,9out+="_ate:"+ _inspect(arr) +"\n"
out+="_isin:"+ _isin(arr,7) +"\n"out+="_limin:"+ _limin(arr,7) +"\n"
_sequence arr,1,5out+="_sequence:"+ _inspect(arr) +"\n"
dim arr
arr=3,5,7,1,8
_slice arr,t,1,3out+="_slice:"+ _inspect(t) +"\n"_slice arr,t,-1,-3out+="_slice(nega):"+ _inspect(t) +"\n"
_copy arr,tout+="_copy:"+ _inspect(t) +"\n"
dim arr
arr=3,5,7,1,8
dim oth
oth=10,45,23
_comp arr,t,othout+="_comp:"+ _inspect(t) +"\n"
dim arr
arr=3,5,7,1,8
_times arr,t,3out+="_times:"+ _inspect(t) +"\n"
dim arr
arr=3,5,7,1,8
out+="_top:"+ _top(arr) +"\n"out+="_buttom:"+ _bottom(arr) +"\n"
_push arr,9out+="_push:"+ _inspect(arr) +"\n"_pop arrout+="_pop:"+ _inspect(arr) +"\n"_unshift arr,2out+="_unshift:"+ _inspect(arr) +"\n"_shift arrout+="_shift:"+ _inspect(arr) +"\n"_insert arr,2,9out+="_insert:"+ _inspect(arr) +"\n"_del arr,2out+="_del:"+ _inspect(arr) +"\n"
_fill arr,6,2out+="_fill:"+ _inspect(arr) +"\n"
dim arr
arr=3,1,7,1,8
_replace arr,1,2out+="_replace"+ _inspect(arr) +"\n"
dim arr
arr=3,1,7,1,8
_set arr,tout+="_set:"+ _inspect(t) +"\n"
dim arr
arr=3,5,7,1,8
_sort_down arrout+="_sort_down:"+ _inspect(arr) +"\n"_sort arrout+="_sort:"+ _inspect(arr) +"\n"out+="_bifind:"+ _bifind(arr,7) +"\n"
arr=3,5,7,1,8
_baqetsort arr,tout+="_baqetsort:"+ _inspect(t) +"\n"
dim arr
arr=3,5,7,1,8
_shuffle arr,tout+="_shuffle:"+ _inspect(t) +"\n"
dim arr
arr=3,5,7,1,8
out+="_find:"+ _find(arr,7) +"\n"
dim arr
arr=3,5,3,1,3
out+="_count:"+ _count(arr,3) +"\n"out+="_sum"+ _sum(arr) +"\n"
dim arr
dim oth
arr=3,4,5,6,7
oth=3,4,5,6,7
out+="_equal:"+ _equal(arr,oth) +"\n"oth.3=2
out+="_mismatch:"+ _mismatch(arr,oth) +"\n"
dim arr
dim oth
arr=3,3,4,5,6,7,5,6,7,3
oth=5,6
out+="_countarr:"+ _countarr(arr,oth) +"\n"out+="_search:"+ _search(arr,oth) +"\n"
dim arr
arr=3,5,7,1,8
_iswap arr,1,3out+="_iswap:"+ _inspect(arr) +"\n"
dim arr
arr=0,0,0,0,0,0,1
out+="_any:"+ _any(arr) +"\n"out+="_all:"+ _all(arr) +"\n"
hoge="moziほげ"
_rawstrtostr hoge,arrout+="_rawstrtostr:"+ _inspect(arr) +"\n"_rawstrtoint hoge,arrout+="_rawstrtoint:"+ _inspect(arr) +"\n"_inttostr arr,tout+="_inttostr:"+ _inspect(t) +"\n"_strtoint t,arrout+="_strtoint:"+ _inspect(arr) +"\n"_strtorawstr t,hogeout+="_strtorawstr:"+ hoge
_inttorawstr arr,hogeout+="_inttorawstr:"+ hoge +"\n"
out+="mpeek:"+ mpeek("あ") +"\n"
hoge="moziほげ"
_rawstrtoint hoge,arr
_bk arr,_reverse
_inttorawstr arr,hoge
out+="strrevarse:"+ hoge +"\n"
hoge="moziほむmomiほんとほむほむ"
_rawstrtoint hoge,arr
out+="strcount:"+ _count(arr,mpeek("ほ")) +"\n"piyo="ほむ"
_rawstrtoint piyo,t
out+="strcountarr:"+ _countarr(arr,t) +"\n"
dim arr
arr=3,5,7,1,8
_filter arr,hoge,_self>4out+="_filter:"+ _inspect(hoge) +"\n"_map arr,hoge,_self*cnt
out+="_map:"+ _inspect(hoge) +"\n"_bk arr,_shuffleout+="_bk:"+ _inspect(arr) +"\n"_bk2 arr,_times,5out+="_bk2:"+ _inspect(arr) +"\n"
arr=2,3,4,5,5
t=4,4,4,4
_zipWith arr,hoge,t,_self+_otherout+="_zipWith:"+ _inspect(hoge) +"\n"_zip arr,hoge,tout+="_zip:"+ _inspect(hoge) +"\n"
mesbox out,640,480
#endif
#endif
|