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