casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HashMapIter.h
Go to the documentation of this file.
1 //# <HashMap.h>: this defines HashMap, which is a hashed associative array
2 //# Copyright (C) 1995,1996,1998,1999,2001
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id$
27 #ifndef CASA_HASHMAPITER_H
28 #define CASA_HASHMAPITER_H
29 
30 #ifndef AIPS_USE_DEPRECATED
31 #error "HashMapIter.h is deprecated; use -DBUILD_DEPRECATED=ON to use it"
32 #endif
33 
34 #include <casacore/casa/aips.h>
36 
37 // <summary>
38 // Step through a const HashMap
39 // </summary>
40 // <use visibility=export>
41 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
42 //
43 // <synopsis>
44 // This class is an iterator, and it used to step through const
45 // <linkto class=HashMap><src>HashMap</src></linkto>s. This is useful
46 // when one wishes to find each of the user defined mappings in a
47 // particular map.
48 // </synopsis>
49 //
50 // <example>
51 // <srcblock>
52 // #include <casacore/casa/Containers/HashMap.h>
53 // #include <casacore/casa/BasicSL/String.h>
54 // #include <casacore/casa/iostream.h>
55 //
56 // main() {
57 // HashMap<String,Int> hash;
58 //
59 // hash.define("one",1);
60 // hash.define("two",2);
61 // hash.define("three",3);
62 // hash.define("four",4);
63 // hash.define("five",5);
64 // hash.define("six",6);
65 //
66 // ConstHashMapIter<String,Int> iter(hash);
67 // for ( iter.toStart(); ! iter.atEnd(); iter++ )
68 // cout << iter.getVal() << ": " << iter.getKey() << endl;
69 // }
70 // </srcblock>
71 // </example>
72 //
73 // <motivation>
74 // Sometimes one needs to step through the defined elements of an
75 // associative array. The user should be told when iterator does
76 // not modify the underlying data structure. The standard C++
77 // <em>const</em> is not sufficient because while the internal
78 // state of the iterator changes, the underlying data structure
79 // is not modified. For this reason, both const and non-const
80 // versions of the iterator are useful.
81 // </motivation>
82 //
83 namespace casacore { //#Begin casa namespace
84 
85 template<class key, class val> class ConstHashMapIter {
86 public:
87 
88  //
89  // Move the iterator to the start of the Map.
90  //
91  void toStart();
92 
93  //
94  // Advance to the next element of the Map.
95  //
96  // <group>
97  void operator++() { step(); }
98  void operator++(int) { step(); }
99  // </group>
100 
101  //
102  // Get the key or value for the current position in
103  // the Map.
104  //
105  // <group>
106  const key &getKey() const;
107  const val &getVal() const;
108  // </group>
109 
110  //
111  // Check to see if the iterator position is at the
112  // end or beginning of the Map.
113  //
114  // <group>
115  Bool atEnd() const { return atEnd_; }
116  Bool atStart() const;
117  // </group>
118 
119  //
120  // Check to see if the iterator is in a valid state.
121  //
122  Bool isValid() const { return Container != 0 ? True : False; }
123 
124  //
125  // Constructs a Map iterator from a Map (with reference semantics).
126  //
128 
129  //
130  // Assign one map iterator to a map (with reference semantics).
131  //
133 
134  //
135  // Constructs a Map iterator from another iterator (with reference semantics).
136  //
138 
139  //
140  // Assign one map iterator to another iterator (with reference semantics).
141  //
143 
144  //
145  // Default constructor creates an invalid Map iterator.
146  //
148 
149 
150  //
151  // Returns the default value for the Map on which this
152  // iterator is operating if it is a valid iterator, otherwise
153  // it throws an exception.
154  //
155  const val &defaultVal() const {
156  if ( ! isValid() )
158  return Container->defaultVal();
159  }
160 
161  //
162  // Allows mapping functions to be performed with the
163  // map on which this iterator operates. If this iterator
164  // is invalid, then an exception will be thrown.
165  //
166  const val &operator()(const key &ky) const {
167  if ( ! isValid() )
169  return Container->operator()(ky);
170  }
171 
172  //
173  // Allows one to check to see if a given key is defined
174  // in the map which this iterator tracks. If this iterator
175  // is invalid, then an exception will be thrown.
176  //
177  Bool isDefined(const key &ky) const {
178  if (! isValid() )
180  return Container->isDefined(ky);
181  }
182 
183  //
184  // Returns the number of user defined mappings
185  //
186  uInt ndefined() const {
187  if (! isValid() )
189  return Container->ndefined();
190  }
191 
192  //
193  // Returns the container on which this iterator is
194  // operating.
195  //
196  const HashMap<key,val> &container() const {
197  if ( !isValid() )
199  return *Container;
200  }
201 
202  // dtor
203  virtual ~ConstHashMapIter();
204 
205 protected:
206 
207  void step();
208 
213 };
214 
215 
216 // <summary>
217 // Step through a non-const HashMap
218 // </summary>
219 // <use visibility=export>
220 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
221 //
222 // <synopsis>
223 // This class is an iterator, and it used to step through non-const
224 // <linkto class=HashMap><src>HashMap</src></linkto>s. This is useful
225 // when one wishes to find each of the user defined mappings in a
226 // particular map.
227 // </synopsis>
228 //
229 // <example>
230 // <srcblock>
231 // #include <aips/Containers/HashMap.h>
232 // #include <casacore/casa/BasicSL/String.h>
233 // #include <iostream>
234 //
235 // main() {
236 // HashMap<String,Int> hash;
237 //
238 // hash.define("one",1);
239 // hash.define("two",2);
240 // hash.define("three",3);
241 // hash.define("four",4);
242 // hash.define("five",5);
243 // hash.define("six",6);
244 //
245 // HashMapIter<String,Int> iter(hash);
246 // for ( iter.toStart(); ! iter.atEnd(); iter++ )
247 // cout << iter.getVal() << ": " << iter.getKey() << endl;
248 // }
249 // </srcblock>
250 // </example>
251 //
252 // <motivation>
253 // Same as <linkto class=ConstHashMapIter><src>ConstHashMapIter</src></linkto>,
254 // but allows for modification of the underlying structure.
255 // </motivation>
256 //
257 template<class key, class val> class HashMapIter : public ConstHashMapIter<key,val> {
258 public:
259  //
260  // Get the key or value for the current position in
261  // the Map.
262  //
263  // <group>
264  val &getVal();
265 
266  virtual const val &getVal() const;
267  // </group>
268 
269  //
270  // These functions allow for the definition and removal of key/value
271  // relations. The "define(key &, value &)" function defines a key/value
272  // relation, and "remove(key &)" function removes a relation if it has
273  // been previously defined.
274  //
275  // <group>
276  val &define(const key &k, const val &v) {
277  if (!this->isValid())
279  return(this->Container->define(k,v));
280  }
281  void remove(const key &k) {
282  if (!this->isValid())
284  this->Container->remove(k);
285  }
286  // </group>
287 
288  //
289  // This returns the default value for the map that this iterator
290  // is tracking. With a non-const iterator the default value can
291  // be changed.
292  //
293  // <group>
294  const val &defaultVal() const {
296  }
297 
298  val &defaultVal() {
299  if (!this->isValid())
301  return this->Container->defaultVal();
302  }
303  // </group>
304 
305  //
306  // Clear all of the mappings.
307  //
308  void clear() {
309  if (!this->isValid())
311  this->Container->clear();
312  }
313 
314  //
315  // Allows mapping functions to be performed with the
316  // map on which this iterator operates. If this iterator
317  // is invalid, then an exception will be thrown. With
318  // a non-const operator, the value can be changed.
319  //
320  // <group>
321  const val &operator()(const key &ky) const {
323  }
324 
325  val &operator()(const key &ky) {
326  if (!this->isValid())
328  return(this->Container->operator()(ky));
329  }
330  // </group>
331 
332  //
333  // This allows a MapIter to be constructed from a Map. When
334  // created the new MapIter maintains a reference to the original
335  // Map. If the Map to which this MapIter points is deleted, then
336  // the MapIter is marked as invalid.
337  //
339 
340  //
341  // This allows a MapIter to be constructed from another MapIter.
342  // When created the new MapIter maintains a reference to the Map
343  // which the MapIter parameter tracked. If this Map is deleted, then
344  // this MapIter is marked as invalid.
345  //
346  HashMapIter(const HashMapIter<key,val> &other) : ConstHashMapIter<key,val>(other) {}
347 
348  //
349  // Default constructor creates an invalid Map iterator.
350  //
351  HashMapIter() : ConstHashMapIter<key,val>() {}
352 
353 
354  //
355  // This assignment operator allows the Map which this MapIter tracks
356  // to be changed. After a call to this operator, the MapIter will track
357  // the Map parameter.
358  //
360 
361  //
362  // This assignment operator allows the Map which this MapIter tracks
363  // to be changed. After a call to this operator, this MapIter will track
364  // the Map which the MapIter parameter tracks, i.e. it will contain a
365  // reference to this new Map.
366  //
367  virtual HashMapIter<key,val> &operator=(const HashMapIter<key,val> &other);
368 
369  //
370  // Returns the container on which this iterator is
371  // operating.
372  //
373  // <group>
375  if (!this->isValid())
377  return(*this->Container);
378  }
379  const HashMap<key,val> &container() const {
380  if (!this->isValid())
382  return(*this->Container);
383  }
384  // </group>
385 
386  // dtor
387  ~HashMapIter();
388 
389 protected:
390  //*display 4
391  //
392  // These assignment operators are private and ONLY throw an
393  // exception to prevent incorrect assignments to a non-const
394  // iterator.
395  //
396  // <group>
399  return *this;}
402  return *this;}
403  // </group>
404 
405 };
406 
407 } //#End casa namespace
408 
409 #ifndef CASACORE_NO_AUTO_TEMPLATES
410 #include <casacore/casa/Containers/HashMapIter.tcc>
411 #endif //# CASACORE_NO_AUTO_TEMPLATES
412 #endif
const val & defaultVal() const
This returns the default value for the map that this iterator is tracking.
Definition: HashMapIter.h:294
Bool isValid() const
Check to see if the iterator is in a valid state.
Definition: HashMapIter.h:122
const val & defaultVal() const
Returns the default value for the Map on which this iterator is operating if it is a valid iterator...
Definition: HashMapIter.h:155
ConstHashMapIter< key, val > & operator=(const HashMap< key, val > &)
Assign one map iterator to a map (with reference semantics).
Definition: HashMapIter.h:397
const key & getKey() const
Get the key or value for the current position in the Map.
const val & operator()(const key &ky) const
Allows mapping functions to be performed with the map on which this iterator operates.
Definition: HashMapIter.h:321
const HashMap< key, val > & container() const
Definition: HashMapIter.h:379
void toStart()
Move the iterator to the start of the Map.
void throw_hashmapiter_init_error()
Bool atEnd() const
Check to see if the iterator position is at the end or beginning of the Map.
Definition: HashMapIter.h:115
HashMap< key, val > & container()
Returns the container on which this iterator is operating.
Definition: HashMapIter.h:374
HashMapIter()
Default constructor creates an invalid Map iterator.
Definition: HashMapIter.h:351
uInt ndefined() const
Returns the number of user defined mappings.
Definition: HashMapIter.h:186
void operator++()
Advance to the next element of the Map.
Definition: HashMapIter.h:97
HashMapIter(HashMap< key, val > &st)
This allows a MapIter to be constructed from a Map.
Definition: HashMapIter.h:338
virtual ConstHashMapIter< key, val > & operator=(const HashMap< key, val > &other)
Assign one map iterator to a map (with reference semantics).
ConstHashMapIter< key, val > & operator=(const ConstHashMapIter< key, val > &)
Assign one map iterator to another iterator (with reference semantics).
Definition: HashMapIter.h:400
HashMapIter(const HashMapIter< key, val > &other)
This allows a MapIter to be constructed from another MapIter.
Definition: HashMapIter.h:346
const HashMap< key, val > & container() const
Returns the container on which this iterator is operating.
Definition: HashMapIter.h:196
val & operator()(const key &ky)
Definition: HashMapIter.h:325
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
const val & operator()(const key &ky) const
Allows mapping functions to be performed with the map on which this iterator operates.
Definition: HashMapIter.h:166
const Bool False
Definition: aipstype.h:44
Bool isDefined(const key &ky) const
Allows one to check to see if a given key is defined in the map which this iterator tracks...
Definition: HashMapIter.h:177
void clear()
Clear all of the mappings.
Definition: HashMapIter.h:308
virtual HashMapIter< key, val > & operator=(HashMap< key, val > &other)
This assignment operator allows the Map which this MapIter tracks to be changed.
virtual ~ConstHashMapIter()
dtor
void throw_invalid_hashmapiter_error()
val & getVal()
Get the key or value for the current position in the Map.
ConstHashMapIter()
Default constructor creates an invalid Map iterator.
Definition: HashMapIter.h:147
ListIter< OrderedPair< key, val > > iter
Definition: HashMapIter.h:209
Doubly linked non-constant list iterator The List class above only provides for the list framework...
Definition: List.h:53
val & define(const key &k, const val &v)
These functions allow for the definition and removal of key/value relations.
Definition: HashMapIter.h:276
const Bool True
Definition: aipstype.h:43
HashMap< key, val > * Container
Definition: HashMapIter.h:210
Step through a non-const HashMap.
Definition: HashMapIter.h:257
unsigned int uInt
Definition: aipstype.h:51
const val & getVal() const
Associative Array with a hash table implementation.
Definition: HashMap.h:301
#define casacore
&lt;X11/Intrinsic.h&gt; #defines true, false, casacore::Bool, and String.
Definition: X11Intrinsic.h:42