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.Wire;
9 import era.mi.logic.wires.Wire.WireEnd;
10 import era.mi.logic.wires.WireArrayObserver;
12 public class Merger implements WireArrayObserver, Component
15 private WireEnd[] inputs;
16 private int[] beginningIndex;
20 * @param union The output of merging n {@link Wire}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(WireEnd union, WireEnd... inputs)
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 WireEnd getInput(int index)
47 public WireEnd getUnion()
53 public void update(Wire initiator, Bit[] oldValues)
55 int index = find(initiator);
56 int beginning = beginningIndex[index];
57 out.feedSignals(beginning, inputs[index].getValues());
60 private int find(Wire w)
62 for (int i = 0; i < inputs.length; i++)
63 if (inputs[i].getWire() == w)
68 public WireEnd[] getInputs()
70 return inputs.clone();
74 public List<WireEnd> getAllInputs()
76 return Collections.unmodifiableList(Arrays.asList(inputs));
80 public List<WireEnd> getAllOutputs()
82 return Collections.unmodifiableList(Arrays.asList(out));