You are on page 1of 5

ADT Hash i Mbyllur Kuadratik

HashKuadratik.h

#ifndef HASHKUADRATIK_H
#define HASHKUADRATIK_H

#include <vector>
#include <algorithm>
#include <functional>
#include <string>
using namespace std;

int nextPrime( int n );

template <typename HashedObj>


class HashKuadratik
{
public:
explicit HashKuadratik( int size = 101 ) : array( nextPrime( size ) )
{ makeEmpty( ); }

bool contains( const HashedObj & x ) const


{
return isActive( findPos( x ) );
}

void makeEmpty( )
{
currentSize = 0;
for( auto & entry : array )
entry.info = EMPTY;
}

bool insert( const HashedObj & x )


{
// Insert x as active
int currentPos = findPos( x );
if( isActive( currentPos ) )
return false;

array[ currentPos ].element = x;


array[ currentPos ].info = ACTIVE;

// Rehash
if( ++currentSize > array.size( ) / 2 )
rehash( );

return true;
}

bool insert( HashedObj && x )

1
ADT Hash i Mbyllur Kuadratik
{
// Insert x as active
int currentPos = findPos( x );
if( isActive( currentPos ) )
return false;

array[ currentPos ] = std::move( x );


array[ currentPos ].info = ACTIVE;

// Rehash;
if( ++currentSize > array.size( ) / 2 )
rehash( );

return true;
}

bool remove( const HashedObj & x )


{
int currentPos = findPos( x );
if( !isActive( currentPos ) )
return false;

array[ currentPos ].info = DELETED;


return true;
}

enum EntryType { ACTIVE, EMPTY, DELETED };

private:
struct HashEntry
{
HashedObj element;
EntryType info;

HashEntry( const HashedObj & e = HashedObj{ }, EntryType i = EMPTY ): element{ e },


info{ i } { }

HashEntry( HashedObj && e, EntryType i = EMPTY ) : element{ std::move( e ) }, info{


i } { }
};

vector<HashEntry> array;
int currentSize;

bool isActive( int currentPos ) const


{ return array[ currentPos ].info == ACTIVE; }

int findPos( const HashedObj & x ) const


{
int offset = 1;
int currentPos = myhash( x );

while( array[ currentPos ].info != EMPTY &&


array[ currentPos ].element != x )
{

2
ADT Hash i Mbyllur Kuadratik
currentPos += offset; // Compute ith probe
offset += 2;
if( currentPos >= array.size( ) )
currentPos -= array.size( );
}

return currentPos;
}

void rehash( )
{
vector<HashEntry> oldArray = array;

// Create new double-sized, empty table


array.resize( nextPrime( 2 * oldArray.size( ) ) );
for( auto & entry : array )
entry.info = EMPTY;

// Copy table over


currentSize = 0;
for( auto & entry : oldArray )
if( entry.info == ACTIVE )
insert( std::move( entry.element ) );
}

size_t myhash( const HashedObj & x ) const


{
static hash<HashedObj> hf;
return hf( x ) % array.size( );
}
};

#endif // HASHKUADRATIK_H

3
ADT Hash i Mbyllur Kuadratik

HashKuadratik.cpp
#include "HashKuadratik.h"
#include <iostream>
using namespace std;

/**
Nuk eshte metode eficente
*/
bool isPrime( int n )
{
if( n == 2 || n == 3 )
return true;

if( n == 1 || n % 2 == 0 )
return false;

for( int i = 3; i * i <= n; i += 2 )


if( n % i == 0 )
return false;

return true;
}

int nextPrime( int n )


{
if( n % 2 == 0 )
++n;

for( ; !isPrime( n ); n += 2 )
;

return n;
}

4
ADT Hash i Mbyllur Kuadratik

Main.cpp
#include <iostream>

#include <iostream>
#include "HashKuadratik.h"
using namespace std;

int main( )
{
HashKuadratik<int> h1;
HashKuadratik<int> h2;

const int NUMS = 4000;


const int GAP = 37;
int i;

for( i = GAP; i != 0; i = ( i + GAP ) % NUMS )


h1.insert( i );

h2 = h1;

for( i = 1; i < NUMS; i += 2 )


h2.remove( i );

for( i = 2; i < NUMS; i += 2 )


if( !h2.contains( i ) )
cout << "Nuk ndodhet " << i << endl;

for( i = 1; i < NUMS; i += 2 )


{
if( h2.contains( i ) )
cout << "!!!! " << i << endl;
}

return 0;
}

You might also like