1 package era.mi.logic.components;
5 import era.mi.logic.types.BitVector;
6 import era.mi.logic.wires.Wire;
7 import era.mi.logic.wires.Wire.WireEnd;
8 import era.mi.logic.wires.WireObserver;
10 public class Merger implements WireObserver, Component
13 private WireEnd[] inputs;
14 private int[] beginningIndex;
18 * @param union The output of merging n {@link Wire}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(WireEnd union, WireEnd... inputs)
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 WireEnd getInput(int index)
45 public WireEnd getUnion()
51 public void update(Wire initiator, BitVector oldValues)
53 int index = find(initiator);
54 int beginning = beginningIndex[index];
55 out.feedSignals(beginning, inputs[index].getValues());
58 private int find(Wire w)
60 for (int i = 0; i < inputs.length; i++)
61 if (inputs[i].getWire() == w)
66 public WireEnd[] getInputs()
68 return inputs.clone();
72 public List<WireEnd> getAllInputs()
74 return List.of(inputs);
78 public List<WireEnd> getAllOutputs()