Jpp
 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 <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
JEvaluator_t evaluator_type
Definition: JHashSet.hh:40
container_type::const_reverse_iterator const_reverse_iterator
Definition: JHashSet.hh:45
JHashSet(const JEvaluator_t &evaluator=JEvaluator_t())
Constructor.
Definition: JHashSet.hh:93
Auxiliary class for ordering of objects in the set by the hash value.
Definition: JHashSet.hh:55
std::pair< const_iterator, bool > pair_type
Definition: JHashSet.hh:49
JEvaluator_t getValue
Function object for evaluation of element.
Definition: JHashSet.hh:84
virtual pair_type insert(typename JClass< value_type >::argument_type element)
Insert element.
virtual pair_type insert(typename JClass< value_type >::argument_type element)
Insert element.
Definition: JHashSet.hh:105
General purpose class for a hash collection of unique elements.
container_type::iterator iterator
Definition: JHashSet.hh:46
JComparator(const JEvaluator_t &evaluator=JEvaluator_t())
Constructor.
Definition: JHashSet.hh:62
JArgument< T >::argument_type argument_type
Definition: JClass.hh:64
General purpose class for hash set of elements.
Definition: JHashSet.hh:34
JHashCollection< value_type, evaluator_type > container_type
Definition: JHashSet.hh:42
JComparator compare
Function object for comparison.
Definition: JHashSet.hh:145
container_type::const_iterator const_iterator
Definition: JHashSet.hh:44
General purpose class for hash collection of unique elements.
container_type::const_iterator const_iterator
Template for generic class types.
Definition: JClass.hh:62
container_type::reverse_iterator reverse_iterator
container_type::iterator iterator
container_type::const_reverse_iterator const_reverse_iterator
const JComparator & getComparator() const
Get comparator.
Definition: JHashSet.hh:135
container_type::reverse_iterator reverse_iterator
Definition: JHashSet.hh:47
JElement_t value_type
Definition: JHashSet.hh:39
bool operator()(typename JClass< value_type >::argument_type first, typename JClass< value_type >::argument_type second) const
Comparison of elements.
Definition: JHashSet.hh:74
JEvaluator_t getValue
Function object for evaluation of element.