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

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