Lolly 1.4.28
Loading...
Searching...
No Matches
hashset.ipp
Go to the documentation of this file.
1
2/******************************************************************************
3 * MODULE : hashset.cpp
4 * DESCRIPTION: fixed size hashsets with reference counting
5 * COPYRIGHT : (C) 1999 Joris van der Hoeven
6 *******************************************************************************
7 * This software falls under the GNU general public license version 3 or later.
8 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
9 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
10 ******************************************************************************/
11
12#ifndef HASHSET_CC
13#define HASHSET_CC
14#include "hashset.hpp"
15
16template <class T>
17void
19 int i;
20 int oldn= n;
21 list<T>* olda= a;
22 n = n2;
23 a = tm_new_array<list<T>> (n);
24 for (i= 0; i < oldn; i++) {
25 list<T> l (olda[i]);
26 while (!is_nil (l)) {
27 list<T>& newl= a[hash (l->item) & (n - 1)];
28 newl = list<T> (l->item, newl);
29 l = l->next;
30 }
31 }
33}
34
35template <class T>
36static T*
37search (list<T> l, T x) {
38 while (!is_nil (l)) {
39 if (l->item == x) return &(l->item);
40 l= l->next;
41 }
42 return NULL;
43}
44
45template <class T>
46bool
48 return (search (a[hash (x) & (n - 1)], x) == NULL ? false : true);
49}
50
51template <class T>
52void
54 if (size == n * max) resize (n << 1);
55 list<T>& l= a[hash (x) & (n - 1)];
56 if (search (l, x) != NULL) return;
57 l= list<T> (x, l);
58 size++;
59}
60
61template <class T>
62void
64 list<T>* lptr= &a[hash (x) & (n - 1)];
65 while (!is_nil (*lptr)) {
66 if ((*lptr)->item == x) {
67 *lptr= (*lptr)->next;
68 size--;
69 return;
70 }
71 lptr= &((*lptr)->next);
72 }
73}
74
75template <class T>
78 int i, n= h->n;
79 hashset<T> h2 (n, h->max);
80 h2->size= h->size;
81 for (i= 0; i < n; i++)
82 h2->a[i]= copy (h->a[i]);
83 return h2;
84}
85
86template <class T>
87bool
89 int i= 0, j= 0, n= h1->n;
90 if (N (h1) > N (h2)) return false;
91 for (; i < n; i++) {
92 list<T> l= h1->a[i];
93 for (; !is_nil (l); l= l->next, j++)
94 if (!h2->contains (l->item)) return false;
95 }
96 return true;
97}
98
99template <class T>
100bool
102 return (N (h1) < N (h2)) && (h1 <= h2);
103}
104
105template <class T>
106bool
108 return (N (h1) == N (h2)) && (h1 <= h2);
109}
110
111template <class T>
114 int i= 0, j= 0, n= h->n, size= h->size;
115 out << "{ ";
116 for (; i < n; i++) {
117 list<T> l= h->a[i];
118 for (; !is_nil (l); l= l->next, j++) {
119 out << l->item;
120 if (j != size - 1) out << ", ";
121 }
122 }
123 out << " }";
124 return out;
125}
126
127template <class T>
129operator<< (hashset<T>& h, T x) {
130 h->insert (x);
131 return h;
132}
133
134#endif // defined HASHSET_CC
int N(array< T > a)
Get the length of the array.
Definition array.hpp:170
int hash(pointer ptr)
Computes a hash value for a pointer.
Definition basic.cpp:17
bool is_nil(blackbox x)
Definition blackbox.hpp:29
void resize(int n)
Definition hashset.ipp:18
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 list class represents a linked list.
Definition list.hpp:48
list(T item)
Construct a new list object with a single item.
Definition list.hpp:137
void tm_delete_array(C *Ptr)
static T * search(list< T > l, T x)
Definition hashset.ipp:37
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
bool is_nil(list< T > l)
Check if a list is nil (i.e., an empty list).
SI max(SI i, SI j)
Returns the maximum of two signed integers.
Definition minmax.hpp:40