Skip to content

Commit

Permalink
Add support for Array of Complex
Browse files Browse the repository at this point in the history
  • Loading branch information
byrnHDF committed Dec 19, 2024
1 parent 2f5e217 commit 9fbe31f
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,7 @@ public Object canonicalToDisplayValue(ILayerCell cell, IConfigRegistry configReg
{
cellRowIdx = cell.getRowIndex();
cellColIdx = cell.getColumnIndex();
log.trace("canonicalToDisplayValue({}): cellRowIdx={} cellRowIdx={}", value, cellRowIdx, cellRowIdx);
return canonicalToDisplayValue(value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,10 @@ else if (baseType.isBitField() || baseType.isOpaque())
else
arraySize = dtype.getDatatypeSize() / baseType.getDatatypeSize();

arrayElements = new Object[(int)arraySize];
if (baseType.isComplex())
arrayElements = new Object[(int) arraySize][2];
else
arrayElements = new Object[(int) arraySize];

if (baseTypeDataProvider instanceof CompoundDataProvider)
nCols = (int)arraySize * ((CompoundDataProvider)baseTypeDataProvider).nCols;
Expand All @@ -961,6 +964,7 @@ else if (baseType.isBitField() || baseType.isOpaque())
@Override
public Object getDataValue(int columnIndex, int rowIndex)
{
log.trace("getDataValue(rowIndex={}, columnIndex={}): start", rowIndex, columnIndex);
try {
int bufIndex = physicalLocationToBufIndex(rowIndex, columnIndex);

Expand All @@ -978,6 +982,12 @@ else if (baseTypeDataProvider instanceof ArrayDataProvider) {
*/
theValue = retrieveArrayOfArrayElements(dataBuf, columnIndex, bufIndex);
}
else if (baseTypeDataProvider instanceof ComplexDataProvider) {
/*
* TODO: assign to global arrayElements.
*/
theValue = retrieveArrayOfComplexElements(dataBuf, columnIndex, bufIndex);
}
else {
/*
* TODO: assign to global arrayElements.
Expand All @@ -998,6 +1008,7 @@ else if (baseTypeDataProvider instanceof ArrayDataProvider) {
@Override
public Object getDataValue(Object obj, int columnIndex, int rowIndex)
{
log.trace("getDataValue(obj={} rowIndex={}, columnIndex={}): start", obj, rowIndex, columnIndex);
try {
long index = rowIndex * arraySize;

Expand All @@ -1010,6 +1021,9 @@ public Object getDataValue(Object obj, int columnIndex, int rowIndex)
else if (baseTypeDataProvider instanceof ArrayDataProvider) {
theValue = retrieveArrayOfArrayElements(obj, columnIndex, (int)index);
}
else if (baseTypeDataProvider instanceof ComplexDataProvider) {
theValue = retrieveArrayOfComplexElements(obj, columnIndex, (int) index);
}
else {
theValue = retrieveArrayOfAtomicElements(obj, (int)index);
}
Expand All @@ -1019,6 +1033,9 @@ else if (baseTypeDataProvider instanceof ArrayDataProvider) {
theValue = DataFactoryUtils.errStr;
}

log.trace("getDataValue(obj={}, rowIndex={}, columnIndex={})=({}): finish", obj, rowIndex, columnIndex,
theValue);

return theValue;
}

Expand Down Expand Up @@ -1048,12 +1065,32 @@ private Object[] retrieveArrayOfArrayElements(Object objBuf, int columnIndex, in
return tempArray;
}

private Object[] retrieveArrayOfComplexElements(Object objBuf, int columnIndex, int rowStartIdx)
{
Object[] tempArray = new Object[(int) arraySize];

log.trace("retrieveArrayOfComplexElements(obj={}, rowIndex={}, columnIndex={}) arraySize={}: start", objBuf,
rowStartIdx, columnIndex, arraySize);

for (int i = 0; i < arraySize; i++) {
tempArray[i] = baseTypeDataProvider.getDataValue(objBuf, columnIndex, rowStartIdx + i * 2);
log.trace("retrieveArrayOfComplexElements(tempArray[{}]={}): start", i, tempArray);
}

return tempArray;
}

private Object[] retrieveArrayOfAtomicElements(Object objBuf, int rowStartIdx)
{
Object[] tempArray = new Object[(int)arraySize];

for (int i = 0; i < arraySize; i++)
log.trace("retrieveArrayOfAtomicElements(obj={}, rowIndex={}) arraySize={}: start", objBuf, rowStartIdx,
arraySize);

for (int i = 0; i < arraySize; i++) {
tempArray[i] = baseTypeDataProvider.getDataValue(objBuf, rowStartIdx + i);
log.trace("retrieveArrayOfAtomicElements(obj[{}]={}): start", i, tempArray[i]);
}

return tempArray;
}
Expand Down Expand Up @@ -1122,6 +1159,8 @@ private void updateArrayElements(Object curBuf, Object newValue, int columnIndex
updateArrayOfCompoundElements(st, curBuf, columnIndex, bufStartIndex);
else if (baseTypeDataProvider instanceof ArrayDataProvider)
updateArrayOfArrayElements(st, curBuf, columnIndex, bufStartIndex);
// else if (baseTypeDataProvider instanceof ComplexDataProvider)
// updateArrayOfComplexElements(st, curBuf, columnIndex, bufStartIndex);
else
updateArrayOfAtomicElements(st, curBuf, bufStartIndex);
}
Expand Down Expand Up @@ -1150,6 +1189,15 @@ private void updateArrayOfArrayElements(StringTokenizer tokenizer, Object curBuf
}
}

private void updateArrayOfComplexElements(StringTokenizer tokenizer, Object curBuf, int columnIndex,
int bufStartIndex)
{
for (int i = 0; i < arraySize; i++) {
baseTypeDataProvider.setDataValue(columnIndex, bufStartIndex + i, curBuf, tokenizer.nextToken().trim());
isValueChanged = isValueChanged || baseTypeDataProvider.getIsValueChanged();
}
}

private void updateArrayOfAtomicElements(StringTokenizer tokenizer, Object curBuf, int bufStartIndex)
{
for (int i = 0; i < arraySize; i++) {
Expand Down Expand Up @@ -2000,6 +2048,30 @@ public Object getDataValue(int columnIndex, int rowIndex)
return theValue;
}

@Override
public Object getDataValue(Object obj, int columnIndex, int rowIndex)
{
log.trace("getDataValue(obj={}, rowIndex={}, columnIndex={}): start", obj, rowIndex, columnIndex);
try {
theValue = retrieveArrayOfAtomicElements(obj, rowIndex);
}
catch (Exception ex) {
log.debug("getDataValue(rowIndex={}, columnIndex={}): failure: ", rowIndex, columnIndex, ex);
theValue = DataFactoryUtils.errStr;
}

log.trace("getDataValue(obj[[{}, [{}]=({}): finish", obj, rowIndex, columnIndex, theValue);

return theValue;
}

@Override
public Object getDataValue(Object obj, int index)
{
throw new UnsupportedOperationException(
"getDataValue(Object, int) should not be called for CompoundDataProviders");
}

private Object[] retrieveArrayOfAtomicElements(Object objBuf, int rowStartIdx)
{
log.debug("retrieveArrayOfAtomicElements(): objBuf={}", objBuf);
Expand All @@ -2012,7 +2084,7 @@ private Object[] retrieveArrayOfAtomicElements(Object objBuf, int rowStartIdx)
tempArray[0] = baseTypeDataProvider.getDataValue(objBuf, rowStartIdx);
tempArray[1] = baseTypeDataProvider.getDataValue(objBuf, rowStartIdx + 1);

log.debug("retrieveArrayOfAtomicElements(): tempArray={}", tempArray);
log.debug("retrieveArrayOfAtomicElements(): tempArray[0]={} tempArray[1]={}", tempArray[0], tempArray[1]);
return tempArray;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ else if (dtype.isOpaque() || dtype.isBitField())
validator = new BitfieldDataValidator(dtype);
else if (dtype.isRef())
validator = new RefDataValidator(dtype);
else if (dtype.isComplex())
validator = new ComplexDataValidator(dtype);
}
catch (Exception ex) {
log.debug("getDataValidator(Datatype): failed to retrieve a DataValidator: ", ex);
Expand Down Expand Up @@ -808,4 +810,59 @@ private static class RefDataValidator extends HDFDataValidator {

RefDataValidator(final Datatype dtype) { super(dtype); }
}

private static class ComplexDataValidator extends HDFDataValidator {
private static final Logger log = LoggerFactory.getLogger(ComplexDataValidator.class);

private final HDFDataValidator baseValidator;

ComplexDataValidator(final Datatype dtype) throws Exception {
super(dtype);

if (!dtype.isComplex()) {
log.debug("datatype is not a complex type: exit");
throw new Exception("ComplexDataValidator: datatype is not a complex type");
}

Datatype baseType = dtype.getDatatypeBase();
if (baseType == null) {
log.debug("base datatype is null: exit");
throw new Exception("ComplexDataValidator: base datatype is null");
}
if (!baseType.isFloat()) {
log.debug("base datatype is not a float type: exit");
throw new Exception("ComplexDataValidator: datatype is not a float type");
}

log.trace("base Datatype is {}", dtype.getDescription());

try {
baseValidator = getDataValidator(baseType);
}
catch (Exception ex) {
log.debug("couldn't get DataValidator for base datatype: exit: ", ex);
throw new Exception("couldn't get DataValidator for base datatype: " + ex.getMessage());
}
}

@Override
public boolean validate(int colIndex, int rowIndex, Object newValue)
{
log.trace("validate({}, {}, {}): start", rowIndex, colIndex, newValue);

try {
super.checkValidValue(newValue);

baseValidator.validate(colIndex, rowIndex, newValue);
}
catch (Exception ex) {
super.throwValidationFailedException(rowIndex, colIndex, newValue, ex.toString());
}
finally {
log.trace("validate({}, {}, {}): finish", rowIndex, colIndex, newValue);
}

return true;
}
}
}

0 comments on commit 9fbe31f

Please sign in to comment.