ich brauche mal grad ein wenig Hilfe,
ich hab mir ne Funktion zum Rehashen geschrieben, bin mir allerdings nicht sicher, ob das die sinnvollste Implementierung ist, könnt ihr bitte mal drüberschauen ob euch noch was dazu einfällt das eleganter zu lösen?
Code: Alles auswählen
//Rehashing
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
void rehashing (vector<int> &);
int primzahl (int);
bool primzahl1 (int);
void main(void)
{
vector <int> hashmap;
hashmap.resize(8);
hashmap[0]=16;
hashmap[1]=9;
hashmap[2]=0;
hashmap[3]=19;
hashmap[4]=32;
hashmap[5]=21;
hashmap[6]=5;
hashmap[7]=0;
rehashing(hashmap);
for (unsigned int i = 0; i < hashmap.size(); i++)
cout << i << "\t" << hashmap[i] << endl;
return;
}
void rehashing (vector<int> &Hash)
{
int M = Hash.size();
int *speicher = new int[M];
int Mnew = primzahl(M);
bool eingefuegt = false;
for (int i = 0; i < M; i++)
{
speicher[i]=Hash[i];
}
Hash.resize(Mnew);
for (int i = 0; i < M; i++)
{
Hash[i]=0;
}
for (int i = 0; i < M; i++)
{
if (Hash[speicher[i]%Mnew]==0)
Hash[speicher[i]%Mnew]=speicher[i];
else
{
eingefuegt=false;
int j = 1;
do
{
if (Hash[speicher[i+j]%Mnew]==0)
{
Hash[speicher[i+j]%Mnew]=speicher[i];
eingefuegt=true;
}
else
{
j++;
eingefuegt=false;
}
}while(!eingefuegt);
}
}
}
int primzahl(int m)
{
int erg;
bool Primzahl=false;
int j=1;
do
{
if (primzahl1(m*2+j))
{
erg=m*2+j;
Primzahl = true;
}
else
j++;
}while (!Primzahl);
return erg;
}
bool primzahl1 (int m)
{
for (int z = 2; z < m/2; z++)
{
if (m%z==0)
return false;
}
return true;
}
Sukka