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

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