From 3f58e5a2eef5960d6833b4f337039aa516a7648f Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Mon, 23 May 2011 22:29:24 -0400 Subject: [PATCH] Code Cleanup - Removed unused Tree code. --- src/CMakeLists.txt | 1 - src/Tree.cpp | 342 -------------------------------------------- src/Tree.h | 87 ----------- test/.gitignore | 2 - test/CMakeLists.txt | 3 +- test/tree.t.cpp | 82 ----------- test/tree2.t.cpp | 156 -------------------- 7 files changed, 1 insertion(+), 672 deletions(-) delete mode 100644 src/Tree.cpp delete mode 100644 src/Tree.h delete mode 100644 test/tree.t.cpp delete mode 100644 test/tree2.t.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4d55b8808..cf5ac75f9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -35,7 +35,6 @@ set (task_SRCS API.cpp API.h TransportCurl.cpp TransportCurl.h TransportRSYNC.cpp TransportRSYNC.h TransportSSH.cpp TransportSSH.h - Tree.cpp Tree.h Uri.cpp Uri.h Variant.cpp Variant.h ViewTask.cpp ViewTask.h diff --git a/src/Tree.cpp b/src/Tree.cpp deleted file mode 100644 index 3068fac72..000000000 --- a/src/Tree.cpp +++ /dev/null @@ -1,342 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -// taskwarrior - a command line task list manager. -// -// Copyright 2010 - 2011, Paul Beckingham, Federico Hernandez. -// All rights reserved. -// -// This program is free software; you can redistribute it and/or modify it under -// the terms of the GNU General Public License as published by the Free Software -// Foundation; either version 2 of the License, or (at your option) any later -// version. -// -// This program is distributed in the hope that it will be useful, but WITHOUT -// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more -// details. -// -// You should have received a copy of the GNU General Public License along with -// this program; if not, write to the -// -// Free Software Foundation, Inc., -// 51 Franklin Street, Fifth Floor, -// Boston, MA -// 02110-1301 -// USA -// -//////////////////////////////////////////////////////////////////////////////// - -#include -#include -#include -#include - -//////////////////////////////////////////////////////////////////////////////// -// - Tree, Branch and Node are synonymous. -// - A Tree may contain any number of branches. -// - A Branch may contain any number of name/value pairs, unique by name. -// - The destructor will delete all branches recursively. -// - Tree::enumerate is a snapshot, and is invalidated by modification. -// - Branch sequence is preserved. -Tree::Tree (const std::string& name) -: _trunk (NULL) -, _name (name) -{ -} - -//////////////////////////////////////////////////////////////////////////////// -Tree::~Tree () -{ - for (std::vector ::iterator i = _branches.begin (); - i != _branches.end (); - ++i) - delete *i; -} - -//////////////////////////////////////////////////////////////////////////////// -Tree::Tree (const Tree& other) -{ - throw "Unimplemented Tree::Tree (Tree&)"; -} - -//////////////////////////////////////////////////////////////////////////////// -Tree& Tree::operator= (const Tree& other) -{ - throw "Unimplemented Tree::operator= ()"; - return *this; -} - -//////////////////////////////////////////////////////////////////////////////// -Tree* Tree::operator[] (const int branch) -{ - if (branch < 0 || - branch > (int) _branches.size () - 1) - throw "Tree::operator[] out of range"; - - return _branches[branch]; -} - -//////////////////////////////////////////////////////////////////////////////// -void Tree::addBranch (Tree* branch) -{ - branch->_trunk = this; - _branches.push_back (branch); -} - -//////////////////////////////////////////////////////////////////////////////// -void Tree::removeBranch (Tree* branch) -{ - for (std::vector ::iterator i = _branches.begin (); - i != _branches.end (); - ++i) - { - if (*i == branch) - { - _branches.erase (i); - return; - } - } -} - -//////////////////////////////////////////////////////////////////////////////// -void Tree::replaceBranch (Tree* from, Tree* to) -{ - for (unsigned int i = 0; i < _branches.size (); ++i) - { - if (_branches[i] == from) - { - to->_trunk = this; - _branches[i] = to; - return; - } - } -} - -//////////////////////////////////////////////////////////////////////////////// -int Tree::branches () -{ - return _branches.size (); -} - -//////////////////////////////////////////////////////////////////////////////// -void Tree::name (const std::string& name) -{ - _name = name; -} - -//////////////////////////////////////////////////////////////////////////////// -std::string Tree::name () const -{ - return _name; -} - -//////////////////////////////////////////////////////////////////////////////// -// Accessor for attributes. -void Tree::attribute (const std::string& name, const std::string& value) -{ - _attributes[name] = value; -} - -//////////////////////////////////////////////////////////////////////////////// -// Accessor for attributes. -void Tree::attribute (const std::string& name, const int value) -{ - _attributes[name] = format (value); -} - -//////////////////////////////////////////////////////////////////////////////// -// Accessor for attributes. -void Tree::attribute (const std::string& name, const double value) -{ - _attributes[name] = format (value, 1, 8); -} - -//////////////////////////////////////////////////////////////////////////////// -// Accessor for attributes. -std::string Tree::attribute (const std::string& name) -{ - // Prevent autovivification. - std::map::iterator i = _attributes.find (name); - if (i != _attributes.end ()) - return i->second; - - return ""; -} - -//////////////////////////////////////////////////////////////////////////////// -void Tree::removeAttribute (const std::string& name) -{ - _attributes.erase (name); -} - -//////////////////////////////////////////////////////////////////////////////// -int Tree::attributes () const -{ - return _attributes.size (); -} - -//////////////////////////////////////////////////////////////////////////////// -std::vector Tree::allAttributes () const -{ - std::vector names; - std::map ::const_iterator it; - for (it = _attributes.begin (); it != _attributes.end (); ++it) - names.push_back (it->first); - - return names; -} - -//////////////////////////////////////////////////////////////////////////////// -// Recursively completes a list of Tree* objects, left to right, depth first. -// The reason for the depth-first enumeration is that a client may wish to -// traverse the tree and delete nodes. With a depth-first iteration, this is a -// safe mechanism, and a node pointer will never be dereferenced after it has -// been deleted. -void Tree::enumerate (std::vector & all) const -{ - for (std::vector ::const_iterator i = _branches.begin (); - i != _branches.end (); - ++i) - { - (*i)->enumerate (all); - all.push_back (*i); - } -} - -//////////////////////////////////////////////////////////////////////////////// -Tree* Tree::parent () const -{ - return _trunk; -} - -//////////////////////////////////////////////////////////////////////////////// -bool Tree::hasTag (const std::string& tag) const -{ - if (std::find (_tags.begin (), _tags.end (), tag) != _tags.end ()) - return true; - - return false; -} - -//////////////////////////////////////////////////////////////////////////////// -void Tree::tag (const std::string& tag) -{ - if (! hasTag (tag)) - _tags.push_back (tag); -} - -//////////////////////////////////////////////////////////////////////////////// -int Tree::tags () const -{ - return _tags.size (); -} - -//////////////////////////////////////////////////////////////////////////////// -std::vector Tree::allTags () const -{ - return _tags; -} - -//////////////////////////////////////////////////////////////////////////////// -int Tree::count () const -{ - int total = 1; // this one. - - for (std::vector ::const_iterator i = _branches.begin (); - i != _branches.end (); - ++i) - { - // Recurse and count the branches. - total += (*i)->count (); - } - - return total; -} - -//////////////////////////////////////////////////////////////////////////////// -Tree* Tree::find (const std::string& path) -{ - std::vector elements; - split (elements, path, '/'); - - // Must start at the trunk. - Tree* cursor = this; - std::vector ::iterator it = elements.begin (); - if (cursor->name () != *it) - return NULL; - - // Perhaps the trunk is what is needed? - if (elements.size () == 1) - return this; - - // Now look for the next branch. - for (++it; it != elements.end (); ++it) - { - bool found = false; - - // If the cursor has a branch that matches *it, proceed. - for (int i = 0; i < cursor->branches (); ++i) - { - if ((*cursor)[i]->name () == *it) - { - cursor = (*cursor)[i]; - found = true; - break; - } - } - - if (!found) - return NULL; - } - - return cursor; -} - -//////////////////////////////////////////////////////////////////////////////// -void Tree::dumpNode (Tree* t, int depth) -{ - // Dump node - for (int i = 0; i < depth; ++i) - std::cout << " "; - - std::cout << t << " \033[1m" << t->name () << "\033[0m"; - - // Dump attributes. - std::string atts; - std::vector attributes = t->allAttributes (); - std::vector ::iterator it; - for (it = attributes.begin (); it != attributes.end (); ++it) - { - if (it != attributes.begin ()) - atts += " "; - - atts += *it + "='\033[33m" + t->attribute (*it) + "\033[0m'"; - } - - if (atts.length ()) - std::cout << " " << atts; - - // Dump tags. - std::string tags; - std::vector allTags = t->allTags (); - for (it = allTags.begin (); it != allTags.end (); ++it) - tags += (tags.length () ? " " : "") + *it; - - if (tags.length ()) - std::cout << " \033[32m" << tags << "\033[0m"; - - std::cout << "\n"; - - // Recurse for branches. - for (int i = 0; i < t->branches (); ++i) - dumpNode ((*t)[i], depth + 1); -} - -//////////////////////////////////////////////////////////////////////////////// -void Tree::dump () -{ - std::cout << "Tree (" << count () << " nodes)\n"; - dumpNode (this, 1); -} - -//////////////////////////////////////////////////////////////////////////////// - diff --git a/src/Tree.h b/src/Tree.h deleted file mode 100644 index 65d70a97e..000000000 --- a/src/Tree.h +++ /dev/null @@ -1,87 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -// taskwarrior - a command line task list manager. -// -// Copyright 2010 - 2011, Paul Beckingham, Federico Hernandez. -// All rights reserved. -// -// This program is free software; you can redistribute it and/or modify it under -// the terms of the GNU General Public License as published by the Free Software -// Foundation; either version 2 of the License, or (at your option) any later -// version. -// -// This program is distributed in the hope that it will be useful, but WITHOUT -// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more -// details. -// -// You should have received a copy of the GNU General Public License along with -// this program; if not, write to the -// -// Free Software Foundation, Inc., -// 51 Franklin Street, Fifth Floor, -// Boston, MA -// 02110-1301 -// USA -// -//////////////////////////////////////////////////////////////////////////////// -#ifndef INCLUDED_TREE -#define INCLUDED_TREE - -#include -#include -#include - -class Tree; - -class Tree -{ -public: - Tree (const std::string&); - ~Tree (); - Tree (const Tree&); - Tree& operator= (const Tree&); - Tree* operator[] (const int); - - void addBranch (Tree*); - void removeBranch (Tree*); - void replaceBranch (Tree*, Tree*); - int branches (); - - void name (const std::string&); - std::string name () const; - void attribute (const std::string&, const std::string&); - void attribute (const std::string&, const int); - void attribute (const std::string&, const double); - std::string attribute (const std::string&); - void removeAttribute (const std::string&); - int attributes () const; - std::vector allAttributes () const; - - bool hasTag (const std::string&) const; - void tag (const std::string&); - int tags () const; - std::vector allTags () const; - - void enumerate (std::vector & all) const; - Tree* parent () const; - - int count () const; - - Tree* find (const std::string&); - - void dump (); - -private: - void dumpNode (Tree*, int); - -private: - Tree* _trunk; // Parent. - std::string _name; // Name. - std::vector _branches; // Children. - std::map _attributes; // Attributes (name->value). - std::vector _tags; // Tags (tag, tag ...). -}; - -#endif - -//////////////////////////////////////////////////////////////////////////////// diff --git a/test/.gitignore b/test/.gitignore index bbc1018f4..a9d1cbf8b 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -30,8 +30,6 @@ tdb.t tdb2.t text.t transport.t -tree.t -tree2.t uri.t util.t variant.t diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 962b94a98..7437f2bf3 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -8,8 +8,7 @@ include_directories (${CMAKE_SOURCE_DIR}/src set (test_SRCS att.t autocomplete.t cmd.t color.t config.t date.t directory.t dom.t duration.t file.t filt.t json.t list.t nibbler.t path.t record.t rectangle.t rx.t seq.t subst.t t.benchmark.t t.t - taskmod.t tdb.t tdb2.t text.t tree.t tree2.t uri.t util.t - variant.t view.t + taskmod.t tdb.t tdb2.t text.t uri.t util.t variant.t view.t json_test) add_custom_target (test ./run_all DEPENDS ${test_SRCS} diff --git a/test/tree.t.cpp b/test/tree.t.cpp deleted file mode 100644 index 473fd1971..000000000 --- a/test/tree.t.cpp +++ /dev/null @@ -1,82 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -// taskwarrior - a command line task list manager. -// -// Copyright 2006 - 2011, Paul Beckingham, Federico Hernandez. -// All rights reserved. -// -// This program is free software; you can redistribute it and/or modify it under -// the terms of the GNU General Public License as published by the Free Software -// Foundation; either version 2 of the License, or (at your option) any later -// version. -// -// This program is distributed in the hope that it will be useful, but WITHOUT -// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more -// details. -// -// You should have received a copy of the GNU General Public License along with -// this program; if not, write to the -// -// Free Software Foundation, Inc., -// 51 Franklin Street, Fifth Floor, -// Boston, MA -// 02110-1301 -// USA -// -//////////////////////////////////////////////////////////////////////////////// -#include "Context.h" -#include "Tree.h" -#include "test.h" - -Context context; - -//////////////////////////////////////////////////////////////////////////////// -int main (int argc, char** argv) -{ - UnitTest ut (8); - - // Construct tree as shown above. - Tree t (""); - - Tree* b = new Tree (""); - b->attribute ("name", "c1"); - b->tag ("tag"); - t.addBranch (b); - - b = new Tree (""); - b->attribute ("name", "c2"); - t.addBranch (b); - - b = new Tree (""); - b->attribute ("name", "c3"); - t.addBranch (b); - - Tree* l = new Tree (""); - l->attribute ("name", "c4"); - - b->addBranch (l); - - // Iterate over tree. - std::vector all; - t.enumerate (all); - ut.is (all[0]->attribute ("name"), "c1", "c1"); - ut.is (all[1]->attribute ("name"), "c2", "c2"); - ut.is (all[2]->attribute ("name"), "c4", "c4"); - ut.is (all[3]->attribute ("name"), "c3", "c3"); - - all[3]->tag ("one"); - all[3]->tag ("two"); - ut.ok (all[3]->hasTag ("one"), "hasTag +"); - ut.notok (all[3]->hasTag ("three"), "hasTag -"); - - ut.is (t.count (), 5, "t.count"); - - all.clear (); - b->enumerate (all); - ut.is (all[0]->attribute ("name"), "c4", "t -> c3 -> c4"); - - return 0; -} - -//////////////////////////////////////////////////////////////////////////////// - diff --git a/test/tree2.t.cpp b/test/tree2.t.cpp deleted file mode 100644 index a888c64d9..000000000 --- a/test/tree2.t.cpp +++ /dev/null @@ -1,156 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -// taskwarrior - a command line task list manager. -// -// Copyright 2006 - 2011, Paul Beckingham, Federico Hernandez. -// All rights reserved. -// -// This program is free software; you can redistribute it and/or modify it under -// the terms of the GNU General Public License as published by the Free Software -// Foundation; either version 2 of the License, or (at your option) any later -// version. -// -// This program is distributed in the hope that it will be useful, but WITHOUT -// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more -// details. -// -// You should have received a copy of the GNU General Public License along with -// this program; if not, write to the -// -// Free Software Foundation, Inc., -// 51 Franklin Street, Fifth Floor, -// Boston, MA -// 02110-1301 -// USA -// -//////////////////////////////////////////////////////////////////////////////// -#include "Context.h" -#include "Tree.h" -#include "test.h" - -Context context; - -//////////////////////////////////////////////////////////////////////////////// -int main (int argc, char** argv) -{ - UnitTest ut (30); - - // Create the following tree: - // t - // | - // +---+---+-+-+---+---+ - // | | | | | | - // a b c d e f - - // Create a tree. - Tree t (""); - - // Create six branches. - Tree* a = new Tree (""); a->attribute ("name", "a"); - Tree* b = new Tree (""); b->attribute ("name", "b"); - Tree* c = new Tree (""); c->attribute ("name", "c"); - Tree* d = new Tree (""); d->attribute ("name", "d"); - Tree* e = new Tree (""); e->attribute ("name", "e"); - Tree* f = new Tree (""); f->attribute ("name", "f"); - - // Create two branches. - Tree* x = new Tree (""); x->attribute ("name", "x"); - Tree* y = new Tree (""); y->attribute ("name", "y"); - - // Add the six. - t.addBranch (a); - t.addBranch (b); - t.addBranch (c); - t.addBranch (d); - t.addBranch (e); - t.addBranch (f); - - // Verify tree structure. - ut.ok (a->parent () == &t, "a -> t"); - ut.ok (b->parent () == &t, "b -> t"); - ut.ok (c->parent () == &t, "c -> t"); - ut.ok (d->parent () == &t, "d -> t"); - ut.ok (e->parent () == &t, "e -> t"); - ut.ok (f->parent () == &t, "f -> t"); - ut.ok (x->parent () == NULL, "x -> NULL"); - ut.ok (y->parent () == NULL, "y -> NULL"); - - ut.ok (t.branches () == 6, "t[6]"); - - ut.diag ("---------------------------------------------------------"); - - // Modify the tree to become: - // t - // | - // +---+-+-+---+ - // | | | | - // a b x f - // | - // +---+---+ - // | | | - // c d e - - // Make x the parent of c, d and e. - x->addBranch (c); - x->addBranch (d); - x->addBranch (e); - - // Make x replace c as one of t's branches. - t.replaceBranch (c, x); - t.removeBranch (d); - t.removeBranch (e); - - // Verify structure. - ut.ok (a->parent () == &t, "a -> t"); - ut.ok (b->parent () == &t, "b -> t"); - ut.ok (c->parent () == x, "c -> x"); - ut.ok (d->parent () == x, "d -> x"); - ut.ok (e->parent () == x, "e -> x"); - ut.ok (f->parent () == &t, "f -> t"); - ut.ok (x->parent () == &t, "x -> t"); - ut.ok (y->parent () == NULL, "y -> NULL"); - - ut.ok (t.branches () == 4, "t[4]"); - ut.ok (x->branches () == 3, "x[3]"); - - ut.diag ("---------------------------------------------------------"); - - // Modify the tree to become: - // t - // | - // +---+---+ - // | | | - // a y f - // | - // +-+-+ - // | | - // b x - // | - // +---+---+ - // | | | - // c d e - - // Now insert y to be parent of b, x. - y->addBranch (b); - y->addBranch (x); - t.replaceBranch (x, y); - t.removeBranch (b); - - ut.ok (a->parent () == &t, "a -> t"); - ut.ok (b->parent () == y, "b -> y"); - ut.ok (c->parent () == x, "c -> x"); - ut.ok (d->parent () == x, "d -> x"); - ut.ok (e->parent () == x, "e -> x"); - ut.ok (f->parent () == &t, "f -> t"); - ut.ok (x->parent () == y, "x -> y"); - ut.ok (y->parent () == &t, "y -> t"); - - ut.ok (t.branches () == 3, "t[3]"); - ut.ok (x->branches () == 3, "x[3]"); - ut.ok (y->branches () == 2, "y[2]"); - - return 0; -} - -//////////////////////////////////////////////////////////////////////////////// -