From 7905926c413e843d9497948cb824059f2c017f6a Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Mon, 26 Aug 2013 03:04:23 +0000 Subject: Support sorting database search results by rank, and add a rank attribute to ATOM entries. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1517412 13f79535-47bb-0310-9956-ffa450edef68 --- sca-cpp/trunk/kernel/tree.hpp | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) (limited to 'sca-cpp/trunk/kernel/tree.hpp') diff --git a/sca-cpp/trunk/kernel/tree.hpp b/sca-cpp/trunk/kernel/tree.hpp index a5d0e3d5b0..77f2f1ea54 100644 --- a/sca-cpp/trunk/kernel/tree.hpp +++ b/sca-cpp/trunk/kernel/tree.hpp @@ -173,26 +173,38 @@ template inline const list rbtreeAssoc(const T& k, const list& return rbtreeAssoc(k, caddr(tree)); } +/** + * Default function used to compare two values while building a rooted binary tree. + */ +template inline const int rbtreeComp(const T& a, const T& b) { + if (a == b) + return 0; + if (a < b) + return -1; + return 1; +} + /** * Construct a new rooted binary tree from a leaf and a tree. */ -template inline const list rbtreeCons(const T& e, const list& tree) { +template inline const list rbtreeCons(const T& e, const list& tree, const lambda& comp = rbtreeComp) { if (isNull(tree)) return mkrbtree(e, list(), list()); - if (e == car(tree)) + const int c = comp(e, car(tree)); + if (c == 0) return tree; - if (e < car(tree)) - return mkrbtree(car(tree), rbtreeCons(e, cadr(tree)), caddr(tree)); - return mkrbtree(car(tree), cadr(tree), rbtreeCons(e, caddr(tree))); + if (c == -1) + return mkrbtree(car(tree), rbtreeCons(e, cadr(tree), comp), caddr(tree)); + return mkrbtree(car(tree), cadr(tree), rbtreeCons(e, caddr(tree), comp)); } /** * Make a rooted binary tree from an unordered list of leaves. */ -template inline const list mkrbtree(const list& l) { +template inline const list mkrbtree(const list& l, const lambda& comp = rbtreeComp) { if (isNull(l)) return l; - return rbtreeCons(car(l), mkrbtree(cdr(l))); + return rbtreeCons(car(l), mkrbtree(cdr(l), comp), comp); } /** @@ -207,8 +219,8 @@ template inline const list flatten(const list& tree) { /** * Sort a list, using a rooted binary tree. */ -template inline const list sort(const list& l) { - return flatten(mkrbtree(l)); +template inline const list sort(const list& l, const lambda& comp = rbtreeComp) { + return flatten(mkrbtree(l, comp)); } /** -- cgit v1.2.3