#ifndef COLLECTIONS_H #define COLLECTIONS_H ///////////////////////////////////////////////////////////////// // collections.h - prototype to test design ideas // // // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2002 // ///////////////////////////////////////////////////////////////// // // Manual and Maintenance pages go here if we decide // that this class is useful enough // #pragma warning(disable : 4786) #include template class collection { public: typedef std::vector::iterator iterator; collection(); void push_back(const T &t); iterator& insertElem(const T &t); iterator& insertElem(const T &t, iterator &it); iterator& deleteElem(); iterator& deleteElem(const iterator &it); iterator& begin(); iterator& end(); bool next(T &t); T& value(); iterator& getIt(); private: std::vector _coll; std::vector::iterator _it; }; // //----< constructor >------------------------------------------ template collection::collection() : _it(_coll.begin()) {} //----< push an element onto end of collection >--------------- // // internal iterator points to the new element // template void collection::push_back(const T &t) { _coll.push_back(t); _it = _coll.end(); _it--; } //----< insert an element in collection >---------------------- // // - inserts at current location, defined by internal iterator // - internal iterator will point to inserted item // template collection::iterator& collection::insertElem(const T&t) { if(_it < _coll.begin()) _it = _coll.begin(); else if(_it > _coll.end()) _it = _coll.end(); return (_it = _coll.insert(_it,t)); } //----< insert an element in collection >---------------------- // // - insert just before the item pointed to by it // - internal iterator points to the inserted item // template collection::iterator& collection::insertElem(const T&t, iterator &it) { return (_it = _coll.insert(it,t)); } // //----< delete an element from collection >-------------------- // // - delete item pointed to by an internal iterator if valid // otherwise return coll.end(); // - internal iterator will point to item that followed the // deleted item in the original collection // template collection::iterator& collection::deleteElem() { if(_coll.begin() <= _it && _it < _coll.end()) return (_it = _coll.erase(_it)); return _it; } //----< delete an element from collection >-------------------- // // - delete item pointed to by it // - internal iterator points to item that followed the // deleted item in the original collection // template collection::iterator& collection::deleteElem(const iterator &it) { return (_it = _coll.erase(it)); } //----< point to beginning of collection >--------------------- template collection::iterator& collection::begin() { return (_it = _coll.begin()); } //----< point to end of collection >--------------------------- template collection::iterator& collection::end() { return (_it = _coll.end()); } // //----< move to next element of collection >------------------- // // - assigns the next element value to t and returns true if // operation results in a valid element // - otherwise returns false and does not modify t // template bool collection::next(T &t) { ++_it; if(_it != _coll.end()) t = *_it; return (_it != _coll.end()); } //----< return value of current element >---------------------- // // - returns element pointed to by an internal iterator // if valid // - otherwise returns a safe but meaningless value // template T& collection::value() { static T safe; if(_coll.begin() <= _it && _it < _coll.end()) return *_it; return safe; } //----< return current iterator >------------------------------ template collection::iterator& collection::getIt() { return _it; } #endif