Integrated new types, tests still work, not used yet
[Mograsim.git] / era.mi / src / era / mi / logic / components / Merger.java
index 591bfc1..ca441ba 100644 (file)
@@ -1,85 +1,82 @@
 package era.mi.logic.components;
 
-import java.util.Arrays;
-import java.util.Collections;
 import java.util.List;
 
-import era.mi.logic.Bit;
-import era.mi.logic.wires.WireArray;
-import era.mi.logic.wires.WireArray.WireArrayInput;
-import era.mi.logic.wires.WireArrayObserver;
+import era.mi.logic.types.Bit;
+import era.mi.logic.wires.Wire;
+import era.mi.logic.wires.Wire.WireEnd;
+import era.mi.logic.wires.WireObserver;
 
-public class Merger implements WireArrayObserver, Component
+public class Merger implements WireObserver, Component
 {
-    private WireArrayInput outI;
-    private WireArray[] inputs;
-    private int[] beginningIndex;
+       private WireEnd out;
+       private WireEnd[] inputs;
+       private int[] beginningIndex;
 
-    /**
-     * 
-     * @param union  The output of merging n {@link WireArray}s into one. Must have
-     *               length = a1.length() + a2.length() + ... + an.length().
-     * @param inputs The inputs to be merged into the union
-     */
-    public Merger(WireArray union, WireArray... inputs)
-    {
-       this.inputs = inputs;
-       this.outI = union.createInput();
-       this.beginningIndex = new int[inputs.length];
-
-       int length = 0;
-       for (int i = 0; i < inputs.length; i++)
+       /**
+        * 
+        * @param union  The output of merging n {@link Wire}s into one. Must have length = a1.length() + a2.length() + ... + an.length().
+        * @param inputs The inputs to be merged into the union
+        */
+       public Merger(WireEnd union, WireEnd... inputs)
        {
-           beginningIndex[i] = length;
-           length += inputs[i].length;
-           inputs[i].addObserver(this);
-       }
+               this.inputs = inputs;
+               this.out = union;
+               this.beginningIndex = new int[inputs.length];
 
-       if (length != union.length)
-           throw new IllegalArgumentException(
-                   "The output of merging n WireArrays into one must have length = a1.length() + a2.length() + ... + an.length().");
-    }
+               int length = 0;
+               for (int i = 0; i < inputs.length; i++)
+               {
+                       beginningIndex[i] = length;
+                       length += inputs[i].length();
+                       inputs[i].addObserver(this);
+               }
 
-    public WireArray getInput(int index)
-    {
-       return inputs[index];
-    }
+               if (length != union.length())
+                       throw new IllegalArgumentException(
+                                       "The output of merging n WireArrays into one must have length = a1.length() + a2.length() + ... + an.length().");
+       }
 
-    public WireArray getUnion()
-    {
-       return outI.owner;
-    }
+       public WireEnd getInput(int index)
+       {
+               return inputs[index];
+       }
 
-    @Override
-    public void update(WireArray initiator, Bit[] oldValues)
-    {
-       int index = find(initiator);
-       int beginning = beginningIndex[index];
-       outI.feedSignals(beginning, initiator.getValues());
-    }
+       public WireEnd getUnion()
+       {
+               return out;
+       }
 
-    private int find(WireArray w)
-    {
-       for (int i = 0; i < inputs.length; i++)
-           if (inputs[i] == w)
-               return i;
-       return -1;
-    }
+       @Override
+       public void update(Wire initiator, Bit[] oldValues)
+       {
+               int index = find(initiator);
+               int beginning = beginningIndex[index];
+               out.feedSignals(beginning, inputs[index].getValues());
+       }
 
-    public WireArray[] getInputs()
-    {
-       return inputs.clone();
-    }
+       private int find(Wire w)
+       {
+               for (int i = 0; i < inputs.length; i++)
+                       if (inputs[i].getWire() == w)
+                               return i;
+               return -1;
+       }
+
+       public WireEnd[] getInputs()
+       {
+               return inputs.clone();
+       }
 
        @Override
-       public List<WireArray> getAllInputs()
+       public List<WireEnd> getAllInputs()
        {
-               return Collections.unmodifiableList(Arrays.asList(inputs));
+               return List.of(inputs);
        }
 
        @Override
-       public List<WireArray> getAllOutputs()
+       public List<WireEnd> getAllOutputs()
        {
-               return Collections.unmodifiableList(Arrays.asList(outI.owner));
+               return List.of(out);
        }
 }