Code Cleanup

- Removed unused Tree code.
This commit is contained in:
Paul Beckingham 2011-05-23 22:29:24 -04:00
parent 8e4a757200
commit 3f58e5a2ee
7 changed files with 1 additions and 672 deletions

View file

@ -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

View file

@ -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 <algorithm>
#include <iostream>
#include <text.h>
#include <Tree.h>
////////////////////////////////////////////////////////////////////////////////
// - 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 <Tree*>::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 <Tree*>::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<std::string, std::string>::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 <std::string> Tree::allAttributes () const
{
std::vector <std::string> names;
std::map <std::string, std::string>::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 <Tree*>& all) const
{
for (std::vector <Tree*>::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 <std::string> Tree::allTags () const
{
return _tags;
}
////////////////////////////////////////////////////////////////////////////////
int Tree::count () const
{
int total = 1; // this one.
for (std::vector <Tree*>::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 <std::string> elements;
split (elements, path, '/');
// Must start at the trunk.
Tree* cursor = this;
std::vector <std::string>::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 <std::string> attributes = t->allAttributes ();
std::vector <std::string>::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 <std::string> 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);
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -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 <map>
#include <vector>
#include <string>
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 <std::string> allAttributes () const;
bool hasTag (const std::string&) const;
void tag (const std::string&);
int tags () const;
std::vector <std::string> allTags () const;
void enumerate (std::vector <Tree*>& 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 <Tree*> _branches; // Children.
std::map <std::string, std::string> _attributes; // Attributes (name->value).
std::vector <std::string> _tags; // Tags (tag, tag ...).
};
#endif
////////////////////////////////////////////////////////////////////////////////

2
test/.gitignore vendored
View file

@ -30,8 +30,6 @@ tdb.t
tdb2.t
text.t
transport.t
tree.t
tree2.t
uri.t
util.t
variant.t

View file

@ -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}

View file

@ -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 <Tree*> 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;
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -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;
}
////////////////////////////////////////////////////////////////////////////////