From 9c1958a5b2b95819172089c04651803d1b937030 Mon Sep 17 00:00:00 2001 From: Daniel Kirschten Date: Sat, 2 May 2020 14:22:36 +0200 Subject: [PATCH] BitVectorSplittingAHLSH now supports minimal and maximal values --- ...rSplittingAtomicHighLevelStateHandler.java | 52 ++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/atomic/BitVectorSplittingAtomicHighLevelStateHandler.java b/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/atomic/BitVectorSplittingAtomicHighLevelStateHandler.java index 4289808d..bc8b9662 100644 --- a/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/atomic/BitVectorSplittingAtomicHighLevelStateHandler.java +++ b/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/atomic/BitVectorSplittingAtomicHighLevelStateHandler.java @@ -1,5 +1,6 @@ package net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic; +import java.math.BigInteger; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -21,6 +22,10 @@ public class BitVectorSplittingAtomicHighLevelStateHandler implements AtomicHigh private final List vectorPartTargetsUnmodifiable; private final List vectorPartLengthes; private final List vectorPartLengthesUnmodifiable; + private BitVector minimalValue; + private BigInteger minimalValueBigInteger; + private BitVector maximalValue; + private BigInteger maximalValueBigInteger; private int length; private final Map, Consumer> targetListeners; @@ -42,12 +47,16 @@ public class BitVectorSplittingAtomicHighLevelStateHandler implements AtomicHigh this.targetListeners = new HashMap<>(); if (params != null) + { setVectorParts(params.vectorPartTargets, params.vectorPartLengthes); + setRange(params.minimalValue, params.maximalValue); + } } - public void set(List targets, List lengthes) + public void set(List targets, List lengthes, BitVector minimalValue, BitVector maximalValue) { setVectorParts(targets, lengthes); + setRange(minimalValue, maximalValue); } public void addVectorPart(String target, int length) @@ -74,6 +83,35 @@ public class BitVectorSplittingAtomicHighLevelStateHandler implements AtomicHigh length += lengthes.stream().mapToInt(Integer::intValue).sum(); } + @SuppressWarnings("null") // explicit checks for null are in place + public void setRange(BitVector minimalValue, BitVector maximalValue) + { + boolean minIsNull = minimalValue == null; + if (minIsNull != (maximalValue == null)) + throw new IllegalArgumentException("minimalValue and maximalValue must either both be null or both non-null"); + if (!minIsNull) + { + if (!minimalValue.isBinary()) + throw new IllegalArgumentException("minimalValue is not binary"); + if (!maximalValue.isBinary()) + throw new IllegalArgumentException("maximalValue is not binary"); + this.minimalValueBigInteger = minimalValue.getUnsignedValue(); + this.maximalValueBigInteger = maximalValue.getUnsignedValue(); + } + this.minimalValue = minimalValue; + this.maximalValue = maximalValue; + } + + public BitVector getMinimalValue() + { + return minimalValue; + } + + public BitVector getMaximalValue() + { + return maximalValue; + } + public List getVectorPartTargets() { return vectorPartTargetsUnmodifiable; @@ -110,6 +148,14 @@ public class BitVectorSplittingAtomicHighLevelStateHandler implements AtomicHigh BitVector newStateCasted = (BitVector) newState; if (newStateCasted.length() != length) throw new IllegalArgumentException("Incorrect vector length: " + newStateCasted.length() + "; expected " + length); + // TODO what for non-binary values? + if (minimalValue != null && newStateCasted.isBinary()) + { + BigInteger newStateBigInteger = newStateCasted.getUnsignedValue(); + if (newStateBigInteger.compareTo(minimalValueBigInteger) < 0 || newStateBigInteger.compareTo(maximalValueBigInteger) > 0) + throw new IllegalArgumentException( + "Value out of range: should be in " + minimalValue + " - " + maximalValue + "; was " + newState); + } for (int partIndex = 0, bitIndex = 0; partIndex < vectorPartTargets.size(); partIndex++) { int vectorPartLength = vectorPartLengthes.get(partIndex); @@ -157,6 +203,8 @@ public class BitVectorSplittingAtomicHighLevelStateHandler implements AtomicHigh BitVectorSplittingAtomicHighLevelStateHandlerParams params = new BitVectorSplittingAtomicHighLevelStateHandlerParams(); params.vectorPartTargets = new ArrayList<>(vectorPartTargets); params.vectorPartLengthes = new ArrayList<>(vectorPartLengthes); + params.minimalValue = minimalValue; + params.maximalValue = maximalValue; return params; } @@ -164,6 +212,8 @@ public class BitVectorSplittingAtomicHighLevelStateHandler implements AtomicHigh { public List vectorPartTargets; public List vectorPartLengthes; + public BitVector minimalValue; + public BitVector maximalValue; } static -- 2.17.1