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;
}
- Data Structures (Weiss) Chapter 5: Hash Tables without Linked Lists (quadratic probing)
- Data Structures (Weiss) Chapter 5: Separate Chain
- Data Structures (Weiss) Chapter 3: Vector C++
- Data Structures (Weiss) Chapter 3: List C++
- Data Structures (Weiss) Chapter 6: Leftist Heap
- Data Structures (Weiss) Chapter 6: Binomial Queue
- Data Structures (Weiss) Chapter 7: Insertion Sort
- Data Structures (Weiss) Chapter 7: HeapSort
- Data Structures (Weiss) Chapter 7: MergeSort
- Data Structures (Weiss) Chapter 12: Treaps
- Data Structures (Weiss) Chapter 12: Deterministic Skip Lists (跳跃链表)
- Data Structures (Weiss) Chapter 12: Deterministic Skip Lists (跳跃链表,Comparable)
- Data Structures (Weiss) Chapter 4: Binary Search Tree C++
- Data Structures (Weiss) Chapter 4: AVL Trees 平衡树
- Data Structures (Weiss) Chapter 6: Binary Heap, Priority Queue
- Data Structures (Weiss) Chapter 3: The Queue ADT, Pointer
- Data Structures (Weiss) Chapter 3: The Queue ADT, Array
- Data Structures (Weiss) Chapter 7: QuickSort 快速排序
- UML从需求到实现----包图 .
- 欧拉函数
- oracle中的日志切换和检查点
- JAVA中堆和堆栈区别
- oracle添加删除重做日志成员
- Data Structures (Weiss) Chapter 5: Hash Tables without Linked Lists (quadratic probing)
- wince注册表编程(新建,读取,修改)
- android 自定义样式文件
- hdu 1714 排列2
- 链表
- oracle中添加删除重做日志文件组
- Datetime格式化大全
- WebService传递XML数据 C#DataSet操作XML 解析WebService返回的XML数据
- Linux 中直接 I/O 机制的介绍