Jpp
Classes | Public Types | Public Member Functions | Public Attributes | Protected Attributes | Private Member Functions | Friends | List of all members
JTOOLS::JHashMap< JKey_t, JValue_t, JEvaluator_t > Struct Template Reference

General purpose class for hash map of (key, value) pairs. More...

#include <JHashMap.hh>

Inheritance diagram for JTOOLS::JHashMap< JKey_t, JValue_t, JEvaluator_t >:
std::vector< std::pair< JKey_t, JValue_t > > JTOOLS::JMappableCollection< JKey_t, JValue_t > JDETECTOR::JPMTDefaultSimulator JDETECTOR::JPMTRunByRunSimulator

Classes

struct  JComparator
 Auxiliary class for ordering of objects in the collection by the hash value of their keys. More...
 

Public Types

typedef JKey_t key_type
 
typedef JValue_t mapped_type
 
typedef std::pair< JKey_t, JValue_t > value_type
 
typedef JEvaluator_t evaluator_type
 
typedef std::vector< value_typecontainer_type
 
typedef container_type::const_iterator const_iterator
 
typedef container_type::const_reverse_iterator const_reverse_iterator
 
typedef container_type::iterator iterator
 
typedef container_type::reverse_iterator reverse_iterator
 
typedef std::pair< const_iterator, bool > pair_type
 

Public Member Functions

 JHashMap (const JEvaluator_t &evaluator=JEvaluator_t())
 Constructor. More...
 
virtual void clear ()
 Clear. More...
 
virtual mapped_typeget (typename JClass< key_type >::argument_type key)
 Get mapped value. More...
 
const mapped_typeget (typename JClass< key_type >::argument_type key) const
 Get mapped value. More...
 
pair_type insert (const value_type &element)
 Insert element. More...
 
void erase (iterator pos)
 Erase element at given position. More...
 
void erase (iterator __begin, iterator __end)
 Erase elements in given range. More...
 
bool erase (typename JClass< key_type >::argument_type key) const
 Erase element. More...
 
bool has (typename JClass< key_type >::argument_type key) const
 Test whether key is present. More...
 
const JComparatorgetComparator () const
 Get comparator. More...
 
mapped_typeoperator[] (typename JClass< key_type >::argument_type key)
 Get mapped value. More...
 
void put (typename JClass< key_type > ::argument_type key, typename JClass< mapped_type >::argument_type value)
 Put pair-wise element (key,value) into collection. More...
 

Public Attributes

JEvaluator_t getValue
 Function object for evaluation of key. More...
 

Protected Attributes

JComparator compare
 Function object for comparison. More...
 
JRouter< int > router
 

Private Member Functions

void resize ()
 
void push_back ()
 
void pop_back ()
 

Friends

JReaderoperator>> (JReader &in, JHashMap &object)
 Read hash map from input. More...
 
JWriteroperator<< (JWriter &out, const JHashMap &object)
 Write hash map to output. More...
 

Detailed Description

template<class JKey_t, class JValue_t, class JEvaluator_t = JHashEvaluator>
struct JTOOLS::JHashMap< JKey_t, JValue_t, JEvaluator_t >

General purpose class for hash map of (key, value) pairs.

The elements in a hash map are sorted according to the specified evaluation. The evaluation of elements corresponds to a unary method returning an integer value for a given key; The default evaluator is JHashEvaluator.

This class implements the JMappableCollection interface.

Definition at line 53 of file JHashMap.hh.

Member Typedef Documentation

◆ key_type

template<class JKey_t, class JValue_t, class JEvaluator_t = JHashEvaluator>
typedef JKey_t JTOOLS::JHashMap< JKey_t, JValue_t, JEvaluator_t >::key_type

Definition at line 59 of file JHashMap.hh.

◆ mapped_type

template<class JKey_t, class JValue_t, class JEvaluator_t = JHashEvaluator>
typedef JValue_t JTOOLS::JHashMap< JKey_t, JValue_t, JEvaluator_t >::mapped_type

Definition at line 60 of file JHashMap.hh.

◆ value_type

template<class JKey_t, class JValue_t, class JEvaluator_t = JHashEvaluator>
typedef std::pair<JKey_t, JValue_t> JTOOLS::JHashMap< JKey_t, JValue_t, JEvaluator_t >::value_type

Definition at line 61 of file JHashMap.hh.

◆ evaluator_type

template<class JKey_t, class JValue_t, class JEvaluator_t = JHashEvaluator>
typedef JEvaluator_t JTOOLS::JHashMap< JKey_t, JValue_t, JEvaluator_t >::evaluator_type

Definition at line 62 of file JHashMap.hh.

◆ container_type

template<class JKey_t, class JValue_t, class JEvaluator_t = JHashEvaluator>
typedef std::vector<value_type> JTOOLS::JHashMap< JKey_t, JValue_t, JEvaluator_t >::container_type

Definition at line 64 of file JHashMap.hh.

◆ const_iterator

template<class JKey_t, class JValue_t, class JEvaluator_t = JHashEvaluator>
typedef container_type::const_iterator JTOOLS::JHashMap< JKey_t, JValue_t, JEvaluator_t >::const_iterator

Definition at line 66 of file JHashMap.hh.

◆ const_reverse_iterator

template<class JKey_t, class JValue_t, class JEvaluator_t = JHashEvaluator>
typedef container_type::const_reverse_iterator JTOOLS::JHashMap< JKey_t, JValue_t, JEvaluator_t >::const_reverse_iterator

Definition at line 67 of file JHashMap.hh.

◆ iterator

template<class JKey_t, class JValue_t, class JEvaluator_t = JHashEvaluator>
typedef container_type::iterator JTOOLS::JHashMap< JKey_t, JValue_t, JEvaluator_t >::iterator

Definition at line 68 of file JHashMap.hh.

◆ reverse_iterator

template<class JKey_t, class JValue_t, class JEvaluator_t = JHashEvaluator>
typedef container_type::reverse_iterator JTOOLS::JHashMap< JKey_t, JValue_t, JEvaluator_t >::reverse_iterator

Definition at line 69 of file JHashMap.hh.

◆ pair_type

template<class JKey_t, class JValue_t, class JEvaluator_t = JHashEvaluator>
typedef std::pair<const_iterator, bool> JTOOLS::JHashMap< JKey_t, JValue_t, JEvaluator_t >::pair_type

Definition at line 71 of file JHashMap.hh.

Constructor & Destructor Documentation

◆ JHashMap()

template<class JKey_t, class JValue_t, class JEvaluator_t = JHashEvaluator>
JTOOLS::JHashMap< JKey_t, JValue_t, JEvaluator_t >::JHashMap ( const JEvaluator_t &  evaluator = JEvaluator_t())
inline

Constructor.

Parameters
evaluatorevaluator

Definition at line 131 of file JHashMap.hh.

131  :
132  getValue(evaluator),
133  compare (evaluator),
134  router (-1) // default address
135  {}

Member Function Documentation

◆ clear()

template<class JKey_t, class JValue_t, class JEvaluator_t = JHashEvaluator>
virtual void JTOOLS::JHashMap< JKey_t, JValue_t, JEvaluator_t >::clear ( )
inlinevirtual

Clear.

Implements JTOOLS::JMappableCollection< JKey_t, JValue_t >.

Definition at line 141 of file JHashMap.hh.

142  {
143  // reset internal router
144 
145  for (iterator i = this->begin(); i != this->end(); ++i) {
146  router.put(this->getValue(i->first), router.getDefaultAddress());
147  }
148 
149  container_type::clear();
150  }

◆ get() [1/2]

template<class JKey_t, class JValue_t, class JEvaluator_t = JHashEvaluator>
virtual mapped_type& JTOOLS::JHashMap< JKey_t, JValue_t, JEvaluator_t >::get ( typename JClass< key_type >::argument_type  key)
inlinevirtual

Get mapped value.

Parameters
keykey
Returns
mapped value

Implements JTOOLS::JMappableCollection< JKey_t, JValue_t >.

Definition at line 159 of file JHashMap.hh.

160  {
161  const int ival = this->getValue(key);
162 
163  if (!router.has(ival)) {
164  insert(value_type(key, mapped_type()));
165  }
166 
167  return container_type::operator[](router.get(ival)).second;
168  }

◆ get() [2/2]

template<class JKey_t, class JValue_t, class JEvaluator_t = JHashEvaluator>
const mapped_type& JTOOLS::JHashMap< JKey_t, JValue_t, JEvaluator_t >::get ( typename JClass< key_type >::argument_type  key) const
inline

Get mapped value.

This method will throw an exception if given key is not present following the prerequisite of constness.

Parameters
keykey
Returns
mapped value

Definition at line 179 of file JHashMap.hh.

180  {
181  const int ival = this->getValue(key);
182 
183  if (router.has(ival))
184  return container_type::operator[](router.get(ival)).second;
185  else
186  THROW(JIndexOutOfRange, "JHasMap::get(): invalid key.");
187  }

◆ insert()

template<class JKey_t, class JValue_t, class JEvaluator_t = JHashEvaluator>
pair_type JTOOLS::JHashMap< JKey_t, JValue_t, JEvaluator_t >::insert ( const value_type element)
inline

