All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.nukkit.utils.SequencedHashSet Maven / Gradle / Ivy

package cn.nukkit.utils;

import it.unimi.dsi.fastutil.ints.Int2ObjectLinkedOpenHashMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.objects.Object2IntLinkedOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2IntMap;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

@SuppressWarnings({"NullableProblems", "SuspiciousMethodCalls"})
public class SequencedHashSet implements List {
    private final Object2IntMap map = new Object2IntLinkedOpenHashMap<>();
    private final Int2ObjectMap inverse = new Int2ObjectLinkedOpenHashMap<>();
    private int index = 0;

    @Override
    public int indexOf(Object o) {
        return map.getInt(o);
    }

    @Override
    public int lastIndexOf(Object o) {
        return map.getInt(o);
    }

    @Override
    public ListIterator listIterator() {
        throw new UnsupportedOperationException();
    }

    @Override
    public ListIterator listIterator(int index) {
        throw new UnsupportedOperationException();
    }

    @Override
    public List subList(int fromIndex, int toIndex) {
        throw new UnsupportedOperationException();
    }

    @Override
    public int size() {
        return map.size();
    }

    @Override
    public boolean isEmpty() {
        return map.isEmpty();
    }

    @Override
    public boolean contains(Object o) {
        return map.containsKey(o);
    }

    @Override
    public Iterator iterator() {
        return map.keySet().iterator();
    }

    @Override
    public Object[] toArray() {
        return map.keySet().toArray();
    }

    @Override
    public  T[] toArray(T[] a) {
        return map.keySet().toArray(a);
    }

    @Override
    public boolean add(E e) {
        if (!this.map.containsKey(e)) {
            int index = this.index++;
            this.map.put(e, index);
            this.inverse.put(index, e);
            return true;
        }
        return false;
    }

    @Override
    public boolean remove(Object o) {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean containsAll(Collection c) {
        return map.keySet().containsAll(c);
    }

    @Override
    public boolean addAll(Collection c) {
        for (E e : c) {
            this.add(e);
        }
        return true;
    }

    @Override
    public boolean addAll(int index, Collection c) {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean retainAll(Collection c) {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean removeAll(Collection c) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void clear() {
        throw new UnsupportedOperationException();
    }

    public E get(int index) {
        return this.inverse.get(index);
    }

    @Override
    public E set(int index, E element) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void add(int index, E element) {
        throw new UnsupportedOperationException();
    }

    @Override
    public E remove(int index) {
        throw new UnsupportedOperationException();
    }

    @Override
    public String toString() {
        return map.keySet().toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy