Jpp  pmt_effective_area_update
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JHashMap.hh
Go to the documentation of this file.
1 #ifndef __JTOOLS__JHASHMAP__
2 #define __JTOOLS__JHASHMAP__
3 
4 #include <utility>
5 
6 #include "JLang/JException.hh"
7 #include "JLang/JClass.hh"
11 #include "JTools/JHashEvaluator.hh"
12 #include "JTools/JRouter.hh"
13 
14 
15 /**
16  * \file
17  *
18  * General purpose class for hash map of unique elements.
19  * \author mdejong
20  */
21 namespace JTOOLS {}
22 namespace JPP { using namespace JTOOLS; }
23 
24 namespace JTOOLS {
25 
26  using JLANG::JClass;
28 
29  /**
30  * Auxiliary class for hash evaluation of map keys and elements.
31  */
32  template<class JEvaluator_t>
34  public JEvaluator_t
35  {
36  using JEvaluator_t::operator();
37 
38  /**
39  * Constructor.
40  *
41  * \param evaluator evaluator
42  */
43  JHashMapEvaluator(const JEvaluator_t& evaluator) :
44  JEvaluator_t(evaluator)
45  {}
46 
47 
48  /**
49  * Get hash value of given pair.
50  *
51  * \param pair pair
52  * \return hash value
53  */
54  template<class JKey_t, class JValue_t>
55  inline int operator()(const std::pair<JKey_t, JValue_t>& pair)
56  {
57  return JEvaluator_t::operator()(pair.first);
58  }
59  };
60 
61 
62  /**
63  * General purpose class for hash map of unique keys.
64  *
65  * The keys in a hash collection are unique according to the specified evaluation.
66  * The evaluation of keys corresponds to a unary method returning an integer value for a given key;
67  * The default evaluator is JHashEvaluator.
68  *
69  * This class implements the JMappableCollection interface.
70  */
71  template<class JKey_t, class JValue_t, class JEvaluator_t = JHashEvaluator>
72  struct JHashMap :
73  public JHashCollection<std::pair<JKey_t, JValue_t>, JHashMapEvaluator<JEvaluator_t> >,
74  public JMappableCollection<JKey_t, JValue_t>
75  {
76  public:
77 
78  typedef JKey_t key_type;
79  typedef JValue_t mapped_type;
82 
85 
86  typedef typename container_type::const_iterator const_iterator;
87  typedef typename container_type::const_reverse_iterator const_reverse_iterator;
88  typedef typename container_type::iterator iterator;
89  typedef typename container_type::reverse_iterator reverse_iterator;
90 
92 
93 
94  /**
95  * Constructor.
96  *
97  * \param evaluator evaluator
98  */
99  JHashMap(const JEvaluator_t& evaluator = JEvaluator_t()) :
100  collection_type(evaluator)
101  {}
102 
103 
104  /**
105  * Clear.
106  */
107  virtual void clear() override
108  {
110  }
111 
112 
113  /**
114  * Get mapped value.
115  *
116  * \param key key
117  * \return mapped value
118  */
119  virtual mapped_type& get(typename JClass<key_type>::argument_type key) override
120  {
121  const int ival = this->getValue(key);
122 
123  if (!this->router.has(ival)) {
125  }
126 
127  return container_type::operator[](this->router.get(ival)).second;
128  }
129 
130 
131  /**
132  * Get mapped value.
133  *
134  * This method will throw an exception if given key is not present following the prerequisite of constness.
135  *
136  * \param key key
137  * \return mapped value
138  */
139  virtual const mapped_type& get(typename JClass<key_type>::argument_type key) const override
140  {
141  const int ival = this->getValue(key);
142 
143  if (this->router.has(ival))
144  return container_type::operator[](this->router.get(ival)).second;
145  else
146  THROW(JIndexOutOfRange, "JHasMap::get(): invalid key " << STREAM("?") << key);
147  }
148  };
149 }
150 
151 #endif
152 
JKey_t key_type
Definition: JHashMap.hh:78
virtual void clear() override
Clear.
Definition: JHashMap.hh:107
Exceptions.
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
JValue_t mapped_type
Definition: JHashMap.hh:79
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
Template interface definition for associative collection of elements.
General purpose class for a hash collection of unique elements.
JHashMapEvaluator< JEvaluator_t > evaluator_type
Definition: JHashMap.hh:81
virtual bool insert(const value_type &element)
Insert element.
Auxiliary class for hash evaluation of map keys and elements.
Definition: JHashMap.hh:33
collection_type::container_type container_type
Definition: JHashMap.hh:84
JArgument< T >::argument_type argument_type
Definition: JClass.hh:82
Auxiliary data structure for handling std::ostream.
int operator()(const std::pair< JKey_t, JValue_t > &pair)
Get hash value of given pair.
Definition: JHashMap.hh:55
General purpose class for hash collection of unique elements.
Template for generic class types.
Definition: JClass.hh:80
JHashCollection< value_type, evaluator_type > collection_type
Definition: JHashMap.hh:83
JHashMap(const JEvaluator_t &evaluator=JEvaluator_t())
Constructor.
Definition: JHashMap.hh:99
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
container_type::reverse_iterator reverse_iterator
Definition: JHashMap.hh:89
std::pair< JKey_t, JValue_t > value_type
Definition: JHashMap.hh:80
JHashMapEvaluator(const JEvaluator_t &evaluator)
Constructor.
Definition: JHashMap.hh:43
container_type::iterator iterator
Definition: JHashMap.hh:88
JHashMapEvaluator< JEvaluator_t > getValue
Function object for evaluation of element.