2015-01-21 21:58:35 +01:00
|
|
|
/*******************************************************************************
|
2021-01-09 14:26:49 +01:00
|
|
|
* Copyright (c) 2015-2021 MINRES Technologies GmbH and others.
|
2015-01-21 21:58:35 +01:00
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
* are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
* which accompanies this distribution, and is available at
|
|
|
|
* http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
*
|
|
|
|
* Contributors:
|
|
|
|
* MINRES Technologies GmbH - initial API and implementation
|
|
|
|
*******************************************************************************/
|
2015-01-09 09:16:40 +01:00
|
|
|
package com.minres.scviewer.database;
|
|
|
|
|
2021-01-09 12:43:02 +01:00
|
|
|
/**
|
|
|
|
* The Class BitVector.
|
|
|
|
*/
|
2020-11-28 10:22:22 +01:00
|
|
|
public class BitVector implements IEvent {
|
2015-01-09 09:16:40 +01:00
|
|
|
|
2021-01-09 12:43:02 +01:00
|
|
|
/** The width. */
|
2015-01-09 09:16:40 +01:00
|
|
|
private final int width;
|
2020-11-29 10:25:48 +01:00
|
|
|
|
2021-01-09 12:43:02 +01:00
|
|
|
/** The packed values. */
|
2018-11-05 18:21:54 +01:00
|
|
|
private int[] packedValues;
|
2020-11-29 10:25:48 +01:00
|
|
|
|
2021-01-09 12:43:02 +01:00
|
|
|
/**
|
|
|
|
* Instantiates a new bit vector.
|
|
|
|
*
|
|
|
|
* @param netWidth the net width
|
|
|
|
*/
|
2015-01-09 09:16:40 +01:00
|
|
|
public BitVector(int netWidth) {
|
2021-01-09 12:43:02 +01:00
|
|
|
this.width = netWidth;
|
|
|
|
packedValues = new int[(netWidth + 15) / 16];
|
|
|
|
for (int i = 0; i < packedValues.length; i++)
|
|
|
|
packedValues[i] = 0;
|
2015-01-09 09:16:40 +01:00
|
|
|
}
|
|
|
|
|
2021-01-09 12:43:02 +01:00
|
|
|
/**
|
|
|
|
* Sets the value.
|
|
|
|
*
|
|
|
|
* @param i the i
|
|
|
|
* @param value the value
|
|
|
|
*/
|
2018-11-05 18:21:54 +01:00
|
|
|
public void setValue(int i, BitValue value) {
|
2021-01-09 12:43:02 +01:00
|
|
|
int bitIndex = i * 2;
|
2018-11-05 18:21:54 +01:00
|
|
|
int wordOffset = bitIndex >> 5;
|
|
|
|
int bitOffset = bitIndex & 31;
|
|
|
|
packedValues[wordOffset] &= ~(3 << bitOffset);
|
|
|
|
packedValues[wordOffset] |= value.ordinal() << bitOffset;
|
2015-01-09 09:16:40 +01:00
|
|
|
}
|
|
|
|
|
2021-01-09 12:43:02 +01:00
|
|
|
/**
|
|
|
|
* Gets the value.
|
|
|
|
*
|
|
|
|
* @return the value
|
|
|
|
*/
|
2015-01-09 09:16:40 +01:00
|
|
|
public char[] getValue() {
|
2020-11-29 10:25:48 +01:00
|
|
|
int bitOffset = 0;
|
|
|
|
int wordOffset = 0;
|
|
|
|
char[] res = new char[width];
|
|
|
|
// Copy values out of packed array
|
|
|
|
for (int i = 0; i < width; i++) {
|
2021-01-09 12:43:02 +01:00
|
|
|
int currentWord = (packedValues[wordOffset] >> bitOffset) & 3;
|
|
|
|
res[width - i - 1] = BitValue.fromInt(currentWord).toChar();
|
2020-11-29 10:25:48 +01:00
|
|
|
bitOffset += 2;
|
|
|
|
if (bitOffset == 32) {
|
|
|
|
wordOffset++;
|
|
|
|
bitOffset = 0;
|
|
|
|
}
|
|
|
|
}
|
2018-11-05 18:21:54 +01:00
|
|
|
return res;
|
2015-01-09 09:16:40 +01:00
|
|
|
}
|
|
|
|
|
2021-01-09 12:43:02 +01:00
|
|
|
/**
|
|
|
|
* Sets the value.
|
|
|
|
*
|
|
|
|
* @param value the new value
|
|
|
|
*/
|
2015-01-09 09:16:40 +01:00
|
|
|
public void setValue(char[] value) {
|
2018-11-05 18:21:54 +01:00
|
|
|
int bitIndex = width;
|
|
|
|
int wordOffset = bitIndex >> 4;
|
|
|
|
int bitOffset = (bitIndex * 2) % 32;
|
|
|
|
for (int i = Math.min(value.length, width) - 1; i >= 0; i--) {
|
|
|
|
packedValues[wordOffset] |= BitValue.fromChar(value[i]).ordinal() << bitOffset;
|
|
|
|
bitOffset += 2;
|
|
|
|
if (bitOffset == 32) {
|
|
|
|
wordOffset++;
|
|
|
|
bitOffset = 0;
|
|
|
|
}
|
|
|
|
}
|
2015-01-09 09:16:40 +01:00
|
|
|
}
|
|
|
|
|
2021-01-09 12:43:02 +01:00
|
|
|
/**
|
|
|
|
* Gets the width.
|
|
|
|
*
|
|
|
|
* @return the width
|
|
|
|
*/
|
2015-01-09 09:16:40 +01:00
|
|
|
public int getWidth() {
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
2021-01-09 12:43:02 +01:00
|
|
|
/**
|
|
|
|
* To string.
|
|
|
|
*
|
|
|
|
* @return the string
|
|
|
|
*/
|
|
|
|
public String toString() {
|
2018-11-05 18:21:54 +01:00
|
|
|
return new String(getValue());
|
2015-01-09 09:16:40 +01:00
|
|
|
}
|
2020-11-29 10:25:48 +01:00
|
|
|
|
2021-01-09 12:43:02 +01:00
|
|
|
/**
|
|
|
|
* To hex string.
|
|
|
|
*
|
|
|
|
* @return the string
|
|
|
|
*/
|
|
|
|
public String toHexString() {
|
|
|
|
int resWidth = (width - 1) / 4 + 1;
|
|
|
|
char[] value = getValue();
|
2015-01-09 09:16:40 +01:00
|
|
|
char[] res = new char[resWidth];
|
2021-01-09 12:43:02 +01:00
|
|
|
for (int i = resWidth - 1; i >= 0; i--) {
|
|
|
|
int digit = 0;
|
|
|
|
for (int j = 3; j >= 0; j--) {
|
|
|
|
if ((4 * i + j) < value.length) {
|
|
|
|
BitValue val = BitValue.fromChar(value[4 * i + j]);
|
|
|
|
switch (val) {
|
2020-11-29 10:25:48 +01:00
|
|
|
case X:
|
|
|
|
case Z:
|
2021-01-09 12:43:02 +01:00
|
|
|
res[i] = val.toChar();
|
2020-11-29 10:25:48 +01:00
|
|
|
continue;
|
|
|
|
case ONE:
|
2021-01-09 12:43:02 +01:00
|
|
|
digit += 1 << (3 - j);
|
2020-11-29 10:25:48 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2018-11-05 18:21:54 +01:00
|
|
|
}
|
2015-01-09 09:16:40 +01:00
|
|
|
}
|
2021-01-09 12:43:02 +01:00
|
|
|
res[i] = Character.forDigit(digit, 16); // ((digit < 10) ? '0' + digit : 'a' + digit -10)
|
2015-01-09 09:16:40 +01:00
|
|
|
}
|
2021-01-09 12:43:02 +01:00
|
|
|
return new String(res);
|
2015-01-09 09:16:40 +01:00
|
|
|
}
|
2020-11-29 10:25:48 +01:00
|
|
|
|
2021-01-09 12:43:02 +01:00
|
|
|
/**
|
|
|
|
* To unsigned value.
|
|
|
|
*
|
|
|
|
* @return the long
|
|
|
|
*/
|
2018-10-14 08:59:17 +02:00
|
|
|
public long toUnsignedValue() {
|
|
|
|
long res = 0;
|
2020-11-29 10:25:48 +01:00
|
|
|
int bitOffset = 0;
|
|
|
|
int wordOffset = 0;
|
|
|
|
int currentWord = 0;
|
|
|
|
// Copy values out of packed array
|
|
|
|
for (int i = 0; i < width; i++) {
|
2021-01-09 12:43:02 +01:00
|
|
|
if (bitOffset == 0)
|
|
|
|
currentWord = packedValues[wordOffset];
|
2018-11-05 18:21:54 +01:00
|
|
|
switch (currentWord & 3) {
|
|
|
|
case 1:
|
2021-01-09 12:43:02 +01:00
|
|
|
res |= 1 << i;
|
2018-10-14 08:59:17 +02:00
|
|
|
break;
|
2018-11-05 18:21:54 +01:00
|
|
|
case 2:
|
|
|
|
case 3:
|
2018-10-14 08:59:17 +02:00
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-11-29 10:25:48 +01:00
|
|
|
bitOffset += 2;
|
|
|
|
if (bitOffset == 32) {
|
|
|
|
wordOffset++;
|
|
|
|
bitOffset = 0;
|
|
|
|
} else {
|
|
|
|
currentWord >>= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
2018-10-14 08:59:17 +02:00
|
|
|
}
|
2020-11-29 10:25:48 +01:00
|
|
|
|
2021-01-09 12:43:02 +01:00
|
|
|
/**
|
|
|
|
* To signed value.
|
|
|
|
*
|
|
|
|
* @return the long
|
|
|
|
*/
|
2018-10-14 08:59:17 +02:00
|
|
|
public long toSignedValue() {
|
|
|
|
long res = 0;
|
2020-11-29 10:25:48 +01:00
|
|
|
int bitOffset = 0;
|
|
|
|
int wordOffset = 0;
|
|
|
|
int currentWord = 0;
|
2021-01-09 12:43:02 +01:00
|
|
|
int lastVal = 0;
|
2020-11-29 10:25:48 +01:00
|
|
|
// Copy values out of packed array
|
|
|
|
for (int i = 0; i < width; i++) {
|
2021-01-09 12:43:02 +01:00
|
|
|
if (bitOffset == 0)
|
|
|
|
currentWord = packedValues[wordOffset];
|
|
|
|
lastVal = 0;
|
2018-11-06 08:24:26 +01:00
|
|
|
switch (currentWord & 3) {
|
|
|
|
case 1:
|
2021-01-09 12:43:02 +01:00
|
|
|
res |= 1 << i;
|
|
|
|
lastVal = 1;
|
2018-11-06 08:24:26 +01:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
case 3:
|
|
|
|
return 0;
|
|
|
|
default:
|
2018-10-14 08:59:17 +02:00
|
|
|
}
|
2020-11-29 10:25:48 +01:00
|
|
|
bitOffset += 2;
|
|
|
|
if (bitOffset == 32) {
|
|
|
|
wordOffset++;
|
|
|
|
bitOffset = 0;
|
|
|
|
} else {
|
|
|
|
currentWord >>= 2;
|
|
|
|
}
|
|
|
|
}
|
2021-01-09 12:43:02 +01:00
|
|
|
if (lastVal != 0)
|
|
|
|
res |= -1l << width;
|
2018-11-06 08:24:26 +01:00
|
|
|
return res;
|
2018-10-14 08:59:17 +02:00
|
|
|
}
|
2020-11-28 10:22:22 +01:00
|
|
|
|
2021-01-09 12:43:02 +01:00
|
|
|
/**
|
|
|
|
* Gets the kind.
|
|
|
|
*
|
|
|
|
* @return the kind
|
|
|
|
*/
|
2020-11-28 10:22:22 +01:00
|
|
|
@Override
|
|
|
|
public EventKind getKind() {
|
|
|
|
return EventKind.SINGLE;
|
|
|
|
}
|
|
|
|
|
2021-01-09 12:43:02 +01:00
|
|
|
/**
|
|
|
|
* Gets the type.
|
|
|
|
*
|
|
|
|
* @return the type
|
|
|
|
*/
|
2020-11-28 10:22:22 +01:00
|
|
|
@Override
|
2020-11-28 14:08:34 +01:00
|
|
|
public WaveformType getType() {
|
|
|
|
return WaveformType.SIGNAL;
|
2020-11-28 10:22:22 +01:00
|
|
|
}
|
|
|
|
|
2021-01-09 12:43:02 +01:00
|
|
|
/**
|
|
|
|
* Duplicate.
|
|
|
|
*
|
|
|
|
* @return the i event
|
|
|
|
* @throws CloneNotSupportedException the clone not supported exception
|
|
|
|
*/
|
2020-11-28 10:22:22 +01:00
|
|
|
@Override
|
|
|
|
public IEvent duplicate() throws CloneNotSupportedException {
|
2021-01-09 12:43:02 +01:00
|
|
|
return (IEvent) this.clone();
|
2020-11-28 10:22:22 +01:00
|
|
|
}
|
2015-01-09 09:16:40 +01:00
|
|
|
}
|