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

org.eclipse.collections.impl.utility.internal.SetIterables Maven / Gradle / Ivy

There is a newer version: 12.0.0.M3
Show newest version
/*
 * Copyright (c) 2015 Goldman Sachs.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v. 1.0 which accompany this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 */

package org.eclipse.collections.impl.utility.internal;

import java.util.Set;

import org.eclipse.collections.api.LazyIterable;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.set.ImmutableSet;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.api.set.SetIterable;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.set.mutable.SetAdapter;
import org.eclipse.collections.impl.set.mutable.UnifiedSet;
import org.eclipse.collections.impl.set.strategy.mutable.UnifiedSetWithHashingStrategy;
import org.eclipse.collections.impl.tuple.Tuples;
import org.eclipse.collections.impl.utility.Iterate;
import org.eclipse.collections.impl.utility.LazyIterate;

/**
 * Set algebra operations.
 * 

* Most operations are non-destructive, i.e. no input sets are modified during execution. * The exception is operations ending in "Into." These accept the target collection of * the final calculation as the first parameter. */ public final class SetIterables { private SetIterables() { throw new AssertionError("Suppress default constructor for noninstantiability"); } public static MutableSet union( SetIterable setA, SetIterable setB) { return SetIterables.unionInto(setA, setB, UnifiedSet.newSet()); } public static > R unionInto( SetIterable setA, SetIterable setB, R targetSet) { Iterate.addAllIterable(setA, targetSet); Iterate.addAllIterable(setB, targetSet); return targetSet; } public static MutableSet intersect( SetIterable setA, SetIterable setB) { return SetIterables.intersectInto(setA, setB, UnifiedSet.newSet()); } public static > R intersectInto( SetIterable setA, SetIterable setB, R targetSet) { MutableSet adapted = SetAdapter.adapt(targetSet); adapted.addAllIterable(setA); adapted.retainAllIterable(setB); return targetSet; } public static MutableSet difference( SetIterable minuendSet, SetIterable subtrahendSet) { return SetIterables.differenceInto(minuendSet, subtrahendSet, UnifiedSet.newSet()); } public static > R differenceInto( SetIterable minuendSet, SetIterable subtrahendSet, R targetSet) { MutableSet adapted = SetAdapter.adapt(targetSet); adapted.addAllIterable(minuendSet); adapted.removeAllIterable(subtrahendSet); return targetSet; } public static MutableSet symmetricDifference( SetIterable setA, SetIterable setB) { return SetIterables.symmetricDifferenceInto(setA, setB, UnifiedSet.newSet()); } public static > R symmetricDifferenceInto( SetIterable setA, SetIterable setB, R targetSet) { return SetIterables.unionInto( SetIterables.difference(setA, setB), SetIterables.difference(setB, setA), targetSet); } public static boolean isSubsetOf( SetIterable candidateSubset, SetIterable candidateSuperset) { return candidateSubset.size() <= candidateSuperset.size() && candidateSuperset.containsAllIterable(candidateSubset); } public static boolean isProperSubsetOf( SetIterable candidateSubset, SetIterable candidateSuperset) { return candidateSubset.size() < candidateSuperset.size() && candidateSuperset.containsAllIterable(candidateSubset); } public static MutableSet> powerSet(Set set) { MutableSet> seed = UnifiedSet.>newSetWith(UnifiedSet.newSet()); return powerSetWithSeed(set, seed); } public static MutableSet> powerSet(UnifiedSetWithHashingStrategy set) { MutableSet> seed = UnifiedSet.>newSetWith(set.newEmpty()); return powerSetWithSeed(set, seed); } private static MutableSet> powerSetWithSeed(Set set, MutableSet> seed) { return Iterate.injectInto(seed, set, new Function2>, T, MutableSet>>() { public MutableSet> value(MutableSet> accumulator, final T element) { return SetIterables.union(accumulator, accumulator.collect(new Function, MutableSet>() { public MutableSet valueOf(MutableSet innerSet) { return innerSet.clone().with(element); } })); } }); } /** * Returns an Immutable version of powerset where the inner sets are also immutable. */ public static ImmutableSet> immutablePowerSet(Set set) { return powerSet(set).collect(new Function, ImmutableSet>() { public ImmutableSet valueOf(MutableSet set) { return set.toImmutable(); } }).toImmutable(); } public static LazyIterable> cartesianProduct(SetIterable set1, final SetIterable set2) { return LazyIterate.flatCollect(set1, new Function>>() { public LazyIterable> valueOf(final A first) { return LazyIterate.collect(set2, new Function>() { public Pair valueOf(B second) { return Tuples.pair(first, second); } }); } }); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy