-
Notifications
You must be signed in to change notification settings - Fork 328
/
Copy pathcc_array.c
1161 lines (1019 loc) · 34.4 KB
/
cc_array.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
/*
* Collections-C
* Copyright (C) 2013-2014 Srđan Panić <[email protected]>
*
* This file is part of Collections-C.
*
* Collections-C is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Collections-C 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Collections-C. If not, see <http://www.gnu.org/licenses/>.
*/
#include "cc_array.h"
#define DEFAULT_CAPACITY 8
#define DEFAULT_EXPANSION_FACTOR 2
struct cc_array_s {
size_t size;
size_t capacity;
float exp_factor;
void **buffer;
void *(*mem_alloc) (size_t size);
void *(*mem_calloc) (size_t blocks, size_t size);
void (*mem_free) (void *block);
};
static enum cc_stat expand_capacity(CC_Array *ar);
/**
* Creates a new empty array and returns a status code.
*
* @param[out] out pointer to where the newly created CC_Array is to be stored
*
* @return CC_OK if the creation was successful, or CC_ERR_ALLOC if the
* memory allocation for the new CC_Array structure failed.
*/
enum cc_stat cc_array_new(CC_Array **out)
{
CC_ArrayConf c;
cc_array_conf_init(&c);
return cc_array_new_conf(&c, out);
}
/**
* Creates a new empty CC_Array based on the specified CC_ArrayConf struct and
* returns a status code.
*
* The CC_Array is allocated using the allocators specified in the CC_ArrayConf
* struct. The allocation may fail if underlying allocator fails. It may also
* fail if the values of exp_factor and capacity in the CC_ArrayConf do not meet
* the following condition: <code>exp_factor < (CC_MAX_ELEMENTS / capacity)</code>.
*
* @param[in] conf array configuration structure
* @param[out] out pointer to where the newly created CC_Array is to be stored
*
* @return CC_OK if the creation was successful, CC_ERR_INVALID_CAPACITY if
* the above mentioned condition is not met, or CC_ERR_ALLOC if the memory
* allocation for the new CC_Array structure failed.
*/
enum cc_stat cc_array_new_conf(CC_ArrayConf const * const conf, CC_Array **out)
{
float ex;
/* The expansion factor must be greater than one for the
* array to grow */
if (conf->exp_factor <= 1)
ex = DEFAULT_EXPANSION_FACTOR;
else
ex = conf->exp_factor;
/* Needed to avoid an integer overflow on the first resize and
* to easily check for any future overflows. */
if (!conf->capacity || ex >= CC_MAX_ELEMENTS / conf->capacity)
return CC_ERR_INVALID_CAPACITY;
CC_Array *ar = conf->mem_calloc(1, sizeof(CC_Array));
if (!ar)
return CC_ERR_ALLOC;
void **buff = conf->mem_alloc(conf->capacity * sizeof(void*));
if (!buff) {
conf->mem_free(ar);
return CC_ERR_ALLOC;
}
ar->buffer = buff;
ar->exp_factor = ex;
ar->capacity = conf->capacity;
ar->mem_alloc = conf->mem_alloc;
ar->mem_calloc = conf->mem_calloc;
ar->mem_free = conf->mem_free;
*out = ar;
return CC_OK;
}
/**
* Initializes the fields of the CC_ArrayConf struct to default values.
*
* @param[in, out] conf CC_ArrayConf structure that is being initialized
*/
void cc_array_conf_init(CC_ArrayConf *conf)
{
conf->exp_factor = DEFAULT_EXPANSION_FACTOR;
conf->capacity = DEFAULT_CAPACITY;
conf->mem_alloc = malloc;
conf->mem_calloc = calloc;
conf->mem_free = free;
}
/**
* Destroys the CC_Array structure, but leaves the data it used to hold intact.
*
* @param[in] ar the array that is to be destroyed
*/
void cc_array_destroy(CC_Array *ar)
{
ar->mem_free(ar->buffer);
ar->mem_free(ar);
}
/**
* Destroys the CC_Array structure along with all the data it holds.
*
* @note
* This function should not be called on a array that has some of its elements
* allocated on the stack.
*
* @param[in] ar the array that is being destroyed
*/
void cc_array_destroy_cb(CC_Array *ar, void (*cb) (void*))
{
size_t i;
for (i = 0; i < ar->size; i++)
cb(ar->buffer[i]);
cc_array_destroy(ar);
}
/**
* Adds a new element to the CC_Array. The element is appended to the array making
* it the last element (the one with the highest index) of the CC_Array.
*
* @param[in] ar the array to which the element is being added
* @param[in] element the element that is being added
*
* @return CC_OK if the element was successfully added, CC_ERR_ALLOC if the
* memory allocation for the new element failed, or CC_ERR_MAX_CAPACITY if the
* array is already at maximum capacity.
*/
enum cc_stat cc_array_add(CC_Array *ar, void *element)
{
if (ar->size >= ar->capacity) {
enum cc_stat status = expand_capacity(ar);
if (status != CC_OK)
return status;
}
ar->buffer[ar->size] = element;
ar->size++;
return CC_OK;
}
/**
* Adds a new element to the array at a specified position by shifting all
* subsequent elements by one. The specified index must be within the bounds
* of the array. This function may also fail if the memory allocation for
* the new element was unsuccessful.
*
* @param[in] ar the array to which the element is being added
* @param[in] element the element that is being added
* @param[in] index the position in the array at which the element is being
* added
*
* @return CC_OK if the element was successfully added, CC_ERR_OUT_OF_RANGE if
* the specified index was not in range, CC_ERR_ALLOC if the memory
* allocation for the new element failed, or CC_ERR_MAX_CAPACITY if the
* array is already at maximum capacity.
*/
enum cc_stat cc_array_add_at(CC_Array *ar, void *element, size_t index)
{
if (index == ar->size)
return cc_array_add(ar, element);
if ((ar->size == 0 && index != 0) || index > (ar->size - 1))
return CC_ERR_OUT_OF_RANGE;
if (ar->size >= ar->capacity) {
enum cc_stat status = expand_capacity(ar);
if (status != CC_OK)
return status;
}
size_t shift = (ar->size - index) * sizeof(void*);
memmove(&(ar->buffer[index + 1]),
&(ar->buffer[index]),
shift);
ar->buffer[index] = element;
ar->size++;
return CC_OK;
}
/**
* Replaces an array element at the specified index and optionally sets the out
* parameter to the value of the replaced element. The specified index must be
* within the bounds of the CC_Array.
*
* @param[in] ar array whose element is being replaced
* @param[in] element replacement element
* @param[in] index index at which the replacement element should be inserted
* @param[out] out pointer to where the replaced element is stored, or NULL if
* it is to be ignored
*
* @return CC_OK if the element was successfully replaced, or CC_ERR_OUT_OF_RANGE
* if the index was out of range.
*/
enum cc_stat cc_array_replace_at(CC_Array *ar, void *element, size_t index, void **out)
{
if (index >= ar->size)
return CC_ERR_OUT_OF_RANGE;
if (out)
*out = ar->buffer[index];
ar->buffer[index] = element;
return CC_OK;
}
enum cc_stat cc_array_swap_at(CC_Array *ar, size_t index1, size_t index2)
{
void *tmp;
if (index1 >= ar->size || index2 >= ar->size)
return CC_ERR_OUT_OF_RANGE;
tmp = ar->buffer[index1];
ar->buffer[index1] = ar->buffer[index2];
ar->buffer[index2] = tmp;
return CC_OK;
}
/**
* Removes the specified element from the CC_Array if such element exists and
* optionally sets the out parameter to the value of the removed element.
*
* @param[in] ar array from which the element is being removed
* @param[in] element element being removed
* @param[out] out pointer to where the removed value is stored, or NULL
* if it is to be ignored
*
* @return CC_OK if the element was successfully removed, or
* CC_ERR_VALUE_NOT_FOUND if the element was not found.
*/
enum cc_stat cc_array_remove(CC_Array *ar, void *element, void **out)
{
size_t index;
enum cc_stat status = cc_array_index_of(ar, element, &index);
if (status == CC_ERR_OUT_OF_RANGE)
return CC_ERR_VALUE_NOT_FOUND;
if (index != ar->size - 1) {
size_t block_size = (ar->size - 1 - index) * sizeof(void*);
memmove(&(ar->buffer[index]),
&(ar->buffer[index + 1]),
block_size);
}
ar->size--;
if (out)
*out = element;
return CC_OK;
}
/**
* Removes an CC_Array element from the specified index and optionally sets the
* out parameter to the value of the removed element. The index must be within
* the bounds of the array.
*
* @param[in] ar the array from which the element is being removed
* @param[in] index the index of the element being removed.
* @param[out] out pointer to where the removed value is stored,
* or NULL if it is to be ignored
*
* @return CC_OK if the element was successfully removed, or CC_ERR_OUT_OF_RANGE
* if the index was out of range.
*/
enum cc_stat cc_array_remove_at(CC_Array *ar, size_t index, void **out)
{
if (index >= ar->size)
return CC_ERR_OUT_OF_RANGE;
if (out)
*out = ar->buffer[index];
if (index != ar->size - 1) {
size_t block_size = (ar->size - 1 - index) * sizeof(void*);
memmove(&(ar->buffer[index]),
&(ar->buffer[index + 1]),
block_size);
}
ar->size--;
return CC_OK;
}
/**
* Removes an CC_Array element from the end of the array and optionally sets the
* out parameter to the value of the removed element.
*
* @param[in] ar the array whose last element is being removed
* @param[out] out pointer to where the removed value is stored, or NULL if it is
* to be ignored
*
* @return CC_OK if the element was successfully removed, or CC_ERR_OUT_OF_RANGE
* if the CC_Array is already empty.
*/
enum cc_stat cc_array_remove_last(CC_Array *ar, void **out)
{
return cc_array_remove_at(ar, ar->size - 1, out);
}
/**
* Removes all elements from the specified array. This function does not shrink
* the array capacity.
*
* @param[in] ar array from which all elements are to be removed
*/
void cc_array_remove_all(CC_Array *ar)
{
ar->size = 0;
}
/**
* Removes and frees all elements from the specified array. This function does
* not shrink the array capacity.
*
* @param[in] ar array from which all elements are to be removed
*/
void cc_array_remove_all_free(CC_Array *ar)
{
size_t i;
for (i = 0; i < ar->size; i++)
free(ar->buffer[i]);
cc_array_remove_all(ar);
}
/**
* Gets an CC_Array element from the specified index and sets the out parameter to
* its value. The specified index must be within the bounds of the array.
*
* @param[in] ar the array from which the element is being retrieved
* @param[in] index the index of the array element
* @param[out] out pointer to where the element is stored
*
* @return CC_OK if the element was found, or CC_ERR_OUT_OF_RANGE if the index
* was out of range.
*/
enum cc_stat cc_array_get_at(CC_Array *ar, size_t index, void **out)
{
if (index >= ar->size)
return CC_ERR_OUT_OF_RANGE;
*out = ar->buffer[index];
return CC_OK;
}
/**
* Gets the last element of the array or the element at the highest index
* and sets the out parameter to its value.
*
* @param[in] ar the array whose last element is being returned
* @param[out] out pointer to where the element is stored
*
* @return CC_OK if the element was found, or CC_ERR_VALUE_NOT_FOUND if the
* CC_Array is empty.
*/
enum cc_stat cc_array_get_last(CC_Array *ar, void **out)
{
if (ar->size == 0)
return CC_ERR_VALUE_NOT_FOUND;
return cc_array_get_at(ar, ar->size - 1, out);
}
/**
* Returns the underlying array buffer.
*
* @note Any direct modification of the buffer may invalidate the CC_Array.
*
* @param[in] ar array whose underlying buffer is being returned
*
* @return array's internal buffer.
*/
const void * const* cc_array_get_buffer(CC_Array *ar)
{
return (const void* const*) ar->buffer;
}
/**
* Gets the index of the specified element. The returned index is the index
* of the first occurrence of the element starting from the beginning of the
* CC_Array.
*
* @param[in] ar array being searched
* @param[in] element the element whose index is being looked up
* @param[out] index pointer to where the index is stored
*
* @return CC_OK if the index was found, or CC_OUT_OF_RANGE if not.
*/
enum cc_stat cc_array_index_of(CC_Array *ar, void *element, size_t *index)
{
size_t i;
for (i = 0; i < ar->size; i++) {
if (ar->buffer[i] == element) {
*index = i;
return CC_OK;
}
}
return CC_ERR_OUT_OF_RANGE;
}
/**
* Creates a subarray of the specified CC_Array, ranging from <code>b</code>
* index (inclusive) to <code>e</code> index (inclusive). The range indices
* must be within the bounds of the CC_Array, while the <code>e</code> index
* must be greater or equal to the <code>b</code> index.
*
* @note The new CC_Array is allocated using the original CC_Array's allocators
* and it also inherits the configuration of the original CC_Array.
*
* @param[in] ar array from which the subarray is being created
* @param[in] b the beginning index (inclusive) of the subarray that must be
* within the bounds of the array and must not exceed the
* the end index
* @param[in] e the end index (inclusive) of the subarray that must be within
* the bounds of the array and must be greater or equal to the
* beginning index
* @param[out] out pointer to where the new sublist is stored
*
* @return CC_OK if the subarray was successfully created, CC_ERR_INVALID_RANGE
* if the specified index range is invalid, or CC_ERR_ALLOC if the memory allocation
* for the new subarray failed.
*/
enum cc_stat cc_array_subarray(CC_Array *ar, size_t b, size_t e, CC_Array **out)
{
if (b > e || e >= ar->size)
return CC_ERR_INVALID_RANGE;
CC_Array *sub_ar = ar->mem_calloc(1, sizeof(CC_Array));
if (!sub_ar)
return CC_ERR_ALLOC;
/* Try to allocate the buffer */
if (!(sub_ar->buffer = ar->mem_alloc(ar->capacity * sizeof(void*)))) {
ar->mem_free(sub_ar);
return CC_ERR_ALLOC;
}
sub_ar->mem_alloc = ar->mem_alloc;
sub_ar->mem_calloc = ar->mem_calloc;
sub_ar->mem_free = ar->mem_free;
sub_ar->size = e - b + 1;
sub_ar->capacity = sub_ar->size;
memcpy(sub_ar->buffer,
&(ar->buffer[b]),
sub_ar->size * sizeof(void*));
*out = sub_ar;
return CC_OK;
}
/**
* Creates a shallow copy of the specified CC_Array. A shallow copy is a copy of
* the CC_Array structure, but not the elements it holds.
*
* @note The new CC_Array is allocated using the original CC_Array's allocators
* and it also inherits the configuration of the original array.
*
* @param[in] ar the array to be copied
* @param[out] out pointer to where the newly created copy is stored
*
* @return CC_OK if the copy was successfully created, or CC_ERR_ALLOC if the
* memory allocation for the copy failed.
*/
enum cc_stat cc_array_copy_shallow(CC_Array *ar, CC_Array **out)
{
CC_Array *copy = ar->mem_alloc(sizeof(CC_Array));
if (!copy)
return CC_ERR_ALLOC;
if (!(copy->buffer = ar->mem_calloc(ar->capacity, sizeof(void*)))) {
ar->mem_free(copy);
return CC_ERR_ALLOC;
}
copy->exp_factor = ar->exp_factor;
copy->size = ar->size;
copy->capacity = ar->capacity;
copy->mem_alloc = ar->mem_alloc;
copy->mem_calloc = ar->mem_calloc;
copy->mem_free = ar->mem_free;
memcpy(copy->buffer,
ar->buffer,
copy->size * sizeof(void*));
*out = copy;
return CC_OK;
}
/**
* Creates a deep copy of the specified CC_Array. A deep copy is a copy of
* both the CC_Array structure and the data it holds.
*
* @note The new CC_Array is allocated using the original CC_Array's allocators
* and it also inherits the configuration of the original CC_Array.
*
* @param[in] ar array to be copied
* @param[in] cp the copy function that should return a pointer to the copy of
* the data
* @param[out] out pointer to where the newly created copy is stored
*
* @return CC_OK if the copy was successfully created, or CC_ERR_ALLOC if the
* memory allocation for the copy failed.
*/
enum cc_stat cc_array_copy_deep(CC_Array *ar, void *(*cp) (void *), CC_Array **out)
{
CC_Array *copy = ar->mem_alloc(sizeof(CC_Array));
if (!copy)
return CC_ERR_ALLOC;
if (!(copy->buffer = ar->mem_calloc(ar->capacity, sizeof(void*)))) {
ar->mem_free(copy);
return CC_ERR_ALLOC;
}
copy->exp_factor = ar->exp_factor;
copy->size = ar->size;
copy->capacity = ar->capacity;
copy->mem_alloc = ar->mem_alloc;
copy->mem_calloc = ar->mem_calloc;
copy->mem_free = ar->mem_free;
size_t i;
for (i = 0; i < copy->size; i++)
copy->buffer[i] = cp(ar->buffer[i]);
*out = copy;
return CC_OK;
}
/**
* Filters the CC_Array by modifying it. It removes all elements that don't
* return true on pred(element).
*
* @param[in] ar array that is to be filtered
* @param[in] pred predicate function which returns true if the element should
* be kept in the CC_Array
*
* @return CC_OK if the CC_Array was filtered successfully, or CC_ERR_OUT_OF_RANGE
* if the CC_Array is empty.
*/
enum cc_stat cc_array_filter_mut(CC_Array *ar, bool (*pred) (const void*))
{
if (ar->size == 0)
return CC_ERR_OUT_OF_RANGE;
size_t rm = 0;
size_t keep = 0;
/* Look for clusters of non matching elements before moving
* in order to minimize the number of memmoves */
for (size_t i = ar->size - 1; i != ((size_t) - 1); i--) {
if (!pred(ar->buffer[i])) {
rm++;
continue;
}
if (rm > 0) {
if (keep > 0) {
size_t block_size = keep * sizeof(void*);
memmove(&(ar->buffer[i + 1]),
&(ar->buffer[i + 1 + rm]),
block_size);
}
ar->size -= rm;
rm = 0;
}
keep++;
}
/* Remove any remaining elements*/
if (rm > 0) {
size_t block_size = keep * sizeof(void*);
memmove(&(ar->buffer[0]),
&(ar->buffer[rm]),
block_size);
ar->size -= rm;
}
return CC_OK;
}
/**
* Filters the CC_Array by creating a new CC_Array that contains all elements from the
* original CC_Array that return true on pred(element) without modifying the original
* CC_Array.
*
* @param[in] ar array that is to be filtered
* @param[in] pred predicate function which returns true if the element should
* be kept in the filtered array
* @param[out] out pointer to where the new filtered CC_Array is to be stored
*
* @return CC_OK if the CC_Array was filtered successfully, CC_ERR_OUT_OF_RANGE
* if the CC_Array is empty, or CC_ERR_ALLOC if the memory allocation for the
* new CC_Array failed.
*/
enum cc_stat cc_array_filter(CC_Array *ar, bool (*pred) (const void*), CC_Array **out)
{
if (ar->size == 0)
return CC_ERR_OUT_OF_RANGE;
CC_Array *filtered = ar->mem_alloc(sizeof(CC_Array));
if (!filtered)
return CC_ERR_ALLOC;
if (!(filtered->buffer = ar->mem_calloc(ar->capacity, sizeof(void*)))) {
ar->mem_free(filtered);
return CC_ERR_ALLOC;
}
filtered->exp_factor = ar->exp_factor;
filtered->size = 0;
filtered->capacity = ar->capacity;
filtered->mem_alloc = ar->mem_alloc;
filtered->mem_calloc = ar->mem_calloc;
filtered->mem_free = ar->mem_free;
size_t f = 0;
for (size_t i = 0; i < ar->size; i++) {
if (pred(ar->buffer[i])) {
filtered->buffer[f++] = ar->buffer[i];
filtered->size++;
}
}
*out = filtered;
return CC_OK;
}
/**
* Reverses the order of elements in the specified array.
*
* @param[in] ar array that is being reversed
*/
void cc_array_reverse(CC_Array *ar)
{
if (ar->size == 0)
return;
size_t i;
size_t j;
for (i = 0, j = ar->size - 1; i < ar->size / 2; i++, j--) {
void *tmp = ar->buffer[i];
ar->buffer[i] = ar->buffer[j];
ar->buffer[j] = tmp;
}
}
/**
* Trims the array's capacity, in other words, it shrinks the capacity to match
* the number of elements in the CC_Array, however the capacity will never shrink
* below 1.
*
* @param[in] ar array whose capacity is being trimmed
*
* @return CC_OK if the capacity was trimmed successfully, or CC_ERR_ALLOC if
* the reallocation failed.
*/
enum cc_stat cc_array_trim_capacity(CC_Array *ar)
{
if (ar->size == ar->capacity)
return CC_OK;
void **new_buff = ar->mem_calloc(ar->size, sizeof(void*));
if (!new_buff)
return CC_ERR_ALLOC;
size_t size = ar->size < 1 ? 1 : ar->size;
memcpy(new_buff, ar->buffer, size * sizeof(void*));
ar->mem_free(ar->buffer);
ar->buffer = new_buff;
ar->capacity = ar->size;
return CC_OK;
}
/**
* Returns the number of occurrences of the element within the specified CC_Array.
*
* @param[in] ar array that is being searched
* @param[in] element the element that is being searched for
*
* @return the number of occurrences of the element.
*/
size_t cc_array_contains(CC_Array *ar, void *element)
{
size_t o = 0;
size_t i;
for (i = 0; i < ar->size; i++) {
if (ar->buffer[i] == element)
o++;
}
return o;
}
/**
* Returns the number of occurrences of the value pointed to by <code>e</code>
* within the specified CC_Array.
*
* @param[in] ar array that is being searched
* @param[in] element the element that is being searched for
* @param[in] cmp comparator function which returns 0 if the values passed to it are equal
*
* @return the number of occurrences of the value.
*/
size_t cc_array_contains_value(CC_Array *ar, void *element, int (*cmp) (const void*, const void*))
{
size_t o = 0;
size_t i;
for (i = 0; i < ar->size; i++) {
if (cmp(element, ar->buffer[i]) == 0)
o++;
}
return o;
}
/**
* Returns the size of the specified CC_Array. The size of the array is the
* number of elements contained within the CC_Array.
*
* @param[in] ar array whose size is being returned
*
* @return the the number of element within the CC_Array.
*/
size_t cc_array_size(CC_Array *ar)
{
return ar->size;
}
/**
* Returns the capacity of the specified CC_Array. The capacity of the CC_Array is
* the maximum number of elements an CC_Array can hold before it has to be resized.
*
* @param[in] ar array whose capacity is being returned
*
* @return the capacity of the CC_Array.
*/
size_t cc_array_capacity(CC_Array *ar)
{
return ar->capacity;
}
/**
* Sorts the specified array.
*
* @note
* Pointers passed to the comparator function will be pointers to the array
* elements that are of type (void*) ie. void**. So an extra step of
* dereferencing will be required before the data can be used for comparison:
* eg. <code>my_type e = *(*((my_type**) ptr));</code>.
*
* @code
* enum cc_stat mycmp(const void *e1, const void *e2) {
* MyType el1 = *(*((enum cc_stat**) e1));
* MyType el2 = *(*((enum cc_stat**) e2));
*
* if (el1 < el2) return -1;
* if (el1 > el2) return 1;
* return 0;
* }
*
* ...
*
* cc_array_sort(array, mycmp);
* @endcode
*
* @param[in] ar array to be sorted
* @param[in] cmp the comparator function that must be of type <code>
* enum cc_stat cmp(const void e1*, const void e2*)</code> that
* returns < 0 if the first element goes before the second,
* 0 if the elements are equal and > 0 if the second goes
* before the first
*/
void cc_array_sort(CC_Array *ar, int (*cmp) (const void*, const void*))
{
qsort(ar->buffer, ar->size, sizeof(void*), cmp);
}
/**
* Expands the CC_Array capacity. This might fail if the the new buffer
* cannot be allocated. In case the expansion would overflow the index
* range, a maximum capacity buffer is allocated instead. If the capacity
* is already at the maximum capacity, no new buffer is allocated.
*
* @param[in] ar array whose capacity is being expanded
*
* @return CC_OK if the buffer was expanded successfully, CC_ERR_ALLOC if
* the memory allocation for the new buffer failed, or CC_ERR_MAX_CAPACITY
* if the array is already at maximum capacity.
*/
static enum cc_stat expand_capacity(CC_Array *ar)
{
if (ar->capacity == CC_MAX_ELEMENTS)
return CC_ERR_MAX_CAPACITY;
size_t new_capacity = (size_t)(ar->capacity * ar->exp_factor);
/* As long as the capacity is greater that the expansion factor
* at the point of overflow, this is check is valid. */
if (new_capacity <= ar->capacity)
ar->capacity = CC_MAX_ELEMENTS;
else
ar->capacity = new_capacity;
void **new_buff = ar->mem_alloc(ar->capacity * sizeof(void*));
if (!new_buff)
return CC_ERR_ALLOC;
memcpy(new_buff, ar->buffer, ar->size * sizeof(void*));
ar->mem_free(ar->buffer);
ar->buffer = new_buff;
return CC_OK;
}
/**
* Applies the function fn to each element of the CC_Array.
*
* @param[in] ar array on which this operation is performed
* @param[in] fn operation function that is to be invoked on each CC_Array
* element
*/
void cc_array_map(CC_Array *ar, void (*fn) (void *e))
{
size_t i;
for (i = 0; i < ar->size; i++)
fn(ar->buffer[i]);
}
/**
* A fold/reduce function that collects all of the elements in the array
* together. For example, if we have an array of [a,b,c...] the end result
* will be (...((a+b)+c)+...).
*
* @param[in] ar the array on which this operation is performed
* @param[in] fn the operation function that is to be invoked on each array
* element
* @param[in] result the pointer which will collect the end result
*/
void cc_array_reduce(CC_Array *ar, void (*fn) (void*, void*, void*), void *result)
{
if (ar->size == 1) {
fn(ar->buffer[0], NULL, result);
return;
}
if (ar->size > 1)
fn(ar->buffer[0], ar->buffer[1], result);
for (size_t i = 2; i < ar->size; i++)
fn(result, ar->buffer[i], result);
}
/**
* Initializes the iterator.
*
* @param[in] iter the iterator that is being initialized
* @param[in] ar the array to iterate over
*/
void cc_array_iter_init(CC_ArrayIter *iter, CC_Array *ar)
{
iter->ar = ar;
iter->index = 0;
iter->last_removed = false;
}
/**
* Advances the iterator and sets the out parameter to the value of the
* next element in the sequence.
*
* @param[in] iter the iterator that is being advanced
* @param[out] out pointer to where the next element is set
*
* @return CC_OK if the iterator was advanced, or CC_ITER_END if the
* end of the CC_Array has been reached.
*/
enum cc_stat cc_array_iter_next(CC_ArrayIter *iter, void **out)
{
if (iter->index >= iter->ar->size)
return CC_ITER_END;
*out = iter->ar->buffer[iter->index];
iter->index++;
iter->last_removed = false;
return CC_OK;
}
/**
* Removes the last returned element by <code>cc_array_iter_next()</code>
* function without invalidating the iterator and optionally sets the out
* parameter to the value of the removed element.
*
* @note This function should only ever be called after a call to <code>
* cc_array_iter_next()</code>.
* @param[in] iter the iterator on which this operation is being performed
* @param[out] out pointer to where the removed element is stored, or NULL
* if it is to be ignored
*
* @return CC_OK if the element was successfully removed, or
* CC_ERR_VALUE_NOT_FOUND.
*/
enum cc_stat cc_array_iter_remove(CC_ArrayIter *iter, void **out)
{
enum cc_stat status = CC_ERR_VALUE_NOT_FOUND;
if (!iter->last_removed) {
status = cc_array_remove_at(iter->ar, iter->index - 1, out);
if (status == CC_OK)
iter->last_removed = true;
}
return status;
}
/**
* Adds a new element to the CC_Array after the last returned element by
* <code>cc_array_iter_next()</code> function without invalidating the
* iterator.
*
* @note This function should only ever be called after a call to <code>
* cc_array_iter_next()</code>.
*
* @param[in] iter the iterator on which this operation is being performed
* @param[in] element the element being added
*
* @return CC_OK if the element was successfully added, CC_ERR_ALLOC if the
* memory allocation for the new element failed, or CC_ERR_MAX_CAPACITY if
* the array is already at maximum capacity.
*/
enum cc_stat cc_array_iter_add(CC_ArrayIter *iter, void *element)
{
return cc_array_add_at(iter->ar, element, iter->index++);
}
/**
* Replaces the last returned element by <code>cc_array_iter_next()</code>
* with the specified element and optionally sets the out parameter to
* the value of the replaced element.
*
* @note This function should only ever be called after a call to <code>
* cc_array_iter_next()</code>.
*
* @param[in] iter the iterator on which this operation is being performed
* @param[in] element the replacement element
* @param[out] out pointer to where the replaced element is stored, or NULL
* if it is to be ignored