Example usage for com.google.common.collect Sets powerSet

List of usage examples for com.google.common.collect Sets powerSet

Introduction

In this page you can find the example usage for com.google.common.collect Sets powerSet.

Prototype

@GwtCompatible(serializable = false)
public static <E> Set<Set<E>> powerSet(Set<E> set) 

Source Link

Document

Returns the set of all possible subsets of set .

Usage

From source file:org.eclipse.xtext.xtext.ParameterConfigHelper.java

public static Set<Parameter> getAssignedParameters(AbstractElement element, int parameterConfig) {
    if (parameterConfig != 0) {
        ParserRule parserRule = GrammarUtil.containingParserRule(element);
        Set<Parameter> allParameters = ImmutableSet.copyOf(parserRule.getParameters());
        int seen = 0;
        for (Set<Parameter> candidate : Sets.powerSet(allParameters)) {
            if (seen == parameterConfig) {
                return candidate;
            }//  w w  w . j  a  v a  2s. co  m
            seen++;
        }
    }
    return Collections.emptySet();
}

From source file:richtercloud.travis.oraclejdk.currency.format.parse.issue.NewMain.java

private static int test(String input) {
    Set<String> inputSplits = new LinkedHashSet<>(Arrays.asList(input.split("[\\s]+")));
    LOGGER.debug(String.format("input split is '%s'", inputSplits));
    Set<Set<String>> inputSplitsPowerSet = Sets.powerSet(inputSplits);
    Set<Entry<Locale, Currency>> results = new HashSet<>();
    int totalCount = inputSplitsPowerSet.size() * Locale.getAvailableLocales().length
            * Currency.getAvailableCurrencies().size() * 2;
    int count = 0;
    Set<String> checked = new HashSet<>();
    for (Set<String> inputSplit : inputSplitsPowerSet) {
        LOGGER.debug(String.format("input split powerset item is '%s'", inputSplit));
        String inputSplitJoin0 = Joiner.on(" ").join(inputSplit);
        //String.join not available in Java < 8
        String inputSplitJoin1 = Joiner.on("").join("", inputSplit);
        LOGGER.trace(String.format("checking input '%s'", inputSplitJoin0));
        LOGGER.trace(String.format("checking input '%s'", inputSplitJoin1));
        for (Locale locale : Locale.getAvailableLocales()) {
            for (Currency currency : Currency.getAvailableCurrencies()) {
                String currencySymbol = currency.getSymbol(locale);
                LOGGER.trace(String.format("currency symbol: %s", currencySymbol));
                if (inputSplitJoin0.contains(currencySymbol)) {
                    inputSplitJoin0 = inputSplitJoin0.replace(currencySymbol, " " + currencySymbol + " ");
                    LOGGER.debug(String.format("replacement result: %s", inputSplitJoin0));
                }/* www.  ja  v a2 s.  co  m*/
                if (inputSplitJoin1.contains(currencySymbol)) {
                    inputSplitJoin1 = inputSplitJoin1.replace(currencySymbol, " " + currencySymbol + " ");
                    LOGGER.debug(String.format("replacement result: %s", inputSplitJoin1));
                }
                NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(locale);
                currencyFormat.setCurrency(currency);
                try {
                    currencyFormat.parse(inputSplitJoin0);
                    results.add(new AbstractMap.SimpleImmutableEntry<>(locale, currency));
                } catch (ParseException ex) {
                    //skip
                }
                try {
                    currencyFormat.parse(inputSplitJoin1);
                    results.add(new AbstractMap.SimpleImmutableEntry<>(locale, currency));
                } catch (ParseException ex) {
                    //skip
                }
                count += 2;
                if (count % 100 == 0) {
                    LOGGER.info(String.format("progress: %d/%d", count, totalCount));
                }
            }
        }
    }
    return results.size();
}

From source file:org.eclipse.xtext.xtext.ParameterConfigHelper.java

public static int getParameterConfig(Set<Parameter> params, ParserRule context) {
    if (params.isEmpty()) {
        return 0;
    }/*from   w  w  w . jav a  2 s  .c o m*/
    Set<Parameter> allParameters = ImmutableSet.copyOf(context.getParameters());
    int result = 0;
    for (Set<Parameter> candidate : Sets.powerSet(allParameters)) {
        if (params.equals(candidate)) {
            return result;
        }
        result++;
    }
    throw new IllegalStateException();
}

