Lolly 1.4.28
Loading...
Searching...
No Matches
hashset.hpp
Go to the documentation of this file.
1
2/**
3 * \file hashset.h
4 * \details A simple implementation of a hash set.
5 * \copyright GPLv3
6 * \author Joris van der Hoeven
7 * \date 1999
8 */
9
10#ifndef HASHSET_H
11#define HASHSET_H
12#include "list.hpp"
13
14template <class T> class hashset;
15template <class T> class hashset_iterator_rep;
16template <class T> int N (hashset<T> h);
17template <class T> tm_ostream& operator<< (tm_ostream& out, hashset<T> h);
18template <class T> bool operator<= (hashset<T> h1, hashset<T> h2);
19template <class T> hashset<T> copy (hashset<T> h);
20
21/**
22 * @brief The hashset_rep class represents an entry in a hash set.
23 *
24 * @tparam T The type of the data stored in the hash set.
25 */
26template <class T> class hashset_rep : concrete_struct {
27 int size; /**< The size of the hash set (number of entries). */
28 int n; /**< The number of keys (a power of two). */
29 int max; /**< The mean number of entries per key. */
30 list<T>* a; /**< The array of entries. */
31
32public:
33 /**
34 * @brief Construct a new hashset_rep object with default values.
35 *
36 */
37 inline hashset_rep ()
38 : size (0), n (1), max (1), a (tm_new_array<list<T>> (1)) {}
39
40 /**
41 * @brief Construct a new hashset_rep object with specified values.
42 *
43 * @param n2 The number of keys (a power of two).
44 * @param max2 The mean number of entries per key.
45 */
46 inline hashset_rep (int n2, int max2= 1)
47 : size (0), n (n2), max (max2), a (tm_new_array<list<T>> (n)) {}
48
49 /**
50 * @brief Destroy the hashset_rep object.
51 */
53
54 bool contains (T x);
55 void resize (int n);
56 void insert (T x);
57 void remove (T x);
58
59 friend class hashset<T>;
60 friend int N LESSGTR (hashset<T> h);
61 friend tm_ostream& operator<< LESSGTR (tm_ostream& out, hashset<T> h);
62 friend bool operator<= LESSGTR (hashset<T> h1, hashset<T> h2);
64 friend class hashset_iterator_rep<T>;
65};
66
67/**
68 * @brief The hashset class represents a hash set.
69 *
70 * @tparam T The type of the data stored in the hash set.
71 */
72template <class T> class hashset {
74
75 /**
76 * @brief Construct a new hashset object with specified values.
77 *
78 * @param n The number of keys (a power of two).
79 * @param max The mean number of entries per key.
80 */
81 inline hashset (int n= 1, int max= 1)
82 : rep (tm_new<hashset_rep<T>> (n, max)) {}
83};
85
86/**
87 * @brief Get the number of entries in a hash set.
88 *
89 * @tparam T The type of the data stored in the hash set.
90 * @param h The hash set to query.
91 * @return int The number of entries in the hash set.
92 */
93template <class T>
94inline int
96 return h->size;
97}
98
99/**
100 * @brief Equality comparison operator for hash sets.
101 *
102 * @tparam T The type of the data stored in the hash sets.
103 * @param h1 The first hash set to be compared.
104 * @param h2 The second hash set to be compared.
105 * @return true if the hash sets are equal, false otherwise.
106 */
107template <class T> bool operator== (hashset<T> h1, hashset<T> h2);
108
109/**
110 * @brief Less-than-or-equal-to comparison operator for hash sets.
111 *
112 * @tparam T The type of the data stored in the hash sets.
113 * @param h1 The first hash set to be compared.
114 * @param h2 The second hash set to be compared.
115 * @return true if the first hash set is less than or equal to the second hash
116 * set, false otherwise.
117 */
118template <class T> bool operator<= (hashset<T> h1, hashset<T> h2);
119
120/**
121 * @brief Less-than comparison operator for hash sets.
122 *
123 * @tparam T The type of the data stored in the hash sets.
124 * @param h1 The first hash set to be compared.
125 * @param h2 The second hash set to be compared.
126 * @return true if the first hash set is less than the second hash set, false
127 * otherwise.
128 */
129template <class T> bool operator< (hashset<T> h1, hashset<T> h2);
130
131/**
132 * @brief Insert an item into a hash set.
133 *
134 * @tparam T The type of the data stored in the hash set * @param h The hash set
135 * to insert into.
136 * @param x The item to insert.
137 * @return hashset<T>& A reference to the modified hash set.
138 */
139template <class T> hashset<T>& operator<< (hashset<T>& h, T x);
140
141#include "hashset.ipp"
142
143#endif // defined HASHSET_H
#define CONCRETE_TEMPLATE_CODE(PTR, TT, T)
Macro used to define the implementation of a concrete smart pointer with reference counting for a sin...
Definition classdef.hpp:215
The hashset_rep class represents an entry in a hash set.
Definition hashset.hpp:26
void resize(int n)
Definition hashset.ipp:18
hashset_rep(int n2, int max2=1)
Construct a new hashset_rep object with specified values.
Definition hashset.hpp:46
list< T > * a
Definition hashset.hpp:30
friend int N LESSGTR(hashset< T > h)
~hashset_rep()
Destroy the hashset_rep object.
Definition hashset.hpp:52
hashset_rep()
Construct a new hashset_rep object with default values.
Definition hashset.hpp:37
void remove(T x)
Definition hashset.ipp:63
void insert(T x)
Definition hashset.ipp:53
bool contains(T x)
Definition hashset.ipp:47
The hashset class represents a hash set.
Definition hashset.hpp:72
CONCRETE_TEMPLATE(hashset, T)
hashset(int n=1, int max=1)
Construct a new hashset object with specified values.
Definition hashset.hpp:81
The list class represents a linked list.
Definition list.hpp:48
C * tm_new_array(int n)
C * tm_new()
void tm_delete_array(C *Ptr)
bool operator<(hashset< T > h1, hashset< T > h2)
Less-than comparison operator for hash sets.
Definition hashset.ipp:101
bool operator<=(hashset< T > h1, hashset< T > h2)
Less-than-or-equal-to comparison operator for hash sets.
Definition hashset.ipp:88
hashset< T > copy(hashset< T > h)
Definition hashset.ipp:77
bool operator==(hashset< T > h1, hashset< T > h2)
Equality comparison operator for hash sets.
Definition hashset.ipp:107
tm_ostream & operator<<(tm_ostream &out, hashset< T > h)
Definition hashset.ipp:113
int N(hashset< T > h)
Get the number of entries in a hash set.
Definition hashset.hpp:95
SI max(SI i, SI j)
Returns the maximum of two signed integers.
Definition minmax.hpp:40
Structure representing a concrete object with a reference count.
Definition classdef.hpp:24
base class of resources
Definition resource.hpp:23