From: Daniel Kirschten Date: Tue, 20 Aug 2019 20:08:56 +0000 (+0200) Subject: Merge remote-tracking branch 'origin/development' into development X-Git-Url: https://mograsim.net/gitweb/?a=commitdiff_plain;h=7aa54daf034a6c19bef2dab40f66046869261565;hp=7683e659eba97d16cbc7f9b39e379ad4f599f9b7;p=Mograsim.git Merge remote-tracking branch 'origin/development' into development --- diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/memory/WordAddressableMemory.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/memory/WordAddressableMemory.java new file mode 100644 index 00000000..fbb76c22 --- /dev/null +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/memory/WordAddressableMemory.java @@ -0,0 +1,80 @@ +package net.mograsim.logic.core.components.memory; + +import java.util.Arrays; +import java.util.HashMap; + +import net.mograsim.logic.core.types.Bit; +import net.mograsim.logic.core.types.BitVector; + +public class WordAddressableMemory +{ + private final int cellWidth; + private final long minimalAddress, maximalAddress; + private final int pageSize = 64; + + private HashMap pages; + + public WordAddressableMemory(int cellWidth, long minimalAddress, long maximalAddress) + { + super(); + this.cellWidth = cellWidth; + this.minimalAddress = minimalAddress; + this.maximalAddress = maximalAddress; + this.pages = new HashMap<>(); + } + + public void setCell(long address, BitVector b) + { + if (address < minimalAddress || address > maximalAddress) + throw new IndexOutOfBoundsException(String.format("Memory address out of bounds! Minimum: %d Maimum: %d Actual: %d", + minimalAddress, maximalAddress, address)); + long page = address / pageSize; + int offset = (int) (address % pageSize); + Page p = pages.get(Long.valueOf(page)); + if (p == null) + pages.put(page, p = new Page()); + p.setCell(offset, b); + } + + public BitVector getCell(long address) + { + long page = address / pageSize; + int offset = (int) (address % pageSize); + Page p = pages.get(Long.valueOf(page)); + if (p == null) + return BitVector.of(Bit.U, cellWidth); + return p.getCell(offset); + } + + private class Page + { + private BitVector[] memory; + + public Page() + { + memory = new BitVector[pageSize]; + } + + public BitVector getCell(int index) + { + BitVector b = memory[index]; + if (b == null) + return BitVector.of(Bit.U, cellWidth); + return memory[index]; + } + + public void setCell(int index, BitVector bits) + { + if (bits.length() != cellWidth) + throw new IllegalArgumentException(String.format( + "BitVector to be saved in memory cell has unexpected length. Expected: %d Actual: %d", cellWidth, bits.length())); + memory[index] = bits; + } + + @Override + public String toString() + { + return Arrays.deepToString(memory); + } + } +} diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/memory/WordAddressableMemoryComponent.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/memory/WordAddressableMemoryComponent.java new file mode 100644 index 00000000..821b1f36 --- /dev/null +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/memory/WordAddressableMemoryComponent.java @@ -0,0 +1,77 @@ +package net.mograsim.logic.core.components.memory; + +import java.util.List; + +import net.mograsim.logic.core.components.BasicComponent; +import net.mograsim.logic.core.timeline.Timeline; +import net.mograsim.logic.core.types.Bit; +import net.mograsim.logic.core.types.BitVector; +import net.mograsim.logic.core.wires.Wire.ReadEnd; +import net.mograsim.logic.core.wires.Wire.ReadWriteEnd; + +/** + * A memory component that only allows access to words of a specific length + */ +public class WordAddressableMemoryComponent extends BasicComponent +{ + private final WordAddressableMemory memory; + private final static Bit read = Bit.ONE; + + private ReadWriteEnd data; + private ReadEnd rWBit, address; + + /** + * @param data The bits of this ReadEnd are the value that is written to/read from memory; The bit width of this wire is the width of + * a memory word + * @param rWBit The value of the 0th bit dictates the mode: 0: Write, 1: Read + * @param address The bits of this ReadEnd address the memory cell to read/write + */ + public WordAddressableMemoryComponent(Timeline timeline, int processTime, long minimalAddress, long maximalAddress, ReadWriteEnd data, + ReadEnd rWBit, ReadEnd address) + { + super(timeline, processTime); + this.data = data; + this.rWBit = rWBit; + this.address = address; + data.registerObserver(this); + rWBit.registerObserver(this); + address.registerObserver(this); + + memory = new WordAddressableMemory(data.length(), minimalAddress, maximalAddress); + } + + @Override + protected void compute() + { + if (!address.hasNumericValue()) + { + if (read.equals(rWBit.getValue())) + data.feedSignals(BitVector.of(Bit.U, data.length())); + else + data.clearSignals(); + return; + } + long addressed = address.getUnsignedValue(); + if (read.equals(rWBit.getValue())) + data.feedSignals(memory.getCell(addressed)); + else + { + data.clearSignals(); + System.out.println(memory); + memory.setCell(addressed, data.getValues()); + } + } + + @Override + public List getAllInputs() + { + return List.of(data, rWBit, address); + } + + @Override + public List getAllOutputs() + { + return List.of(data); + } + +} \ No newline at end of file diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/wires/Wire.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/wires/Wire.java index 6310d5b4..6f747c5f 100644 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/wires/Wire.java +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/wires/Wire.java @@ -265,12 +265,6 @@ public class Wire return Wire.this.getValue(index); } - /** - * @param index Index of the requested bit. - * @return The value of the indexed bit. - * - * @author Fabian Stemmler - */ public BitVector getValues() { return Wire.this.getValues(); diff --git a/net.mograsim.logic.core/test/net/mograsim/logic/core/tests/ComponentTest.java b/net.mograsim.logic.core/test/net/mograsim/logic/core/tests/ComponentTest.java index 9c5a615e..69251d73 100644 --- a/net.mograsim.logic.core/test/net/mograsim/logic/core/tests/ComponentTest.java +++ b/net.mograsim.logic.core/test/net/mograsim/logic/core/tests/ComponentTest.java @@ -4,8 +4,11 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.fail; +import java.math.BigInteger; +import java.util.Random; import java.util.function.LongConsumer; +import org.junit.Before; import org.junit.jupiter.api.Test; import net.mograsim.logic.core.components.Connector; @@ -20,6 +23,7 @@ import net.mograsim.logic.core.components.gates.NorGate; import net.mograsim.logic.core.components.gates.NotGate; import net.mograsim.logic.core.components.gates.OrGate; import net.mograsim.logic.core.components.gates.XorGate; +import net.mograsim.logic.core.components.memory.WordAddressableMemoryComponent; import net.mograsim.logic.core.timeline.Timeline; import net.mograsim.logic.core.types.Bit; import net.mograsim.logic.core.types.BitVector; @@ -32,6 +36,12 @@ class ComponentTest { private Timeline t = new Timeline(11); + @Before + void resetTimeline() + { + t.reset(); + } + @Test void circuitExampleTest() { @@ -59,7 +69,6 @@ class ComponentTest @Test void splitterTest() { - t.reset(); Wire a = new Wire(t, 3, 1), b = new Wire(t, 2, 1), c = new Wire(t, 3, 1), in = new Wire(t, 8, 1); in.createReadWriteEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE); new Splitter(t, in.createReadOnlyEnd(), a.createReadWriteEnd(), b.createReadWriteEnd(), c.createReadWriteEnd()); @@ -74,7 +83,6 @@ class ComponentTest @Test void mergerTest() { - t.reset(); Wire a = new Wire(t, 3, 1), b = new Wire(t, 2, 1), c = new Wire(t, 3, 1), out = new Wire(t, 8, 1); a.createReadWriteEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO); b.createReadWriteEnd().feedSignals(Bit.ONE, Bit.ZERO); @@ -90,7 +98,6 @@ class ComponentTest @Test void fusionTest1() { - t.reset(); Wire a = new Wire(t, 3, 1), b = new Wire(t, 2, 1), c = new Wire(t, 3, 1), out = new Wire(t, 8, 1); Wire.fuse(a, out, 0, 0, a.length); Wire.fuse(b, out, 0, a.length, b.length); @@ -121,7 +128,6 @@ class ComponentTest @Test void fusionTest2() { - t.reset(); Wire a = new Wire(t, 3, 1), b = new Wire(t, 3, 1); Wire.fuse(a, b); ReadWriteEnd rw = a.createReadWriteEnd(); @@ -136,7 +142,6 @@ class ComponentTest @Test void fusionTest3() { - t.reset(); Wire a = new Wire(t, 3, 1), b = new Wire(t, 3, 1); a.createReadWriteEnd().feedSignals(Bit.Z, Bit.U, Bit.X); t.executeAll(); @@ -193,7 +198,6 @@ class ComponentTest @Test void muxTest() { - t.reset(); Wire a = new Wire(t, 4, 3), b = new Wire(t, 4, 6), c = new Wire(t, 4, 4), select = new Wire(t, 2, 5), out = new Wire(t, 4, 1); ReadWriteEnd selectIn = select.createReadWriteEnd(); @@ -221,7 +225,6 @@ class ComponentTest @Test void demuxTest() { - t.reset(); Wire a = new Wire(t, 4, 3), b = new Wire(t, 4, 6), c = new Wire(t, 4, 4), select = new Wire(t, 2, 5), in = new Wire(t, 4, 1); ReadWriteEnd selectIn = select.createReadWriteEnd(); @@ -254,7 +257,6 @@ class ComponentTest @Test void andTest() { - t.reset(); Wire a = new Wire(t, 4, 1), b = new Wire(t, 4, 3), c = new Wire(t, 4, 1); new AndGate(t, 1, c.createReadWriteEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd()); a.createReadWriteEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO); @@ -268,7 +270,6 @@ class ComponentTest @Test void orTest() { - t.reset(); Wire a = new Wire(t, 4, 1), b = new Wire(t, 4, 3), c = new Wire(t, 4, 1); new OrGate(t, 1, c.createReadWriteEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd()); a.createReadWriteEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO); @@ -282,7 +283,6 @@ class ComponentTest @Test void nandTest() { - t.reset(); Wire a = new Wire(t, 4, 1), b = new Wire(t, 4, 3), c = new Wire(t, 4, 1), d = new Wire(t, 4, 1); new NandGate(t, 1, d.createReadWriteEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd(), c.createReadOnlyEnd()); a.createReadWriteEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO); @@ -297,7 +297,6 @@ class ComponentTest @Test void norTest() { - t.reset(); Wire a = new Wire(t, 4, 1), b = new Wire(t, 4, 3), c = new Wire(t, 4, 1), d = new Wire(t, 4, 1); new NorGate(t, 1, d.createReadWriteEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd(), c.createReadOnlyEnd()); a.createReadWriteEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO); @@ -312,7 +311,6 @@ class ComponentTest @Test void xorTest() { - t.reset(); Wire a = new Wire(t, 3, 1), b = new Wire(t, 3, 2), c = new Wire(t, 3, 1), d = new Wire(t, 3, 1); new XorGate(t, 1, d.createReadWriteEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd(), c.createReadOnlyEnd()); a.createReadWriteEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE); @@ -327,7 +325,6 @@ class ComponentTest @Test void notTest() { - t.reset(); Wire a = new Wire(t, 3, 1), b = new Wire(t, 3, 2); new NotGate(t, 1, a.createReadOnlyEnd(), b.createReadWriteEnd()); a.createReadWriteEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE); @@ -340,7 +337,6 @@ class ComponentTest @Test void rsLatchCircuitTest() { - t.reset(); Wire r = new Wire(t, 1, 1), s = new Wire(t, 1, 1), t1 = new Wire(t, 1, 15), t2 = new Wire(t, 1, 1), q = new Wire(t, 1, 1), nq = new Wire(t, 1, 1); @@ -376,8 +372,6 @@ class ComponentTest @Test void numericValueTest() { - t.reset(); - Wire a = new Wire(t, 4, 1); a.createReadWriteEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE); @@ -426,7 +420,6 @@ class ComponentTest @Test void multipleInputs() { - t.reset(); Wire w = new Wire(t, 2, 1); ReadWriteEnd wI1 = w.createReadWriteEnd(), wI2 = w.createReadWriteEnd(); wI1.feedSignals(Bit.ONE, Bit.Z); @@ -459,8 +452,6 @@ class ComponentTest { // Nur ein Experiment, was über mehrere 'passive' Bausteine hinweg passieren würde - t.reset(); - Wire a = new Wire(t, 1, 2); Wire b = new Wire(t, 1, 2); Wire c = new Wire(t, 1, 2); @@ -528,6 +519,47 @@ class ComponentTest test2.assertAfterSimulationIs(Bit.ONE); } + @Test + public void wordAddressableMemoryLargeTest() + { + Wire rW = new Wire(t, 1, 2); + Wire data = new Wire(t, 16, 2); + Wire address = new Wire(t, 64, 2); + ReadWriteEnd rWI = rW.createReadWriteEnd(); + ReadWriteEnd dataI = data.createReadWriteEnd(); + ReadWriteEnd addressI = address.createReadWriteEnd(); + + WordAddressableMemoryComponent memory = new WordAddressableMemoryComponent(t, 4, 4096L, Long.MAX_VALUE, data.createReadWriteEnd(), + rW.createReadOnlyEnd(), address.createReadOnlyEnd()); + + Random r = new Random(); + for (long j = 1; j > 0; j *= 2) + { + for (int i = 0; i < 50; i++) + { + String sAddress = String.format("%64s", BigInteger.valueOf(4096 + i + j).toString(2)).replace(' ', '0'); + sAddress = new StringBuilder(sAddress).reverse().toString(); + BitVector bAddress = BitVector.parse(sAddress); + addressI.feedSignals(bAddress); + t.executeAll(); + String random = BigInteger.valueOf(Math.abs(r.nextInt())).toString(5); + random = random.substring(Integer.max(0, random.length() - 16)); + random = String.format("%16s", random).replace(' ', '0'); + random = random.replace('2', 'X').replace('3', 'Z').replace('4', 'U'); + BitVector vector = BitVector.parse(random); + dataI.feedSignals(vector); + rWI.feedSignals(Bit.ZERO); + t.executeAll(); + rWI.feedSignals(Bit.ONE); + t.executeAll(); + dataI.clearSignals(); + t.executeAll(); + + assertBitArrayEquals(dataI.getValues(), vector.getBits()); + } + } + } + private static void assertBitArrayEquals(BitVector actual, Bit... expected) { assertArrayEquals(expected, actual.getBits()); diff --git a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/ComponentHandle.java b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/ComponentHandle.java index c3afb701..b44f1cf0 100644 --- a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/ComponentHandle.java +++ b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/ComponentHandle.java @@ -22,6 +22,7 @@ public class ComponentHandle extends Handle public ComponentHandle(GUIComponent parent) { + super(4); this.parent = parent; Rectangle bounds = parent.getBounds(); setSize(bounds.width, bounds.height); diff --git a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/CornerHandle.java b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/CornerHandle.java index 74eb080e..959fb896 100644 --- a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/CornerHandle.java +++ b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/CornerHandle.java @@ -15,7 +15,7 @@ public class CornerHandle extends Handle public CornerHandle(DeserializedSubmodelComponent toBeEdited) { - super(); + super(0); this.toBeEdited = toBeEdited; setSize(LENGTH, LENGTH); initPos(); diff --git a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/Handle.java b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/Handle.java index c8f93922..cbfe2a2a 100644 --- a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/Handle.java +++ b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/Handle.java @@ -14,9 +14,11 @@ public abstract class Handle { private final Rectangle bounds; private final Collection redrawListeners, destroyListeners; + private final int priority; - public Handle() + public Handle(int priority) { + this.priority = priority; redrawListeners = new ArrayList<>(); destroyListeners = new ArrayList<>(); bounds = new Rectangle(0, 0, 0, 0); @@ -122,6 +124,11 @@ public abstract class Handle public void onDeselect() {} //@formatter:on + public final int getPriority() + { + return priority; + } + public abstract HandleType getType(); public static enum HandleType diff --git a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/HandleManager.java b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/HandleManager.java index f3ef9a88..24eb36d7 100644 --- a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/HandleManager.java +++ b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/HandleManager.java @@ -15,6 +15,7 @@ import java.util.stream.Collectors; import net.haspamelodica.swt.helper.swtobjectwrappers.Point; import net.mograsim.logic.model.editor.Editor; import net.mograsim.logic.model.editor.states.EditorState; +import net.mograsim.logic.model.editor.util.PrioritySet; import net.mograsim.logic.model.model.ViewModelModifiable; import net.mograsim.logic.model.model.components.GUIComponent; import net.mograsim.logic.model.model.components.submodels.SubmodelComponent; @@ -37,8 +38,6 @@ public class HandleManager private final Editor editor; private boolean initialized = false; - private CornerHandle cornerHandle; - public HandleManager(Editor editor) { this.editor = editor; @@ -46,7 +45,7 @@ public class HandleManager handlePerInterfacePin = new HashMap<>(); pointHandlesPerWire = new HashMap<>(); handlePerComp = new HashMap<>(); - handles = new HashSet<>(); + handles = new PrioritySet<>((a, b) -> Integer.compare(a.getPriority(), b.getPriority())); wirePointHandles = new HashSet<>(); handlePerWire = new HashMap<>(); @@ -93,7 +92,7 @@ public class HandleManager comps.forEach(c -> registerComponent(c)); model.getWiresByName().values().forEach(w -> registerWire(w)); - addHandle(cornerHandle = new CornerHandle(editor.toBeEdited)); + addHandle(new CornerHandle(editor.toBeEdited)); } } @@ -345,15 +344,9 @@ public class HandleManager public void click(Point clicked, int stateMask) { EditorState entryState = editor.stateManager.getState(); - - // TODO: As soon as wires connected to a component being removed also are removed, change priority - if (!cornerHandle.click(clicked.x, clicked.y, stateMask, entryState)) - if (!click(handlePerPin.values(), clicked, entryState, stateMask)) - if (!click(handlePerInterfacePin.values(), clicked, entryState, stateMask)) - if (!click(getWirePointHandles(), clicked, entryState, stateMask)) - if (!click(getWireHandles(), clicked, entryState, stateMask)) - if (!click(handlePerComp.values(), clicked, entryState, stateMask)) - entryState.clickedEmpty(clicked, stateMask); + // TODO: As soon as wires connected to a component being removed also are removed, change priority) + if (!click(handles, clicked, entryState, stateMask)) + entryState.clickedEmpty(clicked, stateMask); entryState.clicked(clicked, stateMask); } diff --git a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/InterfacePinHandle.java b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/InterfacePinHandle.java index 1b963a13..f585224a 100644 --- a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/InterfacePinHandle.java +++ b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/InterfacePinHandle.java @@ -19,7 +19,7 @@ public class InterfacePinHandle extends PinHandle public InterfacePinHandle(MovablePin parent, DeserializedSubmodelComponent pinOwner) { - super(); + super(2); this.parent = parent; this.owner = pinOwner; setSize(CIRCLE_DIAM, CIRCLE_DIAM); diff --git a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/PinHandle.java b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/PinHandle.java index 66c18acb..2e4a9dc6 100644 --- a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/PinHandle.java +++ b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/PinHandle.java @@ -4,9 +4,9 @@ import net.mograsim.logic.model.model.wires.Pin; public abstract class PinHandle extends Handle { - public PinHandle() + public PinHandle(int priority) { - super(); + super(priority); } public abstract Pin getPin(); diff --git a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/StaticPinHandle.java b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/StaticPinHandle.java index 5170ba25..0f4a1524 100644 --- a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/StaticPinHandle.java +++ b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/StaticPinHandle.java @@ -15,7 +15,7 @@ public class StaticPinHandle extends PinHandle public StaticPinHandle(Pin parent) { - super(); + super(1); this.parent = parent; setSize(CIRCLE_DIAM, CIRCLE_DIAM); parent.addPinMovedListener((p) -> updatePos()); diff --git a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/WireHandle.java b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/WireHandle.java index 199df99c..7629dcb1 100644 --- a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/WireHandle.java +++ b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/WireHandle.java @@ -20,6 +20,7 @@ public class WireHandle extends Handle public WireHandle(GUIWire parent) { + super(5); this.parent = parent; parent.addPathChangedListener(c -> updateBounds()); updateBounds(); diff --git a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/WirePointHandle.java b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/WirePointHandle.java index 48c7c227..4c0b9a31 100644 --- a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/WirePointHandle.java +++ b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/WirePointHandle.java @@ -19,7 +19,7 @@ public class WirePointHandle extends Handle public WirePointHandle(HandleManager manager, GUIWire parent, int pointIndex) { - super(); + super(3); this.manager = manager; this.parent = parent; this.pointIndex = pointIndex; diff --git a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/util/PrioritySet.java b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/util/PrioritySet.java new file mode 100644 index 00000000..253dfdd3 --- /dev/null +++ b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/util/PrioritySet.java @@ -0,0 +1,98 @@ +package net.mograsim.logic.model.editor.util; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.Iterator; +import java.util.Set; + +/** + * Similar to a SortedSet, except it is allowed for multiple elements to have the same priority (c.compare(e1, e2) == 0 is + * allowed to be true for two different elements e1 and e2). However, to elements are not allowed to be equal according to + * Object.equals(Object o). + * + * @author Fabian Stemmler + * + * @param the type of elements in this list + */ +public class PrioritySet implements Set +{ + private ArrayList list; + private Comparator c; + + public PrioritySet(Comparator c) + { + setComparator(c); + list = new ArrayList<>(); + } + + @SuppressWarnings("unchecked") + public PrioritySet() + { + this((e1, e2) -> ((Comparable) e1).compareTo(e2)); + } + + public void setComparator(Comparator c) + { + this.c = c; + } + + //@formatter:off + @Override + public int size() { return list.size(); } + @Override + public boolean isEmpty() { return list.isEmpty(); } + @Override + public boolean contains(Object o) { return list.isEmpty(); } + @Override + public Iterator iterator() { return list.iterator(); } + @Override + public Object[] toArray() { return list.toArray(); } + @Override + public E[] toArray(E[] a) { return list.toArray(a); } + @Override + public boolean remove(Object o) { return list.remove(o); } + @Override + public boolean containsAll(Collection c) { return list.containsAll(c); } + @Override + public boolean removeAll(Collection c) { return list.removeAll(c); } + @Override + public boolean retainAll(Collection c) { return list.retainAll(c); } + @Override + public void clear() { list.clear(); } + //@formatter:on + + @Override + public boolean add(T e) + { + if (isEmpty()) + { + list.add(e); + return true; + } + int index = Collections.binarySearch(list, e, c); + if (index < 0) + index = -index - 1; + if (index < size()) + { + if (list.get(index).equals(e)) + return false; + list.add(index, e); + } else + list.add(e); + return true; + } + + @Override + public boolean addAll(Collection c) + { + return c.stream().map(e -> add(e)).reduce(false, (a, b) -> a || b); + } + + @Override + public String toString() + { + return list.toString(); + } +}