Data Structures (Weiss) Chapter 5: Hash Tables without Linked Lists (quadratic probing)

来源:互联网 发布:php无限分类处理类 编辑:程序博客网 时间:2024/06/06 09:10

//

//  main.cpp

//  Data Structure TRY1

//

//  Created by zr9558 on 6/7/13.

//  Copyright (c) 2013 zr9558. All rights reserved.

//



// Data Structure C++, Weiss, P.197 Hash Tables Without Linked Lists

// quadratic probing

// here we do not define the function myhash( const HashedObj &x) 


#include<iostream>

using namespace std;

#include<vector>

#include<list>

#include<algorithm>



bool isPrime(int n)

{

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

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

    

    return true;

}


// nextPrime is used in rehash function.

int nextPrime(int n)

{

   while( !isPrime(n)) ++n;

    

   return n;

}


template<typename HashedObj>

class HashTable

{

public:

    HashTable(int size=101): array( nextPrime(size))

    {

        makeEmpty();

    }

    

   bool contains( const HashedObj &x)const;

    

   void makeEmpty();

    

   bool insert( const HashedObj & x);

    // if x is already in the hash table, then return false and do not insert it; else if x is not in the table, then return true and insert it.

    

   bool remove( const HashedObj &x);

    // removed x successfully, return true; else return false;

    

   enum EntryType{ ACTIVE, EMPTY, DELETED};

    

private:

    

   struct HashEntry

    {

        HashedObj element;

        EntryType info;

        

        HashEntry(const HashedObj &e=HashedObj(), EntryType i=EMPTY)

        :element(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)

        {

            currentPos+=offset;

            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()));

        

        // Copy table over

        currentSize=0;

       for( int i=0; i<oldArray.size(); ++i)

           if( oldArray[i].info==ACTIVE)

                insert(oldArray[i].element);

    }

    

   int myhash( const HashedObj &x)const; // not defined;

};



template<typename HashedObj>

void HashTable<HashedObj>::makeEmpty()

{

    currentSize=0;

   for( int i=0; i<array.size(); ++i)

        array[i].info=EMPTY;

}


template<typename HashedObj>

bool HashTable<HashedObj>::contains(const HashedObj &x) const

{

   return isActive(findPos(x));

}



template<typename HashedObj>

bool HashTable<HashedObj>::insert(const HashedObj &x)

{

    // insert x as active

   int currentPos=findPos(x);

   if( isActive(currentPos))

        return false;

    

    array[currentPos]=HashEntry(x,ACTIVE);

    

    // Rehash

   if( ++currentSize>array.size()/2)

        rehash();

    

    return true;

}


template<typename HashedObj>

bool HashTable<HashedObj>::remove(const HashedObj &x)

{

   int currentPos=findPos(x);

   if( !isActive(currentPos))

        return false;

    

    array[currentPos].info=DELETED;

    return true;

}




int main()

{

    

    

    

    

    

    

    

    return 0;

}


原创粉丝点击