Jpp  18.0.0-rc.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JMultiHashMap.hh
Go to the documentation of this file.
1 #ifndef __JTOOLS__JMULTIHASHMAP__
2 #define __JTOOLS__JMULTIHASHMAP__
3 
4 #include "JLang/JNullType.hh"
5 #include "JLang/JTypeList.hh"
7 #include "JLang/JEquals.hh"
9 #include "JLang/JException.hh"
10 #include "JTools/JHashMap.hh"
11 #include "JTools/JHashEvaluator.hh"
12 #include "JTools/JTuple.hh"
13 #include "JTools/JPair.hh"
14 
15 
16 /**
17  * \file
18  *
19  * General purpose class for multidimensional hash maps.
20  * \author mdejong
21  */
22 namespace JTOOLS {}
23 namespace JPP { using namespace JTOOLS; }
24 
25 namespace JTOOLS {
26 
27  using JLANG::JEquals;
29  using JLANG::JNullType;
30  using JLANG::JTypeList;
31  using JLANG::JTYPELIST;
32  using JLANG::JClass;
34  using JIO::JReader;
35  using JIO::JWriter;
36 
37 
38  /**
39  * Multi-dimensional hash map.
40  */
41  template<class JHead_t, class JTail_t, class JValue_t, class JEvaluator_t>
42  struct JHashMap<JTypeList<JHead_t, JTail_t>, JValue_t, JEvaluator_t> :
43  public JHashMap<JHead_t, JHashMap<JTail_t, JValue_t, JEvaluator_t>, JEvaluator_t>
44  {
45 
46  typedef JHead_t key_type;
49  typedef JEvaluator_t evaluator_type;
52 
55  typedef typename map_type::iterator iterator;
57 
58  using map_type::get;
59  using map_type::put;
60  using map_type::has;
61  using map_type::erase;
62 
63 
64  /**
65  * Constructor.
66  *
67  * \param evaluator evaluator
68  */
69  JHashMap(const JEvaluator_t& evaluator = JEvaluator_t()) :
70  JHashMap<JHead_t, JHashMap<JTail_t, JValue_t, JEvaluator_t>, JEvaluator_t>(evaluator)
71  {}
72 
73 
74  class super_const_iterator; // forward declaration
75 
76 
77  /**
78  * Multidimensional iterator.
79  */
80  class super_iterator :
81  public JEquals <super_iterator>,
82  public JForwardIterator<super_iterator>
83  {
84 
88 
89  friend class JHashMap;
90 
91  public:
92 
93  /**
94  * Default constructor.
95  */
97  {}
98 
99 
100  /**
101  * Smart pointer operator.
102  *
103  * \return pointer to pair of iterators
104  */
106  {
107  return pointer_type(new value_type(i->first, second));
108  }
109 
110 
111  /**
112  * Dereference operator.
113  *
114  * \return multidimensional pair
115  */
117  {
118  return reference_type(i->first, *second);
119  }
120 
121 
122  /**
123  * Equality of super iterator.
124  *
125  * \param cursor super iterator
126  * \return true if equal; else false
127  */
128  virtual bool equals(const super_iterator& cursor) const
129  {
130  return i == cursor.i && (i == range.second || second.equals(cursor.second));
131  }
132 
133 
134  /**
135  * Increment super_iterator.
136  *
137  * \return true if valid; else false
138  */
139  virtual bool increment() override
140  {
141  if (!second.increment()) {
142 
143  while (++i != range.second) {
144 
145  second = i->second.super_begin();
146 
147  if (second != i->second.super_end()) {
148  break;
149  }
150  }
151  }
152 
153  return i != range.second;
154  }
155 
156 
157  /**
158  * Get multi-dimensional key.
159  *
160  * \return key
161  */
163  {
164  return multikey_type(i->first, second.getKey());
165  }
166 
167 
168  /**
169  * Get value.
170  *
171  * \return value
172  */
173  JValue_t& getValue()
174  {
175  return second.getValue();
176  }
177 
178 
179  private:
180  /**
181  * Constructor.
182  *
183  * \param __begin begin of data
184  * \param __end end of data
185  */
187  iterator __end) :
188  range(__begin, __end)
189  {
190  for (i = range.first; i != range.second; ++i) {
191 
192  second = i->second.super_begin();
193 
194  if (second != i->second.super_end()) {
195  break;
196  }
197  }
198  }
199 
200 
203  typename mapped_type::super_iterator second;
204  };
205 
206 
207  /**
208  * Multidimensional const_iterator.
209  */
210  class super_const_iterator :
211  public JEquals <super_const_iterator>,
212  public JForwardIterator<super_const_iterator>
213  {
214 
218 
219  friend class JHashMap;
220 
221  public:
222 
223  /**
224  * Default constructor.
225  */
227  {}
228 
229 
230  /**
231  * Copy constructor.
232  *
233  * \param cursor super_iterator
234  */
235  super_const_iterator(super_iterator cursor) :
236  range (cursor.range),
237  i (cursor.i),
238  second(cursor.second)
239  {}
240 
241 
242  /**
243  * Smart pointer operator.
244  *
245  * \return pointer to pair of iterators
246  */
248  {
249  return pointer_type(new value_type(i->first, second));
250  }
251 
252 
253  /**
254  * Dereference operator.
255  *
256  * \return multidimensional pair
257  */
259  {
260  return reference_type(i->first, *second);
261  }
262 
263 
264  /**
265  * Equality of super iterator.
266  *
267  * \param cursor super iterator
268  * \return true if equal; else false
269  */
270  virtual bool equals(const super_const_iterator& cursor) const
271  {
272  return i == cursor.i && (i == range.second || second.equals(cursor.second));
273  }
274 
275 
276  /**
277  * Increment super_iterator.
278  *
279  * \return true if valid; else false
280  */
281  virtual bool increment() override
282  {
283  if (!second.increment()) {
284 
285  while (++i != range.second) {
286 
287  second = i->second.super_begin();
288 
289  if (second != i->second.super_end()) {
290  break;
291  }
292  }
293  }
294 
295  return i != range.second;
296  }
297 
298 
299  /**
300  * Get multi-dimensional key.
301  *
302  * \return key
303  */
305  {
306  return multikey_type(i->first, second.getKey());
307  }
308 
309 
310  /**
311  * Get value.
312  *
313  * \return value
314  */
315  const JValue_t& getValue()
316  {
317  return second.getValue();
318  }
319 
320 
321  private:
322  /**
323  * Constructor.
324  *
325  * \param __begin begin of data
326  * \param __end end of data
327  */
329  const_iterator __end) :
330  range(__begin, __end)
331  {
332  for (i = range.first; i != range.second; ++i) {
333 
334  second = i->second.super_begin();
335 
336  if (second != i->second.super_end()) {
337  break;
338  }
339  }
340  }
341 
342 
345  typename mapped_type::super_const_iterator second;
346  };
347 
348 
349  /**
350  * Get super_iterator to begin of data.
351  *
352  * \return super iterator
353  */
354  super_const_iterator super_begin() const
355  {
356  return super_const_iterator(this->begin(), this->end());
357  }
358 
359 
360  /**
361  * Get super_iterator to end of data.
362  *
363  * \return super iterator
364  */
365  super_const_iterator super_end() const
366  {
367  return super_const_iterator(this->end(), this->end());
368  }
369 
370 
371  /**
372  * Get super_iterator to begin of data.
373  *
374  * \return super iterator
375  */
376  super_iterator super_begin()
377  {
378  return super_iterator(this->begin(), this->end());
379  }
380 
381 
382  /**
383  * Get super_iterator to end of data.
384  *
385  * \return super iterator
386  */
387  super_iterator super_end()
388  {
389  return super_iterator(this->end(), this->end());
390  }
391 
392 
393  /**
394  * Get mapped value.
395  *
396  * \param key key
397  * \return value
398  */
399  JValue_t& get(const multikey_type& key)
400  {
401  this->get(key.first).get(key.second);
402  }
403 
404 
405  /**
406  * Get mapped value.
407  *
408  * This method will throw an exception if given key is not present following the prerequisite of constness.
409  *
410  * \param key key
411  * \return value
412  */
413  const JValue_t& get(const multikey_type& key) const
414  {
415  this->get(key.first).get(key.second);
416  }
417 
418 
419  /**
420  * Put tuple-wise element (key,value) into collection.
421  *
422  * \param key key
423  * \param value value
424  */
425  void put(const multikey_type& key, const JValue_t& value)
426  {
427  this->get(key.first).put(key.second, value);
428  }
429 
430 
431  /**
432  * Erase element.
433  *
434  * \param key key
435  * \return true if element with given key has been erased; else false
436  */
437  bool erase(const multikey_type& key)
438  {
439  if (this->has(key.first))
440  return this->get(key.first).erase(key.second);
441  else
442  return false;
443  }
444 
445 
446  /**
447  * Test whether key is present.
448  *
449  * \param key key
450  * \return true if present; else false
451  */
452  bool has(const multikey_type& key) const
453  {
454  return map_type::has(key.first) && this->get(key.first).has(key.second);
455  }
456  };
457 
458 
459  /**
460  * Terminator class of recursive class JHashMap.
461  */
462  template<class JHead_t, class JValue_t, class JEvaluator_t>
463  struct JHashMap<JTypeList<JHead_t, JNullType>, JValue_t, JEvaluator_t> :
464  public JHashMap<JHead_t, JValue_t, JEvaluator_t>
465  {
466 
467  typedef JHead_t key_type;
468  typedef JValue_t mapped_type;
470  typedef JEvaluator_t evaluator_type;
471  typedef JHead_t multikey_type;
473 
476  typedef typename map_type::iterator iterator;
478 
479 
480  /**
481  * Constructor.
482  *
483  * \param evaluator evaluator
484  */
485  JHashMap(const JEvaluator_t& evaluator = JEvaluator_t()) :
486  JHashMap<JHead_t, JValue_t, JEvaluator_t>(evaluator)
487  {}
488 
489 
490  class super_const_iterator; // forward declaration
491 
492 
493  /**
494  * Terminator class of multidimensional iterator.
495  */
496  class super_iterator :
497  public JEquals <super_iterator>,
498  public JForwardIterator<super_iterator>
499  {
500 
504 
505  friend class JHashMap;
506 
507  public:
508 
509  /**
510  * Default constructor.
511  */
513  {}
514 
515 
516  /**
517  * Smart pointer operator.
518  *
519  * \return pointer to pair of iterators
520  */
522  {
523  return pointer_type(new value_type(i->first, i->second));
524  }
525 
526 
527  /**
528  * Dereference operator.
529  *
530  * \return multidimensional pair
531  */
533  {
534  return reference_type(i->first, i->second);
535  }
536 
537 
538  /**
539  * Equality of super iterator.
540  *
541  * \param cursor super iterator
542  * \return true if equal; else false
543  */
544  virtual bool equals(const super_iterator& cursor) const
545  {
546  return i == cursor.i;
547  }
548 
549 
550  /**
551  * Increment super_iterator.
552  *
553  * \return true if valid; else false
554  */
555  virtual bool increment() override
556  {
557  return ++i != range.second;
558  }
559 
560 
561  /**
562  * Get multi-dimensional key.
563  *
564  * \return key
565  */
567  {
568  return multikey_type(i->first);
569  }
570 
571 
572  /**
573  * Get value.
574  *
575  * \return value
576  */
577  JValue_t& getValue()
578  {
579  return i->second;
580  }
581 
582  private:
583  /**
584  * Constructor.
585  *
586  * \param __begin begin of data
587  * \param __end end of data
588  */
590  iterator __end) :
591  range(__begin, __end),
592  i (__begin)
593  {}
594 
595 
598  };
599 
600 
601  /**
602  * Terminator class of multidimensional const_iterator.
603  */
604  class super_const_iterator :
605  public JEquals <super_const_iterator>,
606  public JForwardIterator<super_const_iterator>
607  {
608 
612 
613  friend class JHashMap;
614 
615  public:
616 
617  /**
618  * Default constructor.
619  */
621  {}
622 
623 
624  /**
625  * Copy constructor.
626  *
627  * \param cursor super_iterator
628  */
629  super_const_iterator(super_iterator cursor) :
630  range(cursor.range),
631  i (cursor.i)
632  {}
633 
634 
635  /**
636  * Smart pointer operator.
637  *
638  * \return pointer to pair of iterators
639  */
641  {
642  return pointer_type(new value_type(i->first, i->second));
643  }
644 
645 
646  /**
647  * Dereference operator.
648  *
649  * \return multidimensional pair
650  */
652  {
653  return reference_type(i->first, i->second);
654  }
655 
656 
657  /**
658  * Equality of super iterator.
659  *
660  * \param cursor super iterator
661  * \return true if equal; else false
662  */
663  virtual bool equals(const super_const_iterator& cursor) const
664  {
665  return i == cursor.i;
666  }
667 
668 
669  /**
670  * Increment super_iterator.
671  *
672  * \return true if valid; else false
673  */
674  virtual bool increment() override
675  {
676  return ++i != range.second;
677  }
678 
679 
680  /**
681  * Get multi-dimensional key.
682  *
683  * \return key
684  */
686  {
687  return multikey_type(i->first);
688  }
689 
690 
691  /**
692  * Get value.
693  *
694  * \return value
695  */
696  const JValue_t& getValue()
697  {
698  return i->second;
699  }
700 
701  private:
702  /**
703  * Constructor.
704  *
705  * \param __begin begin of data
706  * \param __end end of data
707  */
709  const_iterator __end) :
710  range(__begin, __end),
711  i (__begin)
712  {}
713 
714 
717  };
718 
719 
720  /**
721  * Get super_iterator to begin of data.
722  *
723  * \return super_iterator
724  */
725  super_const_iterator super_begin() const
726  {
727  return super_const_iterator(this->begin(), this->end());
728  }
729 
730 
731  /**
732  * Get super_iterator to end of data.
733  *
734  * \return super_iterator
735  */
736  super_const_iterator super_end() const
737  {
738  return super_const_iterator(this->end(), this->end());
739  }
740 
741 
742  /**
743  * Get super_iterator to begin of data.
744  *
745  * \return super_iterator
746  */
747  super_iterator super_begin()
748  {
749  return super_iterator(this->begin(), this->end());
750  }
751 
752 
753  /**
754  * Get super_iterator to end of data.
755  *
756  * \return super_iterator
757  */
758  super_iterator super_end()
759  {
760  return super_iterator(this->end(), this->end());
761  }
762  };
763 }
764 
765 #endif
766 
virtual bool equals(const super_iterator &cursor) const
Equality of super iterator.
Template interface for method bool increment().
Template data structure.
Definition: JTuple.hh:44
Exceptions.
Interface for binary output.
bool has(const multikey_type &key) const
Test whether key is present.
container_type::const_reverse_iterator const_reverse_iterator
Definition: JHashMap.hh:87
General purpose class for hash map of unique keys.
Definition: JHashMap.hh:72
General purpose class for hash map of unique elements.
T get(const JHead &header)
Get object from header.
virtual bool equals(const super_iterator &cursor) const
Equality of super iterator.
JHashMap(const JEvaluator_t &evaluator=JEvaluator_t())
Constructor.
JHashMap< key_type, mapped_type, evaluator_type > map_type
JHashMap< key_type, mapped_type, evaluator_type > map_type
Type list.
Definition: JTypeList.hh:22
The template JSinglePointer class can be used to hold a pointer to an object.
Template specialisation for a pair of values.
Definition: JPair.hh:28
Auxiliary class for recursive type list generation.
Definition: JTypeList.hh:351
Template specialisation of JTuple for multiple data types.
Definition: JTuple.hh:210
Template definition of auxiliary base class for comparison of data structures.
Definition: JEquals.hh:24
Auxiliary class for no type definition.
Definition: JNullType.hh:19
super_const_iterator super_end() const
Get super_iterator to end of data.
void put(typename JClass< key_type >::argument_type key, typename JClass< mapped_type >::argument_type value)
Put pair-wise element (key,value) into collection.
Interface for binary input.
super_const_iterator super_begin() const
Get super_iterator to begin of data.
super_iterator super_end()
Get super_iterator to end of data.
z range($ZMAX-$ZMIN)< $MINIMAL_DZ." fi fi typeset -Z 4 STRING typeset -Z 2 FLOOR JPlot1D -f $
Template for generic class types.
Definition: JClass.hh:80
JPair< const key_type &, typename mapped_type::super_iterator & > value_type
super_iterator super_end()
Get super_iterator to end of data.
Data structure based on type list.
super_iterator super_begin()
Get super_iterator to begin of data.
JPair< const key_type &, typename mapped_type::super_const_iterator & > value_type
super_const_iterator super_end() const
Get super_iterator to end of data.
container_type::const_iterator const_iterator
Definition: JHashMap.hh:86
Exception for accessing an index in a collection that is outside of its range.
Definition: JException.hh:90
virtual bool equals(const super_const_iterator &cursor) const
Equality of super iterator.
virtual bool equals(const super_const_iterator &cursor) const
Equality of super iterator.
super_const_iterator super_begin() const
Get super_iterator to begin of data.
container_type::reverse_iterator reverse_iterator
Definition: JHashMap.hh:89
super_iterator super_begin()
Get super_iterator to begin of data.
std::pair< JKey_t, JValue_t > value_type
Definition: JHashMap.hh:80
bool has(const T &value) const
Test whether given value is present.
void put(const multikey_type &key, const JValue_t &value)
Put tuple-wise element (key,value) into collection.
JHashMap(const JEvaluator_t &evaluator=JEvaluator_t())
Constructor.
container_type::iterator iterator
Definition: JHashMap.hh:88