-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringlistgen.c
1972 lines (1828 loc) · 52.6 KB
/
stringlistgen.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
/* -------------------------------------------------------------------
StringList
StringList routines sample implementation
-----------------------------------------
This routines handle the StringList container class. This is a very general implementation
and efficiency considerations aren't yet primordial. StringLists can have elements
of any size. This implement single linked StringLists.
The design goals here are just correctness and showing how the implementation of
the proposed interface COULD be done.
---------------------------------------------------------------------- */
#ifndef DEFAULT_START_SIZE
#define DEFAULT_START_SIZE 20
#endif
#include <stdint.h>
#ifndef INT_MAX
#define INT_MAX (((unsigned)-1) >> 1)
#endif
static int IndexOf_nd(const LIST_TYPE(DATA_TYPE) *AL,const CHARTYPE *SearchedElement,void *ExtraArgs,size_t *result);
static int RemoveAt_nd(LIST_TYPE(DATA_TYPE) *AL,size_t idx);
static LIST_TYPE(DATA_TYPE) *CreateWithAllocator(const ContainerAllocator *allocator);
static LIST_TYPE(DATA_TYPE) *Create(void);
static int Finalize(LIST_TYPE(DATA_TYPE) *l);
#define CONTAINER_LIST_SMALL 2
#define CHUNK_SIZE 1000
static int ErrorReadOnly(LIST_TYPE(DATA_TYPE) *L,char *fnName)
{
char buf[512];
snprintf(buf,sizeof(buf),"iStringList.%s",fnName);
L->RaiseError(buf,CONTAINER_ERROR_READONLY);
return CONTAINER_ERROR_READONLY;
}
static int NullPtrError(char *fnName)
{
char buf[512];
snprintf(buf,sizeof(buf),"iStringList.%s",fnName);
return iError.NullPtrError(buf);
}
static LIST_ELEMENT(DATA_TYPE) *NewLink(LIST_TYPE(DATA_TYPE) *li,const CHARTYPE *data,const char *fname)
{
LIST_ELEMENT(DATA_TYPE) *result;
size_t len = STRLEN(data)+1;
result = li->Allocator->malloc(sizeof(*result)+len);
if (result == NULL) {
li->RaiseError(fname,CONTAINER_ERROR_NOMEMORY);
}
else {
result->Next = NULL;
STRCPY(result->Data,data);
}
return result;
}
static int DefaultStringListCompareFunction(const void *left,const void *right,CompareInfo *ExtraArgs)
{
return STRCMP(left,right);
}
/*------------------------------------------------------------------------
Procedure: Contains ID:1
Purpose: Determines if the given data is in the container
Input: The list and the data to be searched
Output: Returns 1 (true) if the data is in there, false
otherwise
Errors: The same as the function IndexOf
------------------------------------------------------------------------*/
static int Contains(const LIST_TYPE(DATA_TYPE) *l,const CHARTYPE *data)
{
size_t idx;
if (l == NULL || data == NULL) {
if (l)
l->RaiseError("iStringList.Contains",CONTAINER_ERROR_BADARG);
else
iError.NullPtrError("iStringList.Contains");
return CONTAINER_ERROR_BADARG;
}
return (IndexOf_nd(l,data,NULL,&idx) < 0) ? 0 : 1;
}
/*------------------------------------------------------------------------
Procedure: Clear ID:1
Purpose: Reclaims all memory used by a list. The list header
itself is not reclaimed but zeroed. Note that the
list must be writable.
Input: The list to be cleared
Output: Returns the number of elemnts that were in the list
if OK, CONTAINER_ERROR_READONLY otherwise.
Errors: None
------------------------------------------------------------------------*/
static int Clear_nd(LIST_TYPE(DATA_TYPE) *l)
{
if (l->Flags & CONTAINER_HAS_OBSERVER)
iObserver.Notify(l,CCL_CLEAR,NULL,NULL);
#ifdef NO_GC
if (l->Heap)
iHeap.Finalize(l->Heap);
else {
LIST_ELEMENT(DATA_TYPE) *rvp = l->First,*tmp;
while (rvp) {
tmp = rvp;
rvp = rvp->Next;
if (l->DestructorFn)
l->DestructorFn(tmp);
l->Allocator->free(tmp);
}
}
#endif
/* Clear the fields that need to be cleared but not all fields */
l->count = 0;
l->Heap = NULL;
l->First = l->Last = NULL;
l->Flags = 0;
l->timestamp = 0;
return 1;
}
static int Clear(LIST_TYPE(DATA_TYPE) *l)
{
if (l == NULL) {
return NullPtrError("Clear");
}
if (l->Flags & CONTAINER_READONLY) {
l->RaiseError("iStringList.Clear",CONTAINER_ERROR_READONLY);
return CONTAINER_ERROR_READONLY;
}
return Clear_nd(l);
}
/*------------------------------------------------------------------------
Procedure: Add ID:1
Purpose: Adds an element to the list
Input: The list and the elemnt to be added
Output: Returns the number of elements in the list or a
negative error code if an error occurs.
Errors: The element to be added can't be NULL, and the list
must be writable.
------------------------------------------------------------------------*/
static int Add_nd(LIST_TYPE(DATA_TYPE) *l,const CHARTYPE *elem)
{
LIST_ELEMENT(DATA_TYPE) *newl;
newl = NewLink(l,elem,"iStringList.Add");
if (newl == 0)
return CONTAINER_ERROR_NOMEMORY;
if (l->count == 0) {
l->First = newl;
}
else {
l->Last->Next = newl;
}
l->Last = newl;
l->timestamp++;
++l->count;
return 1;
}
static int Add(LIST_TYPE(DATA_TYPE) *l,CHARTYPE *elem)
{
int r;
if (l == NULL || elem == NULL) return NullPtrError("Add");
if (l->Flags &CONTAINER_READONLY) return ErrorReadOnly(l,"Add");
r = Add_nd(l,elem);
if (r > 0 && (l->Flags & CONTAINER_HAS_OBSERVER))
iObserver.Notify(l,CCL_ADD,elem,NULL);
return r;
}
/*------------------------------------------------------------------------
Procedure: SetReadOnly ID:1
Purpose: Sets/Unsets the read only flag.
Input: The list and the new value
Output: The old value of the flag
Errors: None
------------------------------------------------------------------------*/
static unsigned SetFlags(LIST_TYPE(DATA_TYPE) *l,unsigned newval)
{
unsigned result;
if (l == NULL) {
NullPtrError("SetFlags");
return 0;
}
result = l->Flags;
l->Flags = newval;
return result;
}
/*------------------------------------------------------------------------
Procedure: IsReadOnly ID:1
Purpose: Queries the read only flag
Input: The list
Output: The state of the flag
Errors: None
------------------------------------------------------------------------*/
static unsigned GetFlags(const LIST_TYPE(DATA_TYPE) *l)
{
if (l == NULL) {
NullPtrError("GetFlags");
return 0;
}
return l->Flags;
}
/*------------------------------------------------------------------------
Procedure: Size ID:1
Purpose: Returns the number of elements in the StringList
Input: The StringList
Output: The number of elements
Errors: None
------------------------------------------------------------------------*/
static size_t Size(const LIST_TYPE(DATA_TYPE) *l)
{
if (l == NULL) {
return (size_t)NullPtrError("Size");
}
return l->count;
}
static LIST_TYPE(DATA_TYPE) *SetAllocator(LIST_TYPE(DATA_TYPE) *l,ContainerAllocator *allocator)
{
if (l == NULL) {
NullPtrError("SetAllocator");
return NULL;
}
if (l->count)
return NULL;
if (allocator != NULL) {
LIST_TYPE(DATA_TYPE) *newStringList;
if (l->Flags&CONTAINER_READONLY) {
l->RaiseError("iStringList.SetAllocator",CONTAINER_READONLY);
return NULL;
}
newStringList = allocator->malloc(sizeof(LIST_TYPE(DATA_TYPE)));
if (newStringList == NULL) {
l->RaiseError("iStringList.SetAllocator",CONTAINER_ERROR_NOMEMORY);
return NULL;
}
memcpy(newStringList,l,sizeof(LIST_TYPE(DATA_TYPE)));
newStringList->Allocator = allocator;
l->Allocator->free(l);
return newStringList;
}
return NULL;
}
/*------------------------------------------------------------------------
Procedure: SetCompareFunction ID:1
Purpose: Defines the function to be used in comparison of
StringList elements
Input: The StringList and the new comparison function. If the new
comparison function is NULL no changes are done.
Output: Returns the old function
Errors: None
------------------------------------------------------------------------*/
static CompareFunction SetCompareFunction(LIST_TYPE(DATA_TYPE) *l,CompareFunction fn)
{
CompareFunction oldfn = l->Compare;
if (l == NULL) {
NullPtrError("iStringList.SetCompareFunction");
return NULL;
}
if (fn != NULL) { /* Treat NULL as an enquiry to get the compare function */
if (l->Flags&CONTAINER_READONLY) {
l->RaiseError("iStringList.SetCompareFunction",CONTAINER_READONLY);
}
else l->Compare = fn;
}
return oldfn;
}
/*------------------------------------------------------------------------
Procedure: Copy ID:1
Purpose: Copies a list. The result is fully allocated (list
elements and data).
Input: The list to be copied
Output: A pointer to the new list
Errors: None. Returns NULL if therfe is no memory left.
------------------------------------------------------------------------*/
static LIST_TYPE(DATA_TYPE) *Copy(const LIST_TYPE(DATA_TYPE) *l)
{
LIST_TYPE(DATA_TYPE) *result;
LIST_ELEMENT(DATA_TYPE) *elem,*newElem;
if (l == NULL) {
NullPtrError("Copy");
return NULL;
}
result = CreateWithAllocator(l->Allocator);
if (result == NULL) {
l->RaiseError("iStringList.Copy",CONTAINER_ERROR_NOMEMORY);
return NULL;
}
result->Flags = l->Flags; /* Same flags */
result->VTable = l->VTable; /* Copy possibly subclassed methods */
result->Compare = l->Compare; /* Copy compare function */
result->RaiseError = l->RaiseError;
elem = l->First;
while (elem) {
newElem = NewLink(result,elem->Data,"iStringList.Copy");
if (newElem == NULL) {
l->RaiseError("iStringList.Copy",CONTAINER_ERROR_NOMEMORY);
Finalize(result);
return NULL;
}
if (elem == l->First) {
result->First = newElem;
result->count++;
}
else {
result->Last->Next = newElem;
result->count++;
}
result->Last = newElem;
elem = elem->Next;
}
if (l->Flags & CONTAINER_HAS_OBSERVER)
iObserver.Notify(l,CCL_COPY,result,NULL);
return result;
}
/*------------------------------------------------------------------------
Procedure: Finalize ID:1
Purpose: Reclaims all memory for a list. The data, list
elements and the list header are reclaimed.
Input: The list to be destroyed. It should NOT be read-
only.
Output: Returns the old count or a negative value if an
error occurs (list not writable)
Errors: Needs a writable list
------------------------------------------------------------------------*/
static int Finalize(LIST_TYPE(DATA_TYPE) *l)
{
int t=0;
unsigned Flags=0;
if (l) Flags = l->Flags;
else return CONTAINER_ERROR_BADARG;
t = Clear(l);
if (t < 0)
return t;
if (Flags & CONTAINER_HAS_OBSERVER)
iObserver.Notify(l,CCL_FINALIZE,NULL,NULL);
if (l->VTable != &iSTRINGLIST(DATA_TYPE))
l->Allocator->free(l->VTable);
l->Allocator->free(l);
return 1;
}
/*------------------------------------------------------------------------
Procedure: GetElement ID:1
Purpose: Returns the data associated with a given position
Input: The StringList and the position
Output: A pointer to the data
Errors: NULL if error in the positgion index
------------------------------------------------------------------------*/
static CHARTYPE *GetElement(LIST_TYPE(DATA_TYPE) *l,size_t position)
{
LIST_ELEMENT(DATA_TYPE) *rvp;
if (l == NULL) {
NullPtrError("GetElement");
return NULL;
}
if (position >= l->count) {
l->RaiseError("GetElement",CONTAINER_ERROR_INDEX,l,position);
return NULL;
}
if (l->Flags & CONTAINER_READONLY) {
l->RaiseError("iStringList.GetElement",CONTAINER_ERROR_READONLY);
return NULL;
}
rvp = l->First;
while (position) {
rvp = rvp->Next;
position--;
}
return rvp->Data;
}
static CHARTYPE *Back(const LIST_TYPE(DATA_TYPE) *l)
{
if (l == NULL) {
NullPtrError("Back");
return NULL;
}
if (0 == l->count) {
return NULL;
}
if (l->Flags & CONTAINER_READONLY) {
l->RaiseError("iStringList.Back",CONTAINER_ERROR_READONLY);
return NULL;
}
return l->Last->Data;
}
static CHARTYPE *Front(const LIST_TYPE(DATA_TYPE) *l)
{
if (l == NULL) {
NullPtrError("Front");
return NULL;
}
if (0 == l->count) {
return NULL;
}
if (l->Flags & CONTAINER_READONLY) {
l->RaiseError("iStringList.Front",CONTAINER_ERROR_READONLY);
return NULL;
}
return l->First->Data;
}
/*------------------------------------------------------------------------
Procedure: CopyElement ID:1
Purpose: Returns the data associated with a given position
Input: The list and the position
Output: A pointer to the data
Errors: NULL if error in the positgion index
------------------------------------------------------------------------*/
static int CopyElement(LIST_TYPE(DATA_TYPE) *l,size_t position,CHARTYPE *outBuffer)
{
LIST_ELEMENT(DATA_TYPE) *rvp;
if (l == NULL || outBuffer == NULL) {
if (l)
l->RaiseError("iStringList.CopyElement",CONTAINER_ERROR_BADARG);
else
NullPtrError("CopyElement");
return CONTAINER_ERROR_BADARG;
}
if (position >= l->count) {
l->RaiseError("iStringList.CopyElement",CONTAINER_ERROR_INDEX,l,position);
return CONTAINER_ERROR_INDEX;
}
rvp = l->First;
while (position) {
rvp = rvp->Next;
position--;
}
STRCPY(outBuffer,rvp->Data);
return 1;
}
static int ReplaceAt(LIST_TYPE(DATA_TYPE) *l,size_t position,CHARTYPE *data)
{
LIST_ELEMENT(DATA_TYPE) *rvp;
/* Error checking */
if (l == NULL || data == NULL) {
if (l)
l->RaiseError("iStringList.ReplaceAt",CONTAINER_ERROR_BADARG);
else
NullPtrError("ReplaceAt");
return CONTAINER_ERROR_BADARG;
}
if (position >= l->count ) {
l->RaiseError("iStringList.ReplaceAt",CONTAINER_ERROR_INDEX,l,position);
return CONTAINER_ERROR_INDEX;
}
if (l->Flags & CONTAINER_READONLY) {
l->RaiseError("iStringList.ReplaceAt",CONTAINER_ERROR_READONLY);
return CONTAINER_ERROR_READONLY;
}
/* Position at the right data item */
if (position == l->count-1)
rvp = l->Last;
else {
rvp = l->First;
while (position) {
rvp = rvp->Next;
position--;
}
}
if (l->DestructorFn)
l->DestructorFn(&rvp->Data);
/* Replace the data there */
STRCPY(rvp->Data , data);
l->timestamp++;
return 1;
}
/*------------------------------------------------------------------------
Procedure: GetRange ID:1
Purpose: Gets a consecutive sub sequence of the list within
two indexes.
Input: The list, the subsequence start and end
Output: A new freshly allocated list. If the source list is
empty or the range is empty, the result list is
empty too.
Errors: None
------------------------------------------------------------------------*/
static LIST_TYPE(DATA_TYPE) *GetRange(LIST_TYPE(DATA_TYPE) *l,size_t start,size_t end)
{
size_t counter;
LIST_TYPE(DATA_TYPE) *result;
LIST_ELEMENT(DATA_TYPE) *rvp;;
if (l == NULL) {
NullPtrError("GetRange");
return NULL;
}
result = Create();
if (result == NULL) return NULL;
result->VTable = l->VTable;
if (l->count == 0)
return result;
if (end >= l->count)
end = l->count;
if (start >= end || start > l->count)
return result;
if (start == l->count-1)
rvp = l->Last;
else {
rvp = l->First;
counter = 0;
while (counter < start) {
rvp = rvp->Next;
counter++;
}
}
while (start < end && rvp != NULL) {
int r = Add_nd(result,rvp->Data);
if (r < 0) {
Finalize(result);
result = NULL;
break;
}
rvp = rvp->Next;
start++;
}
return result;
}
/*------------------------------------------------------------------------
Procedure: Equal ID:1
Purpose: Compares the data of two lists. They must be of the
same length, the elements must have the same size
and the comparison functions must be the same. If
all this is true, the comparisons are done.
Input: The two lists to be compared
Output: Returns 1 if the two lists are equal, zero otherwise
Errors: None
------------------------------------------------------------------------*/
static int Equal(LIST_TYPE(DATA_TYPE) *l1,LIST_TYPE(DATA_TYPE) *l2)
{
LIST_ELEMENT(DATA_TYPE) *link1,*link2;
CompareFunction fn;
CompareInfo ci;
if (l1 == l2)
return 1;
if (l1 == NULL || l2 == NULL)
return 0;
if (l1->count != l2->count)
return 0;
if (l1->Compare != l2->Compare)
return 0;
if (l1->count == 0)
return 1;
fn = l1->Compare;
link1 = l1->First;
link2 = l2->First;
ci.ContainerLeft = l1;
ci.ContainerRight = l2;
ci.ExtraArgs = NULL;
while (link1 && link2) {
if (fn(link1->Data,link2->Data,&ci))
return 0;
link1 = link1->Next;
link2 = link2->Next;
}
if (link1 || link2)
return 0;
return 1;
}
/*------------------------------------------------------------------------
Procedure: PushFront ID:1
Purpose: Inserts an element at the start of the list
Input: The list and the data to insert
Output: The count of the list after insertion, or CONTAINER_ERROR_READONLY
if the list is not writable or CONTAINER_ERROR_NOMEMORY if there is
no more memory left.
Errors: None
------------------------------------------------------------------------*/
static int PushFront(LIST_TYPE(DATA_TYPE) *l,CHARTYPE *pdata)
{
LIST_ELEMENT(DATA_TYPE) *rvp;
if (l == NULL || pdata == NULL) {
if (l)
l->RaiseError("iStringList.PushFront",CONTAINER_ERROR_BADARG);
else
NullPtrError("PushFront");
return CONTAINER_ERROR_BADARG;
}
if (l->Flags & CONTAINER_READONLY) {
return ErrorReadOnly(l,"PushFront");
}
rvp = NewLink(l,pdata,"Insert");
if (rvp == NULL)
return CONTAINER_ERROR_NOMEMORY;
rvp->Next = l->First;
l->First = rvp;
if (l->Last == NULL)
l->Last = rvp;
l->count++;
l->timestamp++;
if (l->Flags & CONTAINER_HAS_OBSERVER)
iObserver.Notify(l,CCL_PUSH,pdata,NULL);
return 1;
}
/*------------------------------------------------------------------------
Procedure: Pop ID:1
Purpose: Takes out the first element of a list.
Input: The list
Output: The first element or NULL if there is none or the
list is read only
Errors: None
------------------------------------------------------------------------*/
static int PopFront(LIST_TYPE(DATA_TYPE) *l,CHARTYPE *result)
{
LIST_ELEMENT(DATA_TYPE) *le;
if (l == NULL) {
iError.RaiseError("iStringList.PopFront",CONTAINER_ERROR_BADARG);
return CONTAINER_ERROR_BADARG;
}
if (l->Flags & CONTAINER_READONLY) {
return ErrorReadOnly(l,"PopFront");
}
if (l->count == 0)
return 0;
le = l->First;
if (l->count == 1) {
l->First = l->Last = NULL;
}
else l->First = l->First->Next;
l->count--;
if (result)
STRCPY(result,le->Data);
l->Allocator->free(le);
l->timestamp++;
if (l->Flags & CONTAINER_HAS_OBSERVER)
iObserver.Notify(l,CCL_POP,result,NULL);
return 1;
}
static int InsertIn(LIST_TYPE(DATA_TYPE) *l, size_t idx,LIST_TYPE(DATA_TYPE) *newData)
{
size_t newCount;
LIST_ELEMENT(DATA_TYPE) *le,*nle;
if (l == NULL || newData == NULL) {
if (l)
l->RaiseError("iStringList.InsertIn",CONTAINER_ERROR_BADARG);
else
NullPtrError("InsertIn");
return CONTAINER_ERROR_BADARG;
}
if (l->Flags & CONTAINER_READONLY) {
return ErrorReadOnly(l,"InsertIn");
}
if (idx > l->count) {
l->RaiseError("iStringList.InsertIn",CONTAINER_ERROR_INDEX,l,idx);
return CONTAINER_ERROR_INDEX;
}
if (newData->count == 0)
return 1;
newData = Copy(newData);
if (newData == NULL) {
l->RaiseError("iStringList.InsertIn",CONTAINER_ERROR_NOMEMORY);
return CONTAINER_ERROR_NOMEMORY;
}
newCount = l->count + newData->count;
if (l->count == 0) {
l->First = newData->First;
l->Last = newData->Last;
}
else {
le = l->First;
while ( idx > 1) {
le = le->Next;
idx--;
}
nle = le->Next;
le->Next = newData->First;
newData->Last->Next = nle;
}
newData->Allocator->free(newData);
l->timestamp++;
l->count = newCount;
if (l->Flags & CONTAINER_HAS_OBSERVER)
iObserver.Notify(l,CCL_INSERT_IN,newData,NULL);
return 1;
}
static int InsertAt(LIST_TYPE(DATA_TYPE) *l,size_t pos,CHARTYPE *pdata)
{
LIST_ELEMENT(DATA_TYPE) *elem;
if (l == NULL || pdata == NULL) {
if (l)
l->RaiseError("iStringList.InsertAt",CONTAINER_ERROR_BADARG);
else
NullPtrError("InsertAt");
return CONTAINER_ERROR_BADARG;
}
if (pos > l->count) {
l->RaiseError("iStringList.InsertAt",CONTAINER_ERROR_INDEX,l,pos);
return CONTAINER_ERROR_INDEX;
}
if (l->Flags & CONTAINER_READONLY) {
return ErrorReadOnly(l,"InsertAt");
}
if (pos == l->count) {
return Add_nd(l,pdata);
}
elem = NewLink(l,pdata,"iStringList.InsertAt");
if (elem == NULL) {
l->RaiseError("iStringList.InsertAt",CONTAINER_ERROR_NOMEMORY);
return CONTAINER_ERROR_NOMEMORY;
}
if (pos == 0) {
elem->Next = l->First;
l->First = elem;
}
else {
LIST_ELEMENT(DATA_TYPE) *rvp = l->First;
while (--pos > 0) {
rvp = rvp->Next;
}
elem->Next = rvp->Next;
rvp->Next = elem;
}
l->count++;
l->timestamp++;
if (l->Flags & CONTAINER_HAS_OBSERVER)
iObserver.Notify(l,CCL_INSERT_AT,pdata,(void *)pos);
return 1;
}
static int Erase(LIST_TYPE(DATA_TYPE) *l, CHARTYPE *elem)
{
size_t position;
LIST_ELEMENT(DATA_TYPE) *rvp,*previous;
int r;
CompareFunction fn;
CompareInfo ci;
if (l == NULL) {
return NullPtrError("Erase");
}
if (elem == NULL) {
l->RaiseError("iList.Erase",CONTAINER_ERROR_BADARG);
return CONTAINER_ERROR_BADARG;
}
if (l->Flags & CONTAINER_READONLY)
return ErrorReadOnly(l,"Erase");
if (l->count == 0) {
return CONTAINER_ERROR_NOTFOUND;
}
position = 0;
rvp = l->First;
previous = NULL;
fn = l->Compare;
ci.ContainerLeft = l;
ci.ContainerRight = NULL;
ci.ExtraArgs = NULL;
while (rvp) {
r = fn(&rvp->Data,elem,&ci);
if (r == 0) {
if (l->Flags & CONTAINER_HAS_OBSERVER)
iObserver.Notify(l,CCL_ERASE_AT,rvp,(void *)position);
if (position == 0) {
if (l->count == 1) {
l->First = l->Last = NULL;
}
else {
l->First = l->First->Next;
}
}
else if (position == l->count - 1) {
if (previous) previous->Next = NULL;
l->Last = previous;
}
else {
if (previous) previous->Next = rvp->Next;
}
if (l->DestructorFn)
l->DestructorFn(&rvp->Data);
if (l->Heap)
iHeap.FreeObject(l->Heap,rvp);
else {
l->Allocator->free(rvp);
}
l->count--;
l->timestamp++;
return 1;
}
previous = rvp;
rvp = rvp->Next;
position++;
}
return CONTAINER_ERROR_NOTFOUND;
}
static int EraseRange(LIST_TYPE(DATA_TYPE) *l,size_t start,size_t end)
{
LIST_ELEMENT(DATA_TYPE) *rvp,*start_pos,*tmp;
size_t toremove;
if (l == NULL) {
return NullPtrError("EraseRange");
}
if (end > l->count)
end = l->count;
if (start >= l->count)
return 0;
if (start >= end)
return 0;
toremove = end - start+1;
rvp = l->First;
while (start > 1) {
rvp = rvp->Next;
start--;
}
start_pos = rvp;
rvp = rvp->Next;
if (rvp == NULL) {
iError.RaiseError("iStringList.EraseRange",CONTAINER_ASSERTION_FAILED);
return CONTAINER_ASSERTION_FAILED;
}
while (toremove > 1) {
tmp = rvp->Next;
if (l->DestructorFn)
l->DestructorFn(&rvp->Data);
l->Allocator->free(rvp);
rvp = tmp;
toremove--;
l->count--;
}
start_pos->Next = rvp;
return 1;
}
static int RemoveAt_nd(LIST_TYPE(DATA_TYPE) *l,size_t position)
{
LIST_ELEMENT(DATA_TYPE) *rvp,*last,*removed;
rvp = l->First;
if (position == 0) {
removed = l->First;
if (l->count == 1) {
l->First = l->Last = NULL;
}
else {
l->First = l->First->Next;
}
}
else if (position == l->count - 1) {
while (rvp->Next != l->Last)
rvp = rvp->Next;
removed = rvp->Next;
rvp->Next = NULL;
l->Last = rvp;
}
else {
last = rvp;
while (position > 0) {
last = rvp;
rvp = rvp->Next;
position --;
}
removed = rvp;
last->Next = rvp->Next;
}
if (l->Flags & CONTAINER_HAS_OBSERVER)
iObserver.Notify(l,CCL_ERASE_AT,removed,(void *)position);
if (l->DestructorFn)
l->DestructorFn(&removed->Data);
l->Allocator->free(removed);
l->timestamp++;
--l->count;
return 1;
}
static int RemoveAt(LIST_TYPE(DATA_TYPE) *l,size_t position)
{
if (l == NULL) {
return NullPtrError("RemoveAt");
}
if (position >= l->count) {
l->RaiseError("iStringListRemoveAt",CONTAINER_ERROR_INDEX,l,position);
return CONTAINER_ERROR_INDEX;
}
if (l->Flags & CONTAINER_READONLY) {
return ErrorReadOnly(l,"RemoveAt");
}
return RemoveAt_nd(l,position);
}
static int Append(LIST_TYPE(DATA_TYPE) *l1,LIST_TYPE(DATA_TYPE) *l2)
{
if (l1 == NULL || l2 == NULL) {
if (l1)
l1->RaiseError("iStringList.Append",CONTAINER_ERROR_BADARG);
else
iError.RaiseError("iStringList.Append",CONTAINER_ERROR_BADARG);
return CONTAINER_ERROR_BADARG;
}
if ((l1->Flags & CONTAINER_READONLY) || (l2->Flags & CONTAINER_READONLY)) {
l1->RaiseError("iStringList.Append",CONTAINER_ERROR_READONLY);
return CONTAINER_ERROR_READONLY;
}
if (l1->Flags & CONTAINER_HAS_OBSERVER)
iObserver.Notify(l1,CCL_APPEND,l2,NULL);
if (l2->Flags & CONTAINER_HAS_OBSERVER)
iObserver.Notify(l2,CCL_FINALIZE,NULL,NULL);
if (l1->count == 0) {
l1->First = l2->First;
l1->Last = l2->Last;
}
else if (l2->count > 0) {
if (l2->First)
l1->Last->Next = l2->First;
if (l2->Last)
l1->Last = l2->Last;
}
l1->count += l2->count;
l1->timestamp++;
l2->Allocator->free(l2);
return 1;
}
static int Reverse(LIST_TYPE(DATA_TYPE) *l)
{
LIST_ELEMENT(DATA_TYPE) *New,*current,*old;
if (l == NULL) {
iError.RaiseError("Reverse",CONTAINER_ERROR_BADARG);
return CONTAINER_ERROR_BADARG;
}
if (l->Flags & CONTAINER_READONLY) {
l->RaiseError("iStringList.Reverse",CONTAINER_ERROR_READONLY);
return CONTAINER_ERROR_READONLY;
}
if (l->count < 2)
return 1;
old = l->First;
l->Last = l->First;
New = NULL;
while (old) {
current = old;
old = old->Next;
current->Next = New;
New = current;
}
l->First = New;
if (l->Last)
l->Last->Next = NULL;
l->timestamp++;
return 1;
}
static int AddRange(LIST_TYPE(DATA_TYPE) * AL,size_t n, CHARTYPE **data)
{
CHARTYPE **p;
LIST_ELEMENT(DATA_TYPE) *oldLast;
if (AL == NULL) return NullPtrError("AddRange");
if (AL->Flags & CONTAINER_READONLY) {
AL->RaiseError("iStringList.AddRange",CONTAINER_ERROR_READONLY);
return CONTAINER_ERROR_READONLY;
}