diff options
-rw-r--r-- | src/tasktree.cc | 96 | ||||
-rw-r--r-- | src/tasktree.h | 2 |
2 files changed, 55 insertions, 43 deletions
diff --git a/src/tasktree.cc b/src/tasktree.cc index d652eae..4d6099e 100644 --- a/src/tasktree.cc +++ b/src/tasktree.cc @@ -29,6 +29,8 @@ #include "debug.h" +#define ROOT_PARENT_ID -1 + static void concatTaskIdLists(TaskIdList& pre, TaskIdList& post) { pre.insert(pre.end(), post.begin(), post.end()); // for(TaskIdList::iterator it = post.begin(); @@ -45,47 +47,48 @@ TaskTree::~TaskTree() { // cleanup tree } +static taskid_t rootid = -1; + TaskIdList TaskTree::insertAsChild(taskid_t parentid, taskid_t id, task_t data) - throw (std::exception) { + throw (std::exception) +{ TaskIdList affectedNodes; // Initialize if(!root) { + rootid = id; node_t* node = createNode(id); root = node; node->data = data; - affectedNodes.push_back(id); - - goto finish; - } - try { - node_t* parent = id2node.at(parentid); - node_t* child = createNode(id); - child->data = data; - insertChild(parent, child); + } else { - // affectedNodes.push_back(parentid); - affectedNodes.push_back(id); - TaskIdList ancestors = ancestorList(id); - concatTaskIdLists(affectedNodes, ancestors); - - goto finish; - } - catch(std::exception& e) { - throw e; + try { + node_t* parent = id2node.at(parentid); + node_t* child = createNode(id); + child->data = data; + insertChild(parent, child); + + // affectedNodes.push_back(parentid); + affectedNodes.push_back(id); + TaskIdList ancestors = ancestorList(id); + concatTaskIdLists(affectedNodes, ancestors); + } + catch(std::exception& e) { + throw e; + } } - - finish: - printf("Child %d added to %d, affecting %d nodes\n", - id, parentid, affectedNodes.size()); + +// printf("Child %d added to %d, affecting %d nodes\n", +// id, parentid, affectedNodes.size()); return affectedNodes; } TaskIdList TaskTree::remove(taskid_t id) - throw (std::exception) { + throw (std::exception) +{ //todo: move all childrin to lost+found WARN(tasktree, "Feature not implemneted yet\n"); TaskIdList affectedNodes; @@ -105,14 +108,15 @@ TaskIdList TaskTree::remove(taskid_t id) } TaskIdList TaskTree::move(taskid_t id, taskid_t toid) - throw (std::exception) { + throw (std::exception) +{ TaskIdList affectedNodes; try { node_t* child = id2node.at(id); node_t* newparent = id2node.at(toid); - + if(!child->parent) { throw std::exception(); } @@ -126,19 +130,17 @@ TaskIdList TaskTree::move(taskid_t id, taskid_t toid) concatTaskIdLists(affectedNodes, ancestors); affectedNodes.push_back(toid); ancestors = ancestorList(toid); - - goto finish; } catch(std::exception& e) { throw e; } - - finish: + return affectedNodes; } TaskIdList TaskTree::updateData(taskid_t id, task_t t) - throw (std::exception) { + throw (std::exception) +{ TaskIdList affectedNodes; @@ -149,37 +151,41 @@ TaskIdList TaskTree::updateData(taskid_t id, task_t t) affectedNodes.push_back(id); TaskIdList ancestors = ancestorList(id); concatTaskIdLists(affectedNodes, ancestors); - goto finish; } catch(std::exception& e) { throw e; } - finish: return affectedNodes; } -task_t TaskTree::getData(taskid_t id) - throw (std::exception) { +task_t TaskTree::data(taskid_t id) + throw (std::exception) +{ task_t t; try { node_t* node = id2node.at(id); t = node->data; - goto finish; + t.id = node->id; + if(node->parent) t.parentid = node->parent->id; + else { + if(t.id != rootid) throw std::exception(); + t.parentid = -1; + } } catch(std::exception& e) { throw e; } - - finish: + return t; } // bfs search from id in tree TaskIdList TaskTree::bfs(taskid_t id) - throw (std::exception) { + throw (std::exception) +{ TaskIdList lst; lst.push_back(id); @@ -207,7 +213,8 @@ TaskIdList TaskTree::bfs(taskid_t id) } TaskIdList TaskTree::ancestorList(taskid_t id) - throw (std::exception) { + throw (std::exception) +{ TaskIdList ancestors; @@ -217,13 +224,11 @@ TaskIdList TaskTree::ancestorList(taskid_t id) ancestors.push_back(current->parent->id); current = current->parent; } - goto finish; } catch(std::exception& e) { throw e; } - finish: printf("Collected %d ancestors to %u\n", ancestors.size(), id); for(TaskIdList::iterator it = ancestors.begin(); it != ancestors.end(); it++) { @@ -288,21 +293,28 @@ TaskTree tree; task_t t; t.title = "root"; +t.id = ROOT_ID; tree.insertAsChild(0, ROOT_ID, t); t.title = "Finished"; +t.id = FINISHED_ID; tree.insertAsChild(ROOT_ID, FINISHED_ID, t); t.title = "Backlog"; +t.id = BACKLOG_ID; tree.insertAsChild(ROOT_ID, BACKLOG_ID, t); t.title = "Lost+Found"; +t.id = LOSTFOUND_ID; tree.insertAsChild(ROOT_ID, LOSTFOUND_ID, t); t.title = "Projects"; +t.id = PROJECTS_ID; tree.insertAsChild(ROOT_ID, PROJECTS_ID, t); TEST_EQUAL_INT(5, tree.bfs(0).size(), "Testing BFS function"); +TEST_EQUAL_INT(PROJECTS_ID, tree.data(PROJECTS_ID).id, "Testing project id"); +TEST_EQUAL_INT(ROOT_ID, tree.data(ROOT_ID).id, "Testing root id"); TEST_END; diff --git a/src/tasktree.h b/src/tasktree.h index aeeddfc..d82b9a4 100644 --- a/src/tasktree.h +++ b/src/tasktree.h @@ -54,7 +54,7 @@ public: TaskIdList remove(taskid_t id) throw (std::exception); TaskIdList move(taskid_t id, taskid_t newParentId) throw (std::exception); TaskIdList updateData(taskid_t id, task_t t) throw (std::exception); - task_t getData(taskid_t id) throw (std::exception); + task_t data(taskid_t id) throw (std::exception); TaskIdList bfs(taskid_t id) throw (std::exception); |