Does java have a skip list implementation

后端 未结 7 1509
北恋
北恋 2021-02-13 03:51

I find ConcurrentSkipListSet in Java Collection Framework, which is backed up with a skip list. But is there a skip list in Java? A set does not work in my use case. I need a in

7条回答
  •  有刺的猬
    2021-02-13 04:11

    I am not claiming that this is my own implementation. I just cannot remember where I found it. If you know let me know and I will update. This has been working quite well for me:

    public class SkipList> implements Iterable {
    
    Node _head = new Node<>(null, 33);
    private final Random rand = new Random();
    private int _levels = 1;
    private AtomicInteger size = new AtomicInteger(0);
    
    /// 
    /// Inserts a value into the skip list.
    /// 
    public void insert(T value) {
        // Determine the level of the new node. Generate a random number R. The
        // number of
        // 1-bits before we encounter the first 0-bit is the level of the node.
        // Since R is
        // 32-bit, the level can be at most 32.
        int level = 0;
        size.incrementAndGet();
        for (int R = rand.nextInt(); (R & 1) == 1; R >>= 1) {
            level++;
            if (level == _levels) {
                _levels++;
                break;
            }
        }
    
        // Insert this node into the skip list
        Node newNode = new Node<>(value, level + 1);
        Node cur = _head;
        for (int i = _levels - 1; i >= 0; i--) {
            for (; cur.next[i] != null; cur = cur.next[i]) {
                if (cur.next[i].getValue().compareTo(value) > 0)
                    break;
            }
    
            if (i <= level) {
                newNode.next[i] = cur.next[i];
                cur.next[i] = newNode;
            }
        }
    }
    
    /// 
    /// Returns whether a particular value already exists in the skip list
    /// 
    public boolean contains(T value) {
        Node cur = _head;
        for (int i = _levels - 1; i >= 0; i--) {
            for (; cur.next[i] != null; cur = cur.next[i]) {
                if (cur.next[i].getValue().compareTo(value) > 0)
                    break;
                if (cur.next[i].getValue().compareTo(value) == 0)
                    return true;
            }
        }
        return false;
    }
    
    /// 
    /// Attempts to remove one occurence of a particular value from the skip
    /// list. Returns
    /// whether the value was found in the skip list.
    /// 
    public boolean remove(T value) {
    
        Node cur = _head;
    
        boolean found = false;
        for (int i = _levels - 1; i >= 0; i--) {
            for (; cur.next[i] != null; cur = cur.next[i]) {
                if (cur.next[i].getValue().compareTo(value) == 0) {
                    found = true;
                    cur.next[i] = cur.next[i].next[i];
                    break;
                }
    
                if (cur.next[i].getValue().compareTo(value) > 0)
                    break;
            }
        }
        if (found)
            size.decrementAndGet();
        return found;
    }
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public Iterator iterator() {
        return new SkipListIterator(this, 0);
    }
    
    public int size() {
        return size.get();
    }
    
    public Double[] toArray() {
        Double[] a = new Double[size.get()];
        int i = 0;
        for (T t : this) {
            a[i] = (Double) t;
            i++;
        }
        return a;
      }
    
     }
    
     class Node> {
    public Node[] next;
    public N value;
    
    @SuppressWarnings("unchecked")
    public Node(N value, int level) {
        this.value = value;
        next = new Node[level];
    }
    
    public N getValue() {
        return value;
    }
    
    public Node[] getNext() {
        return next;
    }
    
    public Node getNext(int level) {
        return next[level];
    }
    
    public void setNext(Node[] next) {
        this.next = next;
    }
    }
    
    class SkipListIterator> implements Iterator {
       SkipList list;
       Node current;
       int level;
    
    public SkipListIterator(SkipList list, int level) {
        this.list = list;
        this.current = list._head;
        this.level = level;
    }
    
    public boolean hasNext() {
        return current.getNext(level) != null;
    }
    
    public E next() {
        current = current.getNext(level);
        return current.getValue();
    }
    
    public void remove() throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }
    }
    

提交回复
热议问题