001/*
002 * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.compiler.common.util;
024
025import java.util.*;
026
027/**
028 * This class implements a two-dimensional bitmap.
029 */
030public final class BitMap2D {
031
032    private BitSet map;
033    private final int bitsPerSlot;
034
035    private int bitIndex(int slotIndex, int bitWithinSlotIndex) {
036        return slotIndex * bitsPerSlot + bitWithinSlotIndex;
037    }
038
039    private boolean verifyBitWithinSlotIndex(int index) {
040        assert index < bitsPerSlot : "index " + index + " is out of bounds " + bitsPerSlot;
041        return true;
042    }
043
044    public BitMap2D(int sizeInSlots, int bitsPerSlot) {
045        map = new BitSet(sizeInSlots * bitsPerSlot);
046        this.bitsPerSlot = bitsPerSlot;
047    }
048
049    public int sizeInBits() {
050        return map.size();
051    }
052
053    // Returns number of full slots that have been allocated
054    public int sizeInSlots() {
055        return map.size() / bitsPerSlot;
056    }
057
058    public boolean isValidIndex(int slotIndex, int bitWithinSlotIndex) {
059        assert verifyBitWithinSlotIndex(bitWithinSlotIndex);
060        return (bitIndex(slotIndex, bitWithinSlotIndex) < sizeInBits());
061    }
062
063    public boolean at(int slotIndex, int bitWithinSlotIndex) {
064        assert verifyBitWithinSlotIndex(bitWithinSlotIndex);
065        return map.get(bitIndex(slotIndex, bitWithinSlotIndex));
066    }
067
068    public void setBit(int slotIndex, int bitWithinSlotIndex) {
069        assert verifyBitWithinSlotIndex(bitWithinSlotIndex);
070        map.set(bitIndex(slotIndex, bitWithinSlotIndex));
071    }
072
073    public void clearBit(int slotIndex, int bitWithinSlotIndex) {
074        assert verifyBitWithinSlotIndex(bitWithinSlotIndex);
075        map.clear(bitIndex(slotIndex, bitWithinSlotIndex));
076    }
077
078    public void atPutGrow(int slotIndex, int bitWithinSlotIndex, boolean value) {
079        int size = sizeInSlots();
080        if (size <= slotIndex) {
081            while (size <= slotIndex) {
082                size *= 2;
083            }
084            BitSet newBitMap = new BitSet(size * bitsPerSlot);
085            newBitMap.or(map);
086            map = newBitMap;
087        }
088
089        if (value) {
090            setBit(slotIndex, bitWithinSlotIndex);
091        } else {
092            clearBit(slotIndex, bitWithinSlotIndex);
093        }
094    }
095
096    public void clear() {
097        map.clear();
098    }
099}