Browse Source

skip list map is only partially working.

master
Charles Reid 8 years ago
parent
commit
2c8e3d1751
  1. 2
      hash/Makefile
  2. 5
      hash/README.md
  3. 66
      hash/SkipList.java
  4. 197
      hash/SkipListMap.java

2
hash/Makefile

@ -1,3 +1,5 @@ @@ -1,3 +1,5 @@
skipmap:
javac SkipListMap.java && java SkipListMap
skip:
javac SkipList.java && java SkipList
bhash:

5
hash/README.md

@ -329,4 +329,9 @@ Random numbers: @@ -329,4 +329,9 @@ Random numbers:
* No idea what's going on with these random numbers and the bit stuffing.
## Skip List Map
This is only partially complete. The core object is finished, but getting the iterators, nodes, and
node type-casting (from value-only SkipNodes to key-value SkipListMapNodes) to work is a nightmare.

66
hash/SkipList.java

@ -1,3 +1,5 @@ @@ -1,3 +1,5 @@
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
@ -106,7 +108,7 @@ public class SkipList<T extends Comparable<T>> { @@ -106,7 +108,7 @@ public class SkipList<T extends Comparable<T>> {
* The skip node is n object that represents a single node
* in a skip list. Nodes may have many neighbors.
*/
protected static class SkipNode<T extends Comparable<T>> {
public static class SkipNode<T extends Comparable<T>> {
// node at level i stores i+1 neighbors
//private ArrayList<SkipNode<T>> next;
private SkipNode<T>[] next;
@ -189,11 +191,23 @@ public class SkipList<T extends Comparable<T>> { @@ -189,11 +191,23 @@ public class SkipList<T extends Comparable<T>> {
this.randomSeed = seedGenerator.nextInt();
}
/** Get the head of the skip list. */
public SkipNode<T> getHead() {
return head;
}
/** Clear out the skip list. */
public void clear() {
// Kill em all, let the garbage collector sort them out.
this.head = null;
}
/** Get size of skip list. */
public int size() {
return this.size;
}
/** Pure magic. */
/** Pure WTF magic. */
private int getRandom() {
int x = randomSeed;
x ^= x << 13;
@ -477,6 +491,16 @@ public class SkipList<T extends Comparable<T>> { @@ -477,6 +491,16 @@ public class SkipList<T extends Comparable<T>> {
}
public boolean contains(T value) {
if(getNode(value)==null) {
return false;
} else {
return true;
}
}
/** Get the SkipNode corresponding to this value.
* Lets getPredecessor do all the work.
* */
@ -517,35 +541,15 @@ public class SkipList<T extends Comparable<T>> { @@ -517,35 +541,15 @@ public class SkipList<T extends Comparable<T>> {
/*
// Phishman's methods:
public boolean add(T value) {};
protected Node<T> addValue(T value) {};
public boolean contains(T value) {};
protected Node<T> getNode(T value) {};
private NodeLevelPair<T> getPredecessor(T value) {};
public boolean remove(T value) {};
protected Node<T> removeValue(T value) {};
protected void swapNode(Node<T> node, Node<T> next) {};
public void clear() {};
public int size() {};
public boolean validate() {};
public java.util.Set<T> toSet() {};
public java.util.Collection<T> toCollection() {};
public String getString(T value, int level) {};
*/
public Iterator<T> iterator() {
return null;
public Iterator<SkipNode<T>> iterator() {
Set<SkipNode<T>> s = new HashSet<>();
SkipNode<T> strider = head;
s.add(strider);
while(strider.getNext(0)!=null) {
strider = strider.getNext(0);
s.add(strider);
}
return s.iterator();
}
}

197
hash/SkipListMap.java

@ -0,0 +1,197 @@ @@ -0,0 +1,197 @@
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.HashSet;
/**
* Skip List Map.
*
* This class implements a sorted map
* using a skip list data structure.
*
* The skip list stores MapItem<K,V> objects
* in a hierarchical, rows-and-towers
* skip list structure.
*
* There are a series of h lists,
* each holding entries of the map
* in sorted order.
*
* Analogous to java.util.ConcurrentSkipList.
*/
public class SkipListMap <K extends Comparable<K>,V>
extends AbstractMap<K,V> {
public static void main(String[] args) {
SkipListMap<String,String> m = new SkipListMap<String,String>();
m.put("documentation","That which we know nothing about, we must pass over in silence.");
m.put("models","All models are wrong, some models are useful.");
m.put("buddha","You will not be punished for your anger, you will be punished by your anger.");
m.put("knowledge","All the evolution we know of proceeds from the vague to the definite.");
System.out.println("Skip list map size: "+m.size());
System.out.println("Skip list map entry for buddha: "+m.get("buddha"));
System.out.println("Removing an item...");
m.remove("knowledge");
System.out.println("Skip list map size: "+m.size());
/*
System.out.println("Iterating over items: ");
for(MapItem<String,String> item : m.itemSet()) {
String i = item.getKey();
String j = item.getValue();
System.out.println(i + " --> " + j);
}
*/
System.out.println("Done.");
}
/**
* Skip List Map Node - utility class.
*/
protected static class SkipListMapNode<K extends Comparable<K>, V>
extends SkipList.SkipNode<K> {
protected V value;
protected SkipListMapNode(int level, K key) {
super(level, key);
}
public K getKey() { return data; }
public V getValue() { return value; }
public String toString() {
StringBuffer buff = new StringBuffer();
buff.append(super.toString());
buff.append("value = ").append(value).append("\n");
return buff.toString();
}
}
/**
* Skip list map class.
* */
private SkipList<K> skipList;
public SkipListMap() {
skipList = new SkipList<K>();
}
/** Put a key-value pair into the map.
* If a value for this key already exists, overwrite the current value.
* */
public void put(K key, V value) {
SkipList.SkipNode<K> node = skipList.addValue(key);
if (node instanceof SkipListMapNode) {
SkipListMapNode<K, V> mapNode = (SkipListMapNode<K, V>) node;
mapNode.value = value;
}
}
/** Get the value corresponding to this key from the map. */
public V get(K key) {
SkipList.SkipNode<K> node = skipList.getNode(key);
if (node instanceof SkipListMapNode) {
SkipListMapNode<K, V> mapNode = (SkipListMapNode<K, V>) node;
return mapNode.value;
}
return null;
}
/** Boolean: does the map contain a value with this key? */
public boolean contains(K key) {
return skipList.contains(key);
}
/** Remove an item from the map. */
public V remove(K key) {
SkipList.SkipNode<K> node = skipList.removeValue(key);
V value = null;
if (node instanceof SkipListMapNode) {
SkipListMapNode<K, V> treeMapNode = (SkipListMapNode<K, V>) node;
value = treeMapNode.value;
treeMapNode.data = null;
treeMapNode.value = null;
}
return value;
}
public void clear() {
skipList.clear();
}
public Iterator<SkipListMapNode<K,V>> iterator() {
Set<SkipListMapNode<K,V>> s = new HashSet<>();
SkipListMapNode<K,V> strider = (SkipListMapNode<K,V>)(skipList.getHead());
s.add(strider);
while(strider.getNext(0)!=null) {
strider = (SkipListMapNode<K,V>)(strider.getNext(0));
s.add(strider);
}
return s.iterator();
}
public int size() {
return skipList.size();
}
/** Iterator over the items in this map. */
public Iterable<MapItem<K,V>> itemSet() {
return null;
/*
ArrayList<MapItem<K,V>> buffer = new ArrayList<>();
Iterator<SkipNode<T>> iter = skipList.iterator();
while(iter.hasNext()) {
SkipNode<K> s = iter.next();
SkipListMapNode<K,V> sm = (SkipListMapNode<K,V>)(s);
buffer.add( new MapItem<K,V>(sm.getKey(), sm.getValue()) );
}
return buffer;
*/
}
/**
* Wrapper function to create a new node
* with the given key at the given skip list level.
*/
public SkipList.SkipNode<K> createNewNode(int level, K key) {
return (new SkipListMapNode<K, V>(level, key));
}
/**
* Swap two nodes
*/
public void swapNode(SkipList.SkipNode<K> node, SkipList.SkipNode<K> next) {
K temp = node.data;
node.data = next.data;
next.data = temp;
if (node instanceof SkipListMapNode && next instanceof SkipListMapNode) {
SkipListMapNode<K,V> node2 = (SkipListMapNode<K,V>) node;
SkipListMapNode<K,V> next2 = (SkipListMapNode<K,V>) next;
V value = node2.value;
node2.value = next2.value;
next2.value = value;
}
}
}
Loading…
Cancel
Save