Added MSB first versions of parse() and toString()
[Mograsim.git] / net.mograsim.logic.core / src / net / mograsim / logic / core / types / BitVector.java
index 1e87f9a..9e09f43 100644 (file)
@@ -23,7 +23,10 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
 
        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)
@@ -140,7 +143,6 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
         *
         * @author Christian Femers
         */
-       @SuppressWarnings("synthetic-access")
        public static final class BitVectorMutator implements LogicType<BitVectorMutator, BitVector>
        {
                private Bit[] bits;
@@ -155,9 +157,16 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
                        return new BitVectorMutator(bv.getBits());
                }
 
+               /**
+                * Returns a new mutator of the specified length, <b>with all bits set to <code>null</code></b>. Use with care!
+                */
+               public static BitVectorMutator ofLength(int length)
+               {
+                       return new BitVectorMutator(new Bit[length]);
+               }
+
                /**
                 * Returns an empty mutator which has no bits set and will simply copy the values from the first binary operation performed.
-                * 
                 */
                public static BitVectorMutator empty()
                {
@@ -169,7 +178,7 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
                 * 
                 * @throws IllegalStateException if the mutator is (still) empty
                 */
-               public BitVector get()
+               public BitVector toBitVector()
                {
                        if (bits == null)
                                throw new IllegalStateException("cannot create a BitVector from an empty mutator");
@@ -215,6 +224,21 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
                        return this;
                }
 
+               public void setBit(int bitIndex, Bit bit)
+               {
+                       bits[bitIndex] = bit;
+               }
+
+               public Bit getBit(int bitIndex)
+               {
+                       return bits[bitIndex];
+               }
+
+               public int length()
+               {
+                       return bits.length;
+               }
+
                private void checkCompatibility(BitVector bv)
                {
                        if (bits != null && bits.length != bv.length())
@@ -262,26 +286,45 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
                return Arrays.equals(bits, offset, offset + other.length(), other.bits, 0, other.length());
        }
 
+       @Override
+       public String toString()
+       {
+               return toBitStringMSBFirst();
+       }
+
        /**
         * All {@link Bit}s symbols concatenated together
         * 
         * @see #parse(String)
         */
-       @Override
-       public String toString()
+       public String toBitStringLSBFirst()
+       {
+               StringBuilder sb = new StringBuilder(bits.length);
+               for (Bit bit : bits)
+                       sb.append(bit);
+               return sb.toString();
+       }
+
+       /**
+        * All {@link Bit}s symbols concatenated together, with the MSB coming first (like a binary number)
+        * 
+        * @see #parse(String)
+        */
+       public String toBitStringMSBFirst()
        {
                StringBuilder sb = new StringBuilder(bits.length);
                for (Bit bit : bits)
                        sb.append(bit);
+               sb.reverse();
                return sb.toString();
        }
 
        /**
         * Parses a String containing solely {@link Bit} symbols
         * 
-        * @see #toString()
+        * @see #toBitStringLSBFirst()
         */
-       public static BitVector parse(String s)
+       public static BitVector parseLSBFirst(String s)
        {
                Bit[] values = new Bit[s.length()];
                for (int i = 0; i < s.length(); i++)
@@ -291,6 +334,21 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
                return new BitVector(values);
        }
 
+       /**
+        * Parses a String containing solely {@link Bit} symbols, with the MSB coming first (like a binary number)
+        * 
+        * @see #toBitStringLSBFirst()
+        */
+       public static BitVector parseMSBFirst(String s)
+       {
+               Bit[] values = new Bit[s.length()];
+               for (int i = 0, j = s.length() - 1; j >= 0; i++, j--)
+               {
+                       values[i] = Bit.parse(s, j);
+               }
+               return new BitVector(values);
+       }
+
        @Override
        public Iterator<Bit> iterator()
        {