1package aud.example.hash;
41 boolean verbose =
false;
53 onCollision_=onCollision;
56 for (
int i=0;i<size;++i)
68 return (
float) n_/(float) table_.
size();
72 int nCollsionsHandled() {
return nCollisions_; }
77 boolean insert(T key) {
78 long h=hash_.hash(key);
79 int idx=(int) (h % table_.
size());
86 System.err.println(
"SimpleHashTable: insert "+key+
" @ "+idx);
89 else if (onCollision_==
null) {
96 "SimpleHashTable: insert "+key+
" @ "+idx+
97 " (bucket size="+bucket.
size()+
")");
101 for (
int j=1;j<table_.
size();++j) {
104 "SimpleHashTable: insert "+key+
" with "+onCollision_);
106 h=onCollision_.
newHash(
this,key,h,j);
109 System.err.print(
"SimpleHashTable: "+idx+
" occupied => ");
111 idx=(int) (h % table_.
size());
114 System.err.println(idx);
118 if ((bucket=table_.
at(idx)).empty()) {
126 throw new RuntimeException(
"collision handling failed!");
135 for (
int i=0;i<table_.
size();++i) {
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);
159 for (
int i=0;i<table_.
size();++i) {
162 history_.
set(i,time_,bucket.
size());
174 for (Integer c : cnt)
175 history_.
set(ri[i++],time_,c);
186 public static void main(String args[]) {
190 public long hash(Integer i) {
return i*13+7; }
195 for (
int i=0;i<100;++i) {
Implementation of a singly linked list.
int size()
determine number of entries [O(n)]
void push_front(T obj)
insert entry at front of list [O(1)]
boolean empty()
determine if list is empty [O(1)]
Implementation of an array-based vector.
void push_back(T obj)
insert new entry obj at the end [O(1) for sufficient capacity]
T at(int i)
get i-th entry [O(1)]
int size()
get number of entries [O(1)]
void reserve(int n)
Ensure capacity for n entries.
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.
sparse matrices for encoding adjacency
Graph data structures and algorithms.
utilities (not related to AuD lecture)
AuD lecture: Data structures, algorithms, examples.