Jpp  18.3.0-rc.1
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  * Reset values.
115  */
116  void reset()
117  {
118  for (iterator i = this->begin(); i != this->end(); ++i) {
119  i->second = mapped_type();
120  }
121  }
122 
123 
124  /**
125  * Set values corresponding to keys in given source.
126  *
127  * \param source source
128  * \param value value
129  */
130  void set(const JHashMap& source, const mapped_type& value)
131  {
132  this->clear();
133 
134  for (const_iterator i = source.begin(); i != source.end(); ++i) {
135  (*this)[i->first] = value;
136  }
137  }
138 
139 
140  /**
141  * Get mapped value.
142  *
143  * \param key key
144  * \return mapped value
145  */
146  virtual mapped_type& get(typename JClass<key_type>::argument_type key) override
147  {
148  const int ival = this->getValue(key);
149 
150  if (!this->router.has(ival)) {
152  }
153 
154  return container_type::operator[](this->router.get(ival)).second;
155  }
156 
157 
158  /**
159  * Get mapped value.
160  *
161  * This method will throw an exception if given key is not present following the prerequisite of constness.
162  *
163  * \param key key
164  * \return mapped value
165  */
166  virtual const mapped_type& get(typename JClass<key_type>::argument_type key) const override
167  {
168  const int ival = this->getValue(key);
169 
170  if (this->router.has(ival))
171  return container_type::operator[](this->router.get(ival)).second;
172  else
173  THROW(JIndexOutOfRange, "JHashMap::get(): invalid key " << STREAM("?") << key);
174  }
175  };
176 }
177 
178 #endif
179 
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:712
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
void reset()
Reset values.
Definition: JHashMap.hh:116
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.
void set(const JHashMap &source, const mapped_type &value)
Set values corresponding to keys in given source.
Definition: JHashMap.hh:130
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:106
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.