1 package era.mi.logic.components;
5 import era.mi.logic.Bit;
6 import era.mi.logic.wires.WireArray;
7 import era.mi.logic.wires.WireArray.WireArrayEnd;
8 import era.mi.logic.wires.WireArrayObserver;
10 public class Merger implements WireArrayObserver, Component
12 private WireArrayEnd outI;
13 private WireArray[] inputs;
14 private int[] beginningIndex;
18 * @param union The output of merging n {@link WireArray}s into one. Must have length = a1.length() + a2.length() + ... + an.length().
19 * @param inputs The inputs to be merged into the union
21 public Merger(WireArray union, WireArray... inputs)
24 this.outI = union.createInput();
25 this.beginningIndex = new int[inputs.length];
28 for (int i = 0; i < inputs.length; i++)
30 beginningIndex[i] = length;
31 length += inputs[i].length;
32 inputs[i].addObserver(this);
35 if (length != union.length)
36 throw new IllegalArgumentException(
37 "The output of merging n WireArrays into one must have length = a1.length() + a2.length() + ... + an.length().");
40 public WireArray getInput(int index)
45 public WireArray getUnion()
51 public void update(WireArray initiator, Bit[] oldValues)
53 int index = find(initiator);
54 int beginning = beginningIndex[index];
55 outI.feedSignals(beginning, initiator.getValues());
58 private int find(WireArray w)
60 for (int i = 0; i < inputs.length; i++)
66 public WireArray[] getInputs()
68 return inputs.clone();
72 public List<WireArray> getAllInputs()
74 return List.of(inputs);
78 public List<WireArray> getAllOutputs()
80 return List.of(outI.owner);