1 /* Broken, tomorrow will be fixed :) */
4 * gcc prop_expand_array.c -o prop_expand_array -lprop -Wall -W -Wextra -ansi -pedantic
10 #include <prop/proplib.h>
12 #define INIT_CAPACITY 10
13 #define NUM_STRINGS 32
14 #define PRINT_STEP 5 /* Print stats every 5 steps */
16 /* Function prototypes */
17 void print_array_stats(const prop_array_t pa
);
20 main(int argc
, char *argv
[])
27 * Create array object with initial capacity set to
28 * `INIT_CAPACITY'. The array will expand on demand
29 * if its count exceeds its capacity.
30 * This happens in prop_array_add() with `EXPAND_STEP'
31 * step, as defined in libprop/prop_array.c file.
33 pa
= prop_array_create_with_capacity(INIT_CAPACITY
);
35 err(EXIT_FAILURE
, "prop_array_create_with_capacity()");
37 /* Create prop_string_t object */
38 ps
= prop_string_create_cstring("foo");
40 prop_object_release(pa
);
41 err(EXIT_FAILURE
, "prop_string_create_cstring()");
45 * Add up to `NUM_STRINGS' references of prop_string_t
46 * object to array and watch if/how the latter expands on demand.
48 for (i
= 0; i
< NUM_STRINGS
; i
++) {
49 /* Print statistics every `PRINT_STEP' step */
50 if (i
% PRINT_STEP
== 0)
51 print_array_stats(pa
);
53 /* Add object reference in array */
54 if (prop_array_add(pa
, ps
) == FALSE
) {
55 prop_object_release(ps
);
56 prop_object_release(pa
);
57 err(EXIT_FAILURE
, "prop_array_add()");
63 * Remove references from array and note that
64 * if an expansion has happened before, array's
65 * capacity won't reduce to its initial value,
66 * i.e. `INIT_CAPACITY'.
68 for (i
= 0; i
< NUM_STRINGS
; i
++) {
69 /* Print statistics every `PRINT_STEP' step */
70 if (i
% PRINT_STEP
== 0)
71 print_array_stats(pa
);
73 prop_array_remove(pa
, NUM_STRINGS
- i
- 1);
76 print_array_stats(pa
);
79 prop_object_release(ps
);
80 prop_object_release(pa
);
85 void print_array_stats(const prop_array_t pa
)
87 printf("count = %u\tcapacity = %u\n",
89 prop_array_capacity(pa
));