path: root/vcs-svn/trp.txt
diff options
authorJason Evans <>2010-08-09 22:17:34 (GMT)
committerJunio C Hamano <>2010-08-15 02:35:37 (GMT)
commit951f316470acc7c785c460a4e40735b22822349f (patch)
tree8cc846b9eead64502e00fc4064d5decfa1897320 /vcs-svn/trp.txt
parent4709455db3891f6cad9a96a574296b4926f70cbe (diff)
Add treap implementation
Provide macros to generate a type-specific treap implementation and various functions to operate on it. It uses obj_pool.h to store memory nodes in a treap. Previously committed nodes are never removed from the pool; after any *_commit operation, it is assumed (correctly, in the case of svn-fast-export) that someone else must care about them. Treaps provide a memory-efficient binary search tree structure. Insertion/deletion/search are about as about as fast in the average case as red-black trees and the chances of worst-case behavior are vanishingly small, thanks to (pseudo-)randomness. The bad worst-case behavior is a small price to pay, given that treaps are much simpler to implement. >From [db: Altered to reference nodes by offset from a common base pointer] [db: Bob Jenkins' hashing implementation dropped for Knuth's] [db: Methods unnecessary for search and insert dropped] [rr: Squelched compiler warnings] [db: Added support for immutable treap nodes] [jn: Reintroduced treap_nsearch(); with tests] Signed-off-by: David Barr <> Signed-off-by: Ramkumar Ramachandra <> Signed-off-by: Jonathan Nieder <> Signed-off-by: Junio C Hamano <>
Diffstat (limited to 'vcs-svn/trp.txt')
1 files changed, 103 insertions, 0 deletions
diff --git a/vcs-svn/trp.txt b/vcs-svn/trp.txt
new file mode 100644
index 0000000..eb4c191
--- /dev/null
+++ b/vcs-svn/trp.txt
@@ -0,0 +1,103 @@
+Treaps provide a memory-efficient binary search tree structure.
+Insertion/deletion/search are about as about as fast in the average
+case as red-black trees and the chances of worst-case behavior are
+vanishingly small, thanks to (pseudo-)randomness. The bad worst-case
+behavior is a small price to pay, given that treaps are much simpler
+to implement.
+The trp API generates a data structure and functions to handle a
+large growing set of objects stored in a pool.
+The caller:
+. Specifies parameters for the generated functions with the
+ trp_gen(static, foo_, ...) macro.
+. Allocates a `struct trp_root` variable and sets it to {~0}.
+. Adds new nodes to the set using `foo_insert`.
+. Can find a specific item in the set using `foo_search`.
+. Can iterate over items in the set using `foo_first` and `foo_next`.
+. Can remove an item from the set using `foo_remove`.
+struct ex_node {
+ const char *s;
+ struct trp_node ex_link;
+static struct trp_root ex_base = {~0};
+obj_pool_gen(ex, struct ex_node, 4096);
+trp_gen(static, ex_, struct ex_node, ex_link, ex, strcmp)
+struct ex_node *item;
+item = ex_pointer(ex_alloc(1));
+item->s = "hello";
+ex_insert(&ex_base, item);
+item = ex_pointer(ex_alloc(1));
+item->s = "goodbye";
+ex_insert(&ex_base, item);
+for (item = ex_first(&ex_base); item; item = ex_next(&ex_base, item))
+ printf("%s\n", item->s);
+trp_gen(attr, foo_, node_type, link_field, pool, cmp)::
+ Generate a type-specific treap implementation.
+. The storage class for generated functions will be 'attr' (e.g., `static`).
+. Generated function names are prefixed with 'foo_' (e.g., `treap_`).
+. Treap nodes will be of type 'node_type' (e.g., `struct treap_node`).
+ This type must be a struct with at least one `struct trp_node` field
+ to point to its children.
+. The field used to access child nodes will be 'link_field'.
+. All treap nodes must lie in the 'pool' object pool.
+. Treap nodes must be totally ordered by the 'cmp' relation, with the
+ following prototype:
+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)::
+ Insert node into treap. If inserted multiple times,
+ a node will appear in the treap multiple times.
+void foo_remove(struct trp_root *treap, node_type \*node)::
+ Remove node from treap. Caller must ensure node is
+ present in treap before using this function.
+node_type *foo_search(struct trp_root \*treap, node_type \*key)::
+ Search for a node that matches key. If no match is found,
+ result is NULL.
+node_type *foo_nsearch(struct trp_root \*treap, node_type \*key)::
+ Like `foo_search`, but if if the key is missing return what
+ would be key's successor, were key in treap (NULL if no
+ successor).
+node_type *foo_first(struct trp_root \*treap)::
+ Find the first item from the treap, in sorted order.
+node_type *foo_next(struct trp_root \*treap, node_type \*node)::
+ Find the next item.