Jpp
JHashSet.hh
Go to the documentation of this file.
1 #ifndef __JTOOLS__JHASHSET__
2 #define __JTOOLS__JHASHSET__
3 
4 #include <utility>
5 #include <algorithm>
6 
7 #include "JLang/JClass.hh"
10 
11 
12 /**
13  * \file
14  *
15  * General purpose class for a hash set of sorted elements.
16  * \author mdejong
17  */
18 namespace JTOOLS {}
19 namespace JPP { using namespace JTOOLS; }
20 
21 namespace JTOOLS {
22 
23  using JLANG::JClass;
24 
25 
26  /**
27  * General purpose class for hash set of elements.
28  *
29  * The elements in a hash set are sorted according to the specified evaluation.
30  * The evaluation of elements corresponds to a unary method returning an integer value for a given element;
31  * The default evaluator is JHashEvaluator.
32  */
33  template<class JElement_t, class JEvaluator_t = JHashEvaluator>
34  class JHashSet :
35  public JHashCollection<JElement_t, JEvaluator_t>
36  {
37  public:
38 
39  typedef JElement_t value_type;
40  typedef JEvaluator_t evaluator_type;
41 
43 
48 
50 
51 
52  /**
53  * Auxiliary class for ordering of objects in the set by the hash value.
54  */
55  struct JComparator {
56 
57  /**
58  * Constructor.
59  *
60  * \param evaluator evaluator
61  */
62  JComparator(const JEvaluator_t& evaluator = JEvaluator_t()) :
63  getValue(evaluator)
64  {}
65 
66 
67  /**
68  * Comparison of elements.
69  *
70  * \param first first element
71  * \param second second element
72  * \return true if first element less than second element; else false
73  */
74  inline bool operator()(typename JClass<value_type>::argument_type first,
75  typename JClass<value_type>::argument_type second) const
76  {
77  return this->getValue(first) < this->getValue(second);
78  }
79 
80 
81  /**
82  * Function object for evaluation of element.
83  */
84  JEvaluator_t getValue;
85  };
86 
87 
88  /**
89  * Constructor.
90  *
91  * \param evaluator evaluator
92  */
93  JHashSet(const JEvaluator_t& evaluator = JEvaluator_t()) :
94  container_type(evaluator),
95  compare (evaluator)
96  {}
97 
98 
99  /**
100  * Insert element.
101  *
102  * \param element element
103  * \return (position, status), where status is true if inserted; else false
104  */
106  {
107  using namespace std;
108 
109  const int ival = this->getValue(element);
110 
111  if (!this->router.has(ival)) {
112 
113  iterator i = container_type::insert(lower_bound(this->begin(), this->end(), element, compare), element);
114 
115  this->router.put(ival, distance(this->begin(), i));
116 
117  for (iterator __i = i; ++__i != this->end(); ) {
118  this->router.put(this->getValue(*__i), distance(this->begin(), __i));
119  }
120 
121  return pair_type(i, true);
122 
123  } else {
124 
125  return pair_type(this->end(), false);
126  }
127  }
128 
129 
130  /**
131  * Get comparator.
132  *
133  * \return comparator
134  */
135  const JComparator& getComparator() const
136  {
137  return compare;
138  }
139 
140 
141  protected:
142  /**
143  * Function object for comparison.
144  */
146  };
147 }
148 
149 #endif
JTOOLS::JHashSet::value_type
JElement_t value_type
Definition: JHashSet.hh:39
JTOOLS::JHashCollection
General purpose class for hash collection of unique elements.
Definition: JHashCollection.hh:46
JTOOLS::JHashCollection::insert
virtual pair_type insert(typename JClass< value_type >::argument_type element)
Insert element.
Definition: JHashCollection.hh:185
JTOOLS::JHashSet::JComparator::JComparator
JComparator(const JEvaluator_t &evaluator=JEvaluator_t())
Constructor.
Definition: JHashSet.hh:62
JLANG::JClass::argument_type
JArgument< T >::argument_type argument_type
Definition: JClass.hh:82
std::vector< value_type >
JTOOLS::JHashSet::JComparator
Auxiliary class for ordering of objects in the set by the hash value.
Definition: JHashSet.hh:55
JTOOLS::JHashSet::const_iterator
container_type::const_iterator const_iterator
Definition: JHashSet.hh:44
JLANG::JClass
Template for generic class types.
Definition: JClass.hh:80
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::JHashSet::JComparator::getValue
JEvaluator_t getValue
Function object for evaluation of element.
Definition: JHashSet.hh:84
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JTOOLS::JHashSet::evaluator_type
JEvaluator_t evaluator_type
Definition: JHashSet.hh:40
JTOOLS::JHashCollection::getValue
JEvaluator_t getValue
Function object for evaluation of element.
Definition: JHashCollection.hh:329
JHashEvaluator.hh
JTOOLS::JHashCollection::const_reverse_iterator
container_type::const_reverse_iterator const_reverse_iterator
Definition: JHashCollection.hh:57
JTOOLS::JHashSet::const_reverse_iterator
container_type::const_reverse_iterator const_reverse_iterator
Definition: JHashSet.hh:45
std::pair
Definition: JSTDTypes.hh:15
JTOOLS::JHashSet::container_type
JHashCollection< value_type, evaluator_type > container_type
Definition: JHashSet.hh:42
JTOOLS::JHashSet::JHashSet
JHashSet(const JEvaluator_t &evaluator=JEvaluator_t())
Constructor.
Definition: JHashSet.hh:93
JHashCollection.hh
JTOOLS::JHashSet::iterator
container_type::iterator iterator
Definition: JHashSet.hh:46
JTOOLS::JHashSet::insert
virtual pair_type insert(typename JClass< value_type >::argument_type element)
Insert element.
Definition: JHashSet.hh:105
std
Definition: jaanetDictionary.h:36
JTOOLS::JHashSet::JComparator::operator()
bool operator()(typename JClass< value_type >::argument_type first, typename JClass< value_type >::argument_type second) const
Comparison of elements.
Definition: JHashSet.hh:74
JTOOLS::JHashCollection::iterator
container_type::iterator iterator
Definition: JHashCollection.hh:58
JClass.hh
JTOOLS::JHashCollection::router
JRouter< int > router
Definition: JHashCollection.hh:345
JTOOLS
Auxiliary classes and methods for multi-dimensional interpolations and histograms.
Definition: JAbstractCollection.hh:9
JTOOLS::JHashCollection::const_iterator
container_type::const_iterator const_iterator
Definition: JHashCollection.hh:56
JTOOLS::JHashSet::getComparator
const JComparator & getComparator() const
Get comparator.
Definition: JHashSet.hh:135
JTOOLS::JHashSet::reverse_iterator
container_type::reverse_iterator reverse_iterator
Definition: JHashSet.hh:47
JTOOLS::JHashSet::pair_type
std::pair< const_iterator, bool > pair_type
Definition: JHashSet.hh:49
JTOOLS::JHashSet
General purpose class for hash set of elements.
Definition: JHashSet.hh:34
JTOOLS::JHashCollection::reverse_iterator
container_type::reverse_iterator reverse_iterator
Definition: JHashCollection.hh:59
JTOOLS::JHashSet::compare
JComparator compare
Function object for comparison.
Definition: JHashSet.hh:145