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

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