Skip to content

Commit

Permalink
Iterator over ancestors of a node (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaud-m committed Jun 12, 2012
1 parent e66002e commit b049a54
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ LinkIterator FacilityNode::lend() {
return LinkIterator(NULL);
}

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

AncestorIterator FacilityNode::aend() {
return AncestorIterator(NULL);
}


ostream & FacilityNode::toDotty(ostream & out) {
out << getID();
Expand Down Expand Up @@ -403,6 +412,15 @@ NodeIterator& NodeIterator::operator++() {
}


//----------------------------------------
// AncestorIterator Implementation
//----------------------------------------

AncestorIterator& AncestorIterator::operator++() {
node = node == NULL || node->isRoot() ? NULL : node->getFather();
return (*this);
}

//----------------------------------------
// istream methods Implementation
//----------------------------------------
Expand Down
58 changes: 57 additions & 1 deletion src/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class FacilityNode;
class NetworkLink;
class LinkIterator;
class NodeIterator;
class AncestorIterator;
class PSLProblem;

typedef vector<unsigned int> IntList;
Expand Down Expand Up @@ -261,6 +262,10 @@ class FacilityNode {
LinkIterator lbegin();
LinkIterator lend();

//For AncestorIterator
AncestorIterator abegin();
AncestorIterator aend();

private:
unsigned int id;
FacilityType* type;
Expand Down Expand Up @@ -424,9 +429,59 @@ class NodeIterator : public std::iterator<std::forward_iterator_tag, FacilityNod

};

//----------------------------------------
// AncestorIterator Declaration
//----------------------------------------

class AncestorIterator : public std::iterator<std::forward_iterator_tag, FacilityNode> {
public:
AncestorIterator(FacilityNode* p) : node( p) {
++(*this);
}

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

AncestorIterator(const AncestorIterator& other) : node(other.node) {}

// The assignment and relational operators are straightforward
AncestorIterator& operator=(const AncestorIterator& other) {
if(*this != other) {
node = other.node;
}
return *this;
}

bool operator==(const AncestorIterator& other) {
return (node == other.node);
}

bool operator!=(const AncestorIterator& other) {
return (node != other.node);
}

AncestorIterator& operator++();

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

FacilityNode* operator*() {
return node;
}

FacilityNode* operator->() {
return node;
}

private:
FacilityNode* node;

};
//----------------------------------------
// PSLProblem Declaration
//----------------------------------------

Expand Down Expand Up @@ -575,6 +630,7 @@ class PSLProblem {
inline int endZij() const {
return endYij() + pathCount() * stageCount();
}

inline int endBij() const {
return endZij() + pathCount() * stageCount();
}
Expand Down
50 changes: 50 additions & 0 deletions test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,56 @@ BOOST_AUTO_TEST_CASE(NetworkIterators)
}
BOOST_CHECK(count_nodes == problem->nodeCount());

/////////////////////////////////////////////////
// UnitTest AncestorIterator
////////////////////////////////////////////////

AncestorIterator itA = problem->getRoot()->getChild(0)->getChild(0)->abegin();
//Copy ctor
//
AncestorIterator itA_copy(itA);
BOOST_CHECK(itA == itA_copy);

//Operator =
AncestorIterator itA_copy2 = problem->getRoot()->getChild(0)->getChild(0)->abegin();
itA_copy2 = itA;
BOOST_CHECK(itA == itA_copy2);

//Operator ++
//
itA++;
itA_copy++;
BOOST_CHECK(itA == itA_copy);

//Operator !=
//
BOOST_CHECK(itA != itA_copy2);
BOOST_CHECK(itA_copy != itA_copy2);

//Operator *
//
BOOST_CHECK(*itA == *itA_copy);
BOOST_CHECK(*itA != *(itA_copy2));

//Operator ->
//
BOOST_CHECK(itA->getID() == itA_copy->getID());

//Count ancestors
int count_ancestors = 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++;
}
BOOST_CHECK(count_ancestors == 2);

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

}

BOOST_AUTO_TEST_CASE(networkGeneration)
Expand Down

0 comments on commit b049a54

Please sign in to comment.