Macaulay2 Engine
Loading...
Searching...
No Matches
monhashtable.cpp
Go to the documentation of this file.
1// Copyright 2005-2021 Michael E. Stillman
2
3#include "f4/monhashtable.hpp"
4#include <string.h> // for memset
5#include <iostream> // for operator<<, endl, ostream, cout, basic_ostream
6
7#define HASHVALUE(m) (M->hash_value(m))
8#define MONOMIAL_EQUAL(m, n) (M->is_equal(m, n))
9
10template <typename ValueType>
12{
13 memset(hashtab.get(), 0, sizeof(value) * size);
14
15 count = 0;
17 nclashes = 0;
21}
22
23template <typename ValueType>
25{
26 logsize = logsize0;
27 size = (1 << logsize);
28 // threshold = size/3; // was 2*size/3
29 // threshold = 2*size/3; // was 2*size/3
30 threshold = size / 16; // was 2*size/3
31 hashtab = std::unique_ptr<value[]>(new value[size]);
32 hashmask = size - 1;
33 reset();
34}
35
36template <typename ValueType>
38{
39 long hashval = HASHVALUE(m) & hashmask;
40 while (hashtab[hashval])
41 {
42 hashval++;
43 nclashes++;
44 if (hashval == size) hashval = 0;
45 }
46 hashtab[hashval] = m;
47 count++;
48 if (count > threshold) grow();
49}
50
51template <typename ValueType>
53{
54 // Increase logsize, reset fields, and repopulate new hash table.
55 // if (M2_gbTrace >= 2) dump();
56 std::unique_ptr<value[]> oldtab = std::move(hashtab);
57 long oldsize = size;
59 for (long i = 0; i < oldsize; i++)
60 if (oldtab[i]) insert(oldtab[i]);
61}
62
63template <typename ValueType>
65 int logsize0)
66 : M(M0),
67 hashtab(nullptr), // set in body
68 size(0),
69 logsize(logsize0),
70 hashmask(0),
71 threshold(0),
72 count(0),
74 nclashes(0),
78{
79 initialize(logsize0);
80}
81
82template <typename ValueType>
84{
85 auto mhash = HASHVALUE(m);
86 auto hashval = mhash & hashmask;
87 if (!hashtab[hashval])
88 {
89 // No entry is there. So, we insert it directly.
90 hashtab[hashval] = m;
91 result = m;
92 count++;
93 if (count > threshold) grow();
94 return false;
95 }
96 else
97 {
98 // Something is there, so we need to find either this value,
99 // or a free spot, whichever comes first.
100 value *hashtop = hashtab.get() + size;
101 for (value *i = hashtab.get() + hashval;; i++)
102 {
103 if (i == hashtop) i = hashtab.get();
104 if (!(*i))
105 {
106 // Spot is empty, so m is a new value
107 *i = m;
108 result = m;
109 count++;
110 if (count > threshold) grow();
111 return false;
112 }
113 // if (HASHVALUE(*i) == mhash)
114 // {
115 if (MONOMIAL_EQUAL(m, *i))
116 {
117 result = *i;
118 return true;
119 }
120 // }
121 }
122 }
123}
124
125template <typename ValueType>
127{
128 std::cout << "--hash table info--" << std::endl;
129 std::cout << " size of hashtable = " << size << std::endl;
130 std::cout << " number of monoms = " << count << std::endl;
131 std::cout << " number of calls = " << nfind_or_insert << std::endl;
132 std::cout << " number of clashes = " << nclashes << std::endl;
133 std::cout << " max run length = " << max_run_length << std::endl;
134 std::cout << " monequal calls = " << monequal_count << std::endl;
135 std::cout << " monequal false = " << monequal_fails << std::endl;
136}
137
138template <typename ValueType>
140{
141 long nzeros = 0;
142 for (long i = 0; i < size; i++)
143 {
144 if (hashtab[i] == 0)
145 nzeros++;
146 else
147 {
148 value m = hashtab[i];
149 if (nzeros > 0)
150 {
151 std::cerr << "-- " << nzeros << " zero elements --" << std::endl;
152 nzeros = 0;
153 }
154 std::cerr << "hash " << M->hash_value(m) << " monomial ";
155 M->show(m);
156 std::cerr << std::endl;
157 }
158 }
159 if (nzeros > 0) std::cerr << "-- " << nzeros << " zero elements --" << std::endl;
160}
161
167
168// Local Variables:
169// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
170// indent-tabs-mode: nil
171// End:
unsigned int logsize
unsigned long monequal_count
std::unique_ptr< value[]> hashtab
unsigned long size
MonomialHashTable(const ValueType *M0, int logsize=24)
unsigned long nclashes
unsigned long threshold
unsigned long monequal_fails
const ValueType * M
void initialize(int logsize0)
ValueType::value value
bool find_or_insert(value m, value &result)
unsigned long count
unsigned long max_run_length
void insert(value m)
unsigned long nfind_or_insert
unsigned long hashmask
VALGRIND_MAKE_MEM_DEFINED & result(result)
#define HASHVALUE(m)
#define MONOMIAL_EQUAL(m, n)
MonomialHashTable<ValueType> — open-addressing intern table for F4 and resolution monomials.