/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ /* $Rev$ $Date$ */ #ifndef tuscany_tree_hpp #define tuscany_tree_hpp /** * Functions to work with trees. */ #include "stream.hpp" #include "string.hpp" #include "function.hpp" #include "list.hpp" #include "monad.hpp" namespace tuscany { /** * Delete assocs matching a path of keys in a tree of assocs. * The path can be a complete or partial path to an assoc. * Requires T to support isList, isAssoc, and cast to list. */ template inline const list treeDelAssoc(const list& k, const list& l) noexcept { if (isNull(k) || isNull(l)) return l; const list lv = l; // If list is an assoc and matches, skip it if (isAssoc(lv)) { if (car(lv) == car(k) && isNull(cdr(k))) return list(); } // If list element is not an assoc, lookup children and rest of the list const T a = car(lv); if (!isAssoc(a)) { if (!isList(a)) return cons(a, treeDelAssoc(k, cdr(lv))); const list da = treeDelAssoc(k, a); return isNull(da)? treeDelAssoc(k, cdr(lv)) : cons(da, treeDelAssoc(k, cdr(lv))); } // If we found a match, skip it and lookup children and rest of the list if (car(a) == car(k)) { if (isNull(cdr(k))) return treeDelAssoc(k, cdr(lv)); return cons(cons(car(a), treeDelAssoc(cdr(k), cdr(a))), treeDelAssoc(k, cdr(lv))); } // No match, lookup children and rest of the list if (isNull(cdr(a))) return cons(a, treeDelAssoc(k, cdr(lv))); if (!isList(cadr(a))) return cons(cons(car(a), cons(cadr(a), treeDelAssoc(k, cddr(a)))), treeDelAssoc(k, cdr(lv))); const list da = treeDelAssoc(k, cadr(a)); if (isNull(da)) return cons(cons(car(a), treeDelAssoc(k, cddr(a))), treeDelAssoc(k, cdr(lv))); return cons(cons(car(a), cons(da, treeDelAssoc(k, cddr(a)))), treeDelAssoc(k, cdr(lv))); } /** * Substitute assocs matching a path of keys in a tree of assocs. * The path can be a complete or partial path to an assoc. * Requires T to support isList, isAssoc, and cast to list. */ template inline const list treeSubstAssoc(const list& k, const list& n, const list& lv) noexcept { if (isNull(k) || isNull(lv)) return lv; // If list is an assoc and matches, substitute it if (isAssoc(lv)) { if (car(lv) == car(k) && isNull(cdr(k))) return n; } // If list element is not an assoc, lookup children and rest of the list const T a = car(lv); if (!isAssoc(a)) { if (!isList(a)) return cons(a, treeSubstAssoc(k, n, cdr(lv))); return cons(treeSubstAssoc(k, n, a), treeSubstAssoc(k, n, cdr(lv))); } // If we found a match, substitute it and lookup children and rest of the list if (car(a) == car(k)) { if (isNull(cdr(k))) return cons(n, treeSubstAssoc(k, n, cdr(lv))); return cons(cons(car(a), treeSubstAssoc(cdr(k), n, cdr(a))), treeSubstAssoc(k, n, cdr(lv))); } // No match, lookup children and rest of the list if (isNull(cdr(a))) return cons(a, treeSubstAssoc(k, n, cdr(lv))); if (!isList(cadr(a))) return cons(cons(car(a), cons(cadr(a), treeSubstAssoc(k, n, cddr(a)))), treeSubstAssoc(k, n, cdr(lv))); return cons(cons(car(a), cons(treeSubstAssoc(k, n, cadr(a)), treeSubstAssoc(k, n, cddr(a)))), treeSubstAssoc(k, n, cdr(lv))); } /** * Select assocs matching a path of keys in a tree of assocs. * The path can be a complete or partial path to an assoc. * Requires T to support isList, isAssoc, and cast to list. */ template inline const list treeSelectAssoc(const list& k, const list& lv) noexcept { if (isNull(k) || isNull(lv)) return list(); // If list is an assoc and matches, select it if (isAssoc(lv)) { if (car(lv) == car(k) && isNull(cdr(k))) return mklist(lv); } // If list element is not an assoc, lookup children and rest of the list const T a = car(lv); if (!isAssoc(a)) { if (!isList(a)) return treeSelectAssoc(k, cdr(lv)); return append(treeSelectAssoc(k, a), treeSelectAssoc(k, cdr(lv))); } // If we found a match, select it and lookup children and rest of the list if (car(a) == car(k)) { if (isNull(cdr(k))) return cons(a, treeSelectAssoc(k, cdr(lv))); return append(treeSelectAssoc(cdr(k), cdr(a)), treeSelectAssoc(k, cdr(lv))); } // No match, lookup children and rest of the list if (isNull(cdr(a))) return treeSelectAssoc(k, cdr(lv)); if (!isList(cadr(a))) return append(treeSelectAssoc(k, cddr(a)), treeSelectAssoc(k, cdr(lv))); return append(append(treeSelectAssoc(k, cadr(a)), treeSelectAssoc(k, cddr(a))), treeSelectAssoc(k, cdr(lv))); } /** * Make a rooted binary tree from a leaf and two branches. */ template inline const list mkrbtree(const T& e, const list& left, const list& right) { return mklist(e, left, right); } /** * Find a leaf with the given key in a rooted binary tree. */ template inline const list rbtreeAssoc(const T& k, const list& tree) { if (isNull(tree)) return tree; if (k == car(car(tree))) return car(tree); if (k < car(car(tree))) return rbtreeAssoc(k, cadr(tree)); return rbtreeAssoc(k, caddr(tree)); } /** * Construct a new rooted binary tree from a leaf and a tree. */ template inline const list rbtreeCons(const T& e, const list& tree) { if (isNull(tree)) return mkrbtree(e, list(), list()); if (e == car(tree)) 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))); } /** * Make a rooted binary tree from an unordered list of leaves. */ template inline const list mkrbtree(const list& l) { if (isNull(l)) return l; return rbtreeCons(car(l), mkrbtree(cdr(l))); } /** * Convert a rooted binary tree to an ordered list of leaves. */ template inline const list flatten(const list& tree) { if (isNull(tree)) return tree; return append(flatten(cadr(tree)), cons(car(tree), flatten(caddr(tree)))); } /** * Sort a list, using a rooted binary tree. */ template inline const list sort(const list& l) { return flatten(mkrbtree(l)); } /** * Make a balanced rooted binary tree from an ordered list of leaves. */ template inline const list brbtreeHelper(const list& elements, const size_t n) { if (n == 0) return cons(list(), elements); const size_t leftSize = (n - 1) / 2; { const list leftResult = brbtreeHelper(elements, leftSize); { const list leftTree = car(leftResult); const list nonLeftElements = cdr(leftResult); const size_t rightSize = n - (leftSize + 1); { const T thisEntry = car(nonLeftElements); const list rightResult = brbtreeHelper(cdr(nonLeftElements), rightSize); { const list rightTree = car(rightResult); const list remainingElements = cdr(rightResult); { return cons(mkrbtree(thisEntry, leftTree, rightTree), remainingElements); } } } } } } template inline const list mkbrbtree(const list& elements) { return car(brbtreeHelper(elements, length(elements))); } } #endif /* tuscany_tree_hpp */