存储键值对id:value值
存储100万数据对
查询value区间,比如10到100;个数?
查询id区间,比如10到100;个数?
查询某id或某value,排名第几?
在跳表实体加个索引号;
SkipList skipList = new SkipList();
skipList.put(5, 5);
skipList.put(55, 55);
skipList.put(45, 45);
skipList.put(35, 35);
skipList.put(25, 25);
skipList.put(15, 15);
skipList.put(53, 53);
System.out.println(skipList.countByValueRange(15,53));
skipList.doPrintln();
跳表实体:
public class SkipListEntry<v> {
public Integer key;
public v value;
public SkipListEntry<v> down,up,left,right;
public static int negative = Integer.MIN_VALUE;
public static int positive = Integer.MAX_VALUE;
public java.util.Random flag;
public int indexCurrent;
public SkipListEntry(int key,v value){
this.key = key;
this.value = value;
this.down = this.up = this.left = this.right = null;
this.flag = new java.util.Random();
}
public Integer getKey(){
return key;
}
public v getValue(){
return value;
}
}
跳表列表:
public class SkipList<v> {
private SkipListEntry<v> head;
private SkipListEntry<v> tail;
public int height;
public int size;
public SkipList(){
this.head = new SkipListEntry(SkipListEntry.negative,SkipListEntry.negative);
this.head.indexCurrent = 1;
this.tail = new SkipListEntry(SkipListEntry.positive,SkipListEntry.positive);
this.tail.indexCurrent = 2;
head.right = tail;
tail.left = head;
size = 0;
height = 0;
}
public SkipListEntry<v> getSkipListEntry(Integer key){
SkipListEntry<v> p = head;
while(true){
while(!p.right.key.equals(this.tail.key)&&p.right.key.compareTo(key)<=0){
p = p.right;
}
if(p.down != null){
p = p.down;
}else{
break;
}
}
return p;
}
public v get(Integer key){
SkipListEntry<v> target = getSkipListEntry(key);
if(target.getKey().equals(key)){
return target.getValue();
}else{
return null;
}
}
public void put(Integer key,v value){
SkipListEntry<v> target = getSkipListEntry(key);
SkipListEntry<v> newTarget = new SkipListEntry(key,value);
newTarget.left = target;
newTarget.right = target.right;
newTarget.indexCurrent = target.indexCurrent;
doAddIndex(newTarget);
target.right.left = newTarget;
target.right = newTarget;
int currentLevel = 0;
while(newTarget.flag.nextDouble()<0.5){
if(currentLevel>=height){
SkipListEntry targetSkipListHead = new SkipListEntry(SkipListEntry.negative,SkipListEntry.negative);
SkipListEntry targetSkipListTail = new SkipListEntry(SkipListEntry.positive,SkipListEntry.positive);
targetSkipListHead.right = targetSkipListTail;
targetSkipListHead.down = this.head;
targetSkipListHead.indexCurrent = this.head.indexCurrent;
this.head.up = targetSkipListHead;
targetSkipListTail.left = targetSkipListHead;
targetSkipListTail.down = this.tail;
targetSkipListTail.indexCurrent = this.tail.indexCurrent;
this.tail.up = targetSkipListTail;
this.head = targetSkipListHead;
this.tail = targetSkipListTail;
}
while(target!=this.head&&target.up==null){
target = target.left;
}
target = target.up;
SkipListEntry skipListEntry = new SkipListEntry(key,value);
skipListEntry.left = target;
skipListEntry.right = target.right;
skipListEntry.down = newTarget;
skipListEntry.indexCurrent = newTarget.indexCurrent;
newTarget.up = skipListEntry;
target.right.left = skipListEntry;
target.right = skipListEntry;
currentLevel++;
}
size++;
}
public void doPrintln(){
SkipListEntry skipListEntry = this.head;
while(skipListEntry.down!=null){
skipListEntry = skipListEntry.down;
}
while(skipListEntry.right!=null){
System.out.println(skipListEntry.key+":"+skipListEntry.value + ":" + skipListEntry.indexCurrent);
skipListEntry = skipListEntry.right;
}
System.out.println(skipListEntry.key+":"+skipListEntry.value + ":" + skipListEntry.indexCurrent);
}
public void doAddIndex(SkipListEntry<v> target){
SkipListEntry<v> temp = target;
while(temp!=null){
SkipListEntry<v> tempUP = temp;
++temp.indexCurrent;
while(tempUP!=null){
tempUP.indexCurrent = temp.indexCurrent;
tempUP=tempUP.up;
}
temp = temp.right;
}
}
public SkipListEntry getSkipListEntryByValue(Integer value){
SkipListEntry current = head;
while(true){
while(current.right.key!=this.tail.key && ((Integer) current.right.value).compareTo(value)<=0){
current = current.right;
}
if(current.down!=null){
current = current.down;
}else{
break;
}
}
return current;
}
public int countByValueRange(int startValue,int endValue){
SkipListEntry skipListEntryStart = getSkipListEntry(startValue);
SkipListEntry skipListEntryEnd = getSkipListEntry(endValue);
return skipListEntryEnd.indexCurrent - skipListEntryStart.indexCurrent;
}
}