@@ -0,0 +1,2599 @@
+package org.cyclops.integrateddynamics.core.network;%0A%0Aimport com.google.common.collect.Sets;%0Aimport org.cyclops.commoncapabilities.api.ingredient.IngredientComponent;%0Aimport org.cyclops.cyclopscore.datastructure.DistinctIterator;%0Aimport org.cyclops.cyclopscore.datastructure.MultitransformIterator;%0Aimport org.cyclops.cyclopscore.ingredient.collection.IIngredientCollectionMutable;%0Aimport org.cyclops.cyclopscore.ingredient.collection.IIngredientMapMutable;%0Aimport org.cyclops.cyclopscore.ingredient.collection.IngredientCollectionMutableWrapper;%0Aimport org.cyclops.cyclopscore.ingredient.collection.IngredientCollectionPrototypeMap;%0Aimport org.cyclops.cyclopscore.ingredient.collection.IngredientHashMap;%0Aimport org.cyclops.integrateddynamics.api.part.PartPos;%0A%0Aimport java.util.Iterator;%0Aimport java.util.TreeSet;%0A%0A/**%0A * An index that maps ingredients to positions that contain that instance.%0A * @author rubensworks%0A */%0Apublic class IngredientPositionsIndex%3CT, M%3E extends IngredientCollectionMutableWrapper%3CT, M, IIngredientCollectionMutable%3CT, M%3E%3E %7B%0A%0A    private final IIngredientMapMutable%3CT, M, TreeSet%3CPartPos%3E%3E positionsMap;%0A%0A    public IngredientPositionsIndex(IngredientComponent%3CT, M%3E component) %7B%0A        super(new IngredientCollectionPrototypeMap%3C%3E(component, false));%0A        this.positionsMap = new IngredientHashMap%3C%3E(component);%0A    %7D%0A%0A    protected T getPrototype(T instance) %7B%0A        return this.positionsMap.getComponent().getMatcher().withQuantity(instance, 1);%0A    %7D%0A%0A    public Iterator%3CPartPos%3E getNonEmptyPositions() %7B%0A        return getPositions(getComponent().getMatcher().getEmptyInstance(), getComponent().getMatcher().getAnyMatchCondition());%0A    %7D%0A%0A    public Iterator%3CPartPos%3E getPositions(T instance, M matchFlags) %7B%0A        return new DistinctIterator%3C%3E(MultitransformIterator.flattenIterableIterator(%0A                this.positionsMap.getAll(getPrototype(instance), matchFlags).iterator()));%0A    %7D%0A%0A    public void addPosition(T instance, PartPos pos) %7B%0A        T prototype = getPrototype(instance);%0A        TreeSet%3CPartPos%3E set = this.positionsMap.get(prototype);%0A        if (set == null) %7B%0A            set = Sets.newTreeSet();%0A            this.positionsMap.put(prototype, set);%0A        %7D%0A        set.add(pos);%0A    %7D%0A%0A    public void removePosition(T instance, PartPos pos) %7B%0A        T prototype = getPrototype(instance);%0A        TreeSet%3CPartPos%3E set = this.positionsMap.get(prototype);%0A        if (set != null) %7B%0A            set.remove(pos);%0A            if (set.isEmpty()) %7B%0A                this.positionsMap.remove(prototype);%0A            %7D%0A        %7D%0A    %7D%0A%0A%7D%0A

Add IngredientPositionsIndex