Skip to content

Commit

Permalink
Add an iterator over the paths in a subtree defined by its root node
Browse files Browse the repository at this point in the history
(ctor parameter) (#16).
  • Loading branch information
arnaud-m committed Jun 14, 2012
1 parent 105b228 commit e604b55
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 9 deletions.
29 changes: 27 additions & 2 deletions src/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ AncestorIterator FacilityNode::aend() {
return AncestorIterator(NULL);
}

//For AncestorIterator
PathIterator FacilityNode::pbegin() {
return PathIterator(this);
}

PathIterator FacilityNode::pend() {
return PathIterator(NULL);
}


ostream & FacilityNode::toDotty(ostream & out) {
out << getID();
Expand Down Expand Up @@ -381,7 +390,7 @@ LinkIterator::LinkIterator(FacilityNode* p) : current(NULL), end(NULL) {
}

LinkIterator& LinkIterator::operator ++() {
queue.push_back((*current)->getDestination());
queue.push_back((*current)->getDestination()); //TODO Do not add leaves ?
current++;
while (current == end && !queue.empty()) {
current = queue.front()->cbegin();
Expand Down Expand Up @@ -411,7 +420,6 @@ NodeIterator& NodeIterator::operator++() {
return (*this);
}


//----------------------------------------
// AncestorIterator Implementation
//----------------------------------------
Expand All @@ -421,6 +429,23 @@ AncestorIterator& AncestorIterator::operator++() {
return (*this);
}

//----------------------------------------
// PathIterator Implementation
//----------------------------------------

PathIterator& PathIterator::operator++() {
if (cdest != edest) cdest++;
while (cdest == edest && cnode != enode) {
cnode++;
cdest = cnode->nbegin();
cdest++;
edest = cnode->nend();
}
return (*this);
}



//----------------------------------------
// istream methods Implementation
//----------------------------------------
Expand Down
64 changes: 64 additions & 0 deletions src/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class NetworkLink;
class LinkIterator;
class NodeIterator;
class AncestorIterator;
class PathIterator;
class PSLProblem;

typedef vector<unsigned int> IntList;
Expand Down Expand Up @@ -266,6 +267,10 @@ class FacilityNode {
AncestorIterator abegin();
AncestorIterator aend();

//For PathIterator
PathIterator pbegin();
PathIterator pend();

private:
unsigned int id;
FacilityType* type;
Expand Down Expand Up @@ -480,6 +485,65 @@ class AncestorIterator : public std::iterator<std::forward_iterator_tag, Facilit
private:
FacilityNode* node;

};

//----------------------------------------
// PathIterator Declaration
//----------------------------------------

class PathIterator : public std::iterator<std::forward_iterator_tag, pair<FacilityNode*, FacilityNode*> > {
public:
PathIterator(FacilityNode* p) : cnode(p->nbegin()), enode(p->nend()), cdest(p->nbegin()), edest(p->nend()) {
cdest++;
}

//Destructor of PathIterator
//Do not delete pointers of iterator
//
~PathIterator() {}

PathIterator(const PathIterator& other) : cnode(other.cnode), enode(other.enode), cdest(other.cdest), edest(other.edest) {}

// The assignment and relational operators are straightforward
PathIterator& operator=(const PathIterator& other) {
if(*this != other) {
cnode= other.cnode;
enode= other.enode;
cdest = other.cdest;
edest = other.edest;
}
return *this;
}

bool operator==(const PathIterator& other) {
return (cnode == other.cnode) && (cdest== other.cdest);
}

bool operator!=(const PathIterator& other) {
return (cnode != other.cnode) || (cdest!= other.cdest);
}

PathIterator& operator++();

PathIterator& operator++(int) {
++(*this);
return *this;
}

pair<FacilityNode*, FacilityNode*> operator*() {
return pair<FacilityNode*, FacilityNode*>(*cnode, *cdest);
}

pair<FacilityNode*, FacilityNode*> operator->() {
return *(*this);
}

private:
NodeIterator cnode;
NodeIterator enode;
NodeIterator cdest;
NodeIterator edest;

};
//----------------------------------------
// PSLProblem Declaration
Expand Down
24 changes: 17 additions & 7 deletions test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,20 +172,30 @@ BOOST_AUTO_TEST_CASE(NetworkIterators)
BOOST_CHECK(itA->getID() == itA_copy->getID());

//Count ancestors
int count_ancestors = 0;
int count = 0;
for( AncestorIterator i = problem->getRoot()->getChild(0)->getChild(0)->abegin();i != problem->getRoot()->getChild(0)->getChild(0)->aend();i++) {
cout << **i << endl;
count_ancestors++;
count++;
}
BOOST_CHECK(count_ancestors == 2);
BOOST_CHECK(count == 2);

//Count root ancestors
count_ancestors = 0;
count = 0;
for( AncestorIterator i = problem->getRoot()->abegin();i != problem->getRoot()->aend();i++) {
count_ancestors++;
count++;
}
BOOST_CHECK(count_ancestors == 0);
BOOST_CHECK(count == 0);


/////////////////////////////////////////////////
// UnitTest PathIterator
////////////////////////////////////////////////
//Count paths
count = 0;
for( PathIterator i = problem->getRoot()->pbegin();i != problem->getRoot()->pend();i++) {
// cout << *(*i).first << " -> "<< *(*i).second << endl;
count++;
}
BOOST_CHECK(count == problem->pathCount());
}

BOOST_AUTO_TEST_CASE(networkGeneration)
Expand Down

0 comments on commit e604b55

Please sign in to comment.