Jpp
Public Types | Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Private Member Functions | Friends | List of all members
JTOOLS::JHashCollection< JElement_t, JEvaluator_t > Class Template Reference

General purpose class for hash collection of unique elements. More...

#include <JHashCollection.hh>

Inheritance diagram for JTOOLS::JHashCollection< JElement_t, JEvaluator_t >:
std::vector< JElement_t > JTOOLS::JHashSet< JElement_t, JEvaluator_t >

Public Types

typedef JElement_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

 JHashCollection (const JEvaluator_t &evaluator=JEvaluator_t())
 Constructor. More...
 
virtual void clear ()
 Clear. More...
 
template<class T >
const_iterator find (const T &value) const
 Find element with given value. More...
 
template<class T >
iterator find (const T &value)
 Find element with given value. More...
 
template<class T >
value_typeget (const T value)
 Get element with given value. More...
 
template<class T >
const value_typeget (const T value) const
 Get element with given value. More...
 
virtual pair_type insert (typename JClass< value_type >::argument_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...
 
template<class T >
bool erase (const T &value)
 Erase element with given value. More...
 
template<class T >
bool has (const T &value) const
 Test whether given value is present. More...
 

Public Attributes

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

Protected Member Functions

iterator insert (iterator pos, typename JClass< value_type >::argument_type element)
 Insert element. More...
 

Protected Attributes

JRouter< int > router
 

Private Member Functions

void operator[] (int)
 
void resize ()
 
void push_back ()
 
void pop_back ()
 

Friends

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

Detailed Description

template<class JElement_t, class JEvaluator_t = JHashEvaluator>
class JTOOLS::JHashCollection< JElement_t, JEvaluator_t >

General purpose class for hash collection of unique elements.

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

For the binary I/O of a collection of elements, the data structure of the elements should provide for an implementation of the following operators:

      JReader& operator>>(JReader& in);
      JWriter& operator<<(JWriter& out);

Definition at line 46 of file JHashCollection.hh.

Member Typedef Documentation

◆ value_type

template<class JElement_t , class JEvaluator_t = JHashEvaluator>
typedef JElement_t JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::value_type

Definition at line 51 of file JHashCollection.hh.

◆ evaluator_type

template<class JElement_t , class JEvaluator_t = JHashEvaluator>
typedef JEvaluator_t JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::evaluator_type

Definition at line 52 of file JHashCollection.hh.

◆ container_type

template<class JElement_t , class JEvaluator_t = JHashEvaluator>
typedef std::vector<value_type> JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::container_type

Definition at line 54 of file JHashCollection.hh.

◆ const_iterator

template<class JElement_t , class JEvaluator_t = JHashEvaluator>
typedef container_type::const_iterator JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::const_iterator

Definition at line 56 of file JHashCollection.hh.

◆ const_reverse_iterator

template<class JElement_t , class JEvaluator_t = JHashEvaluator>
typedef container_type::const_reverse_iterator JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::const_reverse_iterator

Definition at line 57 of file JHashCollection.hh.

◆ iterator

template<class JElement_t , class JEvaluator_t = JHashEvaluator>
typedef container_type::iterator JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::iterator

Definition at line 58 of file JHashCollection.hh.

◆ reverse_iterator

template<class JElement_t , class JEvaluator_t = JHashEvaluator>
typedef container_type::reverse_iterator JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::reverse_iterator

Definition at line 59 of file JHashCollection.hh.

◆ pair_type

template<class JElement_t , class JEvaluator_t = JHashEvaluator>
typedef std::pair<const_iterator, bool> JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::pair_type

Definition at line 61 of file JHashCollection.hh.

Constructor & Destructor Documentation

◆ JHashCollection()

template<class JElement_t , class JEvaluator_t = JHashEvaluator>
JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::JHashCollection ( const JEvaluator_t &  evaluator = JEvaluator_t())
inline

Constructor.

Parameters
evaluatorevaluator

Definition at line 69 of file JHashCollection.hh.

69  :
70  getValue(evaluator),
71  router (-1) // default address
72  {}

Member Function Documentation

◆ clear()

template<class JElement_t , class JEvaluator_t = JHashEvaluator>
virtual void JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::clear ( )
inlinevirtual

Clear.

Definition at line 78 of file JHashCollection.hh.

79  {
80  // reset internal router
81 
82  for (iterator i = this->begin(); i != this->end(); ++i) {
83  router.put(this->getValue(*i), router.getDefaultAddress());
84  }
85 
86  container_type::clear();
87  }

◆ find() [1/2]

template<class JElement_t , class JEvaluator_t = JHashEvaluator>
template<class T >
const_iterator JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::find ( const T &  value) const
inline

Find element with given value.

Parameters
valuevalue
Returns
position of element with given value or end()

Definition at line 97 of file JHashCollection.hh.

98  {
99  const int ival = this->getValue(value);
100 
101  if (router.has(ival)) {
102 
103  const_iterator i = this->begin();
104 
105  std::advance(i, router.get(ival));
106 
107  return i;
108  }
109 
110  return this->end();
111  }

◆ find() [2/2]

template<class JElement_t , class JEvaluator_t = JHashEvaluator>
template<class T >
iterator JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::find ( const T &  value)
inline

Find element with given value.

Parameters
valuevalue
Returns
position of element with given value or end()

Definition at line 121 of file JHashCollection.hh.

122  {
123  const int ival = this->getValue(value);
124 
125  if (router.has(ival)) {
126 
127  iterator i = this->begin();
128 
129  std::advance(i, router.get(ival));
130 
131  return i;
132  }
133 
134  return this->end();
135  }

◆ get() [1/2]

template<class JElement_t , class JEvaluator_t = JHashEvaluator>
template<class T >
value_type& JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::get ( const T  value)
inline

Get element with given value.

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

Parameters
valuevalue
Returns
element

Definition at line 147 of file JHashCollection.hh.

148  {
149  const int ival = this->getValue(value);
150 
151  if (!this->router.has(ival)) {
152  this->insert(value);
153  }
154 
155  return container_type::operator[](router.get(ival)).second;
156  }

◆ get() [2/2]

template<class JElement_t , class JEvaluator_t = JHashEvaluator>
template<class T >
const value_type& JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::get ( const T  value) const
inline

Get element with given value.

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

Parameters
valuevalue
Returns
element

Definition at line 168 of file JHashCollection.hh.

169  {
170  const int ival = this->getValue(value);
171 
172  if (router.has(ival))
173  return container_type::operator[](router.get(ival)).second;
174  else
175  THROW(JIndexOutOfRange, "JHasCollection::get(): invalid value.");
176  }

◆ insert() [1/2]

template<class JElement_t , class JEvaluator_t = JHashEvaluator>
virtual pair_type JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::insert ( typename JClass< value_type >::argument_type  element)
inlinevirtual

Insert element.

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

Reimplemented in JTOOLS::JHashSet< JElement_t, JEvaluator_t >.

Definition at line 185 of file JHashCollection.hh.

186  {
187  using namespace std;
188 
189  const int ival = this->getValue(element);
190 
191  if (!this->router.has(ival)) {
192 
193  container_type::push_back(element);
194 
195  iterator i = this->rbegin().base();
196 
197  router.put(ival, distance(this->begin(), i));
198 
199  return pair_type(i, true);
200 
201  } else {
202 
203  return pair_type(this->end(), false);
204  }
205  }

◆ erase() [1/3]

template<class JElement_t , class JEvaluator_t = JHashEvaluator>
void JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::erase ( iterator  pos)
inline

Erase element at given position.

Parameters
posvalid position

Definition at line 213 of file JHashCollection.hh.

214  {
215  this->router.put(this->getValue(*pos), this->router.getDefaultAddress());
216 
217  iterator i = container_type::erase(pos);
218 
219  for ( ; i != this->end(); ++i) {
220  this->router.put(this->getValue(*i), distance(this->begin(), i));
221  }
222  }

◆ erase() [2/3]

template<class JElement_t , class JEvaluator_t = JHashEvaluator>
void JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::erase ( iterator  __begin,
iterator  __end 
)
inline

Erase elements in given range.

Parameters
__beginbegin position (included)
__endend position (excluded)

Definition at line 231 of file JHashCollection.hh.

232  {
233  for (iterator i = __begin; i != __end; ++i) {
234  this->router.put(this->getValue(*i), this->router.getDefaultAddress());
235  }
236 
237  iterator i = container_type::erase(__begin, __end);
238 
239  for ( ; i != this->end(); ++i) {
240  this->router.put(this->getValue(*i), distance(this->begin(), i));
241  }
242  }

◆ erase() [3/3]

template<class JElement_t , class JEvaluator_t = JHashEvaluator>
template<class T >
bool JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::erase ( const T &  value)
inline

Erase element with given value.

Parameters
valuevalue
Returns
true if element has been erased; else false

Definition at line 252 of file JHashCollection.hh.

253  {
254  const int ival = this->getValue(value);
255 
256  if (this->router.has(ival)) {
257 
258  iterator pos = this->begin();
259 
260  std::advance(pos, this->router.get(ival));
261 
262  this->erase(pos);
263 
264  return true;
265  }
266 
267  return false;
268  }

◆ has()

template<class JElement_t , class JEvaluator_t = JHashEvaluator>
template<class T >
bool JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::has ( const T &  value) const
inline

Test whether given value is present.

Parameters
valuevalue
Returns
true if present; else false

Definition at line 278 of file JHashCollection.hh.

279  {
280  return router.has(this->getValue(value));
281  }

◆ insert() [2/2]

template<class JElement_t , class JEvaluator_t = JHashEvaluator>
iterator JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::insert ( iterator  pos,
typename JClass< value_type >::argument_type  element 
)
inlineprotected

Insert element.

Parameters
posposition
elementelement
Returns
position

Definition at line 340 of file JHashCollection.hh.

341  {
342  return container_type::insert(pos, element);
343  }

◆ operator[]()

template<class JElement_t , class JEvaluator_t = JHashEvaluator>
void JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::operator[] ( int  )
private

◆ resize()

template<class JElement_t , class JEvaluator_t = JHashEvaluator>
void JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::resize ( )
private

◆ push_back()

template<class JElement_t , class JEvaluator_t = JHashEvaluator>
void JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::push_back ( )
private

◆ pop_back()

template<class JElement_t , class JEvaluator_t = JHashEvaluator>
void JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::pop_back ( )
private

Friends And Related Function Documentation

◆ operator>>

template<class JElement_t , class JEvaluator_t = JHashEvaluator>
JReader& operator>> ( JReader in,
JHashCollection< JElement_t, JEvaluator_t > &  object 
)
friend

Read hash collection from input.

Parameters
inreader
objecthash collection
Returns
reader

Definition at line 291 of file JHashCollection.hh.

292  {
293  int n;
294 
295  in >> n;
296 
297  for (value_type element; n != 0 && in >> element; --n) {
298  object.insert(element);
299  }
300 
301  return in;
302  }

◆ operator<<

template<class JElement_t , class JEvaluator_t = JHashEvaluator>
JWriter& operator<< ( JWriter out,
const JHashCollection< JElement_t, JEvaluator_t > &  object 
)
friend

Write hash collection to output.

Parameters
outwriter
objecthash collection
Returns
writer

Definition at line 312 of file JHashCollection.hh.

313  {
314  int n = object.size();
315 
316  out << n;
317 
318  for (typename JHashCollection::const_iterator i = object.begin(); i != object.end(); ++i) {
319  out << *i;
320  }
321 
322  return out;
323  }

Member Data Documentation

◆ getValue

template<class JElement_t , class JEvaluator_t = JHashEvaluator>
JEvaluator_t JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::getValue

Function object for evaluation of element.

Definition at line 329 of file JHashCollection.hh.

◆ router

template<class JElement_t , class JEvaluator_t = JHashEvaluator>
JRouter<int> JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::router
protected

Definition at line 345 of file JHashCollection.hh.


The documentation for this class 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::JHashCollection::insert
virtual pair_type insert(typename JClass< value_type >::argument_type element)
Insert element.
Definition: JHashCollection.hh:185
JTOOLS::n
const int n
Definition: JPolint.hh:628
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::JHashCollection::erase
void erase(iterator pos)
Erase element at given position.
Definition: JHashCollection.hh:213
JTOOLS::JHashCollection::pair_type
std::pair< const_iterator, bool > pair_type
Definition: JHashCollection.hh:61
JTOOLS::JHashCollection::getValue
JEvaluator_t getValue
Function object for evaluation of element.
Definition: JHashCollection.hh:329
THROW
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:669
std
Definition: jaanetDictionary.h:36
JTOOLS::JHashCollection::router
JRouter< int > router
Definition: JHashCollection.hh:345
JTOOLS::JHashCollection::value_type
JElement_t value_type
Definition: JHashCollection.hh:51
JTOOLS::JHashCollection::const_iterator
container_type::const_iterator const_iterator
Definition: JHashCollection.hh:56