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.ReadEnd;
8 import era.mi.logic.wires.Wire.ReadWriteEnd;
9 import era.mi.logic.wires.WireObserver;
11 public class Merger implements WireObserver, Component
13 private ReadWriteEnd out;
14 private ReadEnd[] inputs;
15 private int[] beginningIndex;
19 * @param union The output of merging n {@link Wire}s into one. Must have length = a1.length() + a2.length() + ... + an.length().
20 * @param inputs The inputs to be merged into the union
22 public Merger(ReadWriteEnd union, ReadEnd... inputs)
26 this.beginningIndex = new int[inputs.length];
29 for (int i = 0; i < inputs.length; i++)
31 beginningIndex[i] = length;
32 length += inputs[i].length();
33 inputs[i].addObserver(this);
36 if (length != union.length())
37 throw new IllegalArgumentException(
38 "The output of merging n WireArrays into one must have length = a1.length() + a2.length() + ... + an.length().");
41 public ReadEnd getInput(int index)
46 public ReadEnd getUnion()
52 public void update(ReadEnd initiator, BitVector oldValues)
54 int index = find(initiator);
55 int beginning = beginningIndex[index];
56 out.feedSignals(beginning, inputs[index].getValues());
59 private int find(ReadEnd r)
61 for (int i = 0; i < inputs.length; i++)
67 public ReadEnd[] getInputs()
69 return inputs.clone();
73 public List<ReadEnd> getAllInputs()
75 return List.of(inputs);
79 public List<ReadWriteEnd> getAllOutputs()