001    /*
002     *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003     *
004     *  This file is licensed to You under the Eclipse Public License (EPL);
005     *  You may not use this file except in compliance with the License. You
006     *  may obtain a copy of the License at
007     *
008     *      http://www.opensource.org/licenses/eclipse-1.0.php
009     *
010     *  See the COPYRIGHT.txt file distributed with this work for information
011     *  regarding copyright ownership.
012     */
013    package org.jikesrvm.compilers.opt.util;
014    
015    import org.jikesrvm.VM;
016    import org.jikesrvm.util.BitVector;
017    
018    /**
019     * BitSet.java
020     *
021     * A bit set is a set of elements, each of which corresponds to a unique
022     * integer from [0,MAX].
023     */
024    public final class BitSet {
025    
026      /**
027       * The backing bit vector that determines set membership.
028       */
029      private final BitVector vector;
030    
031      /**
032       * The bijection between integer to object.
033       */
034      private final BitSetMapping map;
035    
036      /**
037       * Constructor: create an empty set corresponding to a given mapping
038       */
039      public BitSet(BitSetMapping map) {
040        int length = map.getMappingSize();
041        vector = new BitVector(length);
042        this.map = map;
043      }
044    
045      /**
046       * Add all elements in bitset B to this bit set
047       */
048      public void addAll(BitSet B) {
049        if (VM.VerifyAssertions) {
050          VM._assert(map == B.map);
051        }
052        vector.or(B.vector);
053      }
054    
055      /**
056       * Add an object to this bit set.
057       */
058      public void add(Object o) {
059        int n = map.getMappedIndex(o);
060        vector.set(n);
061      }
062    
063      /**
064       * Does this set contain a certain object?
065       */
066      public boolean contains(Object o) {
067        int n = map.getMappedIndex(o);
068        return vector.get(n);
069      }
070    
071      /**
072       * @return a String representation
073       */
074      public String toString() {
075        return vector.toString();
076      }
077    }