Added factory methods for integers
[Mograsim.git] / net.mograsim.logic.core / src / net / mograsim / logic / core / types / BitVector.java
index f92401f..18b2d95 100644 (file)
@@ -2,6 +2,7 @@ package net.mograsim.logic.core.types;
 
 import static java.lang.String.format;
 
+import java.math.BigInteger;
 import java.util.Arrays;
 import java.util.Iterator;
 import java.util.NoSuchElementException;
@@ -13,26 +14,46 @@ import java.util.function.UnaryOperator;
 /**
  * Immutable class representing a {@link Bit}Vector
  *
- *
  * @author Christian Femers
  *
  */
 public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit>, RandomAccess
 {
+       public static final BitVector SINGLE_U = new BitVector(Bit.U);
+       public static final BitVector SINGLE_X = new BitVector(Bit.X);
+       public static final BitVector SINGLE_0 = new BitVector(Bit.ZERO);
+       public static final BitVector SINGLE_1 = new BitVector(Bit.ONE);
+       public static final BitVector SINGLE_Z = new BitVector(Bit.Z);
+
+       private static final BitVector[] SINGLE_BIT_MAPPING = { SINGLE_U, SINGLE_X, SINGLE_0, SINGLE_1, SINGLE_Z };
+
        private final Bit[] bits;
 
+       private BitVector(Bit single)
+       {
+               Objects.requireNonNull(single);
+               bits = new Bit[] { single };
+       }
+
        private BitVector(Bit[] bits)
        {
-               this.bits = Objects.requireNonNull(bits);
+               this.bits = Objects.requireNonNull(bits); // do this first to "catch" bits==null before the foreach loop
+               for (Bit bit : bits)
+                       if (bit == null)
+                               throw new NullPointerException();
        }
 
        public static BitVector of(Bit... bits)
        {
+               if (bits.length == 1)
+                       return SINGLE_BIT_MAPPING[bits[0].ordinal()];
                return new BitVector(bits.clone());
        }
 
        public static BitVector of(Bit bit, int length)
        {
+               if (length == 1)
+                       return SINGLE_BIT_MAPPING[bit.ordinal()];
                return new BitVector(bit.makeArray(length));
        }
 
@@ -41,11 +62,22 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
                return BitVectorMutator.of(this);
        }
 
-       public Bit getBit(int bitIndex)
+       /**
+        * Returns the most significant bit at <code>bitIndex</code>. (leftmost bit of a binary number at the given index)
+        */
+       public Bit getMSBit(int bitIndex)
        {
                return bits[bitIndex];
        }
 
+       /**
+        * Returns the least significant bit at <code>bitIndex</code>. (rightmost bit of a binary number at the given index)
+        */
+       public Bit getLSBit(int bitIndex)
+       {
+               return bits[bits.length - bitIndex - 1];
+       }
+
        public Bit[] getBits()
        {
                return bits.clone();
@@ -55,6 +87,8 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
        public BitVector join(BitVector t)
        {
                checkCompatibility(t);
+               if (bits.length == 1)
+                       return SINGLE_BIT_MAPPING[bits[0].join(t.bits[0]).ordinal()];
                return new BitVector(binOp(bits.clone(), t.bits, Bit::join));
        }
 
@@ -62,6 +96,8 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
        public BitVector and(BitVector t)
        {
                checkCompatibility(t);
+               if (bits.length == 1)
+                       return SINGLE_BIT_MAPPING[bits[0].and(t.bits[0]).ordinal()];
                return new BitVector(binOp(bits.clone(), t.bits, Bit::and));
        }
 
@@ -69,6 +105,8 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
        public BitVector or(BitVector t)
        {
                checkCompatibility(t);
+               if (bits.length == 1)
+                       return SINGLE_BIT_MAPPING[bits[0].or(t.bits[0]).ordinal()];
                return new BitVector(binOp(bits.clone(), t.bits, Bit::or));
        }
 
@@ -76,12 +114,16 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
        public BitVector xor(BitVector t)
        {
                checkCompatibility(t);
+               if (bits.length == 1)
+                       return SINGLE_BIT_MAPPING[bits[0].xor(t.bits[0]).ordinal()];
                return new BitVector(binOp(bits.clone(), t.bits, Bit::xor));
        }
 
        @Override
        public BitVector not()
        {
+               if (bits.length == 1)
+                       return SINGLE_BIT_MAPPING[bits[0].not().ordinal()];
                return new BitVector(unOp(bits.clone(), Bit::not));
        }
 
@@ -221,16 +263,38 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
                        return this;
                }
 
-               public void setBit(int bitIndex, Bit bit)
+               /**
+                * Set the most significant bit at <code>bitIndex</code>. (leftmost bit of a binary number at the given index)
+                */
+               public void setMSBit(int bitIndex, Bit bit)
                {
                        bits[bitIndex] = bit;
                }
 
-               public Bit getBit(int bitIndex)
+               /**
+                * Set the least significant bit at <code>bitIndex</code>. (rightmost bit of a binary number at the given index)
+                */
+               public void setLSBit(int bitIndex, Bit bit)
+               {
+                       bits[bits.length - bitIndex - 1] = bit;
+               }
+
+               /**
+                * Returns the most significant bit at <code>bitIndex</code>. (leftmost bit of a binary number at the given index)
+                */
+               public Bit getMSBit(int bitIndex)
                {
                        return bits[bitIndex];
                }
 
+               /**
+                * Returns the least significant bit at <code>bitIndex</code>. (rightmost bit of a binary number at the given index)
+                */
+               public Bit getLSBit(int bitIndex)
+               {
+                       return bits[bits.length - bitIndex - 1];
+               }
+
                public int length()
                {
                        return bits.length;
@@ -284,7 +348,7 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
        }
 
        /**
-        * All {@link Bit}s symbols concatenated together
+        * All {@link Bit}s symbols concatenated together (MSB first)
         * 
         * @see #parse(String)
         */
@@ -298,7 +362,7 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
        }
 
        /**
-        * Parses a String containing solely {@link Bit} symbols
+        * Parses a String containing solely {@link Bit} symbols (MSB first)
         * 
         * @see #toString()
         */
@@ -312,6 +376,24 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
                return new BitVector(values);
        }
 
+       public static BitVector of(long value, int bits)
+       {
+               return of(BigInteger.valueOf(value), bits);
+       }
+
+       public static BitVector of(BigInteger value, int bits)
+       {
+               Bit[] values = new Bit[bits];
+               for (int i = 0; i < bits; i++)
+               {
+                       values[bits - i - 1] = Bit.of(value.testBit(i));
+               }
+               return new BitVector(values);
+       }
+
+       /**
+        * Iterate over the {@link Bit}s of the BitVector <b>from MSB to LSB</b> (left to right).
+        */
        @Override
        public Iterator<Bit> iterator()
        {
@@ -324,7 +406,7 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
                        {
                                if (!hasNext())
                                        throw new NoSuchElementException();
-                               return getBit(pos++);
+                               return getMSBit(pos++);
                        }
 
                        @Override