Jpp  18.2.1-ARCA-DF-PATCH
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JHashSet.hh
Go to the documentation of this file.
1 #ifndef __JTOOLS__JHASHSET__
2 #define __JTOOLS__JHASHSET__
3 
4 #include <algorithm>
5 
8 
9 
10 /**
11  * \file
12  *
13  * General purpose class for a hash set of sorted elements.
14  * \author mdejong
15  */
16 namespace JTOOLS {}
17 namespace JPP { using namespace JTOOLS; }
18 
19 namespace JTOOLS {
20 
21 
22  /**
23  * General purpose class for hash set of elements.
24  *
25  * The elements in a hash set are sorted according to the specified evaluation.
26  * The evaluation of elements corresponds to a unary method returning an integer value for a given element;
27  * The default evaluator is JHashEvaluator.
28  */
29  template<class JElement_t, class JEvaluator_t = JHashEvaluator>
30  class JHashSet :
31  public JHashCollection<JElement_t, JEvaluator_t>
32  {
33  public:
34 
35  typedef JElement_t value_type;
36  typedef JEvaluator_t evaluator_type;
37 
40 
41  typedef typename container_type::const_iterator const_iterator;
42  typedef typename container_type::const_reverse_iterator const_reverse_iterator;
43  typedef typename container_type::iterator iterator;
44  typedef typename container_type::reverse_iterator reverse_iterator;
45 
46 
47  /**
48  * Auxiliary class for ordering of objects in the set by the hash value.
49  */
50  struct JComparator {
51 
52  /**
53  * Constructor.
54  *
55  * \param evaluator evaluator
56  */
57  JComparator(const JEvaluator_t& evaluator = JEvaluator_t()) :
58  getValue(evaluator)
59  {}
60 
61 
62  /**
63  * Comparison of elements.
64  *
65  * \param first first element
66  * \param second second element
67  * \return true if first element less than second element; else false
68  */
69  inline bool operator()(const value_type& first, const value_type& second) const
70  {
71  return this->getValue(first) < this->getValue(second);
72  }
73 
74 
75  /**
76  * Function object for evaluation of element.
77  */
78  JEvaluator_t getValue;
79  };
80 
81 
82  /**
83  * Constructor.
84  *
85  * \param evaluator evaluator
86  */
87  JHashSet(const JEvaluator_t& evaluator = JEvaluator_t()) :
88  JHashCollection<JElement_t, JEvaluator_t>(evaluator),
89  compare(evaluator)
90  {}
91 
92 
93  /**
94  * Insert element.
95  *
96  * \param element element
97  * \return true if inserted; else false
98  */
99  virtual bool insert(const value_type& element) override
100  {
101  const int ival = this->getValue(element);
102 
103  if (!this->router.has(ival)) {
104 
105  iterator p = container_type::insert(lower_bound(this->begin(), this->end(), element, compare), element);
106 
107  this->router.put(ival, std::distance(this->begin(), p));
108 
109  for (iterator i = p; ++i != this->end(); ) {
110  this->router.put(this->getValue(*i), std::distance(this->begin(), i));
111  }
112 
113  return true;
114  }
115 
116  return false;
117  }
118 
119 
120  /**
121  * Get comparator.
122  *
123  * \return comparator
124  */
125  const JComparator& getComparator() const
126  {
127  return compare;
128  }
129 
130 
131  protected:
132  /**
133  * Function object for comparison.
134  */
136  };
137 }
138 
139 #endif
JEvaluator_t evaluator_type
Definition: JHashSet.hh:36
collection_type::container_type container_type
Definition: JHashSet.hh:39
container_type::const_reverse_iterator const_reverse_iterator
Definition: JHashSet.hh:42
JHashSet(const JEvaluator_t &evaluator=JEvaluator_t())
Constructor.
Definition: JHashSet.hh:87
Auxiliary class for ordering of objects in the set by the hash value.
Definition: JHashSet.hh:50
JEvaluator_t getValue
Function object for evaluation of element.
Definition: JHashSet.hh:78
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
General purpose class for a hash collection of unique elements.
container_type::iterator iterator
Definition: JHashSet.hh:43
JComparator(const JEvaluator_t &evaluator=JEvaluator_t())
Constructor.
Definition: JHashSet.hh:57
JTOOLS::JHashCollection::router_type router
then echo The file $DIR KM3NeT_00000001_00000000 root already please rename or remove it first
JHashCollection< value_type, evaluator_type > collection_type
Definition: JHashSet.hh:38
General purpose class for hash set of elements.
Definition: JHashSet.hh:30
JComparator compare
Function object for comparison.
Definition: JHashSet.hh:135
container_type::const_iterator const_iterator
Definition: JHashSet.hh:41
General purpose class for hash collection of unique elements.
bool operator()(const value_type &first, const value_type &second) const
Comparison of elements.
Definition: JHashSet.hh:69
virtual bool insert(const value_type &element) override
Insert element.
Definition: JHashSet.hh:99
container_type::iterator iterator
const JComparator & getComparator() const
Get comparator.
Definition: JHashSet.hh:125
container_type::reverse_iterator reverse_iterator
Definition: JHashSet.hh:44
JElement_t value_type
Definition: JHashSet.hh:35
JEvaluator_t getValue
Function object for evaluation of element.