From source file:com.facebook.presto.sql.tree.Cube.java

@Override
public Set<Set<Expression>> enumerateGroupingSets() {
    return Sets.powerSet(columns.stream().map(QualifiedNameReference::new).collect(toSet()));
}

From source file:DistribCountingMapper.java

@Override
public void map(LongWritable lineNum, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter)
        throws IOException {
    IntWritable one = new IntWritable(1);
    HashSet<String> transactionItems = new HashSet<String>();
    StringTokenizer st = new StringTokenizer(value.toString());
    while (st.hasMoreTokens()) {
        transactionItems.add(st.nextToken());
    }//from  w w w . ja  va 2  s.c  o  m

    Set<Set<String>> powerSet = Sets.powerSet(transactionItems);
    for (Set<String> itemset : powerSet) {
        if (itemset.size() > 0) {
            String[] itemsetArr = new String[itemset.size()];
            itemset.toArray(itemsetArr);
            Arrays.sort(itemsetArr);
            String itemsetStr = "";
            for (int i = 0; i < itemsetArr.length; i++) {
                itemsetStr += itemsetArr[i] + " ";
            }
            output.collect(new Text(itemsetStr), one);
        }
    }
}

From source file:org.eclipse.viatra.query.runtime.localsearch.matcher.integration.AllValidAdornments.java

@Override
public Iterable<Set<PParameter>> getAdornments(PQuery query) {
    final Set<PParameter> ins = query.getParameters().stream()
            .filter(PQueries.parameterDirectionPredicate(PParameterDirection.IN)).collect(Collectors.toSet());
    Set<PParameter> inouts = query.getParameters().stream()
            .filter(PQueries.parameterDirectionPredicate(PParameterDirection.INOUT))
            .collect(Collectors.toSet());
    Set<Set<PParameter>> possibleInouts = Sets.powerSet(inouts);
    return possibleInouts.stream().map(input -> Sets.union(ins, input)).collect(Collectors.toSet());
}

From source file:org.trnltk.morphology.morphotactics.PrecachingSuffixFormSequenceApplier.java

private void initialize() {
    final Collection<Suffix> allSuffixes = suffixGraph.getAllSuffixes();
    final Set<Set<PhoneticAttribute>> modifierAttributesPowerSet = Sets.powerSet(MODIFIER_ATTRIBUTES);

    int expectedRowCount = allSuffixes.size();
    int expectedColumnCount = modifierAttributesPowerSet.size();
    this.suffixFormSequenceTable = HashBasedTable.create(expectedRowCount, expectedColumnCount);

    for (Suffix suffix : allSuffixes) {
        final Set<SuffixForm> suffixForms = suffix.getSuffixForms();
        for (SuffixForm suffixForm : suffixForms) {
            final SuffixFormSequence suffixFormSequence = suffixForm.getForm();
            for (Set<PhoneticAttribute> phoneticAttributes : modifierAttributesPowerSet) {
                final ImmutableSet<PhoneticAttribute> phoneticAttributeImmutableSet = Sets
                        .immutableEnumSet(phoneticAttributes);
                final String appliedSuffixFormStr = this.delegate.apply(suffixFormSequence,
                        phoneticAttributeImmutableSet);
                this.suffixFormSequenceTable.put(suffixFormSequence, phoneticAttributeImmutableSet,
                        appliedSuffixFormStr);
            }/*from ww  w .ja va2s. c o  m*/
        }
    }

}

From source file:org.trnltk.morphology.contextless.parser.parsing.MockPhoneticAttributeSets.java

protected ImmutableMap<Long, Set<PhoneticAttribute>> findValidSets() {

    final Set<Set<PhoneticAttribute>> phoneticAttributePowerSets = Sets
            .powerSet(new HashSet<PhoneticAttribute>(Lists.newArrayList(PhoneticAttribute.values())));

    final ImmutableMap.Builder<Long, Set<PhoneticAttribute>> validPhoneticAttributeSetsMapBuilder = new ImmutableMap.Builder<Long, Set<PhoneticAttribute>>();
    for (Set<PhoneticAttribute> set : phoneticAttributePowerSets) {
        if (PhoneticAttributeMetadata.isValid(set)) {
            final long numberForSet = getNumberForSet(set);
            if (SETS_TO_INCLUDE.contains(numberForSet))
                validPhoneticAttributeSetsMapBuilder.put(numberForSet, set);
        }//from ww w . j  a v  a 2  s . c  om
    }

    return validPhoneticAttributeSetsMapBuilder.build();
}

