This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
I'm unhappy about twalk_r
- From: Florian Weimer <fweimer at redhat dot com>
- To: libc-alpha at sourceware dot org
- Date: Tue, 14 May 2019 14:08:00 +0200
- Subject: I'm unhappy about twalk_r
I have second thoughts about twalk_r. I think the twalk interface is
just broken because the internal tree structure is
implementation-defined (because there is so much flexibility in
balancing a binary tree). Or put differently, while the overall
iteration order is well-defined by the key ordering, it's mostly
unspecified which nodes are leaf nodes and which are internal nodes.
Exposing this information using the VISIT argument seems wrong to me.
I still think the function is useful, but I think the interface should
look like this instead:
int twalk_r (const void *root,
int (*action) (const void *nodep, void *closure),
void *closure);
ACTION is invoked for every node in the tree, in increasing key order,
as long as ACTION returns zero. Otherwise, no further invocations
happen and twalk_r returns this non-zero value. If all calls to ACTION
return zero (and the entire tree is traversed), twalk_r returns zero.
I think this is much more useful: It avoids pointless repeated calls to
ACTION, and it allows premature termination of the iteration.
Thanks,
Florian