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

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