OpendTect-6_4  6.4
sortedtable.h
Go to the documentation of this file.
1 #ifndef sortedtable_h
2 #define sortedtable_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: 19-4-2000
10  Contents: Array sorting
11  RCS: $Id$
12 ________________________________________________________________________
13 
14 -*/
15 
16 #include "sets.h"
17 #include "sortedlist.h"
18 
19 
24 template <class IDT, class T>
26 {
27 public:
28 
29  typedef int size_type;
30  typedef T object_type;
31 
32  SortedTable();
33 
34  size_type size() const { return vals_.size(); }
35  void set( IDT id, T val );
36  /*<! If id is set twice, it the old value will
37  be replaced by the new one */
38  bool get( IDT id, T& val ) const;
44  const IDT& id( size_type idx ) const { return ids_[idx]; }
45  const T& val( size_type idx ) const { return vals_[idx]; }
46 
47  bool remove(IDT id);
48  void erase() { vals_.erase(); ids_.erase(); }
49 
50 private:
51 
54 
55 };
56 
57 
58 template <class IDT, class T> inline
60  : ids_( false )
61 {}
62 
63 
64 template <class IDT, class T> inline
65 void SortedTable<IDT,T>::set( IDT theid, T theval )
66 {
67  int newpos = ids_.indexOf( theid );
68 
69  if ( newpos==-1 )
70  {
71  ids_ += theid;
72 
73  newpos = ids_.indexOf( theid );
74  vals_.insert( newpos, theval );
75  }
76 
77  vals_[newpos] = theval;
78 }
79 
80 
81 template <class IDT, class T> inline
82 bool SortedTable<IDT,T>::get( IDT theid, T& v ) const
83 {
84  int pos = ids_.indexOf( theid );
85 
86  if ( pos==-1 )
87  return false;
88 
89  v = vals_[pos];
90  return true;
91 }
92 
93 
94 template <class IDT, class T> inline
96 {
97  int pos = ids_.indexOf( theid );
98 
99  if ( pos==-1 ) return false;
100 
101  vals_.removeSingle( pos );
102  ids_.remove( pos );
103 
104  return true;
105 }
106 
107 
113 template <class T>
115 {
116 public:
117 
118  typedef int size_type;
119  typedef int id_type;
120 
121  SortedPointers() : ids_(false) {}
123 
124  size_type size() const { return vals_.size(); }
125  void set(id_type,T*);
126  /*<! If id is set twice, it the old value will
127  be replaced by the new one */
128  const T* get(id_type) const;
129  T* get(id_type);
130 
131  const T* getByPos( size_type pos ) const { return vals_[pos]; }
132  T* getByPos( size_type pos ) { return vals_[pos]; }
133 
134 
135  id_type id( size_type pos ) const { return ids_[pos]; }
136 
137  bool remove(id_type id);
138  void removePos(size_type);
139  void erase();
140 
141 private:
142 
145 
146 };
147 
148 
149 
150 template <class T> inline
152 {
153  const size_type newpos = ids_.indexOf( iid );
154 
155  if ( newpos==-1 )
156  {
157  ids_ += iid;
158 
159  newpos = ids_.indexOf( iid );
160  vals_.insertAt( val, newpos );
161  }
162 
163  vals_.replace( newpos, val );
164 }
165 
166 
167 template <class T> inline
168 const T* SortedPointers<T>::get( id_type iid ) const
169 {
170  const size_type pos = ids_.indexOf( iid );
171  return pos < 0 ? 0 : vals_[pos];
172 }
173 
174 
175 template <class T> inline
177 {
178  const size_type pos = ids_.indexOf( iid );
179  return pos < 0 ? 0 : vals_[pos];
180 }
181 
182 
183 template <class T> inline
185 {
186  const size_type pos = ids_.indexOf( iid );
187  if ( pos==-1 ) return false;
188 
189  vals_.removeSingle( pos );
190  ids_.removeSingle( pos );
191 
192  return true;
193 }
194 
195 
196 template <class T> inline
198 {
199  vals_.removeSingle( pos );
200  ids_.removeSingle( pos );
201 }
202 
203 
204 template <class T> inline
206 {
207  vals_.erase();
208  ids_.erase();
209 }
210 
211 
212 #endif
size_type size() const
Definition: sortedtable.h:124
size_type size() const
Definition: sortedtable.h:34
ObjectSet< T > vals_
Definition: sortedtable.h:143
const T * getByPos(size_type pos) const
Definition: sortedtable.h:131
int size_type
Definition: sortedtable.h:118
void erase()
Definition: sortedtable.h:205
void removePos(size_type)
Definition: sortedtable.h:197
TypeSet< T > vals_
Definition: sortedtable.h:52
A SortedTable keeps track of ids and their corresponding values. Each id can only be present once...
Definition: sortedtable.h:25
void removeSingle(size_type)
Definition: sortedlist.h:218
Set of pointers to objects.
Definition: commontypes.h:32
size_type indexOf(const T &) const
Definition: sortedlist.h:120
SortedList< size_type > ids_
Definition: sortedtable.h:144
Set of (small) copyable elements.
Definition: commontypes.h:30
void set(IDT id, T val)
Definition: sortedtable.h:65
const T * get(id_type) const
Definition: sortedtable.h:168
int id_type
Definition: sortedtable.h:119
SortedList< IDT > ids_
Definition: sortedtable.h:53
T * getByPos(size_type pos)
Definition: sortedtable.h:132
T object_type
Definition: sortedtable.h:30
SortedTable()
Definition: sortedtable.h:59
A SortedPointers keeps track of ids and their corresponding pointer. Each id can only be present once...
Definition: sortedtable.h:114
id_type id(size_type pos) const
Definition: sortedtable.h:135
int size_type
Definition: sortedtable.h:29
void set(id_type, T *)
Definition: sortedtable.h:151
void erase()
Definition: sortedtable.h:48
bool get(IDT id, T &val) const
Definition: sortedtable.h:82
void erase()
Definition: sortedlist.h:61
bool remove(IDT id)
Definition: sortedtable.h:95
const IDT & id(size_type idx) const
Definition: sortedtable.h:44
#define mClass(module)
Definition: commondefs.h:164
~SortedPointers()
Definition: sortedtable.h:122
SortedPointers()
Definition: sortedtable.h:121
const T & val(size_type idx) const
Definition: sortedtable.h:45
bool remove(id_type id)
Definition: sortedtable.h:184

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