From source file:org.trnltk.morphology.contextless.parser.PhoneticAttributeSets.java

protected ImmutableMap<Long, Set<PhoneticAttribute>> findValidSets() {

    final Set<Set<PhoneticAttribute>> phoneticAttributePowerSets = Sets
            .powerSet(new HashSet<PhoneticAttribute>(Lists.newArrayList(PhoneticAttribute.values())));

    final ImmutableMap.Builder<Long, Set<PhoneticAttribute>> validPhoneticAttributeSetsMapBuilder = new ImmutableMap.Builder<Long, Set<PhoneticAttribute>>();
    for (Set<PhoneticAttribute> set : phoneticAttributePowerSets) {
        if (PhoneticAttributeMetadata.isValid(set)) {
            validPhoneticAttributeSetsMapBuilder.put(getNumberForSet(set), set);
        }//from   www  .j a va  2  s.  c om
    }

    return validPhoneticAttributeSetsMapBuilder.build();
}

From source file:org.eclipse.viatra.query.runtime.matchers.planning.helpers.FunctionalDependencyHelper.java

/***
 * Returns the dependency set over attributes in {@link targetAttributes} that are implied by a given source dependency set.
 * <p> Note: exponential in the size of the target attribute set.
 * <p> Note: minimality of the returned dependency set is currently not guaranteed.
 * @param originalDependencies all dependencies that are known to hold on a wider set of attributes
 * @param targetAttributes the set of attributes we are interested in
 * @since 1.5//from   ww w .j  a  va  2  s . c  o  m
 */
public static <A> Map<Set<A>, Set<A>> projectDependencies(Map<Set<A>, Set<A>> originalDependencies,
        Set<A> targetAttributes) {
    // only those attributes are considered as left-hand-side candidates that occur at least once in dependencies
    Set<A> leftCandidates = new HashSet<A>();
    for (Entry<Set<A>, Set<A>> dependency : originalDependencies.entrySet()) {
        if (!isTrivial(dependency.getKey(), dependency.getValue())) // only if non-trivial
            leftCandidates.addAll(Sets.intersection(dependency.getKey(), targetAttributes));
    }

    // Compute an initial list of nontrivial projected dependencies - it does not have to be minimal yet
    Map<Set<A>, Set<A>> initialDependencies = new HashMap<Set<A>, Set<A>>();
    for (Set<A> leftSet : Sets.powerSet(leftCandidates)) {
        Set<A> rightSet = Sets.intersection(closureOf(leftSet, originalDependencies), targetAttributes);
        if (!isTrivial(leftSet, rightSet)) {
            initialDependencies.put(leftSet, rightSet);
        }
    }
    // Don't forget to include constants!
    Set<A> constants = Sets.intersection(closureOf(Collections.<A>emptySet(), originalDependencies),
            targetAttributes);
    if (!constants.isEmpty()) {
        initialDependencies.put(Collections.<A>emptySet(), constants);
    }

    // Omit those dependencies where the LHS has superfluous attributes
    Map<Set<A>, Set<A>> solidDependencies = new HashMap<Set<A>, Set<A>>();
    for (Entry<Set<A>, Set<A>> dependency : initialDependencies.entrySet()) {
        Set<A> leftSet = dependency.getKey();
        Set<A> rightSet = dependency.getValue();
        boolean solid = true;
        for (A skipped : leftSet) { // what if we skip one attribute from the left set?
            Set<A> singleton = Collections.singleton(skipped);
            Set<A> candidate = Sets.difference(leftSet, singleton);
            Set<A> rightCandidate = initialDependencies.get(candidate);
            if (rightCandidate != null) {
                if (Sets.union(rightCandidate, singleton).containsAll(rightSet)) {
                    solid = false;
                    break;
                }
            }
        }
        if (solid) {
            solidDependencies.put(leftSet, rightSet);
        }
    }

    // TODO perform proper minimization, 
    // see e.g. page 45 in http://www.cs.ubc.ca/~hkhosrav/db/slides/03.design%20theory.pdf

    return Collections.unmodifiableMap(solidDependencies);
}