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

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