From d011a51039c4c4f4cf81db5f086a0fff192157b3 Mon Sep 17 00:00:00 2001 From: Dana Jansens Date: Mon, 30 Dec 2002 08:52:46 +0000 Subject: [PATCH] way sexier buildtree --- src/bindings.cc | 53 +++++++++++++++++++------------------------------ 1 file changed, 20 insertions(+), 33 deletions(-) diff --git a/src/bindings.cc b/src/bindings.cc index f34b9af3..ba21410c 100644 --- a/src/bindings.cc +++ b/src/bindings.cc @@ -90,50 +90,37 @@ bool OBBindings::translate(const std::string &str, Binding &b) return b.key != 0; } +static void destroytree(BindingTree *tree) +{ + while (tree) { + BindingTree *c = tree->first_child; + delete tree; + tree = c; + } +} + BindingTree *OBBindings::buildtree(const StringVect &keylist, int id) { if (keylist.empty()) return 0; // nothing in the list.. return 0 - BindingTree *ret = new BindingTree(id), *p = 0; + BindingTree *ret = 0, *p; - StringVect::const_iterator it, end = keylist.end(); - for (it = keylist.begin(); it != end; ++it) { - if (p) - p = p->first_child = new BindingTree(id); - else - p = ret; // the first node - - if (!translate(*it, p->binding)) - break; - p->text = *it; - } - if (it != end) { - // build failed.. clean up and return 0 + StringVect::const_reverse_iterator it, end = keylist.rend(); + for (it = keylist.rbegin(); it != end; ++it) { p = ret; - while (p->first_child) { - BindingTree *c = p->first_child; - delete p; - p = c; + ret = new BindingTree(id); + if (!p) ret->chain = false; + ret->first_child = p; + if (!translate(*it, ret->binding)) { + destroytree(ret); + ret = 0; + break; } - delete p; - return 0; - } else { - // set the proper chain status on the last node - p->chain = false; + ret->text = *it; // XXX: rm me } - - // successfully built a tree return ret; } -static void destroytree(BindingTree *tree) -{ - while (tree) { - BindingTree *c = tree->first_child; - delete tree; - tree = c; - } -} OBBindings::OBBindings() { -- 2.39.2