#ifndef NODEPOOL_H #define NODEPOOL_H /////////////////////////////////////////////////////////////// // nodePool.h - class that manages a pool of nodes // // ver 2.1 // // Language: Visual C++, ver 6.0 // // Platform: Dell Dimension 8100, Win2000, SP 2.0 // // Application: CSE691 - Project #3 prototype // // Author: Jim Fawcett, CST 2-187, Syracuse Univ // // (315) 443-3948, jfawcett@twcny.rr.com // /////////////////////////////////////////////////////////////// /* Class Operation =============== An object of the nodePool class recycles nodes used by the mtqueue class. When an mtqueue object deQs a node, it hands the node to the nodePool object for safekeeping. When it enQs a node it retrieves the node from the nodePool object. The nodePool object saves recycled nodes on a singly-linked list. If mtqueue requests a node when the free list is empty the nodePool object creates one from the heap. Public Interface: ================= nodePool np; // create nodePool object with empty free list saveNode(pNode); // add node to free list node *pNode = retrieveNode(); // pop node of free list or create it */ /////////////////////////////////////////////////////////////// // build process // /////////////////////////////////////////////////////////////// // required files: // // nodePool.h, nodePool.cpp // // // // command line compile: // // cl /GX /GZ /DTEST_NODEPOOL nodePool.cpp // /////////////////////////////////////////////////////////////// /* Maintenance History: ==================== ver 2.1 : 09 Oct 2001 - made minor changes to work with greatly revised mtqueue class. Removed returned value references. - that required addition of three node members: node::setValue(const T &t), node::setNext(node *newNP), and node::setPrev(node *newNP) ver 2.0 : 11 Mar 2001 - added flush() to delete all nodes in free pool ver 1.0 : 31 Jan 2000 - first release */ // /////////////////////////////////////////////////////////////// // class declaration for node // /////////////////////////////////////////////////////////////// template class node { public: node(const T &t, node *link); ~node(); T& value() ; void setValue(const T &val) ; node* next() ; void setNext(node *newNP) ; node* prev() ; void setPrev(node *newNP) ; private: node *_prev; node *_next; T _t; }; //----< node constructor >------------------------------------- // // construct node, give it a value, and link to // head of the list // template node::node(const T &t, node *link) : _t(t), _prev(NULL), _next(link) { // _t is stored value // _prev is at head of list so there is no prev // _next is link to old first node } //----< destructor doesn't need to do anything >--------------- template inline node::~node() { } // //----< return node's value >---------------------------------- // // must return by reference so new value can be // written to _t as well as read from _t // template inline T& node::value() { return _t; } //----< set node's value >------------------------------------- template inline void node::setValue(const T &val) { _t = val; } //----< get pointer to previous node >------------------------- // // must return reference so prev() can write a new value // to _prev as well as read the value from _prev // template inline node* node::prev() { return _prev; } //----< get pointer to next node >----------------------------- // // must return reference so next() can write a new value // to _next as well as return the value from _next // template inline node* node::next() { return _next; } //----< set node's prev pointer >------------------------------ template inline void node::setPrev( node *newNP) { _prev = newNP; } //----< set node's next pointer >------------------------------ template inline void node::setNext( node *newNP) { _next = newNP; } // /////////////////////////////////////////////////////////////// // declarations for class nodePool // /////////////////////////////////////////////////////////////// template class nodePool { public: nodePool(); ~nodePool(); void saveNode(node *pNode) ; node* retrieveNode(const T &t, node *link) ; int size() ; void flush() ; private: node *free; int _size; }; //----< return number of saved nodes >------------------------- template inline int nodePool::size() { return _size; } //----< void constructor >------------------------------------- template nodePool::nodePool() : free(NULL), _size(0) { } //----< flush node pool >-------------------------------------- template inline void nodePool::flush() { while(free != NULL) { node *pNode = free->next(); delete free; free = pNode; --_size; } } //----< destructor >------------------------------------------- template nodePool::~nodePool() { flush(); } // //----< node saver >------------------------------------------- template void nodePool::saveNode( node *pNode) { pNode->setNext(free); free = pNode; _size++; } //----< node retriever >--------------------------------------- template node* nodePool::retrieveNode(const T &t, node *link) { if(free == NULL) return new node(t,link); _size--; node *pNode = free; free = free->next(); pNode->setNext(link); pNode->setValue(t); return pNode; } #endif