Coverage Summary for Class: BitSet (co.rsk.vm)

Class Class, % Method, % Line, %
BitSet 100% (1/1) 80% (4/5) 80% (16/20)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2017 RSK Labs Ltd. 4  * 5  * This program is free software: you can redistribute it and/or modify 6  * it under the terms of the GNU Lesser General Public License as published by 7  * the Free Software Foundation, either version 3 of the License, or 8  * (at your option) any later version. 9  * 10  * This program is distributed in the hope that it will be useful, 11  * but WITHOUT ANY WARRANTY; without even the implied warranty of 12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13  * GNU Lesser General Public License for more details. 14  * 15  * You should have received a copy of the GNU Lesser General Public License 16  * along with this program. If not, see <http://www.gnu.org/licenses/>. 17  */ 18  19 package co.rsk.vm; 20  21 /** 22  * Created by ajlopez on 29/04/2017. 23  */ 24 public class BitSet { 25  private byte[] bytes; 26  private int size; 27  28  public BitSet(int size) { 29  if (size < 0) { 30  throw new IllegalArgumentException(String.format("Negative size: %s", size)); 31  } 32  33  this.size = size; 34  int bsize = (size + 7) / 8; 35  this.bytes = new byte[bsize]; 36  } 37  38  public void set(int position) { 39  if (position < 0 || position >= this.size) { 40  throw new IndexOutOfBoundsException(String.format("Index: %s, Size: %s", position, this.size)); 41  } 42  43  int offset = position / 8; 44  int bitoffset = position % 8; 45  46  this.bytes[offset] |= 1 << bitoffset; 47  } 48  49  public boolean get(int position) { 50  if (position < 0 || position >= this.size) { 51  throw new IndexOutOfBoundsException(String.format("Index: %s, Size: %s", position, this.size)); 52  } 53  54  int offset = position / 8; 55  int bitoffset = position % 8; 56  57  return (this.bytes[offset] & 0xff & (1 << bitoffset)) != 0; 58  } 59  60  public int size() { 61  return this.size; 62  } 63  64  protected byte[] getBytes() { 65  return this.bytes; 66  } 67 }