// Node // // Douglas Thrift // // $Id$ #ifndef _Node_hpp_ #define _Node_hpp_ #include "Visitor.hpp" template class Node { private: Type value; protected: Node* next; Node* previous; public: Node() {} Node(const Type& value) : value(value) {} void setValue(const Type& value) { this->value = value; } const Type& getValue(void) const { return value; } void addNext(const Type& value); void addPrevious(const Type& value); void removeNext(void); void removePrevious(void); void setNext(Node* next) { this->next = next; } void setPrevious(Node* previous) { this->previous = previous; } Node* getNext(void) const { return next; } Node* getPrevious(void) const { return previous; } virtual bool contains(const Type& value, Node* end, bool forward); virtual void remove(const Type& value, Node* end, bool forward); virtual void accept(Visitor* visitor, Node* end, bool forward); }; template void Node::addNext(const Type& value) { Node* behind = next; next = new Node(value); next->previous = this; next->next = behind; behind->previous = next; } template void Node::addPrevious(const Type& value) { Node* ahead = previous; previous = new Node(value); previous->next = this; previous->previous = ahead; ahead->next = previous; } template void Node::removeNext(void) { Node* behind = next->next; delete next; next = behind; next->previous = this; } template void Node::removePrevious(void) { Node* ahead = previous->previous; delete previous; previous = ahead; previous->next = this; } template bool Node::contains(const Type& value, Node* end, bool forward) { Node* another = forward ? next : previous; if (value == this->value) return true; else return another->contains(value, end, forward); } template void Node::remove(const Type& value, Node* end, bool forward) { Node* another = forward ? next : previous; if (value == this->value) { next->previous = previous; previous->next = next; delete this; } else another->remove(value, end, forward); } template void Node::accept(Visitor* visitor, Node* end, bool forward) { Node* another = forward ? next : previous; visitor->visit(value); another->accept(visitor, end, forward); } #endif // _Node_hpp_