Jpp 19.3.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
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 >

Classes

struct  router_type
 Internal router. More...
 

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.
 
template<class JAllocator_t >
 JHashCollection (const array_type< JElement_t, JAllocator_t > &buffer, const JEvaluator_t &evaluator=JEvaluator_t())
 Constructor.
 
JHashCollectionoperator= (const JHashCollection &collection)
 Assignment operator.
 
void clear ()
 Clear.
 
void swap (JHashCollection &collection)
 Swap hash collection.
 
template<class T >
const_iterator find (const T &value) const
 Find element with given value.
 
template<class T >
iterator find (const T &value)
 Find element with given value.
 
template<class T >
value_typeget (const T &value)
 Get element with given value.
 
template<class T >
const value_typeget (const T &value) const
 Get element with given value.
 
virtual bool insert (const value_type &element)
 Insert element.
 
template<class T >
void insert (T __begin, T __end)
 Insert values.
 
void erase (iterator pos)
 Erase element at given position.
 
void erase (iterator __begin, iterator __end)
 Erase elements in given range.
 
template<class T >
bool erase (const T &value)
 Erase element with given value.
 
template<class T >
bool has (const T &value) const
 Test whether given value is present.
 
template<class T >
int getIndex (const T &value) const
 Get index of given value.
 

Public Attributes

JEvaluator_t getValue
 Function object for evaluation of element.
 

Protected Attributes

JTOOLS::JHashCollection::router_type router
 

Private Member Functions

void operator[] (int)
 
void assign ()
 
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 36 of file JHashCollection.hh.

Member Typedef Documentation

◆ value_type

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

Definition at line 41 of file JHashCollection.hh.

◆ evaluator_type

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

Definition at line 42 of file JHashCollection.hh.

◆ container_type

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

Definition at line 44 of file JHashCollection.hh.

◆ const_iterator

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

Definition at line 46 of file JHashCollection.hh.

◆ const_reverse_iterator

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

Definition at line 47 of file JHashCollection.hh.

◆ iterator

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

Definition at line 48 of file JHashCollection.hh.

◆ reverse_iterator

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

Definition at line 49 of file JHashCollection.hh.

Constructor & Destructor Documentation

◆ JHashCollection() [1/2]

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

57 :
58 getValue(evaluator)
59 {}
JEvaluator_t getValue
Function object for evaluation of element.

◆ JHashCollection() [2/2]

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

Constructor.

Parameters
bufferinput data
evaluatorevaluator

Definition at line 69 of file JHashCollection.hh.

70 :
71 getValue(evaluator)
72 {
73 for (typename array_type<JElement_t, JAllocator_t>::const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
74 insert(*i);
75 }
76 }
virtual bool insert(const value_type &element)
Insert element.

Member Function Documentation

◆ operator=()

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

Assignment operator.

Parameters
collectionhash collection
Returns
this hash collection

Definition at line 85 of file JHashCollection.hh.

86 {
87 for (const_iterator i = this->begin(); i != this->end(); ++i) {
88 router.put(this->getValue(*i), router.getDefaultAddress());
89 }
90
91 container_type::assign(collection.begin(), collection.end());
92
93 router.align(collection.router);
94
95 getValue = collection.getValue;
96
97 for (iterator i = this->begin(); i != this->end(); ++i) {
98 router.put(this->getValue(*i), std::distance(this->begin(), i));
99 }
100
101 return *this;
102 }
container_type::iterator iterator
container_type::const_iterator const_iterator
JTOOLS::JHashCollection::router_type router

◆ clear()

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

Clear.

Definition at line 108 of file JHashCollection.hh.

109 {
110 for (const_iterator i = this->begin(); i != this->end(); ++i) {
111 router.put(this->getValue(*i), router.getDefaultAddress());
112 }
113
114 container_type::clear();
115 }

◆ swap()

template<class JElement_t , class JEvaluator_t = JHashEvaluator>
void JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::swap ( JHashCollection< JElement_t, JEvaluator_t > & collection)
inline

Swap hash collection.

Parameters
collectionhash collection

Definition at line 123 of file JHashCollection.hh.

124 {
125 router.swap(collection.router);
126
127 container_type::swap(collection);
128 }

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

139 {
140 const int ival = this->getValue(value);
141
142 if (router.has(ival))
143 return this->begin() + router.get(ival);
144 else
145 return this->end();
146 }

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

157 {
158 const int ival = this->getValue(value);
159
160 if (router.has(ival))
161 return this->begin() + router.get(ival);
162 else
163 return this->end();
164 }

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

177 {
178 const int ival = this->getValue(value);
179
180 if (!router.has(ival)) {
181 this->insert(value);
182 }
183
184 return container_type::operator[](router.get(ival)).second;
185 }

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

198 {
199 const int ival = this->getValue(value);
200
201 if (router.has(ival)) {
202 return container_type::operator[](router.get(ival)).second;
203 }
204
205 THROW(JIndexOutOfRange, "JHashCollection::get(): invalid value.");
206 }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.

◆ insert() [1/2]

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

216 {
217 const int ival = this->getValue(element);
218
219 if (!router.has(ival)) {
220
221 container_type::push_back(element);
222
223 router.put(ival, this->size() - 1);
224
225 return true;
226 }
227
228 return false;
229 }

◆ insert() [2/2]

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

Insert values.

Parameters
__beginbegin of values
__endend of values

Definition at line 239 of file JHashCollection.hh.

240 {
241 for (T i = __begin; i != __end; ++i) {
242 insert(*i);
243 }
244 }

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

253 {
254 router.put(this->getValue(*pos), router.getDefaultAddress());
255
256 for (iterator i = container_type::erase(pos); i != this->end(); ++i) {
257 router.put(this->getValue(*i), distance(this->begin(), i));
258 }
259 }
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.

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

269 {
270 for (iterator i = __begin; i != __end; ++i) {
271 router.put(this->getValue(*i), router.getDefaultAddress());
272 }
273
274 for (iterator i = container_type::erase(__begin, __end); i != this->end(); ++i) {
275 router.put(this->getValue(*i), distance(this->begin(), i));
276 }
277 }

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

288 {
289 const int ival = this->getValue(value);
290
291 if (router.has(ival)) {
292
293 this->erase(this->begin() + router.get(ival));
294
295 return true;
296 }
297
298 return false;
299 }
void erase(iterator pos)
Erase element at given position.

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

310 {
311 return router.has(this->getValue(value));
312 }

◆ getIndex()

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

323 {
324 return router.get(this->getValue(value));
325 }

◆ operator[]()

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

◆ assign()

template<class JElement_t , class JEvaluator_t = JHashEvaluator>
void JTOOLS::JHashCollection< JElement_t, JEvaluator_t >::assign ( )
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

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

◆ router

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

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