template <class T>
class ITERATOR_CLASS IteratorBase
{
public:
virtual bool hasNext() = 0;
virtual T next() = 0;
virtual int length() = 0;
virtual ~IteratorBase() {}
};
template <class T>
class ITERATOR_CLASS GeneralIterator
{
public:
explicit GeneralIterator(IteratorBase<T>* iter): _iter(iter) {}
GeneralIterator(const GeneralIterator& other) { this->_iter = other._iter; }
~GeneralIterator() { _iter = nullptr; }
bool hasNext() { return _iter->hasNext();}
T next() { return _iter->next(); }
int length() { return _iter->length(); }
private:
std::shared_ptr<IteratorBase<T> > _iter;
};
template <class _Key, class T >
class ITERATOR_CLASS MapIterator : public IteratorBase<T>{
const static int MAX_SUBSTRATUM = 3;
const map<_Key,T>* _substratum[MAX_SUBSTRATUM];
int _currentSubstratum;
typename map<_Key,T>::const_iterator _it;
MapIterator(const map<_Key,T>* v0, const map<_Key,T>* v1=NULL, const map<_Key,T>* v2=NULL) {
_substratum[0] = v0;
_substratum[1] = v1;
_substratum[2] = v2;
_currentSubstratum = 0;
_it = _substratum[_currentSubstratum]->begin();
}
public:
bool hasNext() {
// if(_it != _substratum[_currentSubstratum]->end()) // jdchen - 04/03/2007
if (_currentSubstratum >= MAX_SUBSTRATUM)
return false;
if (_it != (_substratum[_currentSubstratum] ? _substratum[_currentSubstratum]->end() : _it))
return true;
else {
_currentSubstratum++;
if(_currentSubstratum == MAX_SUBSTRATUM)
return false;
// _it = _substratum[_currentSubstratum]->begin(); // jdchen - 04/03/2007
_it = _substratum[_currentSubstratum] ? _substratum[_currentSubstratum]->begin() : _it;
return hasNext();
}
}
//##ModelId=45F5182502E8
T next() {
T tmp = (*_it).second;
_it++;
return tmp;
}
int length() {
int len = 0;
for(int i=0; i<MAX_SUBSTRATUM; i++) {
if(_substratum[i] != NULL)
len += (int)_substratum[i]->size();
else
break;
}
return len;
}
static GeneralIterator<T> createIterator(const map<_Key,T>* v0, const map<_Key,T>* v1=NULL, const map<_Key,T>* v2=NULL) {
return GeneralIterator<T>(new MapIterator<_Key, T>(v0, v1, v2));
}
};
GeneralIterator<Block*> Network::getAllBlocks() const
{
return MapIterator<AString, Block*>::createIterator(&_blocks);
}
template <typename T ,class Compare = less<T> >
class ITERATOR_CLASS SetIterator : public IteratorBase<T>
{
const static int MAX_SUBSTRATUM = 3;
const set<T, Compare>* _substratum[MAX_SUBSTRATUM];
int _currentSubstratum;
typename set<T, Compare>::const_iterator _it;
SetIterator(const set<T, Compare>* v0, const set<T, Compare>* v1=NULL, const set<T, Compare>* v2=NULL) {
_substratum[0] = v0;
_substratum[1] = v1;
_substratum[2] = v2;
_currentSubstratum = 0;
_it = _substratum[_currentSubstratum]->begin();
}
//MapIterator(const MapIterator& other)
//{
// _substratum[0] = other._substratum[0];
// _substratum[1] = other._substratum[1];
// _substratum[2] = other._substratum[2];
// _currentSubstratum = 0;
// _it = _substratum[_currentSubstratum]->begin();
//}
public:
bool hasNext() {
// if(_it != _substratum[_currentSubstratum]->end()) // jdchen - 04/03/2007
if (_currentSubstratum == MAX_SUBSTRATUM)
return false;
if (_it != (_substratum[_currentSubstratum] ? _substratum[_currentSubstratum]->end() : _it))
return true;
else {
_currentSubstratum++;
if(_currentSubstratum == MAX_SUBSTRATUM)
return false;
// _it = _substratum[_currentSubstratum]->begin(); // jdchen - 04/03/2007
_it = _substratum[_currentSubstratum] ? _substratum[_currentSubstratum]->begin() : _it;
return hasNext();
}
}
//##ModelId=45F5182502E8
T next() {
T tmp = (*_it);
_it++;
return tmp;
}
int length() {
int len = 0;
for(int i=0; i<MAX_SUBSTRATUM; i++) {
if(_substratum[i] != NULL)
len += (int)_substratum[i]->size();
else
break;
}
return len;
}
static GeneralIterator<T> createIterator(const set<T, Compare>* v0, const set<T,Compare>* v1=NULL, const set<T,Compare>* v2=NULL) {
return GeneralIterator<T>(new SetIterator<T, Compare>(v0, v1, v2));
}
};