21 #ifndef SYS_VECTOR_HH_ 22 #define SYS_VECTOR_HH_ 54 iter(cont_par.storage.end()) {}
66 if (it == NULL)
iter = cont_par.storage.end();
67 else iter = cont_par.storage.begin() +
cont.index(it);
72 {
return this->
iter != this->
cont.storage.end(); }
79 {
if (this->
iter != this->
cont.storage.end())
return this->
iter -
cont.storage.begin();
98 {
return &(*(this->
iter)); }
102 {
return *(this->
iter); }
106 {
return &(*(this->
iter)); }
109 { this->
iter+=shift;
return (*
this); }
112 { this->
iter-=shift;
return (*
this); }
125 ++(this->
iter);
return *
this;
134 OLD_ASSERT(
iter !=
cont.storage.begin(),
"Can not advance iterator to previous of begin().\n");
135 this->
iter--;
return *
this;
146 typename Cont :: iterator
iter;
170 {
return this->
cont.id_storage[ this->
index() ]; }
222 Vector(
unsigned int size = 0) : storage(0)
223 {this->reserve(size);}
232 storage.resize( storage.size() + 1 );
233 return FullIter( *
this, storage.begin() + storage.size() - 1 );
239 inline unsigned int index(Iter pointer)
const 241 OLD_ASSERT( pointer >= &(storage.front()) && pointer <= &(storage.back()),
242 "Wrong pointer %d to obtain its index (%d, %d).\n",pointer, &(storage.front()), &(storage.back()));
243 return ( pointer - &(storage.front()) );
248 {
return FullIter( *
this, storage.begin() ); }
252 {
return FullIter( *
this, storage.end() ); }
255 inline unsigned int size()
const 256 {
return storage.size(); }
259 inline void resize(
const unsigned int size)
260 { storage.resize(size); }
266 OLD_ASSERT( idx < this->size(),
"Index %d outside of Vector of size %d\n",idx, this->size());
273 OLD_ASSERT( idx < this->size(),
"Index %d outside of Vector of size %d\n",idx, this->size());
281 OLD_ASSERT( idx < this->size(),
"Index %d outside of Vector of size %d\n",idx, this->size());
282 return FullIter( *
this, begin()+idx );
289 OLD_ASSERT( size >= this->size(),
"Vector can not be reallocated into space %d smaller then its size %d\n",size,this->size());
290 storage.reserve(size);
297 {
return FullIter(*
this, it); }
352 VectorId(
unsigned int size = 0) : storage(0), id_storage(0)
353 {this->reserve(size);}
361 OLD_ASSERT( id_map.find(
id) == id_map.end(),
"Can not add item with id number %d since it already exists.", id);
362 id_storage.push_back(
id);
363 id_map[id]=this->size();
365 this->storage.resize( this->storage.size() + 1 );
366 return FullIter( *
this, this->storage.begin() + this->storage.size() - 1 );
373 inline unsigned int index(
const T * pointer)
const 375 OLD_ASSERT( pointer >= &(storage.front()) && pointer <= &(storage.back()),
376 "Wrong pointer %p to obtain its index (%p, %p).\n",pointer, &(storage.front()), &(storage.back()));
377 return ( pointer - &(storage.front()) );
384 {
return FullIter( *
this, storage.begin() ); }
388 {
return FullIter( *
this, storage.end() ); }
391 inline unsigned int size()
const 392 {
return storage.size(); }
397 OLD_ASSERT( idx < this->size(),
"Index %d outside of Vector of size %d\n",idx, this->size());
405 OLD_ASSERT( idx < this->size(),
"Index %d outside of Vector of size %d\n",idx, this->size());
413 OLD_ASSERT( idx < this->size(),
"Index %d outside of Vector of size %d\n",idx, this->size());
414 return FullIter( *
this, begin()+idx );
420 OLD_ASSERT( idx < this->size(),
"Index %d outside of Vector of size %d\n",idx, this->size());
421 return Iter( &(storage[idx]) );
429 {
return FullIter(*
this, it); }
437 if ( iter == id_map.end() ) {
438 return FullIter(*
this, this->storage.end());
440 return FullIter(*
this, this->storage.begin() + iter->second);
449 if ( iter == id_map.end() ) {
450 return &(*(this->storage.end()));
452 return &(*(this->storage.begin() + iter->second));
460 return *(id_storage.begin() + this->
index(it));
465 return *(id_storage.begin() + idx);
471 OLD_ASSERT( size >= this->size(),
"Vector can not be reallocated into space %d smaller then its size %d\n",size,this->size());
472 storage.reserve(size);
473 id_storage.reserve(size);
FullIter begin()
Returns FullFullIterer of the first element.
VectorId(unsigned int size=0)
Construct with reserve the space. Includes default constructor.
unsigned int size() const
Returns size of the container. This is independent of the allocated space.
std::vector< T >::const_iterator const_iterator
T ElementType
We have to export template type for FullIteraror.
FullIter operator()(unsigned int idx)
Gets iterator of the element specified by index.
const T * find_id(const int id) const
std::vector< T > storage
Underlying vector container.
int get_id(const T *it) const
Envelop over std::vector, use enhanced iterators.
IterConvert< Object, Object > Iter
General iterator template.
int get_id(int idx) const
FullIterator & operator+=(const int shift)
bool operator!=(const FullIterator &it) const
Small extension of Vector<T> container with support to Id numbers.
T ElementType
We have to export template type for FullIteraror.
void resize(const unsigned int size)
Returns size of the container. This is independent of the allocated space.
FullIter end()
Returns FullFullIterer of the fictions past the end element.
unsigned int index(Iter pointer) const
Cont & container()
Get reference to the container of the FullIterator.
const Cont & cont
We have here reference to the container of the iterator. So, an instance of this class can not change...
void reserve(unsigned int size)
Reallocates the container space.
FullIter add_item(int id)
std::vector< T >::const_iterator const_iterator
FullIter find_id(const int id)
FullIterator & operator++()
Prefix. Advance operator.
virtual ~VectorId()
Since storage is direct member it is deallocated by its own destructor.
FullIteratorId(const VectorId< T > &cont, typename std::vector< T >::iterator it)
Constructor. Just call base class.
Iter operator()(unsigned int idx) const
Gets iterator of the element specified by index.
FullIter full_iter(Iter it)
int id()
Returns id of the iterator. See VectorId documentation.
T & operator[](unsigned int idx)
Gets reference to the element specified by index.
unsigned int size() const
Returns size of the container. This is independent of the allocated space.
T * Iter
Default iterator for this class.
void reserve(unsigned int size)
Reallocates the container space.
FullIteratorId(VectorId< T > &cont)
Constructor. Just call base class.
Global macros to enhance readability and debugging, general constants.
FullIteratorId< T > FullIter
Type of FullIterator that should be used to iterate through this container.
std::map< int, unsigned int > id_map
Maps id numbers to indexes into storage vector.
Iterator that keeps also reference to its container. Safer and provides indexes.
std::vector< T >::iterator iterator
bool is_valid()
Check validity of FullIterator.
FullIter operator()(unsigned int idx)
Gets iterator of the element specified by index.
Vector(unsigned int size=0)
Construct with reserve the space. Includes default constructor.
unsigned int index(const T *pointer) const
Cont::ElementType & operator*() const
bool operator==(const FullIterator &it) const
Comparison of two FullIterator.
std::vector< T > storage
Underlying vector container.
FullIterator< Vector< T > > FullIter
Type of FullIterator that should be used to iterate through this container.
FullIterator & operator--()
Prefix. Advance to previous operator.
FullIterator(const Cont &cont_par, const typename Cont::iterator it)
std::vector< int > id_storage
Space to save id numbers.
FullIterator(Cont &cont_par, const typename Cont::Iter it)
Constructor to make FullIterator from container and Iter i.e. T*.
FullIterator & operator=(const FullIterator &orig)
Assign operator. In debugging version check that containers of both FullIterator match.
T & operator[](unsigned int idx)
Gets reference to the element specified by index.
FullIter full_iter(Iter it)
FullIterator(Cont &cont_par)
Constructor to make uninitialized FullIterator for a container.
const T & operator[](unsigned int idx) const
Gets reference to the element specified by index.
T * Iter
Default iterator for this class.
Cont::ElementType * operator->() const
-> dereference operator
FullIteratorId(VectorId< T > &cont, const typename VectorId< T >::Iter it)
Constructor. Just call base class.
virtual ~Vector()
Since storage is direct member it is deallocated by its own destructor.
FullIterator & operator-=(const int shift)
FullIter end()
Returns FullFullIterer of the fictions past the end element.
std::vector< T >::iterator iterator
const T & operator[](unsigned int idx) const
Gets reference to the element specified by index.