Broken
[eleutheria.git] / proplib / prop_expand.c
blob791a7b9706d8896ff37af436103307ad2bc8d793
1 /* Broken, tomorrow will be fixed :) */
3 #include <err.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <prop/proplib.h>
8 #define INIT_CAPACITY 10
9 #define NUM_STRINGS 32
10 #define PRINT_STEP 5 /* Print stats every 5 steps */
12 /* Function prototypes */
13 void print_array_stats(const prop_array_t pa);
15 int
16 main(int argc, char *argv[])
18 prop_array_t pa;
19 prop_string_t ps;
20 unsigned int i;
22 /*
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);
30 if (pa == NULL)
31 errx(EXIT_FAILURE, "prop_array_create_with_capacity()");
33 /* Create prop_string_t object */
34 ps = prop_string_create_cstring("foo");
35 if (ps == NULL) {
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()");
57 CLEANUP:;
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);
72 /* Release objects */
73 prop_object_release(pa);
74 prop_object_release(ps);
76 print_array_stats(pa);
78 return EXIT_SUCCESS;
81 void print_array_stats(const prop_array_t pa)
83 printf("count = %u\tcapacity = %u\n",
84 prop_array_count(pa),
85 prop_array_capacity(pa));