#ifndef COLLECTION_H #define COLLECTION_H ///////////////////////////////////////////////////////////////////// // collection.h - defines collection of name, value pairs // // ver 1.1 // // Language: Visual C++, ver 7.0 // // Platform: Dell Dimension 8300, Windows XP Pro, SP1.0 // // Application: CSE687 - Object Oriented Design, Pr#3 prototype // // Author: Jim Fawcett, CST 2-187, Syracuse University // // (315) 443-3948, jfawcett@twcny.rr.com // ///////////////////////////////////////////////////////////////////// /* * Module Operations: * ================== * This module defines class collection that holds and * discloses Name, Value pairs. * * Public Interface: * ================= * collection fingers; * fingers.push_back("thumb",1); * fingers.push_back("index",2); * : * std::string finger; * int count; * while(fingers.next(finger,count)) * cout << "\n " << finger << " - " << count; * * * Build Process: * ============== * Required files: collection.h, collection.cpp * * Build command: cl /EHsc /DTEST_COLLECTION collection.cpp * * Maintenance History: * ==================== * ver 1.0 : 28 Mar 04 * - first release * ver 1.1 : 06 Apr 04 * - fixed wrong way increments in operator--(int), reported by * Pritesh Valenkar. Also added begin() member. * * Planned Modifications: * ====================== * It was a bad idea to use begin() and end() to return bools * instead of iterators like all the STL. I will rename these * functions when no one is using this class in a design. */ // #include template class collection { public: collection(); collection(const collection& c); collection& push_back(const Name& n, const Value& v); collection& push_front(const Name& n, const Value& v); collection& insert(const Name& n, const Value& v); collection& front(); collection& back(); bool next(Name& n, Value& v); collection& item(Name& n, Value& v); collection& name(Name& n); collection& value(Value& v); collection& operator=(const collection& c); collection& operator++(); collection operator++(int); collection& operator--(); collection operator--(int); bool begin(); // change to isBegin() bool end(); // change to isEnd() size_t size(); private: std::list names; typename std::list::iterator nIter; std::list values; typename std::list::iterator vIter; }; //----< void constructor >------------------------------------------- template collection::collection() { nIter = names.begin(); vIter = values.begin(); } //----< copy constructor >------------------------------------------- template collection::collection(const collection& c) { std::list::const_iterator _nIter = c.names.begin(); std::list::const_iterator _vIter = c.values.begin(); for(_nIter=c.names.begin(); _nIter!=c.names.end(); ++_nIter) { names.push_back(*_nIter); values.push_back(*_vIter++); } nIter = names.begin(); vIter = values.begin(); } // //----< assignment >------------------------------------------------- template collection& collection::operator=(const collection& c) { if(this == &c) return *this; names.clear(); values.clear(); std::list::const_iterator _nIter = c.names.begin(); std::list::const_iterator _vIter = c.values.begin(); for(_nIter=c.names.begin(); _nIter!=c.names.end(); ++_nIter) { names.push_back(*_nIter); values.push_back(*_vIter++); } nIter = names.begin(); vIter = values.begin(); return *this; } //----< move to front of collection >-------------------------------- template collection& collection::front() { nIter = names.begin(); vIter = values.begin(); return *this; } //----< move to back of collection >--------------------------------- template collection& collection::back() { nIter = names.end(); vIter = values.end(); return *this; } //----< returns Name and Value references then increments >---------- template bool collection::next(Name& n, Value& v) { if(nIter == names.end()) return false; n = *nIter++; v = *vIter++; return true; } // //----< insert item at current location >---------------------------- template collection& collection::insert(const Name& n, const Value& v) { names.insert(nIter,n); values.insert(vIter,v); return *this; } //----< push item on back >------------------------------------------ template collection& collection::push_back(const Name& n, const Value& v) { names.push_back(n); values.push_back(v); nIter = names.begin(); vIter = values.begin(); return *this; } //----< push item on front >----------------------------------------- template collection& collection::push_front(const Name& n, const Value& v) { names.push_front(n); values.push_front(v); nIter = names.begin(); vIter = values.begin(); return *this; } // //----< return item Name and Value references >---------------------- template collection& collection::item(Name& name, Value& value) { name = *nIter; value = *vIter; return *this; } //----< return item Name reference >--------------------------------- template collection& collection::name(Name& n) { n = *nIter; return *this; } //----< return item value reference >-------------------------------- template collection& collection::value(Value& v) { v = *vIter; return *this; } // //----< pre-increment >---------------------------------------------- template collection& collection::operator++() { ++nIter; ++vIter; return *this; } //----< post-increment >--------------------------------------------- template collection collection::operator++(int) { collection temp = *this; ++nIter; ++vIter; return temp; } //----< pre-decrement >---------------------------------------------- template collection& collection::operator--() { --nIter; --vIter; return *this; } //----< post-decrement >--------------------------------------------- template collection collection::operator--(int) { collection temp = *this; --nIter; --vIter; return temp; } //----< at beginning? >---------------------------------------------- template bool collection::begin() { return (nIter == names.begin()); } //----< at end? >---------------------------------------------------- template bool collection::end() { return (nIter == names.end()); } //----< return size of collection >---------------------------------- template size_t collection::size() { return names.size(); } #endif