#ifndef Nodes_H #define Nodes_H /////////////////////////////////////////////////////////////////////////// // Nodes.h - demonstrate how to define a template node class // // ver 1.1 // // Language: Visual C++, 2008 // // Platform: Dell Precision T7400, Vista Ultimate, SP1 // // Application: CSE687 - Demo for Project #1, Spring 2009 // // Author: Jim Fawcett, Syracuse University, CST 4-187, // // (315) 443-3948, jfawcett@twcny.rr.com // /////////////////////////////////////////////////////////////////////////// /* * Module Operations: * ================== * Provides a template Node class that holds a finite number of pointers * to child nodes, and a visitation predicate. * * Build Process: * ============== * cl /D:TEST_NODES Nodes.cpp * * Maintenance History: * ==================== * ver 1.1 : 27 Jan 10 * - added private copy and assignment operations to * prevent compiler from generating incorrect versions * ver 1.0 : 23 Jan 09 * - first release */ #include namespace AbstractDataTypes { template class MNode { public: MNode(const T& t=0); void add(MNode* pMNode); bool remove(MNode* pMNode); MNode* nextUnmarkedChild(); T& value(); bool& isMarked(); // Compiler generated copy and assignment are shallow. // That makes sense for a Node class. private: std::vector< MNode* > children; T t_; bool marked_; }; template inline MNode::MNode(const T& t=0) : t_(t), marked_(false) {}; template inline void MNode::add(MNode* pMNode) { children.push_back(pMNode); } template inline T& MNode::value() { return t_; } template inline bool& MNode::isMarked() { return marked_; } template bool MNode::remove(MNode* pMNode) { std::vector< MNode* >::iterator iter = children.begin(); for(iter=children.begin(); iter!=children.end(); ++iter) { if(pMNode == *iter) { children.erase(iter); return true; } } return false; } template MNode* MNode::nextUnmarkedChild() { for(size_t i=0; iisMarked()) return children[i]; return 0; } } #endif