forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfoundation-proper-list.cpp
More file actions
1287 lines (1011 loc) · 34.6 KB
/
foundation-proper-list.cpp
File metadata and controls
1287 lines (1011 loc) · 34.6 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
/* Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include <foundation.h>
#include <foundation-auto.h>
#include <foundation-stdlib.h>
#include "foundation-private.h"
////////////////////////////////////////////////////////////////////////////////
// Returns true if the list is indirect.
bool MCProperListIsIndirect(MCProperListRef self);
// Creates an indirect mutable list with contents.
static bool __MCProperListCreateIndirect(__MCProperList *contents, __MCProperList*& r_list);
// Returns true if the list is indirect.
static bool __MCProperListIsIndirect(__MCProperList *self);
// Replaces all the values in the list with immutable copies.
static bool __MCProperListMakeContentsImmutable(__MCProperList *self);
// Creates an immutable list from this one, changing 'self' to indirect.
static bool __MCProperListMakeIndirect(__MCProperList *self);
// Ensures the given mutable but indirect list is direct.
static bool __MCProperListResolveIndirect(__MCProperList *self);
static bool __MCProperListExpandAt(MCProperListRef self, uindex_t p_at, uindex_t p_count);
static bool __MCProperListShrinkAt(MCProperListRef self, uindex_t p_at, uindex_t p_count);
static void __MCProperListClampRange(MCProperListRef self, MCRange& x_range);
////////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF
bool MCProperListCreate(const MCValueRef *p_values, uindex_t p_length, MCProperListRef& r_list)
{
bool t_success;
t_success = true;
MCProperListRef t_list;
t_list = nil;
if (t_success)
t_success = MCProperListCreateMutable(t_list);
if (t_success)
t_success = MCProperListPushElementsOntoBack(t_list, p_values, p_length);
if (t_success)
return MCProperListCopyAndRelease(t_list, r_list);
MCValueRelease(t_list);
return false;
}
MC_DLLEXPORT_DEF
bool MCProperListCreateWithForeignValues(MCTypeInfoRef p_typeinfo, const void *p_values, uindex_t p_value_count, MCProperListRef& r_list)
{
__MCAssertIsForeignTypeInfo(p_typeinfo);
MCAssert(p_values != nullptr || p_value_count == 0);
MCAutoProperListRef t_list;
if (!MCProperListCreateMutable(&t_list))
{
return false;
}
const MCForeignTypeDescriptor *t_descriptor =
MCForeignTypeInfoGetDescriptor(p_typeinfo);
while(p_value_count > 0)
{
MCAutoValueRef t_value;
if (t_descriptor->doimport != nil)
{
if (!t_descriptor->doimport(t_descriptor,
(void *)p_values,
false,
&t_value))
{
return false;
}
}
else
{
if (!MCForeignValueCreate(p_typeinfo,
(void *)p_values,
(MCForeignValueRef&)&t_value))
{
return false;
}
}
if (!MCProperListPushElementOntoBack(*t_list,
*t_value))
{
return false;
}
p_value_count -= 1;
p_values = (byte_t *)p_values + t_descriptor->size;
}
if (!t_list.MakeImmutable())
{
return false;
}
r_list = t_list.Take();
return true;
}
MC_DLLEXPORT_DEF
bool MCProperListConvertToForeignValues(MCProperListRef self, MCTypeInfoRef p_typeinfo, void*& r_values_ptr, uindex_t& r_values_count)
{
__MCAssertIsForeignTypeInfo(p_typeinfo);
const MCForeignTypeDescriptor *t_descriptor =
MCForeignTypeInfoGetDescriptor(p_typeinfo);
uindex_t t_values_count = MCProperListGetLength(self);
void *t_values = nullptr;
if (!MCMemoryNew(t_values_count * t_descriptor->size, t_values))
{
return false;
}
byte_t *t_values_ptr = (byte_t*)t_values;
for(uindex_t t_index = 0; t_index < t_values_count; t_index++)
{
MCValueRef t_value = MCProperListFetchElementAtIndex(self, t_index);
if (MCValueGetTypeInfo(t_value) == p_typeinfo)
{
MCMemoryCopy(t_values_ptr, MCForeignValueGetContentsPtr(t_value), t_descriptor->size);
}
else if (MCValueGetTypeInfo(t_value) == t_descriptor->bridgetype)
{
if (!t_descriptor->doexport(t_descriptor, t_value, false, t_values_ptr))
{
MCMemoryDelete(t_values);
return false;
}
}
else
{
MCMemoryDelete(t_values);
return false;
}
t_values_ptr += t_descriptor->size;
}
r_values_ptr = t_values;
r_values_count = t_values_count;
return true;
}
MC_DLLEXPORT_DEF
bool MCProperListCreateMutable(MCProperListRef& r_list)
{
if (!__MCValueCreate(kMCValueTypeCodeProperList, r_list))
return false;
r_list -> flags |= kMCProperListFlagIsMutable;
return true;
}
bool
MCProperListCreateAndRelease(MCValueRef *p_values,
uindex_t p_length,
MCProperListRef& r_list)
{
__MCProperList *t_list;
if (!__MCValueCreate(kMCValueTypeCodeProperList, t_list))
return false;
t_list -> list = p_values;
t_list -> length = p_length;
r_list = t_list;
return true;
}
MC_DLLEXPORT_DEF
bool MCProperListCopy(MCProperListRef self, MCProperListRef& r_new_list)
{
// If we aren't mutable, then we can just copy directly.
if (!MCProperListIsMutable(self))
{
r_new_list = MCValueRetain(self);
return true;
}
// If we mutable, but still indirect just take the contents list.
if (__MCProperListIsIndirect(self))
{
r_new_list = MCValueRetain(self -> contents);
return true;
}
// Make the contents immutable.
if (!__MCProperListMakeContentsImmutable(self))
return false;
// Make the array indirect.
if (!__MCProperListMakeIndirect(self))
return false;
// Return a copy of the contents.
r_new_list = MCValueRetain(self -> contents);
return true;
}
MC_DLLEXPORT_DEF
bool MCProperListCopyAndRelease(MCProperListRef self, MCProperListRef& r_new_list)
{
// If we aren't mutable, then new list is just us.
if (!MCProperListIsMutable(self))
{
r_new_list = self;
return true;
}
// If we are indirect, then new list is the contents, we are released.
if (__MCProperListIsIndirect(self))
{
r_new_list = MCValueRetain(self -> contents);
MCValueRelease(self);
return true;
}
// We need to make an immutable copy, so first change all our contents
// to immutable.
if (!__MCProperListMakeContentsImmutable(self))
return false;
// If we have a reference count of one 'self' becomes the immutable copy.
if (self -> references == 1)
{
// We are no longer immutable.
self -> flags &= ~kMCProperListFlagIsMutable;
// Return this as the new list.
r_new_list = self;
return true;
}
// Otherwise we must build a new indirect value.
if (!__MCProperListMakeIndirect(self))
return false;
// Reduce our reference count.
self -> references -= 1;
// Return a copy of the contents.
r_new_list = MCValueRetain(self -> contents);
return true;
}
MC_DLLEXPORT_DEF
bool MCProperListMutableCopy(MCProperListRef self, MCProperListRef& r_new_list)
{
// If the list is immutable, then the new mutable list will be indirect
// referencing it. [ non-mutable lists cannot be indirect so self does not
// need resolving ].
if (!MCProperListIsMutable(self))
return __MCProperListCreateIndirect(self, r_new_list);
// If the list is already indirect, we just create a new reference to the
// same contents.
if (__MCProperListIsIndirect(self))
return __MCProperListCreateIndirect(self -> contents, r_new_list);
// If the list is mutable, we make it immutable and indirect and share
// the indirect copy.
if (!__MCProperListMakeContentsImmutable(self))
return false;
// Make self indirect, and fetch its contents.
if (!__MCProperListMakeIndirect(self))
return false;
// Finally, create a new indirect list with contents of self.
return __MCProperListCreateIndirect(self -> contents, r_new_list);
}
MC_DLLEXPORT_DEF
bool MCProperListMutableCopyAndRelease(MCProperListRef self, MCProperListRef& r_new_list)
{
if (self -> references == 1)
{
if (!MCProperListIsMutable(self))
self -> flags |= kMCProperListFlagIsMutable;
r_new_list = self;
return true;
}
if (!MCProperListMutableCopy(self, r_new_list))
return false;
self -> references -= 1;
return true;
}
////////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF
bool MCProperListIsMutable(MCProperListRef self)
{
return (self -> flags & kMCProperListFlagIsMutable) != 0;
}
bool MCProperListIsIndirect(MCProperListRef self)
{
return (self -> flags & kMCProperListFlagIsIndirect) != 0;
}
MC_DLLEXPORT_DEF
uindex_t MCProperListGetLength(MCProperListRef self)
{
if (!__MCProperListIsIndirect(self))
return self -> length;
return self -> contents -> length;
}
MC_DLLEXPORT_DEF
bool MCProperListIsEmpty(MCProperListRef self)
{
if (!__MCProperListIsIndirect(self))
return (self -> length == 0);
return (self -> contents -> length == 0);
}
////////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF
bool MCProperListPushElementsOntoBack(MCProperListRef self, const MCValueRef *p_values, uindex_t p_length)
{
return MCProperListInsertElements(self, p_values, p_length, MCProperListGetLength(self));
}
MC_DLLEXPORT_DEF
bool MCProperListPushElementsOntoFront(MCProperListRef self, const MCValueRef *p_values, uindex_t p_length)
{
return MCProperListInsertElements(self, p_values, p_length, 0);
}
MC_DLLEXPORT_DEF
bool MCProperListPushElementOntoBack(MCProperListRef self, const MCValueRef p_value)
{
return MCProperListPushElementsOntoBack(self, &p_value, 1);
}
MC_DLLEXPORT_DEF
bool MCProperListPushElementOntoFront(MCProperListRef self, const MCValueRef p_value)
{
return MCProperListPushElementsOntoFront(self, &p_value, 1);
}
MC_DLLEXPORT_DEF
bool MCProperListAppendList(MCProperListRef self, MCProperListRef p_value)
{
if (MCProperListIsIndirect(p_value))
p_value = p_value -> contents;
if (p_value != self)
return MCProperListPushElementsOntoBack(self, p_value -> list, p_value -> length);
MCAutoProperListRef t_list;
if (!MCProperListCopy(p_value, &t_list))
return false;
return MCProperListAppendList(self, *t_list);
}
MC_DLLEXPORT_DEF
bool MCProperListInsertElements(MCProperListRef self, const MCValueRef *p_values, uindex_t p_length, index_t p_index)
{
MCAssert(MCProperListIsMutable(self));
if (__MCProperListIsIndirect(self))
if (!__MCProperListResolveIndirect(self))
return false;
if (!__MCProperListExpandAt(self, p_index, p_length))
return false;
for (uindex_t i = 0; i < p_length; i++)
self -> list[i + p_index] = MCValueRetain(p_values[i]);
return true;
}
MC_DLLEXPORT_DEF
bool MCProperListInsertElement(MCProperListRef self, MCValueRef p_value, index_t p_index)
{
return MCProperListInsertElements(self, &p_value, 1, p_index);
}
MC_DLLEXPORT_DEF
bool MCProperListInsertList(MCProperListRef self, MCProperListRef p_value, index_t p_index)
{
if (MCProperListIsIndirect(p_value))
p_value = p_value -> contents;
if (p_value != self)
return MCProperListInsertElements(self, p_value -> list, p_value -> length, p_index);
MCAutoProperListRef t_list;
if (!MCProperListCopy(p_value, &t_list))
return false;
return MCProperListInsertList(self, *t_list, p_index);
}
MC_DLLEXPORT_DEF
bool MCProperListRemoveElements(MCProperListRef self, uindex_t p_start, uindex_t p_count)
{
MCAssert(MCProperListIsMutable(self));
if (__MCProperListIsIndirect(self))
if (!__MCProperListResolveIndirect(self))
return false;
MCAutoArray<MCValueRef> t_values;
for (uindex_t i = p_start; i < p_start + p_count; i++)
{
if (!t_values . Push(self -> list[i]))
return false;
}
if (!__MCProperListShrinkAt(self, p_start, p_count))
return false;
for (uindex_t i = 0; i < t_values . Size(); i++)
MCValueRelease(t_values[i]);
return true;
}
MC_DLLEXPORT_DEF
bool MCProperListRemoveElement(MCProperListRef self, uindex_t p_index)
{
return MCProperListRemoveElements(self, p_index, 1);
}
////////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF
MCValueRef MCProperListFetchHead(MCProperListRef self)
{
if (MCProperListIsIndirect(self))
self = self -> contents;
MCAssert (self->length > 0);
return self -> list[0];
}
MC_DLLEXPORT_DEF
MCValueRef MCProperListFetchTail(MCProperListRef self)
{
if (MCProperListIsIndirect(self))
self = self -> contents;
MCAssert (self->length > 0);
return self -> list[self -> length - 1];
}
MC_DLLEXPORT_DEF
MCValueRef MCProperListFetchElementAtIndex(MCProperListRef self, uindex_t p_index)
{
if (MCProperListIsIndirect(self))
self = self -> contents;
if (p_index >= self -> length)
return kMCNull;
return self -> list[p_index];
}
MC_DLLEXPORT_DEF
bool MCProperListPopBack(MCProperListRef self, MCValueRef& r_value)
{
MCAssert(MCProperListIsMutable(self));
if (__MCProperListIsIndirect(self))
if (!__MCProperListResolveIndirect(self))
return false;
MCAssert (self -> length > 0);
MCValueRef t_value;
t_value = self -> list[self -> length - 1];
if (!__MCProperListShrinkAt(self, self -> length - 1, 1))
return false;
r_value = t_value;
return true;
}
MC_DLLEXPORT_DEF
bool MCProperListPopFront(MCProperListRef self, MCValueRef& r_value)
{
MCAssert(MCProperListIsMutable(self));
if (__MCProperListIsIndirect(self))
if (!__MCProperListResolveIndirect(self))
return false;
MCAssert (self -> length > 0);
MCValueRef t_value;
t_value = self -> list[0];
if (!__MCProperListShrinkAt(self, 0, 1))
return false;
r_value = t_value;
return true;
}
MC_DLLEXPORT_DEF
bool MCProperListCopySublist(MCProperListRef self, MCRange p_range, MCProperListRef& r_elements)
{
if (MCProperListIsIndirect(self))
self = self -> contents;
__MCProperListClampRange(self, p_range);
return MCProperListCreate(self -> list + p_range . offset, p_range . length, r_elements);
}
////////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF
bool MCProperListFirstIndexOfElement(MCProperListRef self, MCValueRef p_needle, uindex_t p_after, uindex_t& r_offset)
{
return MCProperListFirstIndexOfElementInRange(self, p_needle,
MCRangeMake(p_after, UINDEX_MAX), r_offset);
}
MC_DLLEXPORT_DEF bool
MCProperListFirstIndexOfElementInRange (MCProperListRef self,
MCValueRef p_needle,
MCRange p_range,
uindex_t & r_offset)
{
if (MCProperListIsIndirect (self))
self = self->contents;
__MCProperListClampRange (self, p_range);
for (uindex_t t_offset = 0; /* Relative to start of range */
t_offset < p_range.length;
++t_offset)
{
uindex_t t_index = p_range.offset + t_offset;
if (MCValueIsEqualTo(p_needle, self->list[t_index]))
{
r_offset = t_offset;
return true;
}
}
return false;
}
MC_DLLEXPORT_DEF bool
MCProperListLastIndexOfElementInRange (MCProperListRef self,
MCValueRef p_needle,
MCRange p_range,
uindex_t & r_offset)
{
if (MCProperListIsIndirect (self))
self = self->contents;
__MCProperListClampRange (self, p_range);
uindex_t t_offset = p_range.length; /* Relative to start of range */
while (0 < t_offset--)
{
uindex_t t_index = p_range.offset + t_offset;
if (MCValueIsEqualTo (p_needle, self->list[t_index]))
{
r_offset = t_offset;
return true;
}
}
return false;
}
MC_DLLEXPORT_DEF
bool MCProperListFirstOffsetOfList(MCProperListRef self, MCProperListRef p_needle, uindex_t p_after, uindex_t& r_offset)
{
return MCProperListFirstOffsetOfListInRange (self, p_needle,
MCRangeMake (p_after, UINDEX_MAX), r_offset);
}
MC_DLLEXPORT_DEF bool
MCProperListFirstOffsetOfListInRange (MCProperListRef self,
MCProperListRef p_needle,
MCRange p_range,
uindex_t & r_offset)
{
if (MCProperListIsIndirect(p_needle))
p_needle = p_needle->contents;
/* Empty lists are never found */
if (0 == p_needle->length)
return false;
if (MCProperListIsIndirect(self))
self = self->contents;
__MCProperListClampRange (self, p_range);
/* If the range is too short to contain the needle, the needle
* clearly can't be found. */
if (p_range.length < p_needle->length)
return false;
/* Search algorithm: look forward along the range for the first
* occurrence of the *last* element of the needle, then work
* backward along the needle to see if the lists match. */
for (uindex_t t_offset = 0; /* Relative to start of range */
t_offset <= p_range.length - p_needle->length;
++t_offset)
{
bool t_match = true;
/* Correlate the two lists at this offset */
for (uindex_t t_needle_rindex = 0; /* Relative to *end* of needle */
t_needle_rindex < p_needle->length && t_match;
++t_needle_rindex)
{
uindex_t t_needle_index = p_needle->length - t_needle_rindex - 1;
uindex_t t_self_index = p_range.offset + t_offset + t_needle_index;
t_match = MCValueIsEqualTo (p_needle->list[t_needle_index],
self->list[t_self_index]);
}
if (t_match)
{
r_offset = t_offset;
return true;
}
}
return false;
}
MC_DLLEXPORT_DEF bool
MCProperListLastOffsetOfListInRange (MCProperListRef self,
MCProperListRef p_needle,
MCRange p_range,
uindex_t & r_offset)
{
if (MCProperListIsIndirect (p_needle))
p_needle = p_needle->contents;
/* Empty lists are never found */
if (0 == p_needle->length)
return false;
if (MCProperListIsIndirect (self))
self = self->contents;
__MCProperListClampRange (self, p_range);
/* If the range is too short to contain the needle, the needle clearly
* can't be found. */
if (p_range.length < p_needle->length)
return false;
/* Search algorithm: look backward along the range for the first
* occurrence of the first element of the needle, then work
* forward along the needle to see if the lists match. */
/* t_roffset is the reverse offset of the first index in a
* possible match, relative to the last element in the range
* (i.e. t_roffset = 0 for the last element). */
for (uindex_t t_roffset = p_needle->length - 1;
t_roffset < p_range.length;
++t_roffset)
{
/* Offset of first element in the match, relative to start of
* range (i.e. t_offset = 0 for the first element) */
uindex_t t_offset = p_range.length - t_roffset - 1;
bool t_match = true;
/* Correlate the two lists at this offset */
for (uindex_t t_needle_index = 0; /* Relative to start of needle */
t_needle_index < p_needle->length && t_match;
++t_needle_index)
{
uindex_t t_self_index = p_range.offset + t_offset + t_needle_index;
t_match = MCValueIsEqualTo (p_needle->list[t_needle_index],
self->list[t_self_index]);
}
if (t_match)
{
r_offset = t_offset;
return true;
}
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF
bool MCProperListIterate(MCProperListRef self, uintptr_t& x_iterator, MCValueRef& r_element)
{
if (MCProperListIsIndirect(self))
self = self -> contents;
if (x_iterator == self -> length)
return false;
r_element = self -> list[x_iterator];
x_iterator += 1;
return true;
}
MC_DLLEXPORT_DEF
bool MCProperListApply(MCProperListRef self, MCProperListApplyCallback p_callback, void *context)
{
if (MCProperListIsIndirect(self))
self = self -> contents;
for (uindex_t i = 0; i < self -> length; i++)
if (!p_callback(context, self -> list[i]))
return false;
return true;
}
MC_DLLEXPORT_DEF
bool MCProperListMap(MCProperListRef self, MCProperListMapCallback p_callback, MCProperListRef& r_new_list, void *context)
{
if (MCProperListIsIndirect(self))
self = self -> contents;
MCAutoValueRefArray t_values;
if (!t_values.New (self -> length))
return false;
bool t_success;
t_success = true;
for (uindex_t i = 0; t_success && i < self -> length; i++)
{
MCValueRef t_value;
t_value = NULL;
if (!p_callback(context, self -> list[i], t_value))
t_success = false;
/* In case the callback returns a value into t_value but also
* indicates failure, make sure to release t_value on
* failure. */
if (t_success)
t_values[i] = t_value;
else
MCValueRelease (t_value);
}
if (t_success)
t_success = t_values.TakeAsProperList (r_new_list);
return t_success;
}
MC_DLLEXPORT_DEF
bool MCProperListSort(MCProperListRef self, bool p_reverse, MCProperListQuickSortCallback p_callback)
{
MCAssert(MCProperListIsMutable(self));
uindex_t t_item_count;
t_item_count = MCProperListGetLength(self);
if (t_item_count < 2)
return true;
if (MCProperListIsIndirect(self))
if (!__MCProperListResolveIndirect(self))
return false;
qsort(self -> list, self -> length, sizeof(MCValueRef), (int (*)(const void *, const void *))p_callback);
return true;
}
static void MCProperListDoStableSort(MCValueRef*& list, uindex_t p_item_count, MCValueRef*& p_temp, bool p_reverse, MCProperListCompareElementCallback p_callback, void *context)
{
if (p_item_count <= 1)
return;
uint32_t t_first_half_count = p_item_count / 2;
uint32_t t_second_half_count = p_item_count - t_first_half_count;
MCValueRef *t_first_half = list;
MCValueRef *t_second_half = t_first_half + t_first_half_count;
// Sort the halves recursively
MCProperListDoStableSort(t_first_half, t_first_half_count, p_temp, p_reverse, p_callback, context);
MCProperListDoStableSort(t_second_half, t_second_half_count, p_temp, p_reverse, p_callback, context);
// And interleave appropriately
MCValueRef *tmp = p_temp;
while (t_first_half_count > 0 && t_second_half_count > 0)
{
compare_t t_result;
t_result = p_callback(context, *t_first_half, *t_second_half);
bool t_take_first;
t_take_first = p_reverse ? t_result >= 0 : t_result <= 0;
if (t_take_first)
{
*tmp++ = *t_first_half++;
t_first_half_count--;
}
else
{
*tmp++ = *t_second_half++;
t_second_half_count--;
}
}
for (uindex_t i = 0; i < t_first_half_count; i++)
tmp[i] = t_first_half[i];
for (uindex_t i = 0; i < (p_item_count - t_second_half_count); i++)
list[i] = p_temp[i];
}
MC_DLLEXPORT_DEF
bool MCProperListStableSort(MCProperListRef self, bool p_reverse, MCProperListCompareElementCallback p_callback, void *context)
{
MCAssert(MCProperListIsMutable(self));
uindex_t t_item_count;
t_item_count = MCProperListGetLength(self);
if (t_item_count < 2)
return true;
if (__MCProperListIsIndirect(self))
if (!__MCProperListResolveIndirect(self))
return false;
MCValueRef *t_temp_array = new (nothrow) MCValueRef[t_item_count];
MCProperListDoStableSort(self -> list, t_item_count, t_temp_array, p_reverse, p_callback, context);
delete[] t_temp_array;
return true;
}
MC_DLLEXPORT_DEF
bool MCProperListIsEqualTo(MCProperListRef self, MCProperListRef p_other)
{
return __MCProperListIsEqualTo(self, p_other);
}
MC_DLLEXPORT_DEF
bool MCProperListBeginsWithList(MCProperListRef self, MCProperListRef p_prefix)
{
// If the list is indirect, get the contents.
MCProperListRef t_contents;
if (!__MCProperListIsIndirect(self))
t_contents = self;
else
t_contents = self -> contents;
// If the other list is indirect, get its contents.
MCProperListRef t_other_contents;
if (!__MCProperListIsIndirect(p_prefix))
t_other_contents = p_prefix;
else
t_other_contents = p_prefix -> contents;
if (t_other_contents -> length > t_contents -> length)
return false;
for(uindex_t i = 0; i < t_other_contents -> length; i++)
{
if (!MCValueIsEqualTo(t_contents -> list[i], t_other_contents -> list[i]))
return false;
}
// If we get here it means all values in the p_prefix are the same as their equivalents in self.
return true;
}
MC_DLLEXPORT_DEF
bool MCProperListEndsWithList(MCProperListRef self, MCProperListRef p_suffix)
{
// If the list is indirect, get the contents.
MCProperListRef t_contents;
if (!__MCProperListIsIndirect(self))
t_contents = self;
else
t_contents = self -> contents;
// If the other list is indirect, get its contents.
MCProperListRef t_other_contents;
if (!__MCProperListIsIndirect(p_suffix))
t_other_contents = p_suffix;
else
t_other_contents = p_suffix -> contents;
if (t_other_contents -> length > t_contents -> length)
return false;
for(uindex_t i = 1; i <= t_other_contents -> length; i++)
{
if (!MCValueIsEqualTo(t_contents -> list[t_contents -> length - i], t_other_contents -> list[t_other_contents -> length - i]))
return false;
}
// If we get here it means all values in the p_suffix are the same as their equivalents in self.
return true;
}
////////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF
bool MCProperListIsListOfType(MCProperListRef self, MCValueTypeCode p_type)
{
// If the list is indirect, get the contents.
MCProperListRef t_contents;
if (!__MCProperListIsIndirect(self))
t_contents = self;
else
t_contents = self -> contents;
for(uindex_t i = 0; i < t_contents -> length; i++)
{
if (MCValueGetTypeCode(t_contents -> list[i]) != p_type)
return false;
}
return true;
}
MC_DLLEXPORT_DEF
bool MCProperListIsHomogeneous(MCProperListRef self, MCValueTypeCode& r_type)
{
if (MCProperListIsEmpty(self))
{
r_type = kMCValueTypeCodeNull;
return true;
}
// If the list is indirect, get the contents.
MCProperListRef t_contents;
if (!__MCProperListIsIndirect(self))
t_contents = self;
else
t_contents = self -> contents;
MCValueTypeCode t_type;
t_type = MCValueGetTypeCode(t_contents -> list[0]);
if (MCProperListIsListOfType(t_contents, t_type))
{
r_type = t_type;
return true;
}
return false;