-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.c
2042 lines (1890 loc) · 52.6 KB
/
vector.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 "containers.h"
#include "ccl_internal.h"
/* Number of elements by default */
#ifndef DEFAULT_START_SIZE
#define DEFAULT_START_SIZE 20
#endif
static const guid VectorGuid = {0xba53f11e, 0x5879, 0x49e5,
{0x9e,0x3a,0xea,0x7d,0xd8,0xcb,0xd9,0xd6}
};
static int NullPtrError(const char *fnName)
{
char buf[512];
snprintf(buf,sizeof(buf),"iVector.%s",fnName);
return iError.NullPtrError(buf);
}
static int doerror(const Vector *AL,const char *fnName,int code)
{
char buf[512];
(void)snprintf(buf,sizeof(buf),"iVector.%s",fnName);
AL->RaiseError(buf,code,AL);
return code;
}
static int ErrorReadOnly(const Vector *AL,const char *fnName)
{
return doerror(AL,fnName,CONTAINER_ERROR_READONLY);
}
static int ErrorIncompatible(const Vector *AL,const char *fnName)
{
return doerror(AL,fnName,CONTAINER_ERROR_INCOMPATIBLE);
}
static int NoMemory(const Vector *AL,const char *fnName)
{
return doerror(AL,fnName,CONTAINER_ERROR_NOMEMORY);
}
static Vector *Create(size_t elementsize,size_t startsize);
static void *DuplicateElement(const Vector *AL,void *str,size_t size,const char *functionName)
{
void *result;
if (size == 0)
return str;
if (str == NULL) {
NullPtrError((char *)functionName);
return NULL;
}
result = AL->Allocator->malloc(size);
if (result == NULL) {
NoMemory(AL,(char *)functionName);
}
else memcpy(result,str,size);
return result;
}
#define CHUNKSIZE 20
static int DefaultVectorCompareFunction(const void *left,const void *right,CompareInfo *ExtraArgs)
{
size_t siz=((Vector *)ExtraArgs->ContainerLeft)->ElementSize;
return memcmp(left,right,siz);
}
static size_t Size(const Vector *AL)
{
if (AL == NULL) {
NullPtrError("Size");
return 0;
}
return AL->count;
}
static unsigned GetFlags(const Vector *AL)
{
if (AL == NULL) {
NullPtrError("GetFlags");
return 0;
}
return AL->Flags;
}
static unsigned SetFlags(Vector *AL,unsigned newval)
{
unsigned oldval;
if (AL == NULL) {
NullPtrError("SetFlags");
return 0;
}
oldval = AL->Flags;
AL->Flags = newval;
return oldval;
}
static int grow(Vector *AL)
{
size_t newcapacity;
void **oldcontents;
int r = 1;
newcapacity = AL->capacity + 1+AL->capacity/4;
oldcontents = (void **)AL->contents;
AL->contents = AL->Allocator->realloc(AL->contents,newcapacity*AL->ElementSize);
if (AL->contents == NULL) {
NoMemory(AL,"Resize");
AL->contents = oldcontents;
r = CONTAINER_ERROR_NOMEMORY;
}
else {
AL->capacity = newcapacity;
AL->timestamp++;
}
return r;
}
static int ResizeTo(Vector *AL,size_t newcapacity)
{
void *oldcontents;
if (AL == NULL) {
return NullPtrError("ResizeTo");
}
if (AL->Flags & CONTAINER_READONLY)
return ErrorReadOnly(AL,"ResizeTo");
if (AL->capacity >= newcapacity)
return 0;
if (newcapacity <= AL->count)
return 0;
oldcontents = AL->contents;
AL->contents = AL->Allocator->realloc(AL->contents,newcapacity*AL->ElementSize);
if (AL->contents == NULL) {
AL->contents = oldcontents;
return NoMemory(AL,"ResizeTo");
}
AL->capacity = newcapacity;
AL->timestamp++;
return 1;
}
static int Resize(Vector *AL, size_t newSize)
{
char *p;
size_t i;
if (AL == NULL) return iError.NullPtrError("iVector.Resize");
if (AL->count < newSize) return ResizeTo(AL,newSize);
p = (char *)AL->contents;
if (AL->DestructorFn) {
for (i=newSize; i<AL->count; i++) {
AL->DestructorFn(p + i*AL->ElementSize);
}
}
p = (char *)AL->Allocator->realloc(AL->contents,newSize*AL->ElementSize);
if (p == NULL) {
iError.RaiseError("iVector.Resize",CONTAINER_ERROR_NOMEMORY);
return CONTAINER_ERROR_NOMEMORY;
}
AL->count = newSize;
AL->capacity = newSize;
AL->contents = (newSize ? p : NULL);
return 1;
}
/*------------------------------------------------------------------------
Procedure: Add ID:1
Purpose: Adds an item at the end of the Vector
Input: The Vector and the item to be added
Output: The number of items in the Vector or negative if
error
Errors:
------------------------------------------------------------------------*/
static int Add(Vector *AL,const void *newval)
{
char *p;
if (AL == NULL) {
return NullPtrError("Add");
}
if (AL->Flags & CONTAINER_READONLY) {
return ErrorReadOnly(AL,"Add");
}
if (newval == NULL) {
AL->RaiseError("iVector.Add",CONTAINER_ERROR_BADARG);
return CONTAINER_ERROR_BADARG;
}
if (AL->count >= AL->capacity) {
int r = grow(AL);
if (r <= 0)
return r;
}
p = (char *)AL->contents;
p += AL->count*AL->ElementSize;
memcpy(p,newval,AL->ElementSize);
AL->timestamp++;
if (AL->Flags & CONTAINER_HAS_OBSERVER)
iObserver.Notify(AL,CCL_ADD,newval,NULL);
++AL->count;
return 1;
}
/*------------------------------------------------------------------------
Procedure: AddRange ID:1
Purpose: Adds a range of data at the end of an existing
arrraylist.
Input: The Vector and the data to be added
Output: One (OK) or negative error code
Errors: Vector must be writable and data must be
different of NULL
------------------------------------------------------------------------*/
static int AddRange(Vector * AL,size_t n,const void *data)
{
unsigned char *p;
size_t newcapacity;
if (n == 0)
return 1;
if (AL == NULL) {
return NullPtrError("AddRange");
}
if (AL->Flags & CONTAINER_READONLY) {
return ErrorReadOnly(AL,"AddRange");
}
if (data == NULL) {
AL->RaiseError("iVector.AddRange",CONTAINER_ERROR_BADARG);
return CONTAINER_ERROR_BADARG;
}
newcapacity = AL->count+n;
if (newcapacity >= AL->capacity-1) {
unsigned char *newcontents;
newcapacity += AL->count/4;
newcontents = (unsigned char *)AL->Allocator->realloc(AL->contents,newcapacity*AL->ElementSize);
if (newcontents == NULL) {
return NoMemory(AL,"AddRange");
}
AL->capacity = newcapacity;
AL->contents = newcontents;
}
p = (unsigned char *)AL->contents;
p += AL->count*AL->ElementSize;
memcpy(p,data,n*AL->ElementSize);
AL->count += n;
AL->timestamp++;
if (AL->Flags & CONTAINER_HAS_OBSERVER)
iObserver.Notify(AL,CCL_ADDRANGE,(void *)n,data);
return 1;
}
static Vector *GetRange(const Vector *AL, size_t start,size_t end)
{
Vector *result=NULL;
char *p;
size_t top;
if (AL == NULL) {
iError.RaiseError("iVector.GetRange",CONTAINER_ERROR_BADARG);
return result;
}
if (AL->count == 0)
return result;
if (end >= AL->count)
end = AL->count-1;
if (start > end)
return result;
top = end-start;
result = AL->VTable->Create(AL->ElementSize,top);
if (result == NULL) {
NoMemory(AL,"GetRange");
return NULL;
}
p = (char *)AL->contents;
memcpy(result->contents,p+start*AL->ElementSize,(top)*AL->ElementSize);
result->count = end-start;
return result;
}
/*------------------------------------------------------------------------
Procedure: Clear ID:1
Purpose: Frees all memory from an array list object without
freeing the object itself
Input: The array list to be cleared
Output: The number of objects that were in the array list
Errors: The array list must be writable
------------------------------------------------------------------------*/
static int Clear(Vector *AL)
{
if (AL == NULL) {
return NullPtrError("Clear");
}
if (AL->Flags & CONTAINER_READONLY) {
return ErrorReadOnly(AL,"Clear");
}
if (AL->Flags & CONTAINER_HAS_OBSERVER)
iObserver.Notify(AL,CCL_CLEAR,NULL,NULL);
if (AL->DestructorFn) {
size_t i;
unsigned char *p = (unsigned char *)AL->contents;
for(i=0; i<AL->count;i++) {
AL->DestructorFn(p);
p += AL->ElementSize;
}
}
AL->count = 0;
AL->timestamp = 0;
AL->Flags = 0;
return 1;
}
static int Contains(const Vector *AL,const void *data,void *ExtraArgs)
{
size_t i;
char *p;
CompareInfo ci;
if (AL == NULL) {
return NullPtrError("Contains");
}
if (data == NULL) {
AL->RaiseError("iVector.Contains",CONTAINER_ERROR_BADARG);
return CONTAINER_ERROR_BADARG;
}
p = (char *)AL->contents;
if (ExtraArgs == NULL) {
ExtraArgs = &ci;
ci.ContainerLeft = AL;
ci.ContainerRight = NULL;
ci.ExtraArgs = ExtraArgs;
}
for (i = 0; i<AL->count;i++) {
if (!AL->CompareFn(p,data,&ci))
return 1;
p += AL->ElementSize;
}
return 0;
}
static int Equal(const Vector *AL1,const Vector *AL2)
{
size_t i;
unsigned char *left,*right;
if (AL1 == AL2)
return 1;
if (AL1 == NULL || AL2 == NULL)
return 0;
if (AL1->count != AL2->count)
return 0;
if (AL1->Flags != AL2->Flags)
return 0;
if (AL1->ElementSize != AL2->ElementSize)
return 0;
if (AL1->count == 0)
return 1;
if (AL1->CompareFn != AL2->CompareFn)
return 0;
if (AL1->CompareFn == DefaultVectorCompareFunction) {
if (memcmp(AL1->contents,AL2->contents,AL1->ElementSize*AL1->count) != 0)
return 0;
}
left = (unsigned char *)AL1->contents;
right = (unsigned char *)AL2->contents;
for (i=0; i<AL1->count;i++) {
if (AL1->CompareFn(left,right,NULL) != 0)
return 0;
left += AL1->ElementSize;
right += AL2->ElementSize;
}
return 1;
}
static Vector *Copy(const Vector *AL)
{
Vector *result;
size_t startsize,es;
if (AL == NULL) {
NullPtrError("Copy");
return NULL;
}
result = AL->Allocator->malloc(sizeof(*result));
if (result == NULL) {
NoMemory(AL,"Copy");
return NULL;
}
memset(result,0,sizeof(*result));
startsize = AL->count;
if (startsize == 0)
startsize = DEFAULT_START_SIZE;
es = startsize * AL->ElementSize;
result->contents = AL->Allocator->malloc(es);
if (result->contents == NULL) {
NoMemory(AL,"Copy");
AL->Allocator->free(result);
return NULL;
}
else {
memset(result->contents,0,es);
result->capacity = startsize;
result->VTable = &iVector;
result->ElementSize = AL->ElementSize;
result->CompareFn = DefaultVectorCompareFunction;
result->RaiseError = iError.RaiseError;
result->Allocator = AL->Allocator;
}
memcpy(result->contents,AL->contents,AL->count*AL->ElementSize);
result->CompareFn = AL->CompareFn;
result->Flags = AL->Flags;
result->RaiseError = AL->RaiseError;
result->VTable = AL->VTable;
result->count = AL->count;
if (AL->Flags & CONTAINER_HAS_OBSERVER)
iObserver.Notify(AL,CCL_COPY,result,NULL);
return result;
}
static int CopyElement(const Vector *AL,size_t idx, void *outbuf)
{
char *p;
if (AL == NULL || outbuf == NULL) {
if (AL)
AL->RaiseError("iVector.CopyElement",CONTAINER_ERROR_BADARG);
else
NullPtrError("CopyElement");
return CONTAINER_ERROR_BADARG;
}
if (idx >= AL->count) {
void *err=AL->RaiseError("iVector.CopyElement",CONTAINER_ERROR_INDEX,AL,idx);
if (err) {
/* User overrides the error furnishing a pointer to some */
/* data. This allows implementing infinite arrays */
memcpy(outbuf,err,AL->ElementSize);
return 1;
}
return CONTAINER_ERROR_INDEX;
}
p = (char *)AL->contents;
p += (idx)*AL->ElementSize;
memcpy(outbuf,p,AL->ElementSize);
return 1;
}
static void **CopyTo(const Vector *AL)
{
void **result;
size_t i;
char *p;
if (AL == NULL) {
NullPtrError("CopyTo");
return NULL;
}
result = AL->Allocator->malloc((1+AL->count)*sizeof(void *));
if (result == NULL) {
NoMemory(AL,"CopyTo");
return NULL;
}
p = AL->contents;
for (i=0; i<AL->count;i++) {
result[i] = DuplicateElement(AL,p,AL->ElementSize,"iVector.CopyTo");
if (result[i] == NULL) {
while (i > 0) {
i--;
AL->Allocator->free(result[i]);
}
AL->Allocator->free(result);
return NULL;
}
p += AL->ElementSize;
}
result[i] = NULL;
return result;
}
static int IndexOf(const Vector *AL,const void *data,void *ExtraArgs,size_t *result)
{
size_t i;
char *p;
CompareInfo ci;
if (AL == NULL) {
return NullPtrError("IndexOf");
}
if (data == NULL) {
AL->RaiseError("iVector.IndexOf",CONTAINER_ERROR_BADARG);
return CONTAINER_ERROR_BADARG;
}
p = AL->contents;
ci.ContainerLeft = (Vector *)AL;
ci.ContainerRight = NULL;
ci.ExtraArgs = ExtraArgs;
for (i=0; i<AL->count;i++) {
if (!AL->CompareFn(p,data,&ci)) {
*result = i;
return 1;
}
p += AL->ElementSize;
}
return CONTAINER_ERROR_NOTFOUND;
}
static void *GetElement(const Vector *AL,size_t idx)
{
char *p;
if (AL == NULL) {
NullPtrError("GetElement");
return NULL;
}
if (AL->Flags&CONTAINER_READONLY) {
ErrorReadOnly(AL,"GetElement");
return NULL;
}
if (idx >=AL->count ) {
return AL->RaiseError("iVector.GetElement",CONTAINER_ERROR_INDEX,AL,idx);
}
p = AL->contents;
p += idx*AL->ElementSize;
return p;
}
/*------------------------------------------------------------------------
Procedure: InsertAt ID:1
Purpose: Inserts data at the given position, that should be
between zero and the number of items in the array
list. If the index is equal to the count of items in
the array the item will be inserted at the end. Any
insertion in the middle or at the beginning provokes
a move of all items between the index and the end of
the array list
Input: The array list, the index and the new data to be
inserted
Output: Error code less than zero if error, or positive
number bigger than zero otherwise
Errors: The index must be correct and the array list must be
writable
------------------------------------------------------------------------*/
static int InsertAt(Vector *AL,size_t idx,void *newval)
{
char *p;
if (AL == NULL) {
return NullPtrError("InsertAt");
}
if (newval == NULL) {
AL->RaiseError("iVector.InsertAt",CONTAINER_ERROR_BADARG);
return CONTAINER_ERROR_BADARG;
}
if (AL->Flags & CONTAINER_READONLY) {
return ErrorReadOnly(AL,"InsertAt");
}
if (idx > AL->count) {
p = AL->RaiseError("iVector.InsertAt",CONTAINER_ERROR_INDEX,AL,idx);
if (p) {
int r = ResizeTo(AL,idx+1);
if (r < 0) return r;
}
else return CONTAINER_ERROR_INDEX;
}
if (AL->count >= (AL->capacity-1)) {
int r = grow(AL);
if (r <= 0)
return r;
}
p = (char *)AL->contents;
if (idx < AL->count) {
memmove(p+AL->ElementSize*(idx+1),
p+AL->ElementSize*idx,
(AL->count-idx+1)*AL->ElementSize);
}
p += idx*AL->ElementSize;
memcpy(p,newval,AL->ElementSize);
AL->timestamp++;
++AL->count;
if (AL->Flags & CONTAINER_HAS_OBSERVER)
iObserver.Notify(AL,CCL_INSERT,newval,(void *)idx);
return 1;
}
static int Insert(Vector *AL,void *newval)
{
return InsertAt(AL,0,newval);
}
static int InsertIn(Vector *AL, size_t idx, Vector *newData)
{
size_t newCount;
char *p;
if (AL == NULL) {
return NullPtrError("InsertIn");
}
if (newData == NULL) {
AL->RaiseError("InsertIn",CONTAINER_ERROR_BADARG);
return CONTAINER_ERROR_BADARG;
}
if (AL->Flags & CONTAINER_READONLY)
return ErrorReadOnly(AL,"InsertIn");
if (idx > AL->count) {
AL->RaiseError("iVector.InsertIn",CONTAINER_ERROR_INDEX,AL,idx);
return CONTAINER_ERROR_INDEX;
}
if (AL->ElementSize != newData->ElementSize) {
return ErrorIncompatible(AL,"InsertIn");
}
newCount = AL->count + newData->count;
if (newCount >= (AL->capacity-1)) {
int r = ResizeTo(AL,newCount);
if (r <= 0)
return r;
}
p = (char *)AL->contents;
if (idx < AL->count) {
memmove(p+AL->ElementSize*(idx+newData->count),
p+AL->ElementSize*idx,
(AL->count-idx)*AL->ElementSize);
}
p += idx*AL->ElementSize;
memcpy(p,newData->contents,newData->ElementSize*newData->count);
AL->timestamp++;
AL->count = newCount;
if (AL->Flags & CONTAINER_HAS_OBSERVER)
iObserver.Notify(AL,CCL_INSERT_IN,newData,NULL);
return 1;
}
static int EraseAt(Vector *AL,size_t idx)
{
char *p;
if (AL == NULL) {
return NullPtrError("EraseAt");
}
if (idx >= AL->count) {
AL->RaiseError("iVector.Erase",CONTAINER_ERROR_INDEX,AL,idx);
return CONTAINER_ERROR_INDEX;
}
if (AL->Flags & CONTAINER_READONLY) {
return ErrorReadOnly(AL,"Erase");
}
p = (char *)AL->contents;
p += AL->ElementSize * idx;
if (AL->Flags & CONTAINER_HAS_OBSERVER)
iObserver.Notify(AL,CCL_ERASE_AT,p,NULL);
if (AL->DestructorFn) {
AL->DestructorFn(p);
}
if (idx < (AL->count-1)) {
// Fix by zhaozg@gmail.com
memmove(p,p+AL->ElementSize,(AL->count-idx-1)*AL->ElementSize);
}
AL->count--;
AL->timestamp++;
return 1;
}
static int RemoveRange(Vector *AL,size_t start, size_t end)
{
size_t i;
char *p;
if (AL == NULL)
return NullPtrError("RemoveRange");
if (AL->count == 0)
return 0;
if (end > AL->count)
end = AL->count;
if (start == end) return 0;
if (start >= AL->count) {
return 0;
}
if (AL->Flags & CONTAINER_READONLY) {
return ErrorReadOnly(AL,"Erase");
}
p = AL->contents;
if (AL->DestructorFn) {
for (i=start; i<end; i++) {
AL->DestructorFn(p);
AL->Allocator->free(p);
p += AL->ElementSize;
}
}
else {
for (i=start; i<end; i++) {
AL->Allocator->free(p);
p += AL->ElementSize;
}
}
if (end < AL->count)
memmove(p+start, p+end, (AL->count-end)*AL->ElementSize);
AL->count -= end - start;
AL->timestamp++;
return 1;
}
static int EraseInternal(Vector *AL,const void *elem,int all)
{
size_t i;
char *p;
CompareInfo ci;
int result = CONTAINER_ERROR_NOTFOUND;
if (AL == NULL) {
return NullPtrError("Erase");
}
if (elem == NULL) {
AL->RaiseError("iVector.Erase",CONTAINER_ERROR_BADARG);
return CONTAINER_ERROR_BADARG;
}
if (AL->Flags & CONTAINER_READONLY)
return ErrorReadOnly(AL,"Erase");
restart:
p = AL->contents;
ci.ContainerLeft = AL;
ci.ContainerRight = NULL;
ci.ExtraArgs = NULL;
for (i=0; i<AL->count;i++) {
if (!AL->CompareFn(p,elem,&ci)) {
if (i > 0) {
p = GetElement(AL,--i);
result = EraseAt(AL,i+1);
} else {
result = EraseAt(AL,0);
if (result < 0 || all == 0) return result;
if (all) goto restart;
}
if (all == 0) return result;
result = 1;
}
p += AL->ElementSize;
}
return result;
}
static int Erase(Vector *AL, const void *elem)
{
return EraseInternal(AL,elem,0);
}
static int EraseAll(Vector *AL, const void *elem)
{
return EraseInternal(AL,elem,1);
}
static int PushBack(Vector *AL,const void *str)
{
if (AL == NULL) {
return NullPtrError("PushBack");
}
return InsertAt(AL,AL->count,(void *)str);
}
/*------------------------------------------------------------------------
Procedure: Pop ID:1
Purpose: Removes the last item of the list and returns its
data to the caller. It is the responsability of the
caller to free this memory. This is almost the same as
the "Remove" function, with the only difference that the
data is NOT freed but returned to the caller.
Input: The array list to be popped
Output: a pointer to the last item's data
Errors: If the list is read-only or empty returns NULL
------------------------------------------------------------------------*/
static int PopBack(Vector *AL,void *result)
{
char *p;
if (AL == NULL) {
return CONTAINER_ERROR_BADARG;
}
if (AL->Flags&CONTAINER_READONLY) {
return ErrorReadOnly(AL,"PopBack");
}
if (AL->count == 0)
return 0;
if (result) {
p = AL->contents;
p += AL->ElementSize*(AL->count-1);
memcpy(result,p,AL->ElementSize);
}
AL->count--;
AL->timestamp++;
return 1;
}
/*------------------------------------------------------------------------
Procedure: Finalize ID:1
Purpose: Releases all memory associated with an Vector,
including the Vector structure itself
Input: The Vector to be destroyed
Output: The number of items that the Vector contained or
zero if error
Errors: Input must be writable
------------------------------------------------------------------------*/
static int Finalize(Vector *AL)
{
unsigned Flags;
int result;
if (AL == NULL)
return CONTAINER_ERROR_BADARG;
Flags = AL->Flags;
result = Clear(AL);
if (result < 0)
return result;
if (Flags & CONTAINER_HAS_OBSERVER)
iObserver.Notify(AL,CCL_FINALIZE,NULL,NULL);
if (AL->VTable != &iVector)
AL->Allocator->free(AL->VTable);
AL->Allocator->free(AL->contents);
AL->Allocator->free(AL);
return result;
}
static const ContainerAllocator *GetAllocator(const Vector *AL)
{
if (AL == NULL) {
return NULL;
}
return AL->Allocator;
}
static size_t GetCapacity(const Vector *AL)
{
if (AL == NULL) {
NullPtrError("GetCapacity");
return 0;
}
return AL->capacity;
}
static int Mismatch(Vector *a1, Vector *a2,size_t *mismatch)
{
size_t siz,i;
CompareInfo ci;
char *p1,*p2;
*mismatch = 0;
if (a1 == a2)
return 0;
if (a1 == NULL || a2 == NULL || mismatch == NULL) {
return NullPtrError("Mismatch");
}
if (a1->CompareFn != a2->CompareFn || a1->ElementSize != a2->ElementSize)
return CONTAINER_ERROR_INCOMPATIBLE;
siz = a1->count;
if (siz > a2->count)
siz = a2->count;
if (siz == 0)
return 1;
p1 = a1->contents;
p2 = a2->contents;
ci.ContainerLeft = a1;
ci.ContainerRight = a2;
ci.ExtraArgs = NULL;
for (i=0;i<siz;i++) {
if (a1->CompareFn(p1,p2,&ci) != 0) {
*mismatch = i;
return 1;
}
p1 += a1->ElementSize;
p2 += a2->ElementSize;
}
*mismatch = i;
if (a1->count != a2->count)
return 1;
return 0;
}
static int SetCapacity(Vector *AL,size_t newCapacity)
{
void **newContents;
if (AL == NULL) {
return NullPtrError("SetCapacity");
}
if (AL->Flags & CONTAINER_READONLY) {
return ErrorReadOnly(AL,"SetCapacity");
}
newContents = AL->Allocator->malloc(newCapacity*AL->ElementSize);
if (newContents == NULL) {
return NoMemory(AL,"SetCapacity");
}
memset(AL->contents,0,AL->ElementSize*newCapacity);
AL->capacity = newCapacity;
if (newCapacity > AL->count)
newCapacity = AL->count;
else if (newCapacity < AL->count)
AL->count = newCapacity;
if (newCapacity > 0) {
memcpy(newContents,AL->contents,newCapacity*AL->ElementSize);
}
AL->Allocator->free(AL->contents);
AL->contents = newContents;
AL->timestamp++;
return 1;
}
static int Apply(Vector *AL,int (*Applyfn)(void *,void *),void *arg)
{
size_t i;
unsigned char *p,*pElem=NULL;
if (AL == NULL || Applyfn == NULL) {
return NullPtrError("Apply");
}
if (AL->Flags&CONTAINER_READONLY) {
pElem = AL->Allocator->malloc(AL->ElementSize);
if (pElem == NULL) {
return NoMemory(AL,"Apply");
}
}
p=AL->contents;
for (i=0; i<AL->count;i++) {
if (AL->Flags&CONTAINER_READONLY) {
memcpy(pElem,p,AL->ElementSize);
Applyfn(pElem,arg);
}
else Applyfn(p,arg);
p += AL->ElementSize;
}
if (pElem)
AL->Allocator->free(pElem);
return 1;
}
/*------------------------------------------------------------------------
Procedure: ReplaceAt ID:1
Purpose: Replace the data at the given position with new
data.
Input: The list and the new data
Output: Returns the new data if OK, NULL if error.
Errors: Index error or read only errors are caught
------------------------------------------------------------------------*/
static int ReplaceAt(Vector *AL,size_t idx,void *newval)
{
char *p;
if (AL == NULL || newval == NULL) {
if (AL)
AL->RaiseError("iVector.ReplaceAt",CONTAINER_ERROR_BADARG);
else
NullPtrError("ReplaceAt");
return CONTAINER_ERROR_BADARG;
}
if (AL->Flags & CONTAINER_READONLY) {
AL->RaiseError("iVector.ReplaceAt",CONTAINER_ERROR_READONLY);
return CONTAINER_ERROR_READONLY;
}
if (idx >= AL->count) {
AL->RaiseError("iVector.ReplaceAt",CONTAINER_ERROR_INDEX,AL,idx);
return CONTAINER_ERROR_INDEX;
}
p = AL->contents;
p += idx*AL->ElementSize;
if (AL->Flags & CONTAINER_HAS_OBSERVER) {
iObserver.Notify(AL,CCL_REPLACEAT,p,newval);
}
if (AL->DestructorFn)
AL->DestructorFn(p);
memcpy(p,newval,AL->ElementSize);
AL->timestamp++;
return 1;
}
static Vector *IndexIn(Vector *SC,Vector *AL)
{
Vector *result = NULL;
size_t i,top,idx;
char *p;
int r;
if (SC == NULL || AL == NULL) {
NullPtrError("IndexIn");
return NULL;
}
if (AL == NULL) {
NullPtrError("IndexIn");
return NULL;
}
if (iVector.GetElementSize(AL) != sizeof(size_t)) {
ErrorIncompatible(SC,"IndexIn");
return NULL;
}
top = iVector.Size(AL);
result = iVector.Create(SC->ElementSize,top);
for (i=0; i<top;i++) {
idx = *(size_t *)iVector.GetElement(AL,i);
p = (char *)GetElement(SC,idx);
if (p == NULL)
goto err;
r = Add(result,p);
if (r < 0) {