1 package net.mograsim.logic.core.types;
3 import static java.lang.String.format;
5 import java.math.BigInteger;
6 import java.util.Arrays;
7 import java.util.Iterator;
8 import java.util.NoSuchElementException;
9 import java.util.Objects;
10 import java.util.RandomAccess;
11 import java.util.function.BinaryOperator;
12 import java.util.function.UnaryOperator;
15 * Immutable class representing a {@link Bit}Vector
17 * @author Christian Femers
20 public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit>, RandomAccess
22 public static final BitVector SINGLE_U = new BitVector(Bit.U);
23 public static final BitVector SINGLE_X = new BitVector(Bit.X);
24 public static final BitVector SINGLE_0 = new BitVector(Bit.ZERO);
25 public static final BitVector SINGLE_1 = new BitVector(Bit.ONE);
26 public static final BitVector SINGLE_Z = new BitVector(Bit.Z);
28 private static final BitVector[] SINGLE_BIT_MAPPING = { SINGLE_U, SINGLE_X, SINGLE_0, SINGLE_1, SINGLE_Z };
30 private final Bit[] bits;
32 private BitVector(Bit single)
34 Objects.requireNonNull(single);
35 bits = new Bit[] { single };
38 private BitVector(Bit[] bits)
40 this.bits = Objects.requireNonNull(bits); // do this first to "catch" bits==null before the foreach loop
43 throw new NullPointerException();
46 public static BitVector of(Bit... bits)
49 return SINGLE_BIT_MAPPING[bits[0].ordinal()];
50 return new BitVector(bits.clone());
53 public static BitVector of(Bit bit, int length)
56 return SINGLE_BIT_MAPPING[bit.ordinal()];
57 return new BitVector(bit.makeArray(length));
60 public BitVectorMutator mutator()
62 return BitVectorMutator.of(this);
66 * Returns the most significant bit at <code>bitIndex</code>. (leftmost bit of a binary number at the given index)
68 public Bit getMSBit(int bitIndex)
70 return bits[bitIndex];
74 * Returns the least significant bit at <code>bitIndex</code>. (rightmost bit of a binary number at the given index)
76 public Bit getLSBit(int bitIndex)
78 return bits[bits.length - bitIndex - 1];
81 public Bit[] getBits()
86 public boolean isBinary()
88 for (int i = 0; i < bits.length; i++)
90 if (!bits[i].isBinary())
97 public BitVector join(BitVector t)
99 checkCompatibility(t);
100 if (bits.length == 1)
101 return SINGLE_BIT_MAPPING[bits[0].join(t.bits[0]).ordinal()];
102 return new BitVector(binOp(bits.clone(), t.bits, Bit::join));
106 public BitVector and(BitVector t)
108 checkCompatibility(t);
109 if (bits.length == 1)
110 return SINGLE_BIT_MAPPING[bits[0].and(t.bits[0]).ordinal()];
111 return new BitVector(binOp(bits.clone(), t.bits, Bit::and));
115 public BitVector or(BitVector t)
117 checkCompatibility(t);
118 if (bits.length == 1)
119 return SINGLE_BIT_MAPPING[bits[0].or(t.bits[0]).ordinal()];
120 return new BitVector(binOp(bits.clone(), t.bits, Bit::or));
124 public BitVector xor(BitVector t)
126 checkCompatibility(t);
127 if (bits.length == 1)
128 return SINGLE_BIT_MAPPING[bits[0].xor(t.bits[0]).ordinal()];
129 return new BitVector(binOp(bits.clone(), t.bits, Bit::xor));
133 public BitVector not()
135 if (bits.length == 1)
136 return SINGLE_BIT_MAPPING[bits[0].not().ordinal()];
137 return new BitVector(unOp(bits.clone(), Bit::not));
145 public BitVector concat(BitVector other)
147 Bit[] newBits = Arrays.copyOf(bits, length() + other.length());
148 System.arraycopy(other.bits, 0, newBits, length(), other.length());
149 return new BitVector(newBits);
152 public BitVector subVector(int start)
154 return new BitVector(Arrays.copyOfRange(bits, start, length()));
157 public BitVector subVector(int start, int end)
159 return new BitVector(Arrays.copyOfRange(bits, start, end));
162 private void checkCompatibility(BitVector bv)
164 if (length() != bv.length())
165 throw new IllegalArgumentException(format("BitVector length does not match: %d and %d", length(), bv.length()));
168 static Bit[] binOp(Bit[] dest, Bit[] second, BinaryOperator<Bit> op)
171 return second.clone();
172 for (int i = 0; i < dest.length; i++)
174 dest[i] = op.apply(dest[i], second[i]);
179 static Bit[] unOp(Bit[] dest, UnaryOperator<Bit> op)
183 for (int i = 0; i < dest.length; i++)
185 dest[i] = op.apply(dest[i]);
191 * Class for comfortable and efficient manipulation of {@link BitVector}s, similar to {@link StringBuilder}
193 * @author Christian Femers
195 public static final class BitVectorMutator implements LogicType<BitVectorMutator, BitVector>
199 private BitVectorMutator(Bit[] bits)
204 static BitVectorMutator of(BitVector bv)
206 return new BitVectorMutator(bv.getBits());
210 * Returns a new mutator of the specified length, <b>with all bits set to <code>null</code></b>. Use with care!
212 public static BitVectorMutator ofLength(int length)
214 return new BitVectorMutator(new Bit[length]);
218 * Returns an empty mutator which has no bits set and will simply copy the values from the first binary operation performed.
220 public static BitVectorMutator empty()
222 return new BitVectorMutator(null);
226 * Produces the resulting, immutable {@link BitVector}<br>
228 * @throws IllegalStateException if the mutator is (still) empty
230 public BitVector toBitVector()
233 throw new IllegalStateException("cannot create a BitVector from an empty mutator");
234 return new BitVector(bits);
238 public BitVectorMutator join(BitVector t)
240 checkCompatibility(t);
241 bits = binOp(bits, t.bits, Bit::join);
246 public BitVectorMutator and(BitVector t)
248 checkCompatibility(t);
249 bits = binOp(bits, t.bits, Bit::and);
254 public BitVectorMutator or(BitVector t)
256 checkCompatibility(t);
257 bits = binOp(bits, t.bits, Bit::or);
262 public BitVectorMutator xor(BitVector t)
264 checkCompatibility(t);
265 bits = binOp(bits, t.bits, Bit::xor);
270 public BitVectorMutator not()
272 unOp(bits, Bit::not);
277 * Set the most significant bit at <code>bitIndex</code>. (leftmost bit of a binary number at the given index)
279 public void setMSBit(int bitIndex, Bit bit)
281 bits[bitIndex] = bit;
285 * Set the least significant bit at <code>bitIndex</code>. (rightmost bit of a binary number at the given index)
287 public void setLSBit(int bitIndex, Bit bit)
289 bits[bits.length - bitIndex - 1] = bit;
293 * Returns the most significant bit at <code>bitIndex</code>. (leftmost bit of a binary number at the given index)
295 public Bit getMSBit(int bitIndex)
297 return bits[bitIndex];
301 * Returns the least significant bit at <code>bitIndex</code>. (rightmost bit of a binary number at the given index)
303 public Bit getLSBit(int bitIndex)
305 return bits[bits.length - bitIndex - 1];
313 private void checkCompatibility(BitVector bv)
315 if (bits != null && bits.length != bv.length())
316 throw new IllegalArgumentException(format("BitVector length does not match: %d and %d", bits.length, bv.length()));
321 * @see Arrays#hashCode(Object[])
324 public int hashCode()
326 return Arrays.hashCode(bits);
330 * Does test for equality of values/content
332 * @see Object#equals(Object)
335 public boolean equals(Object obj)
339 if (!(obj instanceof BitVector))
341 BitVector other = (BitVector) obj;
342 return Arrays.equals(bits, other.bits);
346 * Does test for equality of values/content, shifting the other BitVector by <code>offset</code> to the right.<br>
347 * Therefore <code>offset + other.length() <= this.length()</code> needs to be true.
349 * @throws ArrayIndexOutOfBoundsException if <code>offset + other.length() > this.length()</code>
351 * @see Object#equals(Object)
353 public boolean equalsWithOffset(BitVector other, int offset)
357 return Arrays.equals(bits, offset, offset + other.length(), other.bits, 0, other.length());
361 * All {@link Bit}s symbols concatenated together (MSB first)
363 * @see #parse(String)
366 public String toString()
368 StringBuilder sb = new StringBuilder(bits.length);
371 return sb.toString();
375 * Parses a String containing solely {@link Bit} symbols (MSB first)
379 public static BitVector parse(String s)
381 Bit[] values = new Bit[s.length()];
382 for (int i = 0; i < s.length(); i++)
384 values[i] = Bit.parse(s, i);
386 return new BitVector(values);
389 public static BitVector of(long value, int bits)
391 return of(BigInteger.valueOf(value), bits);
394 public static BitVector of(BigInteger value, int bits)
396 Bit[] values = new Bit[bits];
397 for (int i = 0; i < bits; i++)
399 values[bits - i - 1] = Bit.of(value.testBit(i));
401 return new BitVector(values);
405 * Iterate over the {@link Bit}s of the BitVector <b>from MSB to LSB</b> (left to right).
408 public Iterator<Bit> iterator()
410 return new Iterator<>()
418 throw new NoSuchElementException();
419 return getMSBit(pos++);
423 public boolean hasNext()
425 return pos != length();