summaryrefslogtreecommitdiff
path: root/vcs-svn/trp.txt
diff options
context:
space:
mode:
authorJonathan Nieder <jrnieder@gmail.com>2010-12-05 09:35:17 (GMT)
committerJunio C Hamano <gitster@pobox.com>2010-12-08 00:03:55 (GMT)
commit97a5e3453abf63bbf5926979fcd89efb4388e937 (patch)
tree1b07e107f8a422abfb1ba749c6e1502274b8cbb5 /vcs-svn/trp.txt
parentb0ad24be8ca9acd86393ce099d3217872d838915 (diff)
downloadgit-97a5e3453abf63bbf5926979fcd89efb4388e937.zip
git-97a5e3453abf63bbf5926979fcd89efb4388e937.tar.gz
git-97a5e3453abf63bbf5926979fcd89efb4388e937.tar.bz2
treap: make treap_insert return inserted node
Suppose I try the following: struct int_node *node = node_pointer(node_alloc(1)); node->n = 5; treap_insert(&root, node); printf("%d\n", node->n); Usually the result will be 5. But since treap_insert draws memory from the node pool, if the caller is unlucky then (1) the node pool will be full and (2) realloc will be forced to move the node pool to find room, so the node address becomes invalid and the result of dereferencing it is undefined. So we ought to use offsets in preference to pointers for references that would remain valid after a call to treap_insert. Tweak the signature to hint at a certain special case: since the inserted node can change address (though not offset), as a convenience teach treap_insert to return its new address. So the motivational example could be fixed by adding "node =". struct int_node *node = node_pointer(node_alloc(1)); node->n = 5; node = treap_insert(&root, node); printf("%d\n", node->n); Based on a true story. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'vcs-svn/trp.txt')
-rw-r--r--vcs-svn/trp.txt10
1 files changed, 8 insertions, 2 deletions
diff --git a/vcs-svn/trp.txt b/vcs-svn/trp.txt
index eb4c191..5ca6b42 100644
--- a/vcs-svn/trp.txt
+++ b/vcs-svn/trp.txt
@@ -21,7 +21,9 @@ The caller:
. Allocates a `struct trp_root` variable and sets it to {~0}.
-. Adds new nodes to the set using `foo_insert`.
+. Adds new nodes to the set using `foo_insert`. Any pointers
+ to existing nodes cannot be relied upon any more, so the caller
+ might retrieve them anew with `foo_pointer`.
. Can find a specific item in the set using `foo_search`.
@@ -73,10 +75,14 @@ int (*cmp)(node_type \*a, node_type \*b)
and returning a value less than, equal to, or greater than zero
according to the result of comparison.
-void foo_insert(struct trp_root *treap, node_type \*node)::
+node_type {asterisk}foo_insert(struct trp_root *treap, node_type \*node)::
Insert node into treap. If inserted multiple times,
a node will appear in the treap multiple times.
++
+The return value is the address of the node within the treap,
+which might differ from `node` if `pool_alloc` had to call
+`realloc` to expand the pool.
void foo_remove(struct trp_root *treap, node_type \*node)::