Data Structures (Weiss) Chapter 5: Separate Chain
2013-07-10 10:36
232 查看
//
// main.cpp
// Data Structure TRY1
//
// Created by zr9558 on 6/7/13.
// Copyright (c) 2013 zr9558. All rights reserved.
//
// Data Structure C++, Weiss, P.189 Separate Chaining
// we can not use the class HashTable directly, since there is a hash function in the class which we do not define it. Therefore, in P.190, the author gives an example about Employee.
#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;
}
// 37 is a prime. The hash function is a hash function for string.
int hash(
const string &key)
{
int hashVal=0;
for( int i=0; i!=key.size(); ++i)
hashVal+=37*hashVal+key[i];
return hashVal;
}
// The hash function is a hash function for integers.
int hash(
int key)
{
return key;
}
template<typename HashedObj>
class HashTable
{
public:
HashTable(
int size=101);
bool contains( const HashedObj &x)
const;
void makeEmpty();
// clear all lists
bool insert( const HashedObj &x);
// x is already in HashTable, return false; else return true;
bool remove( const HashedObj &x);
// x is in HashTable and remove it, return true; else return false;
private:
vector<list<HashedObj> > theLists;
int currentSize;
void rehash()
{
vector<list<HashedObj> > oldLists=theLists;
// Create new double-sized, empty table;
theLists.resize(nextPrime(2*theLists.size()));
for( int j=0; j<theLists.size(); ++j)
theLists[j].clear();
//Copy table over
currentSize=0;
for( int i=0; i<oldLists.size(); ++i)
{
list<HashedObj>::iterator itr=oldLists[i].begin();
while( itr!=oldLists[i].end())
insert( *itr++);
}
}
int myhash( const HashedObj &x)
const
{
int hashVal=hash(x);
// the hash function changes HashedObj x to an integer
hashVal%=theLists.size();
if( hashVal<0)
hashVal+=theLists.size();
return hashVal;
}
};
template<typename HashedObj>
void
HashTable<HashedObj>::makeEmpty()
{
for( int i=0; i<theLists.size(); ++i)
theLists[i].clear();
}
template<typename HashedObj>
bool
HashTable<HashedObj>::contains(
const HashedObj &x) const
{
const list<HashedObj> & whichList=
theLists[myhash(x)];
return find(whichList.begin(), whichList.end(),x)!=whichList.end();
}
template<typename HashedObj>
bool
HashTable<HashedObj>::remove(
const HashedObj &x)
{
list<HashedObj> & whichList=theLists[myhash(x)];
if( find(whichList.begin(), whichList.end(),x)==whichList.end())
return false;
whichList.erase( find(whichList.begin(), whichList.end(),x)==whichList.end());
--currentSize;
return
true;
}
template<typename HashedObj>
bool
HashTable<HashedObj>::insert(const HashedObj &x)
{
list<HashedObj> & whichList=theLists[myhash(x)];
if( find(whichList.begin(), whichList.end(),x)==whichList.end())
return false;
whichList.push_back(x);
if( ++currentSize>theLists.size())
rehash();
return
true;
}
int main()
{
return
0;
}
// main.cpp
// Data Structure TRY1
//
// Created by zr9558 on 6/7/13.
// Copyright (c) 2013 zr9558. All rights reserved.
//
// Data Structure C++, Weiss, P.189 Separate Chaining
// we can not use the class HashTable directly, since there is a hash function in the class which we do not define it. Therefore, in P.190, the author gives an example about Employee.
#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;
}
// 37 is a prime. The hash function is a hash function for string.
int hash(
const string &key)
{
int hashVal=0;
for( int i=0; i!=key.size(); ++i)
hashVal+=37*hashVal+key[i];
return hashVal;
}
// The hash function is a hash function for integers.
int hash(
int key)
{
return key;
}
template<typename HashedObj>
class HashTable
{
public:
HashTable(
int size=101);
bool contains( const HashedObj &x)
const;
void makeEmpty();
// clear all lists
bool insert( const HashedObj &x);
// x is already in HashTable, return false; else return true;
bool remove( const HashedObj &x);
// x is in HashTable and remove it, return true; else return false;
private:
vector<list<HashedObj> > theLists;
int currentSize;
void rehash()
{
vector<list<HashedObj> > oldLists=theLists;
// Create new double-sized, empty table;
theLists.resize(nextPrime(2*theLists.size()));
for( int j=0; j<theLists.size(); ++j)
theLists[j].clear();
//Copy table over
currentSize=0;
for( int i=0; i<oldLists.size(); ++i)
{
list<HashedObj>::iterator itr=oldLists[i].begin();
while( itr!=oldLists[i].end())
insert( *itr++);
}
}
int myhash( const HashedObj &x)
const
{
int hashVal=hash(x);
// the hash function changes HashedObj x to an integer
hashVal%=theLists.size();
if( hashVal<0)
hashVal+=theLists.size();
return hashVal;
}
};
template<typename HashedObj>
void
HashTable<HashedObj>::makeEmpty()
{
for( int i=0; i<theLists.size(); ++i)
theLists[i].clear();
}
template<typename HashedObj>
bool
HashTable<HashedObj>::contains(
const HashedObj &x) const
{
const list<HashedObj> & whichList=
theLists[myhash(x)];
return find(whichList.begin(), whichList.end(),x)!=whichList.end();
}
template<typename HashedObj>
bool
HashTable<HashedObj>::remove(
const HashedObj &x)
{
list<HashedObj> & whichList=theLists[myhash(x)];
if( find(whichList.begin(), whichList.end(),x)==whichList.end())
return false;
whichList.erase( find(whichList.begin(), whichList.end(),x)==whichList.end());
--currentSize;
return
true;
}
template<typename HashedObj>
bool
HashTable<HashedObj>::insert(const HashedObj &x)
{
list<HashedObj> & whichList=theLists[myhash(x)];
if( find(whichList.begin(), whichList.end(),x)==whichList.end())
return false;
whichList.push_back(x);
if( ++currentSize>theLists.size())
rehash();
return
true;
}
int main()
{
return
0;
}
相关文章推荐
- Data Structures (Weiss) Chapter 6: Leftist Heap
- Data Structures (Weiss) Chapter 6: Binomial Queue
- Data Structures (Weiss) Chapter 4: Binary Search Tree C++
- Data Structures (Weiss) Chapter 4: AVL Trees 平衡树
- Data Structures (Weiss) Chapter 12: Deterministic Skip Lists (跳跃链表)
- Data Structures (Weiss) Chapter 3: Vector C++
- Data Structures (Weiss) Chapter 12: Deterministic Skip Lists (跳跃链表,Comparable)
- Data Structures (Weiss) Chapter 3: List C++
- Data Structures (Weiss) Chapter 8: Union and Find, Disjoint Sets, union by sizes, C++
- Data Structures (Weiss) Chapter 7: Insertion Sort
- Data Structures (Weiss) Chapter 5: Hash Tables without Linked Lists (quadratic probing)
- Data Structures (Weiss) Chapter 7: HeapSort
- Data Structures (Weiss) Chapter 7: MergeSort
- Data Structures (Weiss) Chapter 6: Binary Heap, Priority Queue
- Data Structures (Weiss) Chapter 7: QuickSort 快速排序
- Data Structures (Weiss) Chapter 3: The Queue ADT, Pointer
- Data Structures (Weiss) Chapter 12: Treaps
- Data Structures (Weiss) Chapter 12: Splay Tree 伸展树
- Data Structures (Weiss) Chapter 3: The Queue ADT, Array
- (转载)左倾堆 (leftist heap):方法参见 Data Structures (Weiss)