T
- A hash table entry type that implements the Cache.Entry
interface.public class Cache<T extends Cache.Entry<T>> extends Object implements Iterable<T>
Cache.Entry
interface.
The storage scheme is based on asymmetric hashing with two hash tables with different sizes in decreasing order, thus it does not really
have two unique hash functions. All it ever does is take the absolute value of the hash keys of the entries and derive mod [respective
table's size]; it applies no randomization whatsoever either. Due to the uneven table sizes, look up is biased towards the first table.
This data structure is not thread safe.Modifier and Type | Class and Description |
---|---|
static interface |
Cache.Entry<T extends Cache.Entry<T>>
An interface for hash table entries that extends the
Comparable interface and defines methods required for the
management of entries in the hash table. |
static interface |
Cache.EntryFactory<T extends Cache.Entry<T>>
A factory interface for creating table entries.
|
Constructor and Description |
---|
Cache(Cache.EntryFactory<T> factory,
int capacity)
Constructs a lossy hash table with at least the specified capacity.
|
Cache(Cache.EntryFactory<T> factory,
int capacity,
int recursions)
Constructs a lossy hash table with at least the specified capacity.
|
Modifier and Type | Method and Description |
---|---|
int |
capacity()
Returns the total number of slots in the hash table.
|
void |
clear()
Clears the hash table by emptying all entries.
|
T |
get(long key)
Return the entry identified by the input parameter key or null if it is not in the table.
|
Iterator<T> |
iterator() |
long |
memorySize()
Returns the size of the hash table in bytes.
|
boolean |
put(T entry)
Inserts an entry into the hash table.
|
boolean |
remove(long key)
Removes the entry identified by the input parameter long integer 'key' from the hash table and returns true if it is in the hash table;
returns false otherwise.
|
void |
remove(Predicate<T> condition)
Removes all the entries that match the condition specified in the argument.
|
int |
size()
Returns the number of non-empty entries in the hash table.
|
String |
toString() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
forEach, spliterator
public Cache(Cache.EntryFactory<T> factory, int capacity, int recursions)
factory
- The factory to construct the pre-initialized hash table entries.capacity
- The guaranteed minimum capacity the hash table is to have.recursions
- The maximum number of relocation cycles to perform a la cuckoo hashing.public Cache(Cache.EntryFactory<T> factory, int capacity)
factory
- The factory to construct the pre-initialized hash table entries.capacity
- The guaranteed minimum capacity the hash table is to have.public int capacity()
public int size()
public long memorySize()
public boolean put(T entry) throws NullPointerException
entry
- The entry to be inserted.NullPointerException
- If e is null.public T get(long key)
key
- The 64 bit hash key.public boolean remove(long key)
key
- The 64 bit hash key.public void remove(Predicate<T> condition) throws NullPointerException
condition
- The condition on which an entry should be removed.NullPointerException
public void clear()
public Iterator<T> iterator()
iterator
in interface Iterable<T extends Cache.Entry<T>>
Copyright © 2020. All rights reserved.