OpendTect-6_4  6.4
sortedlist.h
Go to the documentation of this file.
1 #ifndef sortedlist_h
2 #define sortedlist_h
3 
4 /*+
5 ________________________________________________________________________
6 
7  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
8  Author: Kris
9  Date: 19-4-2000
10  Contents: Array sorting
11  RCS: $Id$
12 ________________________________________________________________________
13 
14 -*/
15 
16 #include "gendefs.h"
17 #include "vectoraccess.h"
18 
28 template <class T>
30 {
31 public:
32 
33  typedef int size_type;
34  typedef T object_type;
35 
36  SortedList( bool allowmultiples=true )
37  : allowmultiples_(allowmultiples) {}
38 
39  bool isEmpty() const { return size()<1; }
40  size_type size() const { return vec_.size(); }
41  const T& operator[](size_type idx) const { return (T&)vec_[idx];}
42  bool isPresent( const T& t ) const { return indexOf(t)>=0;}
43  size_type indexOf(const T&) const;
46  SortedList<T>& operator +=(const T&);
47  SortedList<T>& operator -=(const T&);
48  SortedList<T>& add( const T& t ) { *this += t; return *this; }
49 
50  // The following functions handle external indexables: classes or
51  // arrays - things that support the [] operator.
52 
53  template <class U> SortedList<T>& copy(const U&);
54  template <class U> SortedList<T>& operator =(const U&);
55  template <class U> SortedList<T>& operator +=(const U&);
56  template <class U> SortedList<T>& operator -=(const U&);
57  template <class U> void intersect(const U&);
61  void erase() { vec_.erase(); }
62  void setEmpty() { vec_.erase(); }
63  void removeSingle(size_type);
64  void removeRange(size_type,size_type);
65 
66  std::vector<T>& vec() { return vec_.vec(); }
67  const std::vector<T>& vec() const { return vec_.vec(); }
68  T* arr() { return size() ? &(*this)[0] : 0; }
69  const T* arr() const { return size() ? &(*this)[0] : 0; }
70 
71 protected:
72 
73  size_type getPos( const T& ) const;
81 
82 };
83 
84 
85 template <class T> inline
86 typename SortedList<T>::size_type SortedList<T>::getPos( const T& typ ) const
87 {
88  const size_type sz = size();
89  if ( !sz ) return 0;
90 
91  size_type start = 0; size_type stop = sz-1;
92  T startval = (*this)[start]; T stopval = (*this)[stop];
93  if ( typ > stopval ) return sz;
94 
95  while ( stopval > startval )
96  {
97  if ( stop-start==1 )
98  return typ>startval ? stop : start;
99 
100  size_type middle = (start+stop)>>1;
101  T middleval = (*this)[middle];
102 
103  if ( middleval > typ )
104  {
105  stopval = middleval;
106  stop = middle;
107  }
108  else
109  {
110  startval = middleval;
111  start = middle;
112  }
113  }
114 
115  return start;
116 }
117 
118 
119 template <class T> inline
120 typename SortedList<T>::size_type SortedList<T>::indexOf( const T& typ ) const
121 {
122  if ( !size() ) return -1;
123 
124  size_type pos = getPos( typ );
125 
126  if ( pos>=size() || pos<0 || (*this)[pos]!=typ )
127  return -1;
128 
129  return pos;
130 }
131 
132 
133 template <class T> inline
135 {
136  size_type newpos = getPos( nv );
137 
138  if ( newpos == size() )
139  {
140  vec_.push_back( nv );
141  return *this;
142  }
143 
144  if ( !allowmultiples_ && (*this)[newpos] == nv )
145  return *this;
146 
147  vec_.insert( newpos, nv );
148  return *this;
149 }
150 
151 
152 template <class T> inline
154 {
155  const size_type sz = size();
156  if ( !sz ) return *this;
157 
158  const size_type pos = indexOf( nv );
159 
160  if ( pos == -1 ) return *this;
161 
162  vec_.removeSingle( pos );
163  return *this;
164 }
165 
166 
167 template <class T> template <class U> inline
168 void SortedList<T>::intersect( const U& b )
169 {
170  for ( size_type idx=0; idx<size(); idx++ )
171  {
172  if ( b.indexOf( (*this)[idx]) == -1 )
173  {
174  removeSingle( idx );
175  idx--;
176  }
177  }
178 }
179 
180 
181 template <class T> template <class U> inline
183 {
184  erase();
185  const size_type sz = array.size();
186  for ( size_type idx=0; idx<sz; idx++ )
187  (*this) += array[idx];
188  return *this;
189 }
190 
191 
192 template <class T> template <class U> inline
194 { return copy(array); }
195 
196 
197 template <class T> template <class U> inline
199 {
200  const size_type sz = array.size();
201  for ( size_type idx=0; idx<sz; idx++ )
202  (*this) += array[idx];
203  return *this;
204 }
205 
206 
207 template <class T> template <class U> inline
209 {
210  const size_type sz = array.size();
211  for ( size_type idx=0; idx<sz; idx++ )
212  (*this) -= array[idx];
213  return *this;
214 }
215 
216 
217 template <class T> inline
219 {
220  vec_.remove( pos );
221 }
222 
223 template <class T> inline
225 {
226  vec_.remove( p1, p2 );
227 }
228 
229 
230 #endif
const T * arr() const
Definition: sortedlist.h:69
std::vector< T > & vec()
Definition: sortedlist.h:66
void removeRange(size_type, size_type)
Definition: sortedlist.h:224
const T & operator[](size_type idx) const
Definition: sortedlist.h:41
bool isPresent(const T &t) const
Definition: sortedlist.h:42
void intersect(const U &)
Definition: sortedlist.h:168
SortedList< T > & operator-=(const T &)
Definition: sortedlist.h:153
bool isEmpty() const
Definition: sortedlist.h:39
ObjectSet< T >::size_type indexOf(const ObjectSet< T > &os, const S &val)
Locate object in set.
Definition: objectset.h:169
void removeSingle(size_type)
Definition: sortedlist.h:218
size_type getPos(const T &) const
Definition: sortedlist.h:86
size_type indexOf(const T &) const
Definition: sortedlist.h:120
T * arr()
Definition: sortedlist.h:68
size_type size() const
Definition: sortedlist.h:40
SortedList(bool allowmultiples=true)
Definition: sortedlist.h:36
int size_type
Definition: sortedlist.h:33
T object_type
Definition: sortedlist.h:34
SortedList< T > & operator+=(const T &)
Definition: sortedlist.h:134
SortedList< T > & operator=(const U &)
Definition: sortedlist.h:193
void removeRange(ODSET &inst, size_type start, size_type stop)
Removes a range from the set.
Definition: odset.h:57
SortedList< T > & add(const T &t)
Definition: sortedlist.h:48
void setEmpty()
Definition: sortedlist.h:62
VectorAccess< T, size_type > vec_
Definition: sortedlist.h:79
const std::vector< T > & vec() const
Definition: sortedlist.h:67
bool allowmultiples_
Definition: sortedlist.h:80
A SortedList is a list where all objects are stored in ascending order. The objects should be capable...
Definition: sortedlist.h:29
void erase()
Definition: sortedlist.h:61
void copy(TypeSetBase< T, I > &to, const TypeSetBase< S, I > &from)
Definition: typeset.h:212
#define mClass(module)
Definition: commondefs.h:164
SortedList< T > & copy(const U &)
Definition: sortedlist.h:182

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