Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Types | Public Member Functions | Public Attributes | Protected Attributes | Private Member Functions | 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
 

Public Member Functions

 JHashCollection (const JEvaluator_t &evaluator=JEvaluator_t())
 Constructor. More...
 
JHashCollectionoperator= (const JHashCollection &buffer)
 Assignment operator. More...
 
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 bool 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...
 
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...
 
template<class T >
int getIndex (const T &value) const
 Get index of given value. More...
 

Public Attributes

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

Protected Attributes

JRouter< int > router
 

Private Member Functions

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

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.

Definition at line 33 of file JHashCollection.hh.

Member Typedef Documentation

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

Definition at line 38 of file JHashCollection.hh.

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

Definition at line 39 of file JHashCollection.hh.

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

Definition at line 41 of file JHashCollection.hh.

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

Definition at line 43 of file JHashCollection.hh.

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 44 of file JHashCollection.hh.

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

Definition at line 45 of file JHashCollection.hh.

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

Definition at line 46 of file JHashCollection.hh.

Constructor & Destructor Documentation

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 54 of file JHashCollection.hh.

54  :
55  getValue(evaluator),
56  router (-1) // default address
57  {}
JEvaluator_t getValue
Function object for evaluation of element.

Member Function Documentation

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

Assignment operator.

Parameters
bufferhash collection
Returns
this hash collection

Definition at line 66 of file JHashCollection.hh.

67  {
68  for (const_iterator i = this->begin(); i != this->end(); ++i) {
69  router.put(this->getValue(*i), router.getDefaultAddress());
70  }
71 
72  static_cast<container_type&>(*this) = static_cast<const container_type&>(buffer);
73 
74  router.copy(buffer.router, false);
75 
76  for (iterator i = this->begin(); i != this->end(); ++i) {
77  router.put(this->getValue(*i), std::distance(this->begin(), i));
78  }
79 
80  return *this;
81  }
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
container_type::const_iterator const_iterator
container_type::iterator iterator
std::vector< value_type > container_type
JEvaluator_t getValue
Function object for evaluation of element.
template<class JElement_t, class JEvaluator_t = JHashEvaluator>
void JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::clear ( )
inline

Clear.

Definition at line 87 of file JHashCollection.hh.

88  {
89  router.clear();
90 
91  container_type::clear();
92  }
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 102 of file JHashCollection.hh.

103  {
104  const int ival = this->getValue(value);
105 
106  if (router.has(ival))
107  return this->begin() + router.get(ival);
108  else
109  return this->end();
110  }
JEvaluator_t getValue
Function object for evaluation of element.
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 120 of file JHashCollection.hh.

121  {
122  const int ival = this->getValue(value);
123 
124  if (router.has(ival))
125  return this->begin() + router.get(ival);
126  else
127  return this->end();
128  }
JEvaluator_t getValue
Function object for evaluation of element.
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 140 of file JHashCollection.hh.

141  {
142  const int ival = this->getValue(value);
143 
144  if (!router.has(ival)) {
145  this->insert(value);
146  }
147 
148  return container_type::operator[](router.get(ival)).second;
149  }
virtual bool insert(const value_type &element)
Insert element.
JEvaluator_t getValue
Function object for evaluation of element.
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 161 of file JHashCollection.hh.

162  {
163  const int ival = this->getValue(value);
164 
165  if (router.has(ival)) {
166  return container_type::operator[](router.get(ival)).second;
167  }
168 
169  THROW(JIndexOutOfRange, "JHasCollection::get(): invalid value.");
170  }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
JEvaluator_t getValue
Function object for evaluation of element.
template<class JElement_t, class JEvaluator_t = JHashEvaluator>
virtual bool JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::insert ( const value_type element)
inlinevirtual

Insert element.

Parameters
elementelement
Returns
true if inserted; else false

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

Definition at line 179 of file JHashCollection.hh.

180  {
181  const int ival = this->getValue(element);
182 
183  if (!router.has(ival)) {
184 
185  container_type::push_back(element);
186 
187  router.put(ival, this->size() - 1);
188 
189  return true;
190  }
191 
192  return false;
193  }
JEvaluator_t getValue
Function object for evaluation of element.
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 201 of file JHashCollection.hh.

202  {
203  router.put(this->getValue(*pos), router.getDefaultAddress());
204 
205  for (iterator i = container_type::erase(pos); i != this->end(); ++i) {
206  router.put(this->getValue(*i), distance(this->begin(), i));
207  }
208  }
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
container_type::iterator iterator
JEvaluator_t getValue
Function object for evaluation of element.
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 217 of file JHashCollection.hh.

218  {
219  for (iterator i = __begin; i != __end; ++i) {
220  router.put(this->getValue(*i), router.getDefaultAddress());
221  }
222 
223  for (iterator i = container_type::erase(__begin, __end); i != this->end(); ++i) {
224  router.put(this->getValue(*i), distance(this->begin(), i));
225  }
226  }
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
container_type::iterator iterator
JEvaluator_t getValue
Function object for evaluation of element.
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 236 of file JHashCollection.hh.

237  {
238  const int ival = this->getValue(value);
239 
240  if (router.has(ival)) {
241 
242  this->erase(this->begin() + router.get(ival));
243 
244  return true;
245  }
246 
247  return false;
248  }
void erase(iterator pos)
Erase element at given position.
JEvaluator_t getValue
Function object for evaluation of element.
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 258 of file JHashCollection.hh.

259  {
260  return router.has(this->getValue(value));
261  }
JEvaluator_t getValue
Function object for evaluation of element.
template<class JElement_t, class JEvaluator_t = JHashEvaluator>
template<class T >
int JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::getIndex ( const T value) const
inline

Get index of given value.

Parameters
valuevalue
Returns
indecx

Definition at line 271 of file JHashCollection.hh.

272  {
273  return router.get(this->getValue(value));
274  }
JEvaluator_t getValue
Function object for evaluation of element.
template<class JElement_t, class JEvaluator_t = JHashEvaluator>
void JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::operator[] ( int  )
private
template<class JElement_t, class JEvaluator_t = JHashEvaluator>
void JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::resize ( )
private
template<class JElement_t, class JEvaluator_t = JHashEvaluator>
void JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::push_back ( )
private
template<class JElement_t, class JEvaluator_t = JHashEvaluator>
void JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::pop_back ( )
private

Member Data Documentation

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 280 of file JHashCollection.hh.

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

Definition at line 284 of file JHashCollection.hh.


The documentation for this class was generated from the following file: