Jpp
 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::JNullType;
28  using JLANG::JTypeList;
29  using JLANG::JTYPELIST;
30  using JLANG::JClass;
32  using JIO::JReader;
33  using JIO::JWriter;
34 
35 
36  /**
37  * Multi-dimensional hash map.
38  */
39  template<class JHead_t, class JTail_t, class JValue_t, class JEvaluator_t>
40  struct JHashMap<JTypeList<JHead_t, JTail_t>, JValue_t, JEvaluator_t> :
41  public JHashMap<JHead_t, JHashMap<JTail_t, JValue_t, JEvaluator_t>, JEvaluator_t>
42  {
43 
44  typedef JHead_t key_type;
47  typedef JEvaluator_t evaluator_type;
50 
53  typedef typename map_type::iterator iterator;
55 
56  using map_type::get;
57  using map_type::put;
58  using map_type::has;
59  using map_type::erase;
60 
61 
62  /**
63  * Constructor.
64  *
65  * \param evaluator evaluator
66  */
67  JHashMap(const JEvaluator_t& evaluator = JEvaluator_t()) :
68  JHashMap<JHead_t, JHashMap<JTail_t, JValue_t, JEvaluator_t>, JEvaluator_t>(evaluator)
69  {}
70 
71 
72  class super_const_iterator; // forward declaration
73 
74 
75  /**
76  * Multidimensional iterator.
77  */
78  class super_iterator :
79  public JLANG::JEquals <super_iterator>,
80  public JLANG::JForwardIterator<super_iterator>
81  {
82 
86 
87  friend class JHashMap;
88 
89  public:
90 
91  /**
92  * Default constructor.
93  */
95  {}
96 
97 
98  /**
99  * Smart pointer operator.
100  *
101  * \return pointer to pair of iterators
102  */
104  {
105  return pointer_type(new value_type(i->first, second));
106  }
107 
108 
109  /**
110  * Dereference operator.
111  *
112  * \return multidimensional pair
113  */
115  {
116  return reference_type(i->first, *second);
117  }
118 
119 
120  /**
121  * Equality of super iterator.
122  *
123  * \param cursor super iterator
124  * \return true if equal; else false
125  */
126  virtual bool equals(const super_iterator& cursor) const
127  {
128  return i == cursor.i && (i == range.second || second.equals(cursor.second));
129  }
130 
131 
132  /**
133  * Increment super_iterator.
134  *
135  * \return true if valid; else false
136  */
137  virtual bool increment()
138  {
139  if (!second.increment()) {
140 
141  while (++i != range.second) {
142 
143  second = i->second.super_begin();
144 
145  if (second != i->second.super_end()) {
146  break;
147  }
148  }
149  }
150 
151  return i != range.second;
152  }
153 
154 
155  /**
156  * Get multi-dimensional key.
157  *
158  * \return key
159  */
161  {
162  return multikey_type(i->first, second.getKey());
163  }
164 
165 
166  /**
167  * Get value.
168  *
169  * \return value
170  */
171  JValue_t& getValue()
172  {
173  return second.getValue();
174  }
175 
176 
177  private:
178  /**
179  * Constructor.
180  *
181  * \param __begin begin of data
182  * \param __end end of data
183  */
185  iterator __end) :
186  range(__begin, __end)
187  {
188  for (i = range.first; i != range.second; ++i) {
189 
190  second = i->second.super_begin();
191 
192  if (second != i->second.super_end()) {
193  break;
194  }
195  }
196  }
197 
198 
201  typename mapped_type::super_iterator second;
202  };
203 
204 
205  /**
206  * Multidimensional const_iterator.
207  */
208  class super_const_iterator :
209  public JLANG::JEquals <super_const_iterator>,
210  public JLANG::JForwardIterator<super_const_iterator>
211  {
212 
216 
217  friend class JHashMap;
218 
219  public:
220 
221  /**
222  * Default constructor.
223  */
225  {}
226 
227 
228  /**
229  * Copy constructor.
230  *
231  * \param cursor super_iterator
232  */
233  super_const_iterator(super_iterator cursor) :
234  range (cursor.range),
235  i (cursor.i),
236  second(cursor.second)
237  {}
238 
239 
240  /**
241  * Smart pointer operator.
242  *
243  * \return pointer to pair of iterators
244  */
246  {
247  return pointer_type(new value_type(i->first, second));
248  }
249 
250 
251  /**
252  * Dereference operator.
253  *
254  * \return multidimensional pair
255  */
257  {
258  return reference_type(i->first, *second);
259  }
260 
261 
262  /**
263  * Equality of super iterator.
264  *
265  * \param cursor super iterator
266  * \return true if equal; else false
267  */
268  virtual bool equals(const super_const_iterator& cursor) const
269  {
270  return i == cursor.i && (i == range.second || second.equals(cursor.second));
271  }
272 
273 
274  /**
275  * Increment super_iterator.
276  *
277  * \return true if valid; else false
278  */
279  virtual bool increment()
280  {
281  if (!second.increment()) {
282 
283  while (++i != range.second) {
284 
285  second = i->second.super_begin();
286 
287  if (second != i->second.super_end()) {
288  break;
289  }
290  }
291  }
292 
293  return i != range.second;
294  }
295 
296 
297  /**
298  * Get multi-dimensional key.
299  *
300  * \return key
301  */
303  {
304  return multikey_type(i->first, second.getKey());
305  }
306 
307 
308  /**
309  * Get value.
310  *
311  * \return value
312  */
313  const JValue_t& getValue()
314  {
315  return second.getValue();
316  }
317 
318 
319  private:
320  /**
321  * Constructor.
322  *
323  * \param __begin begin of data
324  * \param __end end of data
325  */
327  const_iterator __end) :
328  range(__begin, __end)
329  {
330  for (i = range.first; i != range.second; ++i) {
331 
332  second = i->second.super_begin();
333 
334  if (second != i->second.super_end()) {
335  break;
336  }
337  }
338  }
339 
340 
343  typename mapped_type::super_const_iterator second;
344  };
345 
346 
347  /**
348  * Get super_iterator to begin of data.
349  *
350  * \return super iterator
351  */
352  super_const_iterator super_begin() const
353  {
354  return super_const_iterator(this->begin(), this->end());
355  }
356 
357 
358  /**
359  * Get super_iterator to end of data.
360  *
361  * \return super iterator
362  */
363  super_const_iterator super_end() const
364  {
365  return super_const_iterator(this->end(), this->end());
366  }
367 
368 
369  /**
370  * Get super_iterator to begin of data.
371  *
372  * \return super iterator
373  */
374  super_iterator super_begin()
375  {
376  return super_iterator(this->begin(), this->end());
377  }
378 
379 
380  /**
381  * Get super_iterator to end of data.
382  *
383  * \return super iterator
384  */
385  super_iterator super_end()
386  {
387  return super_iterator(this->end(), this->end());
388  }
389 
390 
391  /**
392  * Get mapped value.
393  *
394  * \param key key
395  * \return value
396  */
397  JValue_t& get(const multikey_type& key)
398  {
399  this->get(key.first).get(key.second);
400  }
401 
402 
403  /**
404  * Get mapped value.
405  *
406  * This method will throw an exception if given key is not present following the prerequisite of constness.
407  *
408  * \param key key
409  * \return value
410  */
411  const JValue_t& get(const multikey_type& key) const
412  {
413  this->get(key.first).get(key.second);
414  }
415 
416 
417  /**
418  * Put tuple-wise element (key,value) into collection.
419  *
420  * \param key key
421  * \param value value
422  */
423  void put(const multikey_type& key, const JValue_t& value)
424  {
425  this->get(key.first).put(key.second, value);
426  }
427 
428 
429  /**
430  * Erase element.
431  *
432  * \param key key
433  * \return true if element with given key has been erased; else false
434  */
435  bool erase(const multikey_type& key)
436  {
437  if (this->has(key.first))
438  return this->get(key.first).erase(key.second);
439  else
440  return false;
441  }
442 
443 
444  /**
445  * Test whether key is present.
446  *
447  * \param key key
448  * \return true if present; else false
449  */
450  bool has(const multikey_type& key) const
451  {
452  return map_type::has(key.first) && this->get(key.first).has(key.second);
453  }
454  };
455 
456 
457  /**
458  * Terminator class of recursive class JHashMap.
459  */
460  template<class JHead_t, class JValue_t, class JEvaluator_t>
461  struct JHashMap<JTypeList<JHead_t, JNullType>, JValue_t, JEvaluator_t> :
462  public JHashMap<JHead_t, JValue_t, JEvaluator_t>
463  {
464 
465  typedef JHead_t key_type;
466  typedef JValue_t mapped_type;
468  typedef JEvaluator_t evaluator_type;
469  typedef JHead_t multikey_type;
471 
474  typedef typename map_type::iterator iterator;
476 
477 
478  /**
479  * Constructor.
480  *
481  * \param evaluator evaluator
482  */
483  JHashMap(const JEvaluator_t& evaluator = JEvaluator_t()) :
484  JHashMap<JHead_t, JValue_t, JEvaluator_t>(evaluator)
485  {}
486 
487 
488  class super_const_iterator; // forward declaration
489 
490 
491  /**
492  * Terminator class of multidimensional iterator.
493  */
494  class super_iterator :
495  public JLANG::JEquals <super_iterator>,
496  public JLANG::JForwardIterator<super_iterator>
497  {
498 
502 
503  friend class JHashMap;
504 
505  public:
506 
507  /**
508  * Default constructor.
509  */
511  {}
512 
513 
514  /**
515  * Smart pointer operator.
516  *
517  * \return pointer to pair of iterators
518  */
520  {
521  return pointer_type(new value_type(i->first, i->second));
522  }
523 
524 
525  /**
526  * Dereference operator.
527  *
528  * \return multidimensional pair
529  */
531  {
532  return reference_type(i->first, i->second);
533  }
534 
535 
536  /**
537  * Equality of super iterator.
538  *
539  * \param cursor super iterator
540  * \return true if equal; else false
541  */
542  virtual bool equals(const super_iterator& cursor) const
543  {
544  return i == cursor.i;
545  }
546 
547 
548  /**
549  * Increment super_iterator.
550  *
551  * \return true if valid; else false
552  */
553  virtual bool increment()
554  {
555  return ++i != range.second;
556  }
557 
558 
559  /**
560  * Get multi-dimensional key.
561  *
562  * \return key
563  */
565  {
566  return multikey_type(i->first);
567  }
568 
569 
570  /**
571  * Get value.
572  *
573  * \return value
574  */
575  JValue_t& getValue()
576  {
577  return i->second;
578  }
579 
580  private:
581  /**
582  * Constructor.
583  *
584  * \param __begin begin of data
585  * \param __end end of data
586  */
588  iterator __end) :
589  range(__begin, __end),
590  i (__begin)
591  {}
592 
593 
596  };
597 
598 
599  /**
600  * Terminator class of multidimensional const_iterator.
601  */
602  class super_const_iterator :
603  public JLANG::JEquals <super_const_iterator>,
604  public JLANG::JForwardIterator<super_const_iterator>
605  {
606 
610 
611  friend class JHashMap;
612 
613  public:
614 
615  /**
616  * Default constructor.
617  */
619  {}
620 
621 
622  /**
623  * Copy constructor.
624  *
625  * \param cursor super_iterator
626  */
627  super_const_iterator(super_iterator cursor) :
628  range(cursor.range),
629  i (cursor.i)
630  {}
631 
632 
633  /**
634  * Smart pointer operator.
635  *
636  * \return pointer to pair of iterators
637  */
639  {
640  return pointer_type(new value_type(i->first, i->second));
641  }
642 
643 
644  /**
645  * Dereference operator.
646  *
647  * \return multidimensional pair
648  */
650  {
651  return reference_type(i->first, i->second);
652  }
653 
654 
655  /**
656  * Equality of super iterator.
657  *
658  * \param cursor super iterator
659  * \return true if equal; else false
660  */
661  virtual bool equals(const super_const_iterator& cursor) const
662  {
663  return i == cursor.i;
664  }
665 
666 
667  /**
668  * Increment super_iterator.
669  *
670  * \return true if valid; else false
671  */
672  virtual bool increment()
673  {
674  return ++i != range.second;
675  }
676 
677 
678  /**
679  * Get multi-dimensional key.
680  *
681  * \return key
682  */
684  {
685  return multikey_type(i->first);
686  }
687 
688 
689  /**
690  * Get value.
691  *
692  * \return value
693  */
694  const JValue_t& getValue()
695  {
696  return i->second;
697  }
698 
699  private:
700  /**
701  * Constructor.
702  *
703  * \param __begin begin of data
704  * \param __end end of data
705  */
707  const_iterator __end) :
708  range(__begin, __end),
709  i (__begin)
710  {}
711 
712 
715  };
716 
717 
718  /**
719  * Get super_iterator to begin of data.
720  *
721  * \return super_iterator
722  */
723  super_const_iterator super_begin() const
724  {
725  return super_const_iterator(this->begin(), this->end());
726  }
727 
728 
729  /**
730  * Get super_iterator to end of data.
731  *
732  * \return super_iterator
733  */
734  super_const_iterator super_end() const
735  {
736  return super_const_iterator(this->end(), this->end());
737  }
738 
739 
740  /**
741  * Get super_iterator to begin of data.
742  *
743  * \return super_iterator
744  */
745  super_iterator super_begin()
746  {
747  return super_iterator(this->begin(), this->end());
748  }
749 
750 
751  /**
752  * Get super_iterator to end of data.
753  *
754  * \return super_iterator
755  */
756  super_iterator super_end()
757  {
758  return super_iterator(this->end(), this->end());
759  }
760  };
761 }
762 
763 #endif
764 
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:86
General purpose class for hash map of unique keys.
Definition: JHashMap.hh:71
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.
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:85
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:88
super_iterator super_begin()
Get super_iterator to begin of data.
std::pair< JKey_t, JValue_t > value_type
Definition: JHashMap.hh:79
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:87