/*
 * @author Ranides Atterwim {@literal <ranides@gmail.com>}
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.collection.sets;

import java.util.NoSuchElementException;
import java.util.function.BinaryOperator;
import java.util.function.IntBinaryOperator;
import net.ranides.assira.collection.IntCollection;
import net.ranides.assira.collection.lists.IntArrayList;

/**
 * Specialization of MultiSet using integer keys.
 * Avoids unecessary boxing and unboxing operations.
 *
 * @author Ranides Atterwim {@literal <ranides@gmail.com>}
 */
public interface IntMultiSet extends IntSet, MultiSet<Integer> {

    /**
     * non-generic version of method {@link #getAll(Object)}
     *
     * @param key key
     * @return view
     */
    IntCollection getAll(int key);

    /**
     * non-generic version of method {@link #get(Object, BinaryOperator)}
     *
     * @param key key
     * @param fold fold
     * @return int
     */
    default int get(int key, IntBinaryOperator fold) {
        return getAll(key).intstream().reduce(fold).orElseThrow(()->new NoSuchElementException());
    }

    /**
     * non-generic version of method {@link #removeAll(Object)}
     *
     * @param key key
     * @return values
     */
    default IntCollection removeAll(int key) {
        IntCollection out = new IntArrayList(getAll(key));
        for(int k : out) {
            remove(k);
        }
        return out;
    }
    
    @Override
    default IntSet uniqueSet() {
        return new IntOpenSet(this);
    }

}
