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.WireArrayEnd;
10 import era.mi.logic.wires.WireArrayObserver;
12 public class Merger implements WireArrayObserver, Component
14 private WireArrayEnd outI;
15 private WireArray[] inputs;
16 private int[] beginningIndex;
20 * @param union The output of merging n {@link WireArray}s into one. Must have length = a1.length() + a2.length() + ... + an.length().
21 * @param inputs The inputs to be merged into the union
23 public Merger(WireArray union, WireArray... inputs)
26 this.outI = union.createInput();
27 this.beginningIndex = new int[inputs.length];
30 for (int i = 0; i < inputs.length; i++)
32 beginningIndex[i] = length;
33 length += inputs[i].length;
34 inputs[i].addObserver(this);
37 if (length != union.length)
38 throw new IllegalArgumentException(
39 "The output of merging n WireArrays into one must have length = a1.length() + a2.length() + ... + an.length().");
42 public WireArray getInput(int index)
47 public WireArray getUnion()
53 public void update(WireArray initiator, Bit[] oldValues)
55 int index = find(initiator);
56 int beginning = beginningIndex[index];
57 outI.feedSignals(beginning, initiator.getValues());
60 private int find(WireArray w)
62 for (int i = 0; i < inputs.length; i++)
68 public WireArray[] getInputs()
70 return inputs.clone();
74 public List<WireArray> getAllInputs()
76 return Collections.unmodifiableList(Arrays.asList(inputs));
80 public List<WireArray> getAllOutputs()
82 return Collections.unmodifiableList(Arrays.asList(outI.owner));