Convenient array & hash iterators & accessors
From: Julian Foad <julianfoad_at_btopenworld.com>
Date: Thu, 5 Mar 2015 12:16:52 +0000
In http://svn.apache.org/r1664127 (very similar to the attached patch),
I added some array & hash iterators & accessors to the 'move-tracking-2' branch
so that I could use more compact iteration code (http://svn.apache.org/r1664285)
and get an iteration pool 'for free' (http://svn.apache.org/r1664290).
Is there any interest in using some of this on trunk?
Here's a summary.
Array: apr_array_header_t of pointers to objects, with
- Simpler syntax for operations such as make, get, set, push, pop.
- no need to specify the element type in these operations
- The get and set and iteration functions avoid the need to use the
non-type-safe APR_ARRAY_IDX. (It does not assert that the sizeof(type)
matches, let alone (type) itself. Note: When I inserted
"assert(sizeof(type)==array->elt_size)" in APR's version, the Subversion
test suite still passed, so that's good.)
- Shallow and deep duplicators for an array of simple or compound elements.
Hash table: same as apr_hash_t but always with C-string keys.
Iteration over an array or a hash shares these features:
- Convenience prioritized over speed (but still good speed).
- Easy access to this key and this value as iterator member variables.
- for an array: it->i and it->val
- for a hash: it->key and it->val (and it->klen for good measure)
- Templated iterator with parameterized element type.
- in an iterator declared as "SVN_ITER_T(svn_branch_family_t) *it;",
it->val has type "svn_branch_family_t *"
- in an iterator declared as "svn_iter_t *it;",
it->val has type "void *"
- Built-in, managed iterpool as an iterator member variable.
- just use it->iterpool; it is automatically created/cleared/destroyed
- Iteration in sorted order (optional).
- just use "for (SVN_ARRAY_ITER_SORTED(it, array, comparator, pool))"
instead of "for (SVN_ARRAY_ITER(it, array, pool))"; same for a hash
- outer_family->sub_families = apr_array_make(result_pool, 1, sizeof(void *));
+ outer_family->sub_families = svn_array_make(result_pool);
- APR_ARRAY_PUSH(outer_family->sub_families, void *) = family;
+ SVN_ARRAY_PUSH(outer_family->sub_families) = family;
- apr_array_header_t *sub_families
- = svn_branch_family_get_children(family, scratch_pool);
- int f;
+ SVN_ITER_T(svn_branch_family_t) *fi;
- for (f = 0; f < sub_families->nelts; f++)
+ for (SVN_ARRAY_ITER(fi, svn_branch_family_get_children(
+ family, scratch_pool), scratch_pool))
- svn_branch_family_t *sub_family
- = APR_ARRAY_IDX(sub_families, f, svn_branch_family_t *);
- SVN_ERR(family_list_branch_instances(rev_root, sub_family, recursive,
+ SVN_ERR(family_list_branch_instances(rev_root, fi->val, recursive,
- verbose, scratch_pool));
- verbose, fi->iterpool));
This is an archived mail posted to the Subversion Dev mailing list.