Macaulay2 Engine
Loading...
Searching...
No Matches
MonomialHashTable.hpp
Go to the documentation of this file.
1/* Copyright 2005-2023, The Macaulay2 group */
2
3#pragma once
4
51
52#include "MonomialTypes.hpp"
53#include "../MemoryBlock.hpp"
54#include "MonomialView.hpp"
55#include <vector>
56
57namespace newf4 {
58
60 private:
61 std::vector<HashInt> mHashValues;
62 public:
64 : mHashValues({
65 12550986463692465404ul, 3911555212215091238ul, 15090669942851505316ul,
66 16174113364685515424ul, 18172793978567602378ul, 4970727551569665824ul,
67 15244287395755336378ul, 3641586221293608170ul, 5697307520845005385ul,
68 17982501052917221133ul, 4205210476184990958ul, 3995014217224167515ul,
69 10391875845945764299ul, 17483720614571824287ul, 1115562083531405255ul,
70 7842315096810324507ul, 673864007402015535ul, 15878473700446701422ul,
71 15632675738063166334ul, 17700395182034373329ul,
72
73 3647400739764648714ul, 15057857142894718214ul, 811470848562388018ul, 4213403134094065187ul,
74 11972908277602038105ul, 753645190001903519ul, 8409368471645073943ul, 17434420111610391988ul,
75 11290684105730860395ul, 6514428039353418497ul, 9766946806568431805ul, 12175828639099505778ul,
76 16153880744947045125ul, 5266962483604231492ul, 7437584603302882318ul, 15236881761158912593ul,
77 737715554071918441ul, 9712750857038529859ul, 7810988205991739696ul, 11544137928619240727ul,
78 2282931637490911949ul, 7479558234611642393ul, 2992086816210816745ul, 14032643290431806479ul,
79 17407261947046967157ul, 13976078027849361926ul, 3703435431725830669ul, 5190225415596727966ul,
80 7326615512078281794ul, 13621935225106626626ul, 11243108442414122854ul, 11383277907705177830ul,
81 1351966556830427717ul, 6210952720194152236ul, 5182286448890172674ul, 6242391044568798640ul,
82 4341180720656093025ul, 7345600385861738727ul, 17930341431249554713ul, 12020897693351196045ul,
83 6437449487804489292ul, 7513956725603664375ul, 1141356756031367723ul, 3599609783664644388ul
84 })
85 {
86 }
87
88 auto hashValue(const MonomialView& w) -> HashInt
89 {
90 HashInt val = 0;
91 for (auto i = w.begin(); i != w.end(); ++i)
92 // TODO: check that the variables are in range in mHashValues.
93 // if not, increase the size by adding in new HashInt's (uint64_t).
94 // val += mHashValues[(*i)%64] * (*(i+1));
95 val += mHashValues[i.var() % 64] * i.power();
96 return val;
97 }
98 };
99
100
101 // MonomialView: varpower type monomial (as in NC) (i.e. stored sparsely, length of a monomial is not constant.)
102 // MonomialIndex: some int type.
103 // HashTable.
104 // usual ops: creation, reset, findOrInsert (returns MonomialIndex)
105 // hashvalue(MonomialView).
106 // monomialAtIndex(MonomialIndex, HashTable) -> (range of MonomialView)
107 // iterator/range(MonomialIndex, HashTable)
108 //
109 // hashtable for monomials in all polynomials in the GB basis.
110 // keeps a vector of pointers to monomials.
111 // also keeps a
112 // MonomialIndex: int index of this monomial.
113
114 // HashTable for monomials in ring.
115 // 1. hash table (size 2^N)
116 // 2. std::vector<MonomialIdx>, or std::vector<int32_t*> points into Memoryblock.
117 // 3. MemoryBlock<int32_t>
118 //
119 // struct MonomiaIndex { int32_t* first; }, or struct MonomialIndex { int32_t idx; }
120 // Operations:
121 // MonomialHashTable()
122 // reset()
123 // std::pair<value result, bool> findOrInsert(value m)
124 // monomialAtIndex(idx) // returns MonomialView, or range...
125 // Requires:
126 // hashFunction(MonomialView).
127 // monomialSize(MonomialView).
128 // Question: where to store hash value?
129
130 // This doesn't work. What changes are needed for that?
131 // template<typename T>
132 // concept MonoidHashable {
133 // auto hash(T a) -> uint64_t;
134 // auto eq(T a, T b) -> bool;
135 // auto show(T a) -> std::string;
136 // };
137
139 {
140 unsigned long n_calls_find;
141 unsigned long n_clashes;
142 unsigned long max_run_length;
143 unsigned long monequal_count;
144 unsigned long monequal_fails;
145
147 : n_calls_find(0),
148 n_clashes(0),
152 {
153 }
154
155 void dump() const;
156 };
157
158 // This class depends on properties of MonomialView's.
159 // monom.size()
160 // monom == monom2
161
163 {
164 public:
165 MonomialHashTable(int log2size = 19); // use 16??
166
171
172 // Clear out the hash table, resetting all values to 0, and
173 // all stats values back to 0.
174 // BUT: the size is kept the same.
175 void reset();
176
178 auto find(const MonomialView& m, HashInt mhash) -> MonomialIndex;
180 {
181 return find(m, mHashFunction.hashValue(m));
182 }
183
187 {
188 return static_cast<const MonomialView>(
190 }
191
193 auto size() const -> size_t { return mMonomialPointers.size() - 1; } // -1 because 0 index is unused.
194
196 void dump() const;
197 void dumpBuckets() const;
198 private:
199 void reInsert(MonomialIndex i);
200 void grow();
201 private:
202 // Backing storage
204 std::vector<MonomialView> mMonomialPointers; // First element is ignored.
205 std::vector<HashInt> mHashValues;
206
207 // Hash function
209
210 // hash table itself.
211 unsigned int mLog2Size; // size of table is 2^mLog2Size
212 unsigned long mHashMask; // 2^mLog2Size - 1: mask with this to get the
213 unsigned long mThreshold; // when #elements in the table (size()) is >= this value, we grow the table.
214 std::vector<MonomialIndex> mBuckets; // each bucket contains: either 0, or MonomialIndex >= 1.
215
217 };
218
219
220
221}; // end namespace newf4
222
223// Local Variables:
224// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
225// indent-tabs-mode: nil
226// End:
Bump-pointer arena allocator for transient inner-loop allocations.
Typed integer vocabulary for namespace newf4 (indices, monomial words, hashes, masks).
newf4::MonomialView — non-owning view over a [length, var_1, e_1, ...]-encoded monomial.
Thin RAII wrapper around memtailor::Arena providing bump-pointer array allocation with optional mutex...
auto hashValue(const MonomialView &w) -> HashInt
std::vector< HashInt > mHashValues
MonomialHashFunction mHashFunction
auto find(const MonomialView &m, HashInt mhash) -> MonomialIndex
Essentially the previous case when monomial(n) = monomial 1.
void dump() const
stats and debugging information.
std::vector< MonomialView > mMonomialPointers
std::vector< HashInt > mHashValues
auto size() const -> size_t
The actual number of monomials in the table.
MonomialHashTable(int log2size=19)
void reInsert(MonomialIndex i)
std::vector< MonomialIndex > mBuckets
auto monomialAt(MonomialIndex m) const -> MonomialView
auto find(const MonomialView &m) -> MonomialIndex
uint64_t HashInt
int32_t MonomialIndex