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

org.nd4j.linalg.collection.IntArrayKeySet Maven / Gradle / Ivy

There is a newer version: 1.0.0-M2.1
Show newest version
package org.nd4j.linalg.collection;

import java.util.*;

/**
 * Provides a wrapper for a {@link TreeSet}
 * that uses {@link IntArrayKeyMap.IntArray}
 * for proper comparison of int arrays
 * as keys.
 *
 * @author Adam Gibson
 */
public class IntArrayKeySet implements Set {
    private Set set = new LinkedHashSet<>();
    @Override
    public int size() {
        return set.size();
    }

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

    @Override
    public boolean contains(Object o) {
        return set.contains(new IntArrayKeyMap.IntArray((int[]) o));
    }

    @Override
    public Iterator iterator() {
        List ret = new ArrayList<>();
        for(IntArrayKeyMap.IntArray arr : set) {
            ret.add(arr.getBackingArray());
        }

        return ret.iterator();
    }

    @Override
    public Object[] toArray() {
        Object[] ret = new Object[size()];
        int count = 0;
        for(IntArrayKeyMap.IntArray intArray : set) {
            ret[count++] = intArray.getBackingArray();
        }

        return ret;
    }

    @Override
    public  T[] toArray(T[] ts) {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean add(int[] ints) {
        return set.add(new IntArrayKeyMap.IntArray(ints));
    }

    @Override
    public boolean remove(Object o) {
        return set.remove(new IntArrayKeyMap.IntArray((int[]) o));
    }

    @Override
    public boolean containsAll(Collection collection) {
        return set.containsAll(getCollection(collection));

    }

    @Override
    public boolean addAll(Collection collection) {
        return set.addAll(getCollection(collection));
    }

    @Override
    public boolean retainAll(Collection collection) {
        return set.retainAll(getCollection(collection));
    }

    @Override
    public boolean removeAll(Collection collection) {
        return set.removeAll(getCollection(collection));
    }

    @Override
    public void clear() {
        set.clear();
    }

    private Collection getCollection(Collection coll) {
        List ret = new ArrayList<>();
        Collection casted = (Collection) coll;
        for(int[] arr : casted) {
            ret.add(new IntArrayKeyMap.IntArray(arr));
        }
        return ret;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy