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
14 private WireArrayInput outI;
15 private WireArray[] inputs;
16 private int[] beginningIndex;
20 * @param union The output of merging n {@link WireArray}s into one. Must have
21 * length = a1.length() + a2.length() + ... + an.length().
22 * @param inputs The inputs to be merged into the union
24 public Merger(WireArray union, WireArray... inputs)
27 this.outI = union.createInput();
28 this.beginningIndex = new int[inputs.length];
31 for (int i = 0; i < inputs.length; i++)
33 beginningIndex[i] = length;
34 length += inputs[i].length;
35 inputs[i].addObserver(this);
38 if (length != union.length)
39 throw new IllegalArgumentException(
40 "The output of merging n WireArrays into one must have length = a1.length() + a2.length() + ... + an.length().");
43 public WireArray getInput(int index)
48 public WireArray getUnion()
54 public void update(WireArray initiator, Bit[] oldValues)
56 int index = find(initiator);
57 int beginning = beginningIndex[index];
58 outI.feedSignals(beginning, initiator.getValues());
61 private int find(WireArray w)
63 for (int i = 0; i < inputs.length; i++)
69 public WireArray[] getInputs()
71 return inputs.clone();
75 public List<WireArray> getAllInputs()
77 return Collections.unmodifiableList(Arrays.asList(inputs));
81 public List<WireArray> getAllOutputs()
83 return Collections.unmodifiableList(Arrays.asList(outI.owner));