java.lang.Object
net.metanotion.util.skiplist.SkipList<K,V>
- Type Parameters:
K- type of keys, must be comparableV- type of values
- Direct Known Subclasses:
BSkipList
public class SkipList<K extends Comparable<? super K>, V>extends Object implements Flushable, Iterable<V>
Skip list data structure implementation for efficient key-value storage and retrieval. Provides O(log n) average time complexity for search, insertion, and deletion operations.
Field Summary
Modifier and TypeFieldDescriptionFirst span in the list.private static final intWhatmaxLevels()reports - span counts are no longer tracked here.protected static final intThe probability of each next higher level.static final RandomRandom number generator, the I2P one rather than the upstream default.protected intNumber of items in the list.protected SkipLevels<K, V> Level stack above the first span.Constructor Summary
Method Summary
All MethodsModifier and TypeMethodDescriptionvoidaddItem()Increment item count.voidbalance()Rebalance the skip list levels, adjusted to guarantee O(log n) searches.voiddelItem()Decrement item count, minimum zero.Look up the first stored key greater than or equal to the one given, returning an iterator already positioned there.voidflush()Flush any pending writes.intChoose the column height for a newly inserted node, using the geometric distribution that keeps the list's search cost logarithmic.Get the value for a key.iterator()Return an iterator over all entries.intFix if for some reason you want a huge in-memory skiplist.voidInsert or update a key-value pair in the skip list.Remove a key-value pair from the skip list.intsize()Return the number of items.Methods inherited from class Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Iterable
forEach, spliterator
Field Details
P
protected static final int PThe probability of each next higher level.- See Also:
MIN_SLOTS
private static final int MIN_SLOTSWhatmaxLevels()reports - span counts are no longer tracked here.- See Also:
first
First span in the list. Reassigned, though it is never replaced after construction.stack
Level stack above the first span.rng
Random number generator, the I2P one rather than the upstream default.size
protected int sizeNumber of items in the list.
Constructor Details
SkipList
protected SkipList()Constructor for subclass use.SkipList
public SkipList(int span) Create a new skip list.- Parameters:
span- the maximum number of entries per span, 1 to SkipSpan.MAX_SIZE- Throws:
IllegalArgumentException- if size too big or too small
Method Details
flush
size
public int size()Return the number of items.- Returns:
- the number of entries currently held in the list
addItem
public void addItem()Increment item count.delItem
public void delItem()Decrement item count, minimum zero.maxLevels
public int maxLevels()Fix if for some reason you want a huge in-memory skiplist.- Returns:
- 4 since we don't track span count here any more - see override
generateColHeight
public int generateColHeight()Choose the column height for a newly inserted node, using the geometric distribution that keeps the list's search cost logarithmic.- Returns:
- 0..
maxLevels(), each successive one with probability 1 / P
put
remove
get
iterator
Return an iterator over all entries.- Specified by:
iteratorin interfaceIterable<K extends Comparable<? super K>>
find
Look up the first stored key greater than or equal to the one given, returning an iterator already positioned there.- Parameters:
key- the key to position the iterator at- Returns:
- an iterator where nextKey() is the first one greater than or equal to 'key'
balance
public void balance()Rebalance the skip list levels, adjusted to guarantee O(log n) searches. The work is proportional to the number of spans.