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()
87 public BitVector join(BitVector t)
89 checkCompatibility(t);
91 return SINGLE_BIT_MAPPING[bits[0].join(t.bits[0]).ordinal()];
92 return new BitVector(binOp(bits.clone(), t.bits, Bit::join));
96 public BitVector and(BitVector t)
98 checkCompatibility(t);
100 return SINGLE_BIT_MAPPING[bits[0].and(t.bits[0]).ordinal()];
101 return new BitVector(binOp(bits.clone(), t.bits, Bit::and));
105 public BitVector or(BitVector t)
107 checkCompatibility(t);
108 if (bits.length == 1)
109 return SINGLE_BIT_MAPPING[bits[0].or(t.bits[0]).ordinal()];
110 return new BitVector(binOp(bits.clone(), t.bits, Bit::or));
114 public BitVector xor(BitVector t)
116 checkCompatibility(t);
117 if (bits.length == 1)
118 return SINGLE_BIT_MAPPING[bits[0].xor(t.bits[0]).ordinal()];
119 return new BitVector(binOp(bits.clone(), t.bits, Bit::xor));
123 public BitVector not()
125 if (bits.length == 1)
126 return SINGLE_BIT_MAPPING[bits[0].not().ordinal()];
127 return new BitVector(unOp(bits.clone(), Bit::not));
135 public BitVector concat(BitVector other)
137 Bit[] newBits = Arrays.copyOf(bits, length() + other.length());
138 System.arraycopy(other.bits, 0, newBits, length(), other.length());
139 return new BitVector(newBits);
142 public BitVector subVector(int start)
144 return new BitVector(Arrays.copyOfRange(bits, start, length()));
147 public BitVector subVector(int start, int end)
149 return new BitVector(Arrays.copyOfRange(bits, start, end));
152 private void checkCompatibility(BitVector bv)
154 if (length() != bv.length())
155 throw new IllegalArgumentException(format("BitVector length does not match: %d and %d", length(), bv.length()));
158 static Bit[] binOp(Bit[] dest, Bit[] second, BinaryOperator<Bit> op)
161 return second.clone();
162 for (int i = 0; i < dest.length; i++)
164 dest[i] = op.apply(dest[i], second[i]);
169 static Bit[] unOp(Bit[] dest, UnaryOperator<Bit> op)
173 for (int i = 0; i < dest.length; i++)
175 dest[i] = op.apply(dest[i]);
181 * Class for comfortable and efficient manipulation of {@link BitVector}s, similar to {@link StringBuilder}
183 * @author Christian Femers
185 public static final class BitVectorMutator implements LogicType<BitVectorMutator, BitVector>
189 private BitVectorMutator(Bit[] bits)
194 static BitVectorMutator of(BitVector bv)
196 return new BitVectorMutator(bv.getBits());
200 * Returns a new mutator of the specified length, <b>with all bits set to <code>null</code></b>. Use with care!
202 public static BitVectorMutator ofLength(int length)
204 return new BitVectorMutator(new Bit[length]);
208 * Returns an empty mutator which has no bits set and will simply copy the values from the first binary operation performed.
210 public static BitVectorMutator empty()
212 return new BitVectorMutator(null);
216 * Produces the resulting, immutable {@link BitVector}<br>
218 * @throws IllegalStateException if the mutator is (still) empty
220 public BitVector toBitVector()
223 throw new IllegalStateException("cannot create a BitVector from an empty mutator");
224 return new BitVector(bits);
228 public BitVectorMutator join(BitVector t)
230 checkCompatibility(t);
231 bits = binOp(bits, t.bits, Bit::join);
236 public BitVectorMutator and(BitVector t)
238 checkCompatibility(t);
239 bits = binOp(bits, t.bits, Bit::and);
244 public BitVectorMutator or(BitVector t)
246 checkCompatibility(t);
247 bits = binOp(bits, t.bits, Bit::or);
252 public BitVectorMutator xor(BitVector t)
254 checkCompatibility(t);
255 bits = binOp(bits, t.bits, Bit::xor);
260 public BitVectorMutator not()
262 unOp(bits, Bit::not);
267 * Set the most significant bit at <code>bitIndex</code>. (leftmost bit of a binary number at the given index)
269 public void setMSBit(int bitIndex, Bit bit)
271 bits[bitIndex] = bit;
275 * Set the least significant bit at <code>bitIndex</code>. (rightmost bit of a binary number at the given index)
277 public void setLSBit(int bitIndex, Bit bit)
279 bits[bits.length - bitIndex - 1] = bit;
283 * Returns the most significant bit at <code>bitIndex</code>. (leftmost bit of a binary number at the given index)
285 public Bit getMSBit(int bitIndex)
287 return bits[bitIndex];
291 * Returns the least significant bit at <code>bitIndex</code>. (rightmost bit of a binary number at the given index)
293 public Bit getLSBit(int bitIndex)
295 return bits[bits.length - bitIndex - 1];
303 private void checkCompatibility(BitVector bv)
305 if (bits != null && bits.length != bv.length())
306 throw new IllegalArgumentException(format("BitVector length does not match: %d and %d", bits.length, bv.length()));
311 * @see Arrays#hashCode(Object[])
314 public int hashCode()
316 return Arrays.hashCode(bits);
320 * Does test for equality of values/content
322 * @see Object#equals(Object)
325 public boolean equals(Object obj)
329 if (!(obj instanceof BitVector))
331 BitVector other = (BitVector) obj;
332 return Arrays.equals(bits, other.bits);
336 * Does test for equality of values/content, shifting the other BitVector by <code>offset</code> to the right.<br>
337 * Therefore <code>offset + other.length() <= this.length()</code> needs to be true.
339 * @throws ArrayIndexOutOfBoundsException if <code>offset + other.length() > this.length()</code>
341 * @see Object#equals(Object)
343 public boolean equalsWithOffset(BitVector other, int offset)
347 return Arrays.equals(bits, offset, offset + other.length(), other.bits, 0, other.length());
351 * All {@link Bit}s symbols concatenated together (MSB first)
353 * @see #parse(String)
356 public String toString()
358 StringBuilder sb = new StringBuilder(bits.length);
361 return sb.toString();
365 * Parses a String containing solely {@link Bit} symbols (MSB first)
369 public static BitVector parse(String s)
371 Bit[] values = new Bit[s.length()];
372 for (int i = 0; i < s.length(); i++)
374 values[i] = Bit.parse(s, i);
376 return new BitVector(values);
379 public static BitVector of(long value, int bits)
381 return of(BigInteger.valueOf(value), bits);
384 public static BitVector of(BigInteger value, int bits)
386 Bit[] values = new Bit[bits];
387 for (int i = 0; i < bits; i++)
389 values[bits - i - 1] = Bit.of(value.testBit(i));
391 return new BitVector(values);
395 * Iterate over the {@link Bit}s of the BitVector <b>from MSB to LSB</b> (left to right).
398 public Iterator<Bit> iterator()
400 return new Iterator<>()
408 throw new NoSuchElementException();
409 return getMSBit(pos++);
413 public boolean hasNext()
415 return pos != length();