Jpp 19.3.0-rc.3
the software that should make you happy
Loading...
Searching...
No Matches
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 */
16namespace JTOOLS {}
17namespace JPP { using namespace JTOOLS; }
18
19namespace 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 */
126 {
127 return compare;
128 }
129
130
131 protected:
132 /**
133 * Function object for comparison.
134 */
136 };
137}
138
139#endif
General purpose class for a hash collection of unique elements.
General purpose class for hash collection of unique elements.
JTOOLS::JHashCollection::router_type router
General purpose class for hash set of elements.
Definition JHashSet.hh:32
JComparator compare
Function object for comparison.
Definition JHashSet.hh:135
container_type::reverse_iterator reverse_iterator
Definition JHashSet.hh:44
collection_type::container_type container_type
Definition JHashSet.hh:39
JElement_t value_type
Definition JHashSet.hh:35
JHashCollection< value_type, evaluator_type > collection_type
Definition JHashSet.hh:38
virtual bool insert(const value_type &element) override
Insert element.
Definition JHashSet.hh:99
JHashSet(const JEvaluator_t &evaluator=JEvaluator_t())
Constructor.
Definition JHashSet.hh:87
JEvaluator_t evaluator_type
Definition JHashSet.hh:36
container_type::iterator iterator
Definition JHashSet.hh:43
container_type::const_reverse_iterator const_reverse_iterator
Definition JHashSet.hh:42
container_type::const_iterator const_iterator
Definition JHashSet.hh:41
const JComparator & getComparator() const
Get comparator.
Definition JHashSet.hh:125
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for multi-dimensional interpolations and histograms.
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
JComparator(const JEvaluator_t &evaluator=JEvaluator_t())
Constructor.
Definition JHashSet.hh:57
bool operator()(const value_type &first, const value_type &second) const
Comparison of elements.
Definition JHashSet.hh:69