Jpp test-rotations-new
the software that should make you happy
Loading...
Searching...
No Matches
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"
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 */
21namespace JTOOLS {}
22namespace JPP { using namespace JTOOLS; }
23
24namespace 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>
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
91 using JMappableCollection<key_type, mapped_type>::operator[];
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
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
General purpose class for a hash collection of unique elements.
Exception for accessing an index in a collection that is outside of its range.
General purpose class for hash collection of unique elements.
virtual bool insert(const value_type &element)
Insert element.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for multi-dimensional interpolations and histograms.
Template for generic class types.
Definition JClass.hh:80
JArgument< T >::argument_type argument_type
Definition JClass.hh:82
Auxiliary class for hash evaluation of map keys and elements.
Definition JHashMap.hh:35
int operator()(const std::pair< JKey_t, JValue_t > &pair)
Get hash value of given pair.
Definition JHashMap.hh:55
JHashMapEvaluator(const JEvaluator_t &evaluator)
Constructor.
Definition JHashMap.hh:43
General purpose class for hash map of unique keys.
Definition JHashMap.hh:75
void set(const JHashMap &source, const mapped_type &value)
Set values corresponding to keys in given source.
Definition JHashMap.hh:130
JValue_t mapped_type
Definition JHashMap.hh:79
virtual mapped_type & get(typename JClass< key_type >::argument_type key) override
Get mapped value.
Definition JHashMap.hh:146
container_type::const_iterator const_iterator
Definition JHashMap.hh:86
virtual const mapped_type & get(typename JClass< key_type >::argument_type key) const override
Get mapped value.
Definition JHashMap.hh:166
JHashCollection< value_type, evaluator_type > collection_type
Definition JHashMap.hh:83
JHashMapEvaluator< JEvaluator_t > evaluator_type
Definition JHashMap.hh:81
container_type::iterator iterator
Definition JHashMap.hh:88
void reset()
Reset values.
Definition JHashMap.hh:116
container_type::const_reverse_iterator const_reverse_iterator
Definition JHashMap.hh:87
collection_type::container_type container_type
Definition JHashMap.hh:84
container_type::reverse_iterator reverse_iterator
Definition JHashMap.hh:89
JHashMap(const JEvaluator_t &evaluator=JEvaluator_t())
Constructor.
Definition JHashMap.hh:99
virtual void clear() override
Clear.
Definition JHashMap.hh:107
std::pair< JKey_t, JValue_t > value_type
Definition JHashMap.hh:80
Template interface definition for associative collection of elements.
Auxiliary data structure for handling std::ostream.