1 package era.mi.logic.components;
3 import java.util.Arrays;
4 import java.util.Collections;
7 import era.mi.logic.Bit;
8 import era.mi.logic.wires.WireArray;
9 import era.mi.logic.wires.WireArray.WireArrayInput;
10 import era.mi.logic.wires.WireArrayObserver;
12 public class Merger implements WireArrayObserver, Component {
13 private WireArrayInput outI;
14 private WireArray[] inputs;
15 private int[] beginningIndex;
19 * @param union The output of merging n {@link WireArray}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(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++) {
29 beginningIndex[i] = length;
30 length += inputs[i].length;
31 inputs[i].addObserver(this);
34 if (length != union.length)
35 throw new IllegalArgumentException(
36 "The output of merging n WireArrays into one must have length = a1.length() + a2.length() + ... + an.length().");
39 public WireArray getInput(int index) {
43 public WireArray getUnion() {
48 public void update(WireArray initiator, Bit[] oldValues) {
49 int index = find(initiator);
50 int beginning = beginningIndex[index];
51 outI.feedSignals(beginning, initiator.getValues());
54 private int find(WireArray w) {
55 for (int i = 0; i < inputs.length; i++)
61 public WireArray[] getInputs() {
62 return inputs.clone();
66 public List<WireArray> getAllInputs() {
67 return Collections.unmodifiableList(Arrays.asList(inputs));
71 public List<WireArray> getAllOutputs() {
72 return Collections.unmodifiableList(Arrays.asList(outI.owner));