-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathc_array.c
More file actions
1385 lines (1114 loc) · 32.9 KB
/
Copy pathc_array.c
File metadata and controls
1385 lines (1114 loc) · 32.9 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
/*! @file
@brief
mruby/c Array class
<pre>
Copyright (C) 2015- Kyushu Institute of Technology.
Copyright (C) 2015-2026 Shimane IT Open-Innovation Center.
Copyright (C) 2026- Shimane Institute for Industrial Technology.
This file is distributed under BSD 3-Clause License.
Function summary
(constructor)
mrbc_array_new()
(destructor)
mrbc_array_delete()
(setter)
--[name]-------------[arg]---[ret]-------------------------------------------
mrbc_array_set() *V int
mrbc_array_push() *V int
mrbc_array_push_m() *V int
mrbc_array_unshift() *V int
mrbc_array_insert() *V int
(getter)
--[name]-------------[arg]---[ret]---[note]----------------------------------
mrbc_array_get() idx V Data remains in the container
mrbc_array_get_p() idx *V Data remains in the container
mrbc_array_pop() V Data does not remain in the container
mrbc_array_shift() V Data does not remain in the container
mrbc_array_remove() idx V Data does not remain in the container
(finder)
mrbc_array_index()
mrbc_array_include()
(others)
mrbc_array_size()
mrbc_array_resize()
mrbc_array_clear()
mrbc_array_compare()
mrbc_array_minmax()
mrbc_array_dup()
mrbc_array_divide()
mrbc_array_uniq()
mrbc_array_uniq_self()
</pre>
*/
/***** Feature test switches ************************************************/
/***** System headers *******************************************************/
//@cond
#include "vm_config.h"
#include <string.h>
#include <assert.h>
//@endcond
/***** Local headers ********************************************************/
#include "mrubyc.h"
/***** Constat values *******************************************************/
/***** Macros ***************************************************************/
/***** Typedefs *************************************************************/
/***** Function prototypes **************************************************/
/***** Local variables ******************************************************/
/***** Global variables *****************************************************/
/***** Signal catching functions ********************************************/
/***** Local functions ******************************************************/
/***** Global functions *****************************************************/
//================================================================
/*! constructor
@param vm pointer to VM.
@param size initial size
@return array object
*/
mrbc_value mrbc_array_new(mrbc_vm *vm, int size)
{
// Allocate handle and data buffer.
mrbc_array *ary = mrbc_alloc(vm, sizeof(mrbc_array));
mrbc_value *data = mrbc_alloc(vm, sizeof(mrbc_value) * size);
*ary = (mrbc_array){
MRBC_INIT_OBJECT_HEADER_DI(AR)
.data_size = size,
.n_stored = 0,
.data = data,
};
return mrbc_immediate_value(MRBC_TT_ARRAY, .array = ary);
}
//================================================================
/*! destructor
@param ary pointer to target value
*/
void mrbc_array_delete(mrbc_value *ary)
{
mrbc_array *h = ary->array;
mrbc_value *p1 = h->data;
const mrbc_value *p2 = p1 + h->n_stored;
while( p1 < p2 ) {
mrbc_decref(p1++);
}
mrbc_array_delete_handle(ary);
}
//================================================================
/*! resize buffer
@param ary pointer to target value
@param size size
@return mrbc_error_code
*/
int mrbc_array_resize(mrbc_value *ary, int size)
{
if( size <= 0 ) size = 1;
mrbc_array *h = ary->array;
mrbc_value *data = mrbc_raw_realloc(h->data, sizeof(mrbc_value) * size);
h->data = data;
h->data_size = size;
return 0;
}
//================================================================
/*! setter
@param ary pointer to target value
@param idx index
@param set_val set value
@return mrbc_error_code
*/
int mrbc_array_set(mrbc_value *ary, int idx, mrbc_value *set_val)
{
mrbc_array *h = ary->array;
if( idx < 0 ) {
idx = h->n_stored + idx;
if( idx < 0 ) return E_INDEX_ERROR;
}
// need resize?
if( idx >= h->data_size ) {
mrbc_array_resize(ary, idx + 1);
}
if( idx < h->n_stored ) {
// release existing data.
mrbc_decref( &h->data[idx] );
} else {
// clear empty cells.
for( int i = h->n_stored; i < idx; i++ ) {
mrbc_set_nil( &h->data[i] );
}
h->n_stored = idx + 1;
}
h->data[idx] = *set_val;
return 0;
}
//================================================================
/*! getter
@param ary pointer to target value
@param idx index
@return mrbc_value data at index position or Nil.
*/
mrbc_value mrbc_array_get(const mrbc_value *ary, int idx)
{
mrbc_array *h = ary->array;
if( idx < 0 ) idx = h->n_stored + idx;
if( idx < 0 || idx >= h->n_stored ) return mrbc_nil_value();
return h->data[idx];
}
//================================================================
/*! getter
@param ary pointer to target value
@param idx index
@return pointer to mrbc_value or NULL.
*/
mrbc_value * mrbc_array_get_p(const mrbc_value *ary, int idx)
{
mrbc_array *h = ary->array;
if( idx < 0 ) idx = h->n_stored + idx;
if( idx < 0 || idx >= h->n_stored ) return NULL;
return &h->data[idx];
}
//================================================================
/*! push a data to tail
@param ary pointer to target value
@param set_val set value
@return mrbc_error_code
*/
int mrbc_array_push(mrbc_value *ary, mrbc_value *set_val)
{
mrbc_array *h = ary->array;
if( h->n_stored >= h->data_size ) {
mrbc_array_resize(ary, h->data_size + 6);
}
h->data[h->n_stored++] = *set_val;
return 0;
}
//================================================================
/*! push multiple data to tail
@param ary pointer to target value
@param set_val set value (array)
@return mrbc_error_code
*/
int mrbc_array_push_m(mrbc_value *ary, mrbc_value *set_val)
{
mrbc_array *ha_d = ary->array;
mrbc_array *ha_s = set_val->array;
int new_size = ha_d->n_stored + ha_s->n_stored;
if( new_size > ha_d->data_size ) {
mrbc_array_resize(ary, new_size);
}
memcpy( &ha_d->data[ha_d->n_stored], ha_s->data,
sizeof(mrbc_value) * ha_s->n_stored );
ha_d->n_stored += ha_s->n_stored;
return 0;
}
//================================================================
/*! pop a data from tail.
@param ary pointer to target value
@return tail data or Nil
*/
mrbc_value mrbc_array_pop(mrbc_value *ary)
{
mrbc_array *h = ary->array;
if( h->n_stored <= 0 ) return mrbc_nil_value();
return h->data[--h->n_stored];
}
//================================================================
/*! insert a data to the first.
@param ary pointer to target value
@param set_val set value
@return mrbc_error_code
*/
int mrbc_array_unshift(mrbc_value *ary, mrbc_value *set_val)
{
return mrbc_array_insert(ary, 0, set_val);
}
//================================================================
/*! removes the first data and returns it.
@param ary pointer to target value
@return first data or Nil
*/
mrbc_value mrbc_array_shift(mrbc_value *ary)
{
mrbc_array *h = ary->array;
if( h->n_stored <= 0 ) return mrbc_nil_value();
mrbc_value ret = h->data[0];
memmove(h->data, h->data+1, sizeof(mrbc_value) * --h->n_stored);
return ret;
}
//================================================================
/*! insert a data
@param ary pointer to target value
@param idx index
@param set_val set value
@return mrbc_error_code
*/
int mrbc_array_insert(mrbc_value *ary, int idx, mrbc_value *set_val)
{
mrbc_array *h = ary->array;
if( idx < 0 ) {
idx = h->n_stored + idx + 1;
if( idx < 0 ) return E_INDEX_ERROR;
}
// need resize?
int size = 0;
if( idx >= h->data_size ) {
size = idx + 1;
} else if( h->n_stored >= h->data_size ) {
size = h->data_size + 1;
}
if( size ) {
mrbc_array_resize(ary, size);
}
// move datas.
if( idx < h->n_stored ) {
memmove(h->data + idx + 1, h->data + idx,
sizeof(mrbc_value) * (h->n_stored - idx));
}
// set data
h->data[idx] = *set_val;
h->n_stored++;
// clear empty cells if need.
if( idx >= h->n_stored ) {
for( int i = h->n_stored-1; i < idx; i++ ) {
mrbc_set_nil( &h->data[i] );
}
h->n_stored = idx + 1;
}
return 0;
}
//================================================================
/*! remove a data
@param ary pointer to target value
@param idx index
@return mrbc_value data at index position or Nil.
*/
mrbc_value mrbc_array_remove(mrbc_value *ary, int idx)
{
mrbc_array *h = ary->array;
if( idx < 0 ) idx = h->n_stored + idx;
if( idx < 0 || idx >= h->n_stored ) return mrbc_nil_value();
mrbc_value ret = h->data[idx];
h->n_stored--;
if( idx < h->n_stored ) {
memmove(h->data + idx, h->data + idx + 1,
sizeof(mrbc_value) * (h->n_stored - idx));
}
return ret;
}
//================================================================
/*! clear all
@param ary pointer to target value
*/
void mrbc_array_clear(mrbc_value *ary)
{
mrbc_array *h = ary->array;
mrbc_value *p1 = h->data;
const mrbc_value *p2 = p1 + h->n_stored;
while( p1 < p2 ) {
mrbc_decref(p1++);
}
h->n_stored = 0;
}
//================================================================
/*! compare
@param v1 Pointer to mrbc_value
@param v2 Pointer to another mrbc_value
@retval 0 v1 == v2
@retval plus v1 > v2
@retval minus v1 < v2
*/
int mrbc_array_compare(const mrbc_value *v1, const mrbc_value *v2)
{
for( int i = 0; ; i++ ) {
if( i >= mrbc_array_size(v1) || i >= mrbc_array_size(v2) ) {
return mrbc_array_size(v1) - mrbc_array_size(v2);
}
int res = mrbc_compare( &v1->array->data[i], &v2->array->data[i] );
if( res != 0 ) return res;
}
}
//================================================================
/*! get min, max value
@param ary pointer to target value
@param pp_min_value returns minimum mrbc_value
@param pp_max_value returns maxmum mrbc_value
*/
void mrbc_array_minmax(mrbc_value *ary, mrbc_value **pp_min_value, mrbc_value **pp_max_value)
{
mrbc_array *h = ary->array;
if( h->n_stored == 0 ) {
*pp_min_value = NULL;
*pp_max_value = NULL;
return;
}
mrbc_value *p_min_value = h->data;
mrbc_value *p_max_value = h->data;
for( int i = 1; i < h->n_stored; i++ ) {
if( mrbc_compare( &h->data[i], p_min_value ) < 0 ) {
p_min_value = &h->data[i];
}
if( mrbc_compare( &h->data[i], p_max_value ) > 0 ) {
p_max_value = &h->data[i];
}
}
*pp_min_value = p_min_value;
*pp_max_value = p_max_value;
}
//================================================================
/*! duplicate (shallow copy)
@param vm pointer to VM.
@param ary source
@return result
*/
mrbc_value mrbc_array_dup(mrbc_vm *vm, const mrbc_value *ary)
{
mrbc_array *sh = ary->array;
mrbc_value dv = mrbc_array_new(vm, sh->n_stored);
memcpy( dv.array->data, sh->data, sizeof(mrbc_value) * sh->n_stored );
dv.array->n_stored = sh->n_stored;
mrbc_value *p1 = dv.array->data;
const mrbc_value *p2 = p1 + dv.array->n_stored;
while( p1 < p2 ) {
mrbc_incref(p1++);
}
return dv;
}
//================================================================
/*! divide into two parts
@param vm pointer to VM.
@param src source
@param pos divide position
@return divided array
@note
src = [0,1,2,3]
ret = divide(src, 2)
src = [0,1], ret = [2,3]
*/
mrbc_value mrbc_array_divide(mrbc_vm *vm, mrbc_value *src, int pos)
{
mrbc_array *ha_s = src->array;
if( pos < 0 ) pos = 0;
int new_size = ha_s->n_stored - pos;
if( new_size < 0 ) new_size = 0;
int remain_size = ha_s->n_stored - new_size;
mrbc_value ret = mrbc_array_new(vm, new_size);
mrbc_array *ha_r = ret.array;
memcpy( ha_r->data, ha_s->data + remain_size, sizeof(mrbc_value) * new_size );
ha_s->n_stored = remain_size;
mrbc_array_resize( src, remain_size );
ha_r->n_stored = new_size;
return ret;
}
//================================================================
/*! index
@param ary target array.
@param val search object.
@return index value or -1 if not found.
*/
int mrbc_array_index(const mrbc_value *ary, const mrbc_value *val)
{
int n = ary->array->n_stored;
for( int i = 0; i < n; i++ ) {
if( mrbc_compare(&ary->array->data[i], val) == 0 ) return i;
}
return -1;
}
//================================================================
/*! removes duplicate elements and return allocated new mrbc_value
@param vm pointer to VM.
@param ary source
@return result
*/
mrbc_value mrbc_array_uniq(mrbc_vm *vm, const mrbc_value *ary)
{
mrbc_value ret = mrbc_array_dup(vm, ary);
mrbc_array_uniq_self( &ret );
return ret;
}
//================================================================
/*! removes duplicate elements
@param ary target
@return num of deleted
*/
int mrbc_array_uniq_self(mrbc_value *ary)
{
mrbc_array *ah = ary->array;
int size = ah->n_stored;
for( int i = 0; i < size-1; i++ ) {
for( int j = i+1; j < size; j++ ) {
if( mrbc_compare( &ah->data[i], &ah->data[j] ) != 0 ) continue;
mrbc_decref( &ah->data[j] );
int rest = --size - j;
if( rest == 0 ) break;
memmove( &ah->data[j], &ah->data[j+1], sizeof(mrbc_value) * rest );
j--;
}
}
int ret = ah->n_stored - size;
ah->n_stored = size;
return ret;
}
//================================================================
/*! method new
*/
static void c_array_new(mrbc_vm *vm, mrbc_value v[], int argc)
{
/*
in case of new()
*/
if( argc == 0 ) {
mrbc_value ret = mrbc_array_new(vm, 0);
SET_RETURN(ret);
return;
}
/*
in case of new(num)
*/
if( argc == 1 && mrbc_type(v[1]) == MRBC_TT_INTEGER && mrbc_integer(v[1]) >= 0 ) {
int num = mrbc_integer(v[1]);
mrbc_value ret = mrbc_array_new(vm, num);
if( num > 0 ) {
mrbc_array_set(&ret, num - 1, &mrbc_nil_value());
}
SET_RETURN(ret);
return;
}
/*
in case of new(num, value)
*/
if( argc == 2 && mrbc_type(v[1]) == MRBC_TT_INTEGER && mrbc_integer(v[1]) >= 0 ) {
int num = mrbc_integer(v[1]);
mrbc_value ret = mrbc_array_new(vm, num);
for( int i = 0; i < num; i++ ) {
mrbc_incref(&v[2]);
mrbc_array_set(&ret, i, &v[2]);
}
SET_RETURN(ret);
return;
}
/*
other case
*/
mrbc_raise( vm, MRBC_CLASS(ArgumentError), 0 );
}
//================================================================
/*! (operator) +
*/
static void c_array_add(mrbc_vm *vm, mrbc_value v[], int argc)
{
if( mrbc_type(v[1]) != MRBC_TT_ARRAY ) {
mrbc_raise( vm, MRBC_CLASS(TypeError), 0 );
return;
}
mrbc_array *h1 = v[0].array;
mrbc_array *h2 = v[1].array;
mrbc_value value = mrbc_array_new(vm, h1->n_stored + h2->n_stored);
memcpy( value.array->data, h1->data,
sizeof(mrbc_value) * h1->n_stored );
memcpy( value.array->data + h1->n_stored, h2->data,
sizeof(mrbc_value) * h2->n_stored );
value.array->n_stored = h1->n_stored + h2->n_stored;
mrbc_value *p1 = value.array->data;
const mrbc_value *p2 = p1 + value.array->n_stored;
while( p1 < p2 ) {
mrbc_incref(p1++);
}
mrbc_decref_empty(v+1);
SET_RETURN(value);
}
//================================================================
/*! (operator) []
*/
static void c_array_get(mrbc_vm *vm, mrbc_value v[], int argc)
{
/*
in case of Array[...] -> Array
*/
if( mrbc_type(v[0]) == MRBC_TT_CLASS ) {
mrbc_value ret = mrbc_array_new(vm, argc);
memcpy( ret.array->data, &v[1], sizeof(mrbc_value) * argc );
for( int i = 1; i <= argc; i++ ) {
mrbc_set_tt( &v[i], MRBC_TT_EMPTY );
}
ret.array->n_stored = argc;
SET_RETURN(ret);
return;
}
/*
in case of self[nth] -> object | nil
*/
if( argc == 1 && mrbc_type(v[1]) == MRBC_TT_INTEGER ) {
mrbc_value ret = mrbc_array_get(v, mrbc_integer(v[1]));
mrbc_incref(&ret);
SET_RETURN(ret);
return;
}
/*
in case of self[Range] -> Array | nil
*/
if (argc == 1 && mrbc_type(v[1]) == MRBC_TT_RANGE) {
int len = mrbc_array_size(&v[0]);
mrbc_value *range_ptr = &v[1];
// Get range start and end from range object
mrbc_value start_val = mrbc_range_first(range_ptr);
mrbc_value end_val = mrbc_range_last(range_ptr);
int flag_exclude = mrbc_range_exclude_end(range_ptr);
// Handle beginless range (e.g., ..1, ...2)
int start;
if (mrbc_type(start_val) == MRBC_TT_NIL) {
start = 0;
} else if (mrbc_type(start_val) == MRBC_TT_INTEGER) {
start = mrbc_integer(start_val);
} else {
goto TYPE_ERROR;
}
// Handle endless range (e.g., 0.., 1..., 0..., 1...)
int end;
if (mrbc_type(end_val) == MRBC_TT_NIL) {
end = len;
} else if (mrbc_type(end_val) == MRBC_TT_INTEGER) {
end = mrbc_integer(end_val);
} else {
goto TYPE_ERROR;
}
// Negative indices
if (start < 0) start += len;
if (end < 0) end += len;
if (start < 0 || start > len) goto RETURN_NIL;
// Allow end < 0 only for empty arrays with endless ranges
if (end < 0 && len > 0) goto RETURN_NIL;
// Adjust end for exclusive range
if (!flag_exclude) end++;
// Calculate size
int size = end - start;
if (size < 0) size = 0;
if (start + size > len) size = len - start;
mrbc_value ret = mrbc_array_new(vm, size);
if (ret.array == NULL) return; // ENOMEM
for (int i = 0; i < size; i++) {
mrbc_value val = mrbc_array_get(v, start + i);
mrbc_incref(&val);
mrbc_array_push(&ret, &val);
}
SET_RETURN(ret);
return;
}
/*
in case of self[start, length] -> Array | nil
*/
if( argc == 2 && mrbc_type(v[1]) == MRBC_TT_INTEGER && mrbc_type(v[2]) == MRBC_TT_INTEGER ) {
int len = mrbc_array_size(&v[0]);
int idx = mrbc_integer(v[1]);
if( idx < 0 ) idx += len;
if( idx < 0 ) goto RETURN_NIL;
int size = (mrbc_integer(v[2]) < (len - idx)) ? mrbc_integer(v[2]) : (len - idx);
// min( mrbc_integer(v[2]), (len - idx) )
if( size < 0 ) goto RETURN_NIL;
mrbc_value ret = mrbc_array_new(vm, size);
for( int i = 0; i < size; i++ ) {
mrbc_value val = mrbc_array_get(v, mrbc_integer(v[1]) + i);
mrbc_incref(&val);
mrbc_array_push(&ret, &val);
}
SET_RETURN(ret);
return;
}
/*
other case
*/
mrbc_raise( vm, MRBC_CLASS(ArgumentError), 0 );
return;
TYPE_ERROR:
mrbc_raise( vm, MRBC_CLASS(TypeError), 0 );
return;
RETURN_NIL:
SET_NIL_RETURN();
}
//================================================================
/*! (operator) []=
*/
static void c_array_set(mrbc_vm *vm, mrbc_value v[], int argc)
{
/*
in case of self[nth] = val
*/
if( argc == 2 && mrbc_type(v[1]) == MRBC_TT_INTEGER ) {
if( mrbc_array_set(v, mrbc_integer(v[1]), &v[2]) != 0 ) {
mrbc_raise( vm, MRBC_CLASS(IndexError), "too small for array");
return;
}
// return val
mrbc_incref(&v[2]);
mrbc_decref(&v[0]);
v[0] = v[2];
mrbc_set_tt(&v[2], MRBC_TT_EMPTY);
return;
}
/*
in case of self[start, length] = val
*/
if( argc == 3 && mrbc_type(v[1]) == MRBC_TT_INTEGER &&
mrbc_type(v[2]) == MRBC_TT_INTEGER ) {
int pos = mrbc_integer(v[1]);
int len = mrbc_integer(v[2]);
if( pos < 0 ) {
pos = v[0].array->n_stored + pos;
if( pos < 0 ) {
mrbc_raise( vm, MRBC_CLASS(IndexError), "index too small for array");
return;
}
} else if( pos > v[0].array->n_stored ) {
mrbc_array_set( &v[0], pos-1, &mrbc_nil_value() );
len = 0;
}
if( len < 0 ) {
mrbc_raise( vm, MRBC_CLASS(IndexError), "negative length");
return;
}
if( pos+len > v[0].array->n_stored ) {
len = v[0].array->n_stored - pos;
}
// split 2 part
mrbc_value v1 = mrbc_array_divide(vm, &v[0], pos+len);
mrbc_array *ha0 = v[0].array;
// delete data from tail.
for( int i = 0; i < len; i++ ) {
mrbc_decref( &ha0->data[--ha0->n_stored] );
}
// append data
if( mrbc_type(v[3]) == MRBC_TT_ARRAY ) {
mrbc_array_push_m(&v[0], &v[3]);
for( int i = 0; i < v[3].array->n_stored; i++ ) {
mrbc_incref( &v[3].array->data[i] );
}
} else {
mrbc_incref(&v[3]);
mrbc_array_push(&v[0], &v[3]);
}
mrbc_array_push_m(&v[0], &v1);
mrbc_array_delete_handle( &v1 );
// return val
mrbc_decref(&v[0]);
v[0] = v[3];
mrbc_set_tt(&v[3], MRBC_TT_EMPTY);
return;
}
/*
other case
*/
mrbc_raise( vm, MRBC_CLASS(ArgumentError), 0 );
}
//================================================================
/*! (method) clear
*/
static void c_array_clear(mrbc_vm *vm, mrbc_value v[], int argc)
{
mrbc_array_clear(v);
}
//================================================================
/*! (method) difference(*other_arrays) -> Array
*/
static void c_array_difference(mrbc_vm *vm, mrbc_value v[], int argc)
{
mrbc_value ret = mrbc_array_dup(vm, &v[0]);
for( int i = 1; i <= argc; i++ ) {
if( mrbc_type(v[i]) != MRBC_TT_ARRAY ) {
mrbc_raise( vm, MRBC_CLASS(TypeError), 0 );
return;
}
for( int j = 0; j < mrbc_array_size(&v[i]); j++ ) {
int idx;
while( (idx = mrbc_array_index( &ret, &v[i].array->data[j] )) >= 0 ) {
mrbc_array *ah = ret.array;
ah->n_stored--;
memmove(ah->data + idx, ah->data + idx + 1,
sizeof(mrbc_value) * (ah->n_stored - idx));
}
}
}
SET_RETURN(ret);
}
//================================================================
/*! (method) delete_at
*/
static void c_array_delete_at(mrbc_vm *vm, mrbc_value v[], int argc)
{
if( argc == 1 && mrbc_type(v[1]) == MRBC_TT_INTEGER ) {
mrbc_value val = mrbc_array_remove(v, mrbc_integer(v[1]));
SET_RETURN(val);
} else {
mrbc_raise( vm, MRBC_CLASS(ArgumentError), 0 );
}
}
//================================================================
/*! (method) empty?
*/
static void c_array_empty(mrbc_vm *vm, mrbc_value v[], int argc)
{
int n = mrbc_array_size(v);
SET_BOOL_RETURN( !n );
}
//================================================================
/*! (method) size,length,count
*/
static void c_array_size(mrbc_vm *vm, mrbc_value v[], int argc)
{
int n = mrbc_array_size(v);
SET_INT_RETURN(n);
}
//================================================================
/*! (method) include?
*/
static void c_array_include(mrbc_vm *vm, mrbc_value v[], int argc)
{
SET_BOOL_RETURN(0 < mrbc_array_include(&v[0], &v[1]));
}
//================================================================
/*! (method) &
*/
static void c_array_and(mrbc_vm *vm, mrbc_value v[], int argc)
{
if( mrbc_type(v[1]) != MRBC_TT_ARRAY ) {
mrbc_raisef( vm, MRBC_CLASS(TypeError), "no implicit conversion into %s", "Array");
return;
}
mrbc_value result = mrbc_array_new(vm, 0);
for( int i = 0; i < v[0].array->n_stored; i++) {
mrbc_value *data = &v[0].array->data[i];
if (0 < mrbc_array_include(&v[1], data) && 0 == mrbc_array_include(&result, data))
{
mrbc_array_push(&result, data);
}
}
SET_RETURN(result);
}
//================================================================
/*! (method) |
*/
static void c_array_or(mrbc_vm *vm, mrbc_value v[], int argc)
{
if( mrbc_type(v[1]) != MRBC_TT_ARRAY ) {
mrbc_raisef( vm, MRBC_CLASS(TypeError), "no implicit conversion into %s", "Array");
return;
}
mrbc_value result = mrbc_array_new(vm, 0);
for( int i = 0; i < v[0].array->n_stored; i++) {
mrbc_value *data = &v[0].array->data[i];
if (0 == mrbc_array_include(&result, data))
{
mrbc_array_push(&result, data);
}
}
for( int i = 0; i < v[1].array->n_stored; i++) {
mrbc_value *data = &v[1].array->data[i];
if (0 == mrbc_array_include(&result, data))
{