forked from gabordemooij/citrine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collections.c
1372 lines (1308 loc) · 43 KB
/
collections.c
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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <math.h>
#include <unistd.h>
#include <stdint.h>
#include <time.h>
#include "citrine.h"
#include "siphash.h"
/**
* [List] new
*
* Creates a new list or array. List is an alias for array.
* An array is a collection of items. To create a new array,
* send the 'new' message to the array. To add an element send
* the 'push:' message to an array with the element to add as
* an argument. Instead of using the push-message you can also
* use the • message. This message is suitable for vertically
* written arrays because they look similar to lists seen in
* regular documents. Besides 'push:' and • you can also use
* the ; message to push an new element on top of the array.
* The arrow message is the same as 'new' plus 'push:', just a
* shorter notation. The ; message is very suitable for
* horizontally written arrays. Finally, the last example
* depicts a notation using just ascii characters.
*
* Usage:
*
* ☞ meals :=
* List new
* • 'hamburger'
* • 'pizza'
* • 'haggis'.
*
* ☞ todo := List ← 'dishes' ; 'cleaning'.
*
* var a := Array < 1 ; 2 ; 3.
*/
/**
* [Array] new
*
* Creates a new array.
* An array is a collection of items. To create a new array,
* send the 'new' message to the array. To add an element send
* the 'push:' message to an array with the element to add as
* an argument. Instead of using the push-message you can also
* use the • message. This message is suitable for vertically
* written arrays because they look similar to lists seen in
* regular documents. Besides 'push:' and • you can also use
* the ; message to push an new element on top of the array.
* The arrow message is the same as 'new' plus 'push:', just a
* shorter notation. The ; message is very suitable for
* horizontally written arrays. Finally, the last example
* depicts a notation using just ascii characters.
*
* Usage:
*
* ☞ meals :=
* List new
* • 'hamburger'
* • 'pizza'
* • 'haggis'.
*
* ☞ todo := List ← 'dishes' ; 'cleaning'.
*
* var a := Array < 1 ; 2 ; 3.
*/
ctr_object* ctr_array_new(ctr_object* myclass, ctr_argument* argumentList) {
ctr_object* s = ctr_internal_create_object(CTR_OBJECT_TYPE_OTARRAY);
s->link = myclass;
s->value.avalue = (ctr_collection*) ctr_heap_allocate(sizeof(ctr_collection));
s->value.avalue->length = 1;
s->value.avalue->elements = (ctr_object**) ctr_heap_allocate(sizeof(ctr_object*)*1);
s->value.avalue->head = 0;
s->value.avalue->tail = 0;
return s;
}
/**
* [Array] type
*
* Returns the string 'Array'.
*
**/
ctr_object* ctr_array_type(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_string_from_cstring("Array");
}
/**
* [Array] add: [Element].
*
* Alias for [Array] push. Might be more readable
* in some situations.
*/
/**
* [Array] push: [Element]
*
* Pushes an element on top of the array.
*
* Usage:
*
* numbers := Array new.
* numbers push: 3.
*/
ctr_object* ctr_array_push(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* pushValue;
if (myself->value.avalue->length <= (myself->value.avalue->head + 1)) {
myself->value.avalue->length = myself->value.avalue->length * 3;
myself->value.avalue->elements = (ctr_object**) ctr_heap_reallocate(myself->value.avalue->elements,
(sizeof(ctr_object*) * (myself->value.avalue->length))
);
}
pushValue = argumentList->object;
*(myself->value.avalue->elements + myself->value.avalue->head) = pushValue;
myself->value.avalue->head++;
return myself;
}
/**
* [Array] min
*
* Returns the minimum value from an array.
*
* Usage:
*
* a := Array ← 8 ; 4 ; 2 ; 16.
* m := a min. #2
*
*/
ctr_object* ctr_array_min(ctr_object* myself, ctr_argument* argumentList) {
double min = 0;
double v = 0;
ctr_object* el;
size_t i = 0;
for(i = 0; i < myself->value.avalue->head; i++) {
el = *(myself->value.avalue->elements + i);
v = ctr_internal_cast2number(el)->value.nvalue;
if (i == 0 || v < min) {
min = v;
}
}
return ctr_build_number_from_float(min);
}
/**
* [Array] max
*
* Returns the maximum value from an array.
*
* Usage:
*
* a := Array ← 8 ; 4 ; 2 ; 16.
* m := a max. #16
*
*/
ctr_object* ctr_array_max(ctr_object* myself, ctr_argument* argumentList) {
double max = 0;
double v = 0;
ctr_object* el;
size_t i = 0;
for(i = 0; i < myself->value.avalue->head; i++) {
el = *(myself->value.avalue->elements + i);
v = ctr_internal_cast2number(el)->value.nvalue;
if (i == 0 || max < v) {
max = v;
}
}
return ctr_build_number_from_float(max);
}
/**
* [Array] sum
*
* Takes the sum of an array. This message will calculate the
* sum of the numerical elements in the array.
*
* Usage:
*
* a := Array ← 1 ; 2 ; 3.
* s := a sum. #6
*
* In the example above, the sum of array will be stored in s and
* it's value will be 6.
*/
ctr_object* ctr_array_sum(ctr_object* myself, ctr_argument* argumentList) {
double sum = 0;
ctr_object* el;
size_t i = 0;
for(i = 0; i < myself->value.avalue->head; i++) {
el = *(myself->value.avalue->elements + i);
sum += ctr_internal_cast2number(el)->value.nvalue;
}
return ctr_build_number_from_float(sum);
}
/**
* [Array] product
*
* Takes the product of an array. On receiving this message, the
* Array recipient object will calculate the product of its
* numerical elements.
*
* Usage:
*
* a := Array ← 2 ; 4 ; 8.
* p := a product. #64
*
* In the example above, the product of the array will be calculated
* because the array receives the message 'product'. The product of the elements
* ( 2 * 4 * 8 = 64 ) will be stored in p.
*/
ctr_object* ctr_array_product(ctr_object* myself, ctr_argument* argumentList) {
double product = 1;
ctr_object* el;
size_t i = 0;
for(i = 0; i < myself->value.avalue->head; i++) {
el = *(myself->value.avalue->elements + i);
product *= ctr_internal_cast2number(el)->value.nvalue;
}
return ctr_build_number_from_float(product);
}
/**
* [Array] map: [Block].
*
* Iterates over the array. Passing each element as a key-value pair to the
* specified block.
* The map message will pass the following arguments to the block, the key,
* the value and a reference to the array itself. The last argument might seem
* redundant but allows for a more functional programming style.
*
* Usage:
*
* files map: showName.
* files map: {
* :key :filename :files
* Pen write: filename, brk.
* }.
*/
ctr_object* ctr_array_map(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* block = argumentList->object;
int i = 0;
if (block->info.type != CTR_OBJECT_TYPE_OTBLOCK) {
CtrStdFlow = ctr_build_string_from_cstring("Expected Block.");
CtrStdFlow->info.sticky = 1;
}
block->info.sticky = 1;
for(i = myself->value.avalue->tail; i < myself->value.avalue->head; i++) {
ctr_argument* arguments = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
ctr_argument* argument2 = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
ctr_argument* argument3 = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
arguments->object = ctr_build_number_from_float((double) i);
argument2->object = *(myself->value.avalue->elements + i);
argument3->object = myself;
arguments->next = argument2;
argument2->next = argument3;
ctr_block_run(block, arguments, NULL);
ctr_heap_free( arguments );
ctr_heap_free( argument2 );
ctr_heap_free( argument3 );
if (CtrStdFlow == CtrStdContinue) CtrStdFlow = NULL;
if (CtrStdFlow) break;
}
if (CtrStdFlow == CtrStdBreak) CtrStdFlow = NULL; /* consume break */
block->info.mark = 0;
block->info.sticky = 0;
return myself;
}
/**
* [Array] each: [Block].
*
* Alias for [Array] map: [Block].
*/
/**
* [Array] ← [Element1] ; [Element2] ; ...
*
* Creates a new instance of an array and initializes this
* array with a first element, useful for literal-like Array
* notations.
*
* Usage:
*
* a := Array ← 1 ; 2 ; 3.
*
* or if you like ASCII-only:
*
* a := Array < 1 ; 2 ; 3.
*
* Note that the ; symbol here is an alias for 'push:'.
*/
ctr_object* ctr_array_new_and_push(ctr_object* myclass, ctr_argument* argumentList) {
ctr_object* s = ctr_array_new(myclass, NULL);
return ctr_array_push(s, argumentList);
}
/**
* [Array] unshift: [Element].
*
* Unshift operation for array.
* Adds the specified element to the beginning of the array.
*
* Usage:
*
* a := Array new.
* a push: 1.
* a unshift: 3. #now contains: 3,1
*/
ctr_object* ctr_array_unshift(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* pushValue = argumentList->object;
if (myself->value.avalue->tail > 0) {
myself->value.avalue->tail--;
} else {
if (myself->value.avalue->length <= (myself->value.avalue->head + 1)) {
myself->value.avalue->length = myself->value.avalue->length * 3;
myself->value.avalue->elements = (ctr_object**) ctr_heap_reallocate(myself->value.avalue->elements, (sizeof(ctr_object*) * (myself->value.avalue->length)));
}
myself->value.avalue->head++;
memmove(myself->value.avalue->elements+1, myself->value.avalue->elements,myself->value.avalue->head*sizeof(ctr_object*));
}
*(myself->value.avalue->elements + myself->value.avalue->tail) = pushValue;
return myself;
}
/**
* [Array] join: [Glue].
*
* Joins the elements of an array together in a string
* separated by a specified glue string.
*
* Usage:
*
* collection := Array new.
* collection push: 1, push: 2, push 3.
* collection join: ','. # results in string: '1,2,3'
*/
ctr_object* ctr_array_join(ctr_object* myself, ctr_argument* argumentList) {
int i;
char* result;
ctr_size len = 0;
ctr_size pos;
ctr_object* o;
ctr_object* str;
ctr_object* resultStr;
ctr_object* glue = ctr_internal_cast2string(argumentList->object);
ctr_size glen = glue->value.svalue->vlen;
for(i=myself->value.avalue->tail; i<myself->value.avalue->head; i++) {
o = *( myself->value.avalue->elements + i );
str = ctr_internal_cast2string(o);
pos = len;
if (i == myself->value.avalue->tail) {
len = str->value.svalue->vlen;
result = ctr_heap_allocate(sizeof(char)*len);
} else {
len += str->value.svalue->vlen + glen;
result = ctr_heap_reallocate(result, sizeof(char)*len );
memcpy(result+pos, glue->value.svalue->value, glen);
pos += glen;
}
memcpy(result+pos, str->value.svalue->value, str->value.svalue->vlen);
}
resultStr = ctr_build_string(result, len);
if (i > myself->value.avalue->tail) ctr_heap_free( result );
return resultStr;
}
/**
* [Array] at: [Index]
*
* Returns the element in the array at the specified index.
* Note that the first index of the array is index 0.
* If you attempt to retrieve an element of the array
* using a an index that is something other than a number
* a catchable error will be triggered. An error will
* also be triggered if your index is out of bounds.
*
* Usage:
*
* fruits := Array ← 'apples' ; 'oranges' ; 'bananas'.
* fruits at: 1. #returns 'oranges'
*/
ctr_object* ctr_array_get(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* getIndex = argumentList->object;
int i;
if (getIndex->info.type != CTR_OBJECT_TYPE_OTNUMBER) {
CtrStdFlow = ctr_build_string_from_cstring("Index must be number.");
CtrStdFlow->info.sticky = 1;
return CtrStdNil;
}
i = (int) getIndex->value.nvalue;
if (myself->value.avalue->head <= (i + myself->value.avalue->tail) || i < 0) {
CtrStdFlow = ctr_build_string_from_cstring("Index out of bounds.");
CtrStdFlow->info.sticky = 1;
return CtrStdNil;
}
return *(myself->value.avalue->elements + myself->value.avalue->tail + i);
}
/**
* [Array] first.
*
* Returns the first element of the array.
* If the array is empty, Nil will be returned.
*/
ctr_object* ctr_array_first(ctr_object* myself, ctr_argument* argumentList) {
ctr_size length = 0;
length = (ctr_size) myself->value.avalue->head - myself->value.avalue->tail;
if ( length < 1 ) {
return CtrStdNil;
}
return *(myself->value.avalue->elements + myself->value.avalue->tail);
}
/**
* [Array] last.
*
* Returns the last element of the array.
* If the array is empty, Nil will be returned.
*/
ctr_object* ctr_array_last(ctr_object* myself, ctr_argument* argumentList) {
ctr_size length = 0;
length = (ctr_size) myself->value.avalue->head - myself->value.avalue->tail;
if ( length < 1 ) {
return CtrStdNil;
}
return *(myself->value.avalue->elements + myself->value.avalue->tail + (length - 1));
}
/**
* [Array] secondLast.
*
* Returns the second last element of the array.
* If the array is empty, Nil will be returned.
*/
ctr_object* ctr_array_second_last(ctr_object* myself, ctr_argument* argumentList) {
ctr_size length = 0;
length = (ctr_size) myself->value.avalue->head - myself->value.avalue->tail;
if ( length < 2 ) {
return CtrStdNil;
}
return *(myself->value.avalue->elements + myself->value.avalue->tail + (length - 2));
}
/**
* [Array] @ [Index]
*
* Alias for [Array] at: [Index]
*/
/**
* [Array] put: [Element] at: [Index]
*
* Puts a value in the array at the specified index.
* Array will be automatically expanded if the index is higher than
* the maximum index of the array.
*
* Usage:
*
* fruits := Array new.
* fruits put: 'apples' at: 5.
*/
ctr_object* ctr_array_put(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* putValue = argumentList->object;
ctr_object* putIndex = ctr_internal_cast2number(argumentList->next->object);
ctr_size putIndexNumber;
ctr_size head;
ctr_size tail;
if (putIndex->value.nvalue < 0) {
CtrStdFlow = ctr_build_string_from_cstring("Index out of bounds.");
CtrStdFlow->info.sticky = 1;
return myself;
}
head = (ctr_size) myself->value.avalue->head;
tail = (ctr_size) myself->value.avalue->tail;
putIndexNumber = (ctr_size) putIndex->value.nvalue;
if (head <= putIndexNumber) {
ctr_size j;
for(j = head; j <= putIndexNumber; j++) {
ctr_argument* argument;
argument = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
argument->object = CtrStdNil;
ctr_array_push(myself, argument);
ctr_heap_free( argument );
}
myself->value.avalue->head = putIndexNumber + 1;
}
if (putIndexNumber < tail) {
ctr_size j;
for(j = tail; j > putIndexNumber; j--) {
*(myself->value.avalue->elements + j) = CtrStdNil;
}
myself->value.avalue->tail = putIndexNumber;
}
*(myself->value.avalue->elements + putIndexNumber) = putValue;
return myself;
}
/**
* [Array] pop
*
* Pops off the last element of the array.
*/
ctr_object* ctr_array_pop(ctr_object* myself, ctr_argument* argumentList) {
if (myself->value.avalue->tail >= myself->value.avalue->head) {
return CtrStdNil;
}
myself->value.avalue->head--;
return *(myself->value.avalue->elements + myself->value.avalue->head);
}
/**
* [Array] - [Number]
*
* Deletes the element specified by the index number and
* shrinks the array accordingly. If the index number does not exist,
* the array will remain the same. This operation changes the array itself.
*
* Usage:
*
* x := Array ← 1 ; 2 ; 3.
* x - 1. #1 ; 3
*/
ctr_object* ctr_array_delete(ctr_object* myself, ctr_argument* argumentList) {
ctr_size index = ctr_internal_cast2number(argumentList->object)->value.nvalue;
ctr_size length = (ctr_size) myself->value.avalue->head - myself->value.avalue->tail;
ctr_size i;
ctr_size found = 0;
for( i = index; i < length-1; i ++ ) {
*(myself->value.avalue->elements + i) = *(myself->value.avalue->elements + (i+1));
found = 1;
}
if (found) {
myself->value.avalue->head--;
}
return myself;
}
/**
* [Array] shift
*
* Shifts off the first element of the array.
*/
ctr_object* ctr_array_shift(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* shiftedOff;
if (myself->value.avalue->tail >= myself->value.avalue->head) {
return CtrStdNil;
}
shiftedOff = *(myself->value.avalue->elements + myself->value.avalue->tail);
myself->value.avalue->tail++;
return shiftedOff;
}
/**
* [Array] count
*
* Returns the number of elements in the array.
*/
ctr_object* ctr_array_count(ctr_object* myself, ctr_argument* argumentList) {
ctr_number d = 0;
d = (ctr_number) myself->value.avalue->head - myself->value.avalue->tail;
return ctr_build_number_from_float( (ctr_number) d );
}
/**
* [Array] from: [Begin] length: [End]
*
* Copies part of an array indicated by from and to and
* returns a new array consisting of a copy of this region.
*/
ctr_object* ctr_array_from_length(ctr_object* myself, ctr_argument* argumentList) {
ctr_argument* pushArg;
ctr_argument* elnumArg;
ctr_object* elnum;
ctr_object* startElement = ctr_internal_cast2number(argumentList->object);
ctr_object* count = ctr_internal_cast2number(argumentList->next->object);
int start = (int) startElement->value.nvalue;
int len = (int) count->value.nvalue;
int i = 0;
ctr_object* newArray = ctr_array_new(CtrStdArray, NULL);
for(i = start; i < start + len; i++) {
pushArg = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
elnumArg = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
elnum = ctr_build_number_from_float((ctr_number) i);
elnumArg->object = elnum;
pushArg->object = ctr_array_get(myself, elnumArg);
ctr_array_push(newArray, pushArg);
ctr_heap_free( elnumArg );
ctr_heap_free( pushArg );
}
return newArray;
}
/**
* [Array] replace: [Number] length: [Number] with: [Array].
*
* Returns a copy of the array with the specified elements replaced.
* The first argument indicates the start index to begin the replacement.
* Here, 0 means the beginning of the array. The second argument (length)
* must indicate the number of elements to delete in the copy, counting
* from the starting point. Finally, one has to provide the replacement
* array as the third argument.
* If the replacement array is empty, the specified elements will only be
* removed from the copy.
* If the replacement is not an array an error will be thrown.
*
* Usage:
*
* ☞ cakes := Array ← 'apple' ; 'berry' ; 'choco' ; 'cheese'.
* #apple, cinnamon, pineapple, cheese
* ☞ buy := cakes replace: 1 length: 2 with: ( Array ← 'cinnamon' ; 'pineapple' ).
* #apple, cinnamon, pineapple
* ☞ buy := cakes replace: 1 length: 12 with: ( Array ← 'cinnamon' ; 'pineapple' ).
* #apple, berry
* ☞ buy := cakes replace: 2 length: 10 with: ( Array new ).
* #berry, choco, cheese
* ☞ buy := cakes replace: '' length: '1' with: ( Array new ).
* #error...
* ☞ buy := cakes replace: '' length: '1' with: 'x'.
*
*/
ctr_object* ctr_array_splice(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* newArray = ctr_array_new(CtrStdArray, NULL);
ctr_object* start = ctr_internal_cast2number(argumentList->object);
ctr_object* deleteCount = ctr_internal_cast2number(argumentList->next->object);
ctr_object* replacement = argumentList->next->next->object;
ctr_object* remainder;
ctr_argument* sliceFromArg;
ctr_argument* sliceLengthArg;
ctr_argument* replacementArg;
ctr_argument* remainderArg;
ctr_size n;
if ( replacement->info.type != CTR_OBJECT_TYPE_OTARRAY ) {
CtrStdFlow = ctr_build_string_from_cstring( "Replacement must be an array." );
return myself;
}
n = ( start->value.nvalue + deleteCount->value.nvalue );
sliceFromArg = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
sliceLengthArg = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
replacementArg = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
remainderArg = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
sliceFromArg->object = ctr_build_number_from_float(0);
sliceLengthArg->object = start;
sliceFromArg->next = sliceLengthArg;
newArray = ctr_array_from_length( myself, sliceFromArg );
replacementArg->object = replacement;
newArray = ctr_array_add(newArray, replacementArg);
sliceFromArg->object = ctr_build_number_from_float( n );
if ( n < (myself->value.avalue->head - myself->value.avalue->tail) ) {
sliceLengthArg->object = ctr_build_number_from_float( (myself->value.avalue->head - myself->value.avalue->tail) - n );
sliceFromArg->next = sliceLengthArg;
remainder = ctr_array_from_length( myself, sliceFromArg );
remainderArg->object = remainder;
newArray = ctr_array_add( newArray, remainderArg );
}
ctr_heap_free( sliceFromArg );
ctr_heap_free( sliceLengthArg );
ctr_heap_free( replacementArg );
ctr_heap_free( remainderArg );
return newArray;
}
/**
* [Array] + [Array]
*
* Returns a new array, containing elements of itself and the other
* array.
*/
ctr_object* ctr_array_add(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* otherArray = argumentList->object;
ctr_object* newArray = ctr_array_new(CtrStdArray, NULL);
int i;
for(i = myself->value.avalue->tail; i<myself->value.avalue->head; i++) {
ctr_argument* pushArg = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
ctr_argument* elnumArg = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
ctr_object* elnum = ctr_build_number_from_float((ctr_number) i);
elnumArg->object = elnum;
pushArg->object = ctr_array_get(myself, elnumArg);
ctr_array_push(newArray, pushArg);
ctr_heap_free( elnumArg );
ctr_heap_free( pushArg );
}
if (otherArray->info.type == CTR_OBJECT_TYPE_OTARRAY) {
for(i = otherArray->value.avalue->tail; i<otherArray->value.avalue->head; i++) {
ctr_argument* pushArg = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
ctr_argument* elnumArg = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
ctr_object* elnum = ctr_build_number_from_float((ctr_number) i);
elnumArg->object = elnum;
pushArg->object = ctr_array_get(otherArray, elnumArg);
ctr_array_push(newArray, pushArg);
ctr_heap_free( elnumArg );
ctr_heap_free( pushArg );
}
}
return newArray;
}
/**
* [Array] by: [Array].
*
* Combines the first array with the second one thus creating
* a map. The keys of the newly generated map will be provided by the
* first array while the values are extracted from the second one.
*
* Usage:
*
* ☞ city := Array ← 'London' ; 'Paris' ; 'Berlin'.
* ☞ temperature := Array ← '15' ; '16' ; '15'.
* ☞ weather := temperature by: city.
*/
ctr_object* ctr_array_combine(ctr_object* myself, ctr_argument* argumentList) {
ctr_size i;
ctr_argument* key = ctr_heap_allocate( sizeof( ctr_argument ) );
ctr_argument* value = ctr_heap_allocate( sizeof( ctr_argument ) );
ctr_argument* index = ctr_heap_allocate( sizeof( ctr_argument ) );
ctr_object* map = ctr_map_new( CtrStdMap, argumentList );
for(i = myself->value.avalue->tail; i<myself->value.avalue->head; i++) {
index->object = ctr_build_number_from_float((ctr_number) i);
key->object = ctr_array_get( myself, index );
value->object = ctr_array_get( argumentList->object, index );
key->next = value;
ctr_map_put( map, key );
}
ctr_heap_free(key);
ctr_heap_free(value);
ctr_heap_free(index);
return map;
}
/**
* [Array] copy
*
* Copies the array. The array object will answer this message by
* returning a shallow copy of itself. This means that the values in the
* newly returned array can be replaced or deleted without affecting
* the original array. However, modifying the values in the array will
* still cause their counterparts in the original array to be modified
* as well.
*
* Usage:
*
* ☞ a := Array ← 1 ; 2 ; 3.
* ☞ b := a copy.
* b put: 999 at: 1. #b @ 1 = 999
*/
ctr_object* ctr_array_copy(ctr_object* myself, ctr_argument* argumentList) {
ctr_size i = 0;
ctr_object* copy = ctr_array_new( CtrStdArray, argumentList );
ctr_argument* arg = ctr_heap_allocate(sizeof(ctr_argument));
ctr_argument* index = ctr_heap_allocate( sizeof( ctr_argument ) );
for(i = myself->value.avalue->tail; i<myself->value.avalue->head; i++) {
index->object = ctr_build_number_from_float((ctr_number) i);
arg->object = ctr_array_get( myself, index );
ctr_array_push( copy, arg );
}
ctr_heap_free( arg );
ctr_heap_free( index );
return copy;
}
/**
* @internal
*
* Internal sort function, for use with ArraySort.
* Interfaces with qsort-compatible function.
*/
ctr_object* temp_sorter;
int ctr_sort_cmp(const void * a, const void * b) {
ctr_argument* arg1 = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
ctr_argument* arg2 = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
ctr_object* result;
ctr_object* numResult;
arg1->next = arg2;
arg1->object = *((ctr_object**) a);
arg2->object = *((ctr_object**) b);
result = ctr_block_run(temp_sorter, arg1, NULL);
numResult = ctr_internal_cast2number(result);
ctr_heap_free( arg1 );
ctr_heap_free( arg2 );
return (int) numResult->value.nvalue;
}
/**
* [Array] sort: [Block]
*
* Sorts the contents of an array using a sort block.
* Uses qsort.
*/
ctr_object* ctr_array_sort(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* sorter = argumentList->object;
if (sorter->info.type != CTR_OBJECT_TYPE_OTBLOCK) {
CtrStdFlow = ctr_build_string_from_cstring("Expected block.");
CtrStdFlow->info.sticky = 1;
return myself;
}
temp_sorter = sorter;
qsort((myself->value.avalue->elements+myself->value.avalue->tail), myself->value.avalue->head-myself->value.avalue->tail, sizeof(ctr_object*), ctr_sort_cmp);
return myself;
}
/**
* [Array] toString
*
* Returns a string representation of the array and its contents.
* This representation will be encoded in the Citrine language itself and is
* therefore evallable.
*
* Usage:
*
* a := Array ← 'hello' ; 'world'.
* b := a toString.
* c := b eval.
* x := c @ 1. #world
*
* toString messages are implicitly send by some objects, for instance when
* attempting to write an Array using a Pen.
*
* You can also use the alias 'serialize'.
*/
ctr_object* ctr_array_to_string( ctr_object* myself, ctr_argument* argumentList ) {
int i;
ctr_object* arrayElement;
ctr_argument* newArgumentList;
ctr_object* string = ctr_build_empty_string();
newArgumentList = ctr_heap_allocate( sizeof( ctr_argument ) );
if ( myself->value.avalue->tail == myself->value.avalue->head ) {
newArgumentList->object = ctr_build_string_from_cstring( CTR_DICT_CODEGEN_ARRAY_NEW );
string = ctr_string_append( string, newArgumentList );
} else {
newArgumentList->object = ctr_build_string_from_cstring( CTR_DICT_CODEGEN_ARRAY_NEW_PUSH );
string = ctr_string_append( string, newArgumentList );
}
for(i=myself->value.avalue->tail; i<myself->value.avalue->head; i++) {
arrayElement = *( myself->value.avalue->elements + i );
if ( arrayElement->info.type == CTR_OBJECT_TYPE_OTBOOL || arrayElement->info.type == CTR_OBJECT_TYPE_OTNUMBER
|| arrayElement->info.type == CTR_OBJECT_TYPE_OTNIL ) {
newArgumentList->object = arrayElement;
string = ctr_string_append( string, newArgumentList );
} else if ( arrayElement->info.type == CTR_OBJECT_TYPE_OTSTRING ) {
newArgumentList->object = ctr_build_string_from_cstring("'");
string = ctr_string_append( string, newArgumentList );
newArgumentList->object = ctr_string_quotes_escape( arrayElement, newArgumentList );
string = ctr_string_append( string, newArgumentList );
newArgumentList->object = ctr_build_string_from_cstring("'");
string = ctr_string_append( string, newArgumentList );
} else {
newArgumentList->object = ctr_build_string_from_cstring("(");
ctr_string_append( string, newArgumentList );
newArgumentList->object = arrayElement;
string = ctr_string_append( string, newArgumentList );
newArgumentList->object = ctr_build_string_from_cstring(")");
ctr_string_append( string, newArgumentList );
}
if ( (i + 1 )<myself->value.avalue->head ) {
newArgumentList->object = ctr_build_string_from_cstring(" ; ");
string = ctr_string_append( string, newArgumentList );
}
}
ctr_heap_free( newArgumentList );
return string;
}
/**
* [Array] fill: [Number] with: [Object]
*
* Fills the array with the specified number of objects.
*
* Usage:
*
* ☞ a := Array new fill: 42 with: 'x'.
*/
ctr_object* ctr_array_fill( ctr_object* myself, ctr_argument* argumentList ) {
size_t n;
int i;
ctr_argument* newArgumentList;
n = ctr_internal_cast2number( argumentList->object )->value.nvalue;
newArgumentList = ctr_heap_allocate( sizeof(ctr_argument) );
newArgumentList->object = argumentList->next->object;
for(i = 0; i < n; i ++ ) {
ctr_array_push( myself, newArgumentList );
}
ctr_heap_free(newArgumentList);
return myself;
}
/**
* [Array] column: [Number]
*
* Extracts the specified column from the array.
* In a nested array this message will select the Nth
* element of every array. N is specified using the
* Number argument.
*
* Usage:
*
* ☞ a := Array ←
* (Array ← 1 ; 2 ; 3) ;
* (Array ← 4 ; 5 ; 6) ;
* (Array ← 7 ; 8 ; 9).
* ☞ b := a column: 1. #2,5,8
*/
ctr_object* ctr_array_column( ctr_object* myself, ctr_argument* argumentList ) {
int i;
size_t n;
ctr_argument* newArgumentList;
ctr_object* newArray;
ctr_object* element;
newArray = ctr_array_new( CtrStdArray, NULL );
n = ctr_internal_cast2number( argumentList->object )->value.nvalue;
if ( n <= 0 ) {
return newArray;
}
newArgumentList = ctr_heap_allocate(sizeof(ctr_argument));
for(i = myself->value.avalue->tail; i < myself->value.avalue->head; i++) {
element = *(myself->value.avalue->elements + i);
if ( element->info.type != CTR_OBJECT_TYPE_OTARRAY ) continue;
if ( n >= element->value.avalue->head ) continue;
newArgumentList->object = *(element->value.avalue->elements + element->value.avalue->tail + n);
ctr_array_push( newArray, newArgumentList );
}
ctr_heap_free( newArgumentList );
return newArray;
}
/**
* [Array] indexOf: [Object].
*
* Checks whether the specified object occurs in the array and returns the index number
* if so. If not, the index number -1 will be returned. Note that the comparison
* will be performed by converting both values to strings.
*
* Usage:
*
* ☞ colors := Array ← 'red' ; 'green' ; 'blue' ; 3 ; (Array new) ; False ; Nil.
*
* ✎ write: (colors indexOf: 'green'), brk. #1
* ✎ write: (colors indexOf: 'blue'), brk. #2
* ✎ write: (colors indexOf: 'red'), brk. #0
* ✎ write: (colors indexOf: 3), brk. #3
* ✎ write: (colors indexOf: (Array new)), brk. #4
* ✎ write: (colors indexOf: 'False'), brk. #5
* ✎ write: (colors indexOf: Nil), brk. #6
* ✎ write: (colors indexOf: 'purple'), brk. #-1
*/
ctr_object* ctr_array_index_of( ctr_object* myself, ctr_argument* argumentList ) {
int64_t found = -1, i = 0;
ctr_object* needle = ctr_internal_cast2string(argumentList->object);
ctr_object* element;
for(i = myself->value.avalue->tail; i < myself->value.avalue->head; i++) {
element = ctr_internal_cast2string( (ctr_object*) *(myself->value.avalue->elements + i) );
if (
element->value.svalue->vlen == needle->value.svalue->vlen &&
strncmp(element->value.svalue->value,needle->value.svalue->value,needle->value.svalue->vlen)==0) {
found = i;
break;
}
}
return ctr_build_number_from_float(found);
}
/**
* [Array] serialize
*
* Alias for [Array] toString.
*
* See 'Map serialize' for the reason for this alias.
*/
/**
* Map
*
* Creates a Map object.
*
* Usage:
*
* files := Map new.
* files put: 'readme.txt' at: 'textfile'.
*/
ctr_object* ctr_map_new(ctr_object* myclass, ctr_argument* argumentList) {
ctr_object* s = ctr_internal_create_object(CTR_OBJECT_TYPE_OTOBJECT);
s->link = myclass;
return s;
}
/**
* [Map] type
*
* Returns the string 'Map'.
*
**/