AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
aud.example.hash.SimpleHashtable< T > Class Template Reference

Base class for simple hash tables (mainly for demonstration). More...

+ Collaboration diagram for aud.example.hash.SimpleHashtable< T >:

Public Member Functions

 SimpleHashtable (int size, HashFunction< T > hash, CollisionHandler< T > onCollision)
 create new hash table More...
 
int getNumEntries ()
 get number of entries More...
 
int getTableSize ()
 get size of hash table More...
 
float getLoadFactor ()
 get load factor More...
 
String toString ()
 
void beginRecording ()
 start recording of history More...
 
void nextTimeStep ()
 advance one time step in history, e.g., after every insert More...
 
SVGViewer showHistory ()
 show history More...
 

Static Public Member Functions

static void main (String args[])
 

Detailed Description

Base class for simple hash tables (mainly for demonstration).

Note:This class is intended to visualize hash tables, it is not an efficient or particularly useful implementation of a hash table!

Parameters
<T>table entry

Definition at line 17 of file SimpleHashtable.java.

Constructor & Destructor Documentation

◆ SimpleHashtable()

aud.example.hash.SimpleHashtable< T >.SimpleHashtable ( int  size,
HashFunction< T >  hash,
CollisionHandler< T >  onCollision 
)

create new hash table

Parameters
sizesize of table (could be any "bad" size, "good" sizes are prime)
hashhash function
onCollisionstrategy for collision handling, a value null forces separate chaining

Definition at line 49 of file SimpleHashtable.java.

51 {
52 hash_=hash;
53 onCollision_=onCollision;
54
55 table_.reserve(size);
56 for (int i=0;i<size;++i)
57 table_.push_back(new SList<T>());
58 }

Member Function Documentation

◆ beginRecording()

void aud.example.hash.SimpleHashtable< T >.beginRecording ( )

start recording of history

Definition at line 154 of file SimpleHashtable.java.

154 {
155 history_=new SparseMatrix<Integer>();
156 time_=1;
157 history_.set(getTableSize(),1,0);
158
159 for (int i=0;i<table_.size();++i) {
160 SList<T> bucket=table_.at(i);
161 if (!bucket.empty())
162 history_.set(i,time_,bucket.size());
163 }
164 }
int getTableSize()
get size of hash table
T set(int i, int j, T data)
Set entry (i,j) to data [O(log(nnz))].

Referenced by aud.example.hash.SimpleHashtable< T >.main().

+ Here is the caller graph for this function:

◆ getLoadFactor()

float aud.example.hash.SimpleHashtable< T >.getLoadFactor ( )

get load factor

Definition at line 67 of file SimpleHashtable.java.

67 {
68 return (float) n_/(float) table_.size();
69 }

Referenced by aud.example.hash.SimpleHashtable< T >.toString().

+ Here is the caller graph for this function:

◆ getNumEntries()

int aud.example.hash.SimpleHashtable< T >.getNumEntries ( )

get number of entries

Definition at line 61 of file SimpleHashtable.java.

61{ return n_; }

Referenced by aud.example.hash.SimpleHashtable< T >.toString().

+ Here is the caller graph for this function:

◆ getTableSize()

int aud.example.hash.SimpleHashtable< T >.getTableSize ( )

get size of hash table

Definition at line 64 of file SimpleHashtable.java.

64{ return table_.size(); }

Referenced by aud.example.hash.SimpleHashtable< T >.toString().

+ Here is the caller graph for this function:

◆ main()

static void aud.example.hash.SimpleHashtable< T >.main ( String  args[])
static

Definition at line 186 of file SimpleHashtable.java.

186 {
187 SimpleHashtable<Integer> h=
188 new SimpleHashtable<Integer>(37,
189 new HashFunction<Integer>() {
190 public long hash(Integer i) { return i*13+7; }
191 },null
192 );
193
194 h.beginRecording();
195 for (int i=0;i<100;++i) {
196 h.nextTimeStep();
197 h.insert(i);
198 }
199 h.showHistory();
200 }

References aud.example.hash.SimpleHashtable< T >.beginRecording(), aud.example.hash.SimpleHashtable< T >.nextTimeStep(), and aud.example.hash.SimpleHashtable< T >.showHistory().

+ Here is the call graph for this function:

◆ nextTimeStep()

void aud.example.hash.SimpleHashtable< T >.nextTimeStep ( )

advance one time step in history, e.g., after every insert

Definition at line 167 of file SimpleHashtable.java.

167 {
168 // copy previous row
169 Vector<Integer> cnt=history_.getColumnEntries(time_);
170 int[] ri=history_.getColumnRowIndices(time_);
171 ++time_;
172
173 int i=0;
174 for (Integer c : cnt)
175 history_.set(ri[i++],time_,c);
176 }

Referenced by aud.example.hash.SimpleHashtable< T >.main().

+ Here is the caller graph for this function:

◆ showHistory()

show history

Definition at line 179 of file SimpleHashtable.java.

179 {
180 return history_.spy(
181 "size m="+getTableSize()+", n="+getNumEntries()+
182 ", n/m="+getLoadFactor()+" ("+time_+" time steps)",
183 new ColormapCount());
184 }
float getLoadFactor()
get load factor
int getNumEntries()
get number of entries

Referenced by aud.example.hash.SimpleHashtable< T >.main().

+ Here is the caller graph for this function:

◆ toString()

String aud.example.hash.SimpleHashtable< T >.toString ( )

Definition at line 130 of file SimpleHashtable.java.

130 {
131 String s=
132 "SimpleHashTable [m="+getTableSize()+
133 ", n="+getNumEntries()+", n/m="+getLoadFactor()+", counted "+nCollisions_+
134 " collisions]\n";
135 for (int i=0;i<table_.size();++i) {
136 SList<T> bucket=table_.at(i);
137 s+=i+": ";
138 if (!bucket.empty())
139 s+=bucket.toString();
140 s+="\n";
141 }
142 return s;
143 }

References aud.example.hash.SimpleHashtable< T >.getLoadFactor(), aud.example.hash.SimpleHashtable< T >.getNumEntries(), and aud.example.hash.SimpleHashtable< T >.getTableSize().

+ Here is the call graph for this function:

The documentation for this class was generated from the following file: