1#ifndef SIMPLEMULTIHASH_H
2#define SIMPLEMULTIHASH_H
52 memcpy(_data,other._data,
_size*
sizeof(T));
77 cout <<
"copy assignment" <<endl;
78 if (
this == &other)
return *
this;
94 std::swap(_data, other._data);
100 T& operator [] (
size_t i){
return at(i); _data[i]; }
101 T& operator [] (
size_t i)
const {
return at(i); _data[i]; }
109 T&
at(
size_t i) {
if(i>=
_size)
throw out_of_range(
"Illegal vector access");
return _data[i];}
110 T&
at(
size_t i)
const {
if(i>=
_size) {
111 cout <<
"Cap: " <<
_capacity <<
"Size: " <<
_size <<
", Idx: " << i <<endl;
112 throw out_of_range(
"Illegal vector access2");
115 void reserve(
size_t newCapacity){
119 _data =
new T[newCapacity]();
122 memcpy(_data,oldData,
_size *
sizeof(T));
128 void push_back(
const T& value){
130 _data[
_size] = value;
134 size_t size()
const {
return _size;}
135 size_t capacity()
const {
return _capacity;}
136 T* data() {
return _data;}
137 T* data()
const {
return _data;}
140 T* begin(){
return _data;};
141 T* begin()
const {
return _data;};
142 T* end(){
return _data+
_size;};
143 T* end()
const {
return _data+
_size;};
153template<
typename T,
typename D>
157 unsigned long hash(
const T& point,
const unsigned long tableSize)
const {
160 unsigned long value = point.getIndex(0);
161 for (
int d = 1; d < DimensionSparseGrid; ++d) {
162 value = value + point.getIndex(d) * PrimeNumbers::getPrimeForHash(d);
165 value = value + depth.at(0) * PrimeNumbers::getPrimeForHash(DimensionSparseGrid);
166 for (
int d = 1; d < DimensionSparseGrid; d++)
167 value = value + depth.at(d) * PrimeNumbers::getPrimeForHash(DimensionSparseGrid + d);
169 return value % tableSize;
179 unsigned long getEndOfHashgroup(
const unsigned long index)
const{
180 unsigned long tmp = index;
181 while (secondTable[tmp]>1)
183 tmp = secondTable[tmp]-2;
199 while (secondTable[tmp]>=1)
201 if( items[tmp] == item ){
205 if(secondTable[tmp]==1)
break;
206 tmp = secondTable[tmp]-2;
212 void checkIncreaseSecondTableSize() {
213 if(secondTable.capacity() == secondTable.size()){
214 secondTable.reserve(secondTable.size()*2);
215 items.reserve(items.size()*2);
221 unsigned long size()
const {
222 return secondTable.size();
231 SimpleMultiHash(D depth_ ,
const unsigned long firstTableSize = 100,
const unsigned long secondTableInitSize=300):firstTable(firstTableSize){
233 secondTable.reserve(secondTableInitSize);
234 items.reserve(secondTableInitSize);
245 unsigned long pos = hash(point,firstTable.size());
246 if(firstTable[pos]==0){
247 firstTable[pos]=secondTable.size()+1;
251 unsigned long lastPos;
255 secondTable[lastPos] = secondTable.size()+2;
256 checkIncreaseSecondTableSize();
258 secondTable.push_back(1);
259 items.push_back(point);
307 unsigned long pos = hash(point,firstTable.size());
308 if(firstTable[pos]==0){
309 firstTable[pos]=secondTable.size()+1;
311 unsigned long lastPos = getEndOfHashgroup(firstTable[pos]-1);
312 secondTable[lastPos] = secondTable.size()+2;
313 checkIncreaseSecondTableSize();
315 secondTable.push_back(1);
316 items.push_back(point);
319 bool occupied(
unsigned long& foundPos,
const T& pointToSearch)
const {
320 unsigned long pos = hash(pointToSearch,firstTable.size());
321 if(firstTable[pos]==0)
330 T getIndexOfTable(
unsigned long i)
const {
335 cout <<
"First Table: "<<endl;
336 for (
auto &&i : firstTable)
338 cout << i <<
", " <<endl;
348 unsigned long getSizeOfHashgroup(
const unsigned long index)
const{
349 unsigned long tmp = index;
350 unsigned long size = 1;
351 while (secondTable[tmp]>1)
354 tmp = secondTable[tmp]-2;
359 long double getSTDOfHash(){
360 unsigned long sum =0;
361 for (
auto &&i : firstTable)
364 unsigned long numOfItems = getSizeOfHashgroup(i-1);
367 long double mean = sum/firstTable.size();
370 for (
auto &&i : firstTable)
373 unsigned long numOfItems = getSizeOfHashgroup(i-1);
374 std += (numOfItems-mean)*(numOfItems-mean);
376 std /= firstTable.size() -1;
380 long double getRedDragonHashQuality(){
382 for (
auto &&i : firstTable)
385 unsigned long numOfItems = getSizeOfHashgroup(i-1);
386 res += (numOfItems*(numOfItems+1)/2.0)/((double)(secondTable.size()+2*firstTable.size()-1));
A simple multihash implementation. Is able to store multiple elements with the same hash.
Definition simpleMultiHash.h:154
bool addPoint(const T &point)
Will add the given point to the MultiHash. If the same point already exists it will not be added.
Definition simpleMultiHash.h:244
void addPoint_Unique(const T &point)
Will add the given point to the MultiHash. If the point already exists inside the Hashmap the behavio...
Definition simpleMultiHash.h:306
SimpleMultiHash(D depth_, const unsigned long firstTableSize=100, const unsigned long secondTableInitSize=300)
Construct a new Simple Multi Hash object.
Definition simpleMultiHash.h:231
bool checkIfItemInHashgroup(const unsigned long index, const T &item, unsigned long &outFoundPos) const
Will try to find the given item in the Hashgroup.
Definition simpleMultiHash.h:197
A simple vector implementation so as not to rely on the standard library implementation....
Definition simpleMultiHash.h:17
T & at(size_t i)
Array access with error checking.
Definition simpleMultiHash.h:109
SimpleVector(const SimpleVector &other)
copy constructor.
Definition simpleMultiHash.h:45
~SimpleVector()
Destroy the Simple Vector object.
Definition simpleMultiHash.h:34
SimpleVector & operator=(const SimpleVector &other)
copy assignment
Definition simpleMultiHash.h:75
SimpleVector(SimpleVector &&other) noexcept
move constructor
Definition simpleMultiHash.h:60
size_t _size
current number of elements in the Vector
Definition simpleMultiHash.h:23
size_t _capacity
reserved capacity for this vector. Always bigger or euqal to size
Definition simpleMultiHash.h:21
SimpleVector & operator=(SimpleVector &&other) noexcept
move assignment
Definition simpleMultiHash.h:91