9 * ----------------------------------------------
10 * | name1 | name2 | name3 | | nameN |
11 * ----------------------------------------------
14 * ----------------------------------------------
15 * | memb1 | memb2 | memb3 | | membN |
16 * ----------------------------------------------
19 * --------------------
20 * | pointer to name1 |
21 * --------------------
23 * --------------------
31 * Copy the names in the hashed array.
32 * Pointers to the names are already in the array, but they point
33 * to the source array names. We duplicate these source names and
34 * replace the entries.
36 static void r_array_oncopy_rstr(rarray_t *array)
41 for (index = 0; index < r_array_length(array); index++) {
42 src = r_array_index(array, index, rstr_t*);
44 dst = r_rstrdup(src->str, src->size);
45 r_array_replace(array, index, &dst);
50 * Destroy the names in the hashed array.
52 static void r_array_oncleanup_rstr(rarray_t *array)
57 for (index = 0; index < r_array_length(array); index++) {
58 src = r_array_index(array, index, rstr_t*);
65 robject_t *r_harray_init(robject_t *obj, ruint32 type, r_object_cleanupfun cleanup, r_object_copyfun copy, ruint elt_size)
67 rharray_t *harray = (rharray_t*)obj;
68 r_object_init(obj, type, cleanup, copy);
69 harray->hash = r_hash_create(5, r_hash_rstrequal, r_hash_rstrhash);
70 harray->members = r_carray_create(elt_size);
71 harray->names = r_array_create(sizeof(rstr_t*));
72 harray->names->oncleanup = r_array_oncleanup_rstr;
73 harray->names->oncopy = r_array_oncopy_rstr;
77 rharray_t *r_harray_create(ruint elt_size)
81 harray = (rharray_t*)r_object_create(sizeof(*harray));
82 r_harray_init((robject_t*)harray, R_OBJECT_HARRAY,r_harray_cleanup, r_harray_copy, elt_size);
87 robject_t *r_harray_copy(const robject_t *obj)
92 const rharray_t *src = (const rharray_t *)obj;
94 harray = (rharray_t*)r_object_create(sizeof(*harray));
95 r_object_init(&harray->obj, R_OBJECT_HARRAY, r_harray_cleanup, r_harray_copy);
96 harray->names = (rarray_t*)r_array_copy((robject_t*)src->names);
97 harray->members = (rcarray_t*)r_carray_copy((robject_t*)src->members);
98 harray->hash = r_hash_create(5, r_hash_rstrequal, r_hash_rstrhash);
99 for (i = 0; i < r_carray_length(src->members); i++) {
100 n = r_array_index(harray->names, i, rstr_t*);
101 r_hash_insert_indexval(harray->hash, (rconstpointer)n, i);
103 return (robject_t*)harray;
107 void r_harray_cleanup(robject_t *obj)
109 rharray_t *harray = (rharray_t *)obj;
110 r_object_destroy((robject_t*)harray->members);
111 r_object_destroy((robject_t*)harray->names);
112 r_object_destroy((robject_t*)harray->hash);
113 r_object_cleanup(&harray->obj);
117 rlong r_harray_add(rharray_t *harray, const rchar *name, ruint namesize, rconstpointer pval)
122 membrName = r_rstrdup(name, namesize);
123 index = r_carray_add(harray->members, pval);
124 r_array_add(harray->names, &membrName);
125 r_hash_insert_indexval(harray->hash, (rconstpointer)membrName, index);
130 rlong r_harray_add_s(rharray_t *harray, const rchar *name, rconstpointer pval)
132 return r_harray_add(harray, name, r_strlen(name), pval);
136 rlong r_harray_replace(rharray_t *harray, const rchar *name, ruint namesize, rconstpointer pval)
138 rlong index = r_harray_lookup(harray, name, namesize);
141 return r_harray_add(harray, name, namesize, pval);
142 index = r_carray_replace(harray->members, index, pval);
146 rlong r_harray_replace_s(rharray_t *harray, const rchar *name, rconstpointer pval)
148 return r_harray_replace(harray, name, r_strlen(name), pval);
152 rlong r_harray_lookup(rharray_t *harray, const rchar *name, ruint namesize)
156 rstr_t lookupstr = {(char*)name, namesize};
157 found = r_hash_lookup_indexval(harray->hash, &lookupstr);
158 if (found == R_HASH_INVALID_INDEXVAL)
164 rlong r_harray_lookup_s(rharray_t *harray, const rchar *name)
166 return r_harray_lookup(harray, name, r_strlen(name));
170 rlong r_harray_taillookup(rharray_t *harray, const rchar *name, ruint namesize)
174 rstr_t lookupstr = {(char*)name, namesize};
175 found = r_hash_taillookup_indexval(harray->hash, &lookupstr);
176 if (found == R_HASH_INVALID_INDEXVAL)
182 rlong r_harray_taillookup_s(rharray_t *harray, const rchar *name)
184 return r_harray_lookup(harray, name, r_strlen(name));
189 rhash_node_t* r_harray_nodelookup(rharray_t *harray, rhash_node_t *cur, const rchar *name, ruint namesize)
191 rstr_t lookupstr = {(char*)name, namesize};
192 return r_hash_nodelookup(harray->hash, cur, &lookupstr);
196 rhash_node_t* r_harray_nodelookup_s(rharray_t *harray, rhash_node_t *cur, const rchar *name)
198 return r_harray_nodelookup(harray, cur, name, r_strlen(name));
202 rhash_node_t* r_harray_nodetaillookup(rharray_t *harray, rhash_node_t *cur, const rchar *name, ruint namesize)
204 rstr_t lookupstr = {(char*)name, namesize};
205 return r_hash_nodetaillookup(harray->hash, cur, &lookupstr);
209 rhash_node_t* r_harray_nodetaillookup_s(rharray_t *harray, rhash_node_t *cur, const rchar *name)
211 return r_harray_nodetaillookup(harray, cur, name, r_strlen(name));
215 rint r_harray_set(rharray_t *harray, rlong index, rconstpointer pval)
219 r_carray_replace(harray->members, index, pval);
224 rpointer r_harray_get(rharray_t *harray, rulong index)
226 if (index >= r_carray_length(harray->members) || index < 0)
228 return r_carray_slot(harray->members, index);