AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
SimpleHashtable.java
Go to the documentation of this file.
1package aud.example.hash;
2
3import aud.Vector;
4import aud.SList;
7import aud.util.SVGViewer;
8
17public class SimpleHashtable<T> {
18
19 HashFunction<T> hash_ = null;
20
22 Vector<SList<T> > table_ = new Vector<SList<T> >();
23
25 int n_ = 0;
26
27
29 CollisionHandler<T> onCollision_;
30
32 int nCollisions_ = 0;
33
35 SparseMatrix<Integer> history_ = null;
36
38 int time_ = 1;
39
41 boolean verbose = false;
42
50 int size,HashFunction<T> hash,
51 CollisionHandler<T> onCollision) {
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 }
59
61 public int getNumEntries() { return n_; }
62
64 public int getTableSize() { return table_.size(); }
65
67 public float getLoadFactor() {
68 return (float) n_/(float) table_.size();
69 }
70
72 int nCollsionsHandled() { return nCollisions_; }
73
77 boolean insert(T key) {
78 long h=hash_.hash(key);
79 int idx=(int) (h % table_.size());
80 SList<T> bucket=table_.at(idx);
81 if (bucket.empty()) {
82 ++n_;
83 bucket.push_front(key);
84 recordInsert(idx);
85 if (verbose)
86 System.err.println("SimpleHashTable: insert "+key+" @ "+idx);
87 return false;
88 }
89 else if (onCollision_==null) { // separate chaining
90 ++n_;
91 bucket.push_front(key);
92 recordInsert(idx);
93 ++nCollisions_;
94 if (verbose)
95 System.err.println(
96 "SimpleHashTable: insert "+key+" @ "+idx+
97 " (bucket size="+bucket.size()+")");
98 return true;
99 }
100 else {
101 for (int j=1;j<table_.size();++j) {
102 if (verbose)
103 System.err.println(
104 "SimpleHashTable: insert "+key+" with "+onCollision_);
105
106 h=onCollision_.newHash(this,key,h,j);
107
108 if (verbose)
109 System.err.print("SimpleHashTable: "+idx+" occupied => ");
110
111 idx=(int) (h % table_.size());
112
113 if (verbose)
114 System.err.println(idx);
115
116 ++nCollisions_;
117
118 if ((bucket=table_.at(idx)).empty()) {
119 ++n_;
120 bucket.push_front(key);
121 recordInsert(idx);
122 return true;
123 }
124 }
125 // give up after N tries !
126 throw new RuntimeException("collision handling failed!");
127 }
128 }
129
130 @Override public String toString() {
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 }
144
146 void recordInsert(int i) {
147 if (history_!=null) {
148 Integer k=history_.get(i+1,time_);
149 history_.set(i+1,time_,k==null ? 1 : k+1);
150 }
151 }
152
154 public void beginRecording() {
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 }
165
167 public void nextTimeStep() {
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 }
177
180 return history_.spy(
181 "size m="+getTableSize()+", n="+getNumEntries()+
182 ", n/m="+getLoadFactor()+" ("+time_+" time steps)",
183 new ColormapCount());
184 }
185
186 public static void main(String args[]) {
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 }
201}
Implementation of a singly linked list.
Definition: SList.java:54
String toString()
Definition: SList.java:305
int size()
determine number of entries [O(n)]
Definition: SList.java:81
void push_front(T obj)
insert entry at front of list [O(1)]
Definition: SList.java:143
boolean empty()
determine if list is empty [O(1)]
Definition: SList.java:94
Implementation of an array-based vector.
Definition: Vector.java:44
void push_back(T obj)
insert new entry obj at the end [O(1) for sufficient capacity]
Definition: Vector.java:155
T at(int i)
get i-th entry [O(1)]
Definition: Vector.java:92
int size()
get number of entries [O(1)]
Definition: Vector.java:60
void reserve(int n)
Ensure capacity for n entries.
Definition: Vector.java:120
Collision handling strategy in SimpleHashtable.
abstract long newHash(SimpleHashtable< T > table, T key, long h, int count)
Handle collision by computing a new hash value.
interface for a hash function
Base class for simple hash tables (mainly for demonstration).
static void main(String args[])
float getLoadFactor()
get load factor
void nextTimeStep()
advance one time step in history, e.g., after every insert
SimpleHashtable(int size, HashFunction< T > hash, CollisionHandler< T > onCollision)
create new hash table
int getNumEntries()
get number of entries
void beginRecording()
start recording of history
SVGViewer showHistory()
show history
int getTableSize()
get size of hash table
int[] getColumnRowIndices(int j)
get row indices in column j as array
T get(int i, int j)
get entry (i,j) [O(log(nnz))]
SVGViewer spy(String caption, Colormap< T > colormap)
render spy plot in new window
Vector< T > getColumnEntries(int j)
get entries in column j as array
Simple sparse matrix data structure.
T set(int i, int j, T data)
Set entry (i,j) to data [O(log(nnz))].
color map for (small) positive integer counts
Simple SVG viewer based on Batik's SVGCanvas.
Definition: SVGViewer.java:33
sparse matrices for encoding adjacency
Definition: Coordinate.java:1
Graph data structures and algorithms.
utilities (not related to AuD lecture)
Definition: Colormap.java:1
AuD lecture: Data structures, algorithms, examples.
Definition: A234Tree.java:1