Insert element.

Parameters
elementelement
Returns
(position, status), where status is true if inserted; else false

Definition at line 196 of file JHashMap.hh.

197  {
198  using namespace std;
199 
200  const int ival = this->getValue(element.first);
201 
202  if (!router.has(ival)) {
203 
204  iterator i = container_type::insert(lower_bound(this->begin(), this->end(), element.first, compare), element);
205 
206  router.put(ival, distance(this->begin(), i));
207 
208  for (iterator __i = i; ++__i != this->end(); ) {
209  router.put(this->getValue(__i->first), distance(this->begin(), __i));
210  }
211 
212  return pair_type(i, true);
213 
214  } else {
215 
216  return pair_type(this->end(), false);
217  }
218  }

◆ erase() [1/3]

template<class JKey_t, class JValue_t, class JEvaluator_t = JHashEvaluator>
void JTOOLS::JHashMap< JKey_t, JValue_t, JEvaluator_t >::erase ( iterator  pos)
inline

Erase element at given position.

Parameters
posvalid position

Definition at line 226 of file JHashMap.hh.

227  {
228  this->router.put(this->getValue(*pos), this->router.getDefaultAddress());
229 
230  iterator i = pos;
231 
232  container_type::erase(i++);
233 
234  for ( ; i != this->end(); ++i) {
235  this->router.put(this->getValue(*i), distance(this->begin(), i));
236  }
237  }

◆ erase() [2/3]

template<class JKey_t, class JValue_t, class JEvaluator_t = JHashEvaluator>
void JTOOLS::JHashMap< JKey_t, JValue_t, JEvaluator_t >::erase ( iterator  __begin,
iterator  __end 
)
inline

Erase elements in given range.

Parameters
__beginbegin position (included)
__endend position (excluded)

Definition at line 246 of file JHashMap.hh.

247  {
248  iterator i = __begin;
249 
250  while (i != __end) {
251 
252  this->router.put(this->getValue(*i), this->router.getDefaultAddress());
253 
254  container_type::erase(i++);
255  }
256 
257  for ( ; i != this->end(); ++i) {
258  this->router.put(this->getValue(*i), distance(this->begin(), i));
259  }
260  }

◆ erase() [3/3]

template<class JKey_t, class JValue_t, class JEvaluator_t = JHashEvaluator>
bool JTOOLS::JHashMap< JKey_t, JValue_t, JEvaluator_t >::erase ( typename JClass< key_type >::argument_type  key) const
inline

Erase element.

Parameters
keykey
Returns
true if element with given key has been erased; else false

Definition at line 269 of file JHashMap.hh.

270  {
271  const int ival = this->getValue(key);
272 
273  if (router.has(ival)) {
274 
275  iterator pos = this->begin();
276 
277  std::advance(pos, this->router.get(ival));
278 
279  iterator i = container_type::erase(pos);
280 
281  for ( ; i != this->end(); ++i) {
282  this->router.put(this->getValue(*i), distance(this->begin(), i));
283  }
284 
285  return true;
286  }
287 
288  return false;
289  }

◆ has()

template<class JKey_t, class JValue_t, class JEvaluator_t = JHashEvaluator>
bool JTOOLS::JHashMap< JKey_t, JValue_t, JEvaluator_t >::has ( typename JClass< key_type >::argument_type  key) const
inline

Test whether key is present.

Parameters
keykey
Returns
true if present; else false

Definition at line 298 of file JHashMap.hh.

299  {
300  return router.has(this->getValue(key));
301  }

◆ getComparator()

template<class JKey_t, class JValue_t, class JEvaluator_t = JHashEvaluator>
const JComparator& JTOOLS::JHashMap< JKey_t, JValue_t, JEvaluator_t >::getComparator ( ) const
inline

Get comparator.

Returns
comparator

Definition at line 309 of file JHashMap.hh.

310  {
311  return compare;
312  }

◆ resize()

template<class JKey_t, class JValue_t, class JEvaluator_t = JHashEvaluator>
void JTOOLS::JHashMap< JKey_t, JValue_t, JEvaluator_t >::resize ( )
private

◆ push_back()

template<class JKey_t, class JValue_t, class JEvaluator_t = JHashEvaluator>
void JTOOLS::JHashMap< JKey_t, JValue_t, JEvaluator_t >::push_back ( )
private

◆ pop_back()

template<class JKey_t, class JValue_t, class JEvaluator_t = JHashEvaluator>
void JTOOLS::JHashMap< JKey_t, JValue_t, JEvaluator_t >::pop_back ( )
private

◆ operator[]()

template<class JKey_t, class JValue_t>
mapped_type& JTOOLS::JMappableCollection< JKey_t, JValue_t >::operator[] ( typename JClass< key_type >::argument_type  key)
inlineinherited

Get mapped value.

Parameters
keykey
Returns
value

Definition at line 64 of file JMappableCollection.hh.

65  {
66  return get(key);
67  }

◆ put()

template<class JKey_t, class JValue_t>
void JTOOLS::JMappableCollection< JKey_t, JValue_t >::put ( typename JClass< key_type > ::argument_type  key,
typename JClass< mapped_type >::argument_type  value 
)
inlineinherited

Put pair-wise element (key,value) into collection.

Parameters
keykey
valuevalue

Definition at line 76 of file JMappableCollection.hh.

78  {
79  get(key) = value;
80  }

Friends And Related Function Documentation

◆ operator>>

template<class JKey_t, class JValue_t, class JEvaluator_t = JHashEvaluator>
JReader& operator>> ( JReader in,
JHashMap< JKey_t, JValue_t, JEvaluator_t > &  object 
)
friend

Read hash map from input.

Parameters
inreader
objecthash map
Returns
reader

Definition at line 322 of file JHashMap.hh.

323  {
324  using namespace JIO;
325 
326  int n;
327 
328  in >> n;
329 
330  object.resize(n);
331 
332  for (value_type element; n != 0 && in >> element; --n) {
333  object.insert(element);
334  }
335 
336  return in;
337  }

◆ operator<<

template<class JKey_t, class JValue_t, class JEvaluator_t = JHashEvaluator>
JWriter& operator<< ( JWriter out,
const JHashMap< JKey_t, JValue_t, JEvaluator_t > &  object 
)
friend

Write hash map to output.

Parameters
outwriter
objecthash map
Returns
writer

Definition at line 347 of file JHashMap.hh.

348  {
349  using namespace JIO;
350 
351  int n = object.size();
352 
353  out << n;
354 
355  for (typename JHashMap::const_iterator i = object.begin(); i != object.end(); ++i) {
356  out << *i;
357  }
358 
359  return out;
360  }

Member Data Documentation

◆ getValue

template<class JKey_t, class JValue_t, class JEvaluator_t = JHashEvaluator>
JEvaluator_t JTOOLS::JHashMap< JKey_t, JValue_t, JEvaluator_t >::getValue

Function object for evaluation of key.

Definition at line 366 of file JHashMap.hh.

◆ compare

template<class JKey_t, class JValue_t, class JEvaluator_t = JHashEvaluator>
JComparator JTOOLS::JHashMap< JKey_t, JValue_t, JEvaluator_t >::compare
protected

Function object for comparison.

Definition at line 373 of file JHashMap.hh.

◆ router

template<class JKey_t, class JValue_t, class JEvaluator_t = JHashEvaluator>
JRouter<int> JTOOLS::JHashMap< JKey_t, JValue_t, JEvaluator_t >::router
protected

Definition at line 375 of file JHashMap.hh.


The documentation for this struct was generated from the following file:
std::iterator
Definition: JSTDTypes.hh:18
JROOT::advance
counter_type advance(counter_type &counter, const counter_type value, const counter_type limit=std::numeric_limits< counter_type >::max())
Advance counter.
Definition: JCounter.hh:35
JTOOLS::JHashMap::value_type
std::pair< JKey_t, JValue_t > value_type
Definition: JHashMap.hh:61
JTOOLS::n
const int n
Definition: JPolint.hh:628
JTOOLS::JHashMap::compare
JComparator compare
Function object for comparison.
Definition: JHashMap.hh:373
distance
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
Definition: PhysicsEvent.hh:434
JTOOLS::JMappableCollection::get
virtual mapped_type & get(typename JClass< key_type >::argument_type key)=0
Get mapped value.
JTOOLS::JHashMap::insert
pair_type insert(const value_type &element)
Insert element.
Definition: JHashMap.hh:196
THROW
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
JTOOLS::JHashMap::pair_type
std::pair< const_iterator, bool > pair_type
Definition: JHashMap.hh:71
JTOOLS::JHashMap::const_iterator
container_type::const_iterator const_iterator
Definition: JHashMap.hh:66
std
Definition: jaanetDictionary.h:36
JIO
Auxiliary classes and methods for binary I/O.
Definition: JBinaryFileReader.hh:17
JTOOLS::JHashMap::router
JRouter< int > router
Definition: JHashMap.hh:375
JTOOLS::JHashMap::mapped_type
JValue_t mapped_type
Definition: JHashMap.hh:60
JTOOLS::JHashMap::getValue
JEvaluator_t getValue
Function object for evaluation of key.
Definition: JHashMap.hh:366