OpendTect  6.6
vectoraccess.h
Go to the documentation of this file.
1 #pragma once
2 
3 /*+
4 ________________________________________________________________________
5 
6  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
7  Author: A.H.Bril
8  Date: Mar 2002
9  Contents: Access to STL vector class with extensions
10  RCS: $Id$
11 ________________________________________________________________________
12 
13 -*/
14 
15 #include "debug.h"
16 #include "gendefs.h"
17 #include <algorithm>
18 #include <vector>
19 #include <stdexcept>
20 
36 template <class T,class IT>
38 {
39 public:
40 
41  typedef IT size_type;
43  typedef T object_type;
44  typedef std::vector<T> impl_type;
45  typedef typename impl_type::iterator iterator;
46  typedef typename impl_type::const_iterator const_iterator;
47 
48  inline iterator begin() { return v_.begin(); }
49  inline const_iterator begin() const { return v_.cbegin(); }
50  inline const_iterator cbegin() const { return v_.cbegin(); }
51  inline iterator end() { return v_.end(); }
52  inline const_iterator end() const { return v_.cend(); }
53  inline const_iterator cend() const { return v_.end(); }
54  inline idx_type getIdx( iterator it ) const
55  { return (idx_type)(it-cbegin()); }
56  inline idx_type getIdx( const_iterator it ) const
57  { return (idx_type)(it-cbegin()); }
58 
59  inline VectorAccess() {}
60  inline VectorAccess( IT n ) : v_(n) {}
61  inline VectorAccess( IT n, const T& t )
62  : v_(n,t) {}
63  inline VectorAccess( const VectorAccess& v2 )
64  : v_(v2.v_) {}
65  inline impl_type& vec() { return v_; }
66  inline const impl_type& vec() const { return v_; }
67 
68  inline T& operator[](IT);
69  inline const T& operator[](IT) const;
70  inline T& first() { return v_.front(); }
71  inline const T& first() const { return v_.front(); }
72  inline T& last() { return v_.back(); }
73  inline const T& last() const { return v_.back(); }
74  inline IT size() const { return (IT)v_.size();}
75  inline bool setCapacity(IT sz,bool withmargin);
77  inline IT getCapacity() const { return (IT)v_.capacity();}
79  inline bool setSize(IT sz,T val);
80 
81  inline bool validIdx( IT i ) const { return i>=0 && i<size();}
82  inline IT indexOf(const T&,bool forward,IT start=-1) const;
83  inline IT count(const T&) const;
84  inline bool isPresent(const T&) const;
85 
86  inline VectorAccess& operator =( const VectorAccess& v2 )
87  { v_ = v2.v_; return *this; }
88  inline bool push_back( const T& t );
89  inline T pop_back();
90  inline void insert( IT pos, const T& val )
91  { v_.insert(v_.begin() + pos,val); }
92  inline void erase()
93  { std::vector<T>().swap(v_); }
94  inline void erase( const T& t )
95  {
96  for ( IT i=size()-1; i!=-1; i-- )
97  { if ( v_[i] == t ) { remove(i); return; } }
98  }
99  inline void remove( IT i )
100  {
101  if ( i < size() )
102  v_.erase( v_.begin() + i );
103  }
104  inline void remove( IT i1, IT i2 )
105  {
106  if ( i1 == i2 ) { remove( i1 ); return; }
107  if ( i1 > i2 ) std::swap( i1, i2 );
108  const IT sz = size();
109  if ( i1 >= sz ) return;
110 
111  if ( i2 >= sz-1 ) i2 = sz-1;
112  v_.erase( v_.begin()+i1, v_.begin()+i2+1 );
113  }
114  inline void swapElems( IT i, IT j )
115  { std::swap( v_[i], v_[j] ); }
116  inline void fillWith( const T& val )
117  { std::fill( v_.begin(), v_.end(), val ); }
118  inline void replace( const T& val, const T& newval )
119  { std::replace( v_.begin(), v_.end(), val, newval ); }
120  inline void swap( VectorAccess& oth )
121  { v_.swap( oth.v_ ); }
122 
123  void moveAfter( const T& t, const T& aft )
124  {
125  if ( t == aft || size() < 2 ) return;
126  IT tidx = -1; IT aftidx = -1;
127  for ( IT vidx=size()-1; vidx!=-1; vidx-- )
128  {
129  if ( v_[vidx] == t )
130  { tidx = vidx; if ( aftidx != -1 ) break; }
131  if ( v_[vidx] == aft )
132  { aftidx = vidx; if ( tidx != -1 ) break; }
133  }
134  if ( tidx == -1 || aftidx == -1 || tidx == aftidx ) return;
135  if ( aftidx > tidx )
136  for ( IT vidx=tidx; vidx<aftidx; vidx++ )
137  swapElems( vidx, vidx+1 );
138  else
139  for ( IT vidx=tidx; vidx>aftidx+1; vidx-- )
140  swapElems( vidx, vidx-1 );
141  }
142 
143  void moveToStart( const T& t )
144  {
145  if ( size() < 2 ) return;
146  IT tidx = -1;
147  for ( IT vidx=size()-1; vidx!=-1; vidx-- )
148  if ( v_[vidx] == t ) { tidx = vidx; break; }
149  for ( IT vidx=tidx; vidx>0; vidx-- )
150  swapElems( vidx, vidx-1 );
151  }
152 
153 protected:
154 
156 
157 };
158 
159 
160 #define mExportVectorAccess(mod,tp,itp) \
161 template mExpClass(mod) std::allocator<tp>;\
162 template mExpClass(mod) std::_Vector_val<tp,std::allocator<tp> >;\
163 template mExpClass(mod) std::vector<tp>;\
164 template mExpClass(mod) VectorAccess<tp,itp>;\
165 
166 
167 template<class T,class IT> inline
168 bool VectorAccess<T,IT>::setCapacity( IT sz, bool withmargin )
169 {
170  if ( sz<=v_.capacity() )
171  return true;
172 
173  if ( withmargin )
174  {
175  IT tmp = sz-1;
176  sz = 1;
177 
178  while ( tmp )
179  {
180  tmp >>= 1;
181  sz <<= 1;
182  }
183  }
184 
185  try { v_.reserve(sz); }
186  catch ( std::bad_alloc )
187  { return false; }
188  catch ( std::length_error )
189  { return false; }
190 
191  return true;
192 }
193 
194 
195 template<class T,class IT> inline
197 {
198  try
199  { v_.push_back(t); }
200  catch ( std::bad_alloc )
201  { return false; }
202 
203  return true;
204 }
205 
206 
207 template<class T,class IT> inline
209 {
210  const T lastelem = v_.back();
211  v_.pop_back();
212  return lastelem;
213 }
214 
215 
216 template<class T,class IT> inline
217 bool VectorAccess<T,IT>::setSize( IT sz, T val )
218 {
219  try { v_.resize(sz,val); }
220  catch ( std::bad_alloc )
221  { return false; }
222 
223  return true;
224 }
225 
226 #ifdef __debug__
227 
228 #define mImplOperator \
229  try { return v_.at(vidx); } \
230  catch ( std::out_of_range ) \
231  { DBG::forceCrash(true); } \
232  return v_[(typename std::vector<T>::size_type)vidx]
233 
234 #else
235 
236 #define mImplOperator \
237  return v_[(typename std::vector<T>::size_type)vidx]
238 
239 #endif
240 
241 
242 template<class T,class IT> inline
244 {
246 }
247 
248 
249 template<class T,class IT> inline
250 const T& VectorAccess<T,IT>::operator[]( IT vidx ) const
251 {
253 }
254 
255 #undef mImplOperator
256 
257 
258 template<class T,class IT> inline
259 IT VectorAccess<T,IT>::indexOf( const T& t, bool forward, IT start ) const
260 {
261  if ( forward )
262  {
263  typename std::vector<T>::const_iterator begit = v_.cbegin();
264  const typename std::vector<T>::const_iterator endit = v_.cend();
265  if ( start>0 )
266  begit += start;
267 
268  const typename std::vector<T>::const_iterator res =
269  std::find( begit, endit, t );
270  if ( res==endit )
271  return -1;
272 
273  return mCast( IT, res-v_.begin() );
274  }
275 
276  typename std::vector<T>::const_reverse_iterator rebegit = v_.rbegin();
277  const typename std::vector<T>::const_reverse_iterator rendit = v_.rend();
278  if ( start>0 )
279  {
280  const IT nrskipped = size()-1-start;
281  rebegit += nrskipped;
282  }
283 
284  const typename impl_type::const_reverse_iterator res
285  = std::find( rebegit, rendit, t );
286  if ( res == rendit )
287  return -1;
288 
289  return mCast( IT, rendit-res) - 1;
290 }
291 
292 
293 template<class T,class IT> inline
294 IT VectorAccess<T,IT>::count( const T& t ) const
295 {
296  return mCast( IT, std::count(v_.cbegin(),v_.end(),t) );
297 }
298 
299 
300 template<class T,class IT> inline
301 bool VectorAccess<T,IT>::isPresent( const T& t ) const
302 {
303  const_iterator endit = v_.cend();
304  return std::find( v_.cbegin(), endit, t ) != endit;
305 }
VectorAccess::cbegin
const_iterator cbegin() const
Definition: vectoraccess.h:50
VectorAccess::getIdx
idx_type getIdx(iterator it) const
Definition: vectoraccess.h:54
VectorAccess::validIdx
bool validIdx(IT i) const
Definition: vectoraccess.h:81
VectorAccess::moveAfter
void moveAfter(const T &t, const T &aft)
Definition: vectoraccess.h:123
VectorAccess::getCapacity
IT getCapacity() const
Definition: vectoraccess.h:77
VectorAccess::count
IT count(const T &) const
Definition: vectoraccess.h:294
VectorAccess::iterator
impl_type::iterator iterator
Definition: vectoraccess.h:45
VectorAccess
Simple vector-based container simplifying index-based work.
Definition: vectoraccess.h:38
VectorAccess::end
const_iterator end() const
Definition: vectoraccess.h:52
VectorAccess::VectorAccess
VectorAccess(IT n)
Definition: vectoraccess.h:60
VectorAccess::idx_type
size_type idx_type
Definition: vectoraccess.h:42
VectorAccess::last
T & last()
Definition: vectoraccess.h:72
VectorAccess::impl_type
std::vector< T > impl_type
Definition: vectoraccess.h:44
VectorAccess::vec
const impl_type & vec() const
Definition: vectoraccess.h:66
VectorAccess::cend
const_iterator cend() const
Definition: vectoraccess.h:53
mImplOperator
#define mImplOperator
Definition: vectoraccess.h:236
VectorAccess::size_type
IT size_type
Definition: vectoraccess.h:41
VectorAccess::first
const T & first() const
Definition: vectoraccess.h:71
VectorAccess::getIdx
idx_type getIdx(const_iterator it) const
Definition: vectoraccess.h:56
VectorAccess::remove
void remove(IT i1, IT i2)
Definition: vectoraccess.h:104
find
const BufferString * find(const BufferStringSet &, const char *)
File::remove
bool remove(const char *)
VectorAccess::v_
impl_type v_
Definition: vectoraccess.h:155
VectorAccess::operator[]
T & operator[](IT)
Definition: vectoraccess.h:243
VectorAccess::object_type
T object_type
Definition: vectoraccess.h:43
mClass
#define mClass(module)
Definition: commondefs.h:181
VectorAccess::const_iterator
impl_type::const_iterator const_iterator
Definition: vectoraccess.h:46
VectorAccess::swap
void swap(VectorAccess &oth)
Definition: vectoraccess.h:120
gendefs.h
VectorAccess::push_back
bool push_back(const T &t)
Definition: vectoraccess.h:196
VectorAccess::operator[]
const T & operator[](IT) const
Definition: vectoraccess.h:250
VectorAccess::isPresent
bool isPresent(const T &) const
Definition: vectoraccess.h:301
VectorAccess::end
iterator end()
Definition: vectoraccess.h:51
VectorAccess::begin
iterator begin()
Definition: vectoraccess.h:48
VectorAccess::VectorAccess
VectorAccess(IT n, const T &t)
Definition: vectoraccess.h:61
VectorAccess::replace
void replace(const T &val, const T &newval)
Definition: vectoraccess.h:118
mCast
#define mCast(tp, v)
Definition: commondefs.h:137
VectorAccess::VectorAccess
VectorAccess(const VectorAccess &v2)
Definition: vectoraccess.h:63
VectorAccess::pop_back
T pop_back()
Definition: vectoraccess.h:208
VectorAccess::erase
void erase(const T &t)
Definition: vectoraccess.h:94
swap
void swap(BufferStringSet &o1, BufferStringSet &o2)
Definition: bufstringset.h:154
VectorAccess::VectorAccess
VectorAccess()
Definition: vectoraccess.h:59
VectorAccess::remove
void remove(IT i)
Definition: vectoraccess.h:99
VectorAccess::swapElems
void swapElems(IT i, IT j)
Definition: vectoraccess.h:114
VectorAccess::vec
impl_type & vec()
Definition: vectoraccess.h:65
VectorAccess::erase
void erase()
Definition: vectoraccess.h:92
VectorAccess::size
IT size() const
Definition: vectoraccess.h:74
VectorAccess::insert
void insert(IT pos, const T &val)
Definition: vectoraccess.h:90
VectorAccess::moveToStart
void moveToStart(const T &t)
Definition: vectoraccess.h:143
VectorAccess::setSize
bool setSize(IT sz, T val)
Definition: vectoraccess.h:217
VectorAccess::last
const T & last() const
Definition: vectoraccess.h:73
VectorAccess::setCapacity
bool setCapacity(IT sz, bool withmargin)
Definition: vectoraccess.h:168
VectorAccess::fillWith
void fillWith(const T &val)
Definition: vectoraccess.h:116
VectorAccess::first
T & first()
Definition: vectoraccess.h:70
VectorAccess::indexOf
IT indexOf(const T &, bool forward, IT start=-1) const
Definition: vectoraccess.h:259
debug.h
VectorAccess::begin
const_iterator begin() const
Definition: vectoraccess.h:49

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B.V. 1995-2021