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 */
013package org.jikesrvm.compilers.opt.util;
014
015import org.jikesrvm.VM;
016import 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 */
022public 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   * Creates an empty set corresponding to a given mapping.
036   *
037   * @param map a 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   * @param B the elements to add to this set
048   */
049  public void addAll(BitSet B) {
050    if (VM.VerifyAssertions) {
051      VM._assert(map == B.map);
052    }
053    vector.or(B.vector);
054  }
055
056  /**
057   * Add an object to this bit set.
058   * @param o an object that's present in the mapping
059   */
060  public void add(Object o) {
061    int n = map.getMappedIndex(o);
062    vector.set(n);
063  }
064
065  /**
066   * @param o an object that's present in the mapping
067   * @return whether this set contains the object
068   */
069  public boolean contains(Object o) {
070    int n = map.getMappedIndex(o);
071    return vector.get(n);
072  }
073
074  /**
075   * @return a String representation
076   */
077  @Override
078  public String toString() {
079    return vector.toString();
080  }
081}