way sexier buildtree
authorDana Jansens <danakj@orodu.net>
Mon, 30 Dec 2002 08:52:46 +0000 (08:52 +0000)
committerDana Jansens <danakj@orodu.net>
Mon, 30 Dec 2002 08:52:46 +0000 (08:52 +0000)
src/bindings.cc

index f34b9af3d682e0d521bbe8c1cc2e7b191080119c..ba21410cbdf49f1a7ebcf38ce370d81c90f99efe 100644 (file)
@@ -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()
 {