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

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