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     * A bit set is a set of elements, each of which corresponds to a unique
020     * integer from [0,MAX].
021     */
022    public final class BitSet {
023    
024      /**
025       * The backing bit vector that determines set membership.
026       */
027      private final BitVector vector;
028    
029      /**
030       * The bijection between integer to object.
031       */
032      private final BitSetMapping map;
033    
034      /**
035       * Constructor: create an empty set corresponding to a given mapping
036       */
037      public BitSet(BitSetMapping map) {
038        int length = map.getMappingSize();
039        vector = new BitVector(length);
040        this.map = map;
041      }
042    
043      /**
044       * Add all elements in bitset B to this bit set
045       */
046      public void addAll(BitSet B) {
047        if (VM.VerifyAssertions) {
048          VM._assert(map == B.map);
049        }
050        vector.or(B.vector);
051      }
052    
053      /**
054       * Add an object to this bit set.
055       */
056      public void add(Object o) {
057        int n = map.getMappedIndex(o);
058        vector.set(n);
059      }
060    
061      /**
062       * Does this set contain a certain object?
063       */
064      public boolean contains(Object o) {
065        int n = map.getMappedIndex(o);
066        return vector.get(n);
067      }
068    
069      /**
070       * @return a String representation
071       */
072      @Override
073      public String toString() {
074        return vector.toString();
075      }
076    }