Java Set Super set isSupersetOf(BitSet set0, BitSet set1)

Here you can find the source of isSupersetOf(BitSet set0, BitSet set1)

Description

Returns whether a bit set contains every bit of another bit set.

License

Apache License

Declaration

public static boolean isSupersetOf(BitSet set0, BitSet set1) 

Method Source Code

//package com.java2s;
/*/*from  w  w w .j av  a  2  s. c o  m*/
// Licensed to Julian Hyde under one or more contributor license
// agreements. See the NOTICE file distributed with this work for
// additional information regarding copyright ownership.
//
// Julian Hyde licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/

import java.util.*;

public class Main {
    /** Returns whether a bit set contains every bit of another bit set. */
    public static boolean isSupersetOf(BitSet set0, BitSet set1) {
        if (set1.isEmpty()) {
            return true;
        }
        if (!set0.intersects(set1)) {
            return false;
        }
        for (Integer integer : toIter(set1)) {
            if (!set0.get(integer)) {
                return false;
            }
        }
        return true;
    }

    /**
     * Returns an iterable over the bits in a bitmap that are set to '1'.
     *
     * <p>This allows you to iterate over a bit set using a 'foreach' construct.
     * For instance:
     *
     * <blockquote><code>
     * BitSet bitSet;<br/>
     * for (int i : Util.toIter(bitSet)) {<br/>
     * &nbsp;&nbsp;print(i);<br/>
     * }<br/></code></blockquote>
     *
     * @param bitSet Bit set
     * @return Iterable
     */
    public static Iterable<Integer> toIter(final BitSet bitSet) {
        return new Iterable<Integer>() {
            public Iterator<Integer> iterator() {
                return new Iterator<Integer>() {
                    int i = bitSet.nextSetBit(0);

                    public boolean hasNext() {
                        return i >= 0;
                    }

                    public Integer next() {
                        int prev = i;
                        i = bitSet.nextSetBit(i + 1);
                        return prev;
                    }

                    public void remove() {
                        throw new UnsupportedOperationException();
                    }
                };
            }
        };
    }
}

Related

  1. isSuperset(Set setA, Set setB)