Speedup by changing redraw listener system
[Mograsim.git] / net.mograsim.logic.core / src / net / mograsim / logic / core / types / BitVector.java
index 1e87f9a..cce83f4 100644 (file)
@@ -13,7 +13,6 @@ import java.util.function.UnaryOperator;
 /**
  * Immutable class representing a {@link Bit}Vector
  *
- *
  * @author Christian Femers
  *
  */
@@ -23,7 +22,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)
@@ -41,11 +43,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();
@@ -140,7 +153,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 +167,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 +188,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 +234,43 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
                        return this;
                }
 
+               /**
+                * 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;
+               }
+
+               /**
+                * 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;
+               }
+
                private void checkCompatibility(BitVector bv)
                {
                        if (bits != null && bits.length != bv.length())
@@ -263,7 +319,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)
         */
@@ -277,7 +333,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()
         */
@@ -291,6 +347,9 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
                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()
        {
@@ -303,7 +362,7 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
                        {
                                if (!hasNext())
                                        throw new NoSuchElementException();
-                               return getBit(pos++);
+                               return getMSBit(pos++);
                        }
 
                        @Override