1 /* Broken, tomorrow will be fixed :) */
6 #include <prop/proplib.h>
8 #define INIT_CAPACITY 10
10 #define PRINT_STEP 5 /* Print stats every 5 steps */
12 /* Function prototypes */
13 void print_array_stats(const prop_array_t pa
);
16 main(int argc
, char *argv
[])
23 * Create array object with initial capacity set to
24 * ``INIT_CAPACITY''. The array will expand on demand
25 * if its count exceeds its capacity.
26 * This happens in prop_array_add() with ``EXPAND_STEP''
27 * step, as defined in libprop/prop_array.c file.
29 pa
= prop_array_create_with_capacity(INIT_CAPACITY
);
31 errx(EXIT_FAILURE
, "prop_array_create_with_capacity()");
33 /* Create prop_string_t object */
34 ps
= prop_string_create_cstring("foo");
36 prop_object_release(pa
);
37 errx(EXIT_FAILURE
, "prop_string_create_cstring()");
41 * Add up to ``NUM_STRINGS'' references of prop_string_t
42 * object to array and watch if/how the latter expands on demand.
44 for (i
= 0; i
< NUM_STRINGS
; i
++) {
45 /* Print statistics every ``PRINT_STEP'' step */
46 if (i
% PRINT_STEP
== 0)
47 print_array_stats(pa
);
49 /* Add object reference in array */
50 if (prop_array_add(pa
, ps
) == FALSE
) {
51 prop_object_release(pa
);
52 prop_object_release(ps
);
53 err(EXIT_FAILURE
, "prop_array_add()");
59 * Remove references from array and note that
60 * if an expansion has happened before, array's
61 * capacity won't reduce to its initial value,
62 * i.e. ``INIT_CAPACITY''.
64 for (i
= 0; i
< NUM_STRINGS
; i
++) {
65 /* Print statistics every ``PRINT_STEP'' step */
66 if (i
% PRINT_STEP
== 0)
67 print_array_stats(pa
);
69 prop_array_remove(pa
, NUM_STRINGS
- i
- 1);
73 prop_object_release(pa
);
74 prop_object_release(ps
);
76 print_array_stats(pa
);
81 void print_array_stats(const prop_array_t pa
)
83 printf("count = %u\tcapacity = %u\n",
85 prop_array_capacity(pa
));