Skip to content

Commit

Permalink
Merge pull request #1748 from silx-kit/split-vlen
Browse files Browse the repository at this point in the history
Split `VLenType` out of `ArrayType`
  • Loading branch information
axelboc authored Feb 3, 2025
2 parents 85e418b + 5bb616f commit 8f5eb7c
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 38 deletions.
7 changes: 5 additions & 2 deletions packages/app/src/providers/h5grove/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
strType,
timeType,
unknownType,
vlenType,
} from '@h5web/shared/hdf5-utils';
import { describe, expect, it } from 'vitest';

Expand Down Expand Up @@ -94,15 +95,17 @@ describe('parseDType', () => {
).toStrictEqual(boolType(intType(true, 8)));
});

it('should convert array types', () => {
it('should convert vlen type', () => {
expect(
parseDType({
class: 9,
size: 1,
base: { class: 1, size: 4, order: 0 },
}),
).toStrictEqual(arrayType(floatType()));
).toStrictEqual(vlenType(floatType()));
});

it('should convert array type', () => {
expect(
parseDType({
class: 10,
Expand Down
3 changes: 2 additions & 1 deletion packages/app/src/providers/h5grove/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
strType,
timeType,
unknownType,
vlenType,
} from '@h5web/shared/hdf5-utils';
import {
type BigIntTypedArrayConstructor,
Expand Down Expand Up @@ -214,7 +215,7 @@ export function parseDType(type: H5GroveType): DType {
}

if (h5tClass === H5T_CLASS.VLEN) {
return arrayType(parseDType(type.base));
return vlenType(parseDType(type.base));
}

// `H5GroveType` doesn't allow for any other `h5tClass` value at this point,
Expand Down
10 changes: 8 additions & 2 deletions packages/app/src/providers/hsds/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export interface HsdsShape {
export type HsdsType =
| HsdsNumericType
| HsdsStringType
| HsdsVLenType
| HsdsArrayType
| HsdsCompoundType
| HsdsEnumType;
Expand All @@ -110,10 +111,15 @@ export interface HsdsStringType {
length: number | 'H5T_VARIABLE';
}

export interface HsdsVLenType {
class: 'H5T_VLEN';
base: HsdsType;
}

export interface HsdsArrayType {
class: 'H5T_ARRAY' | 'H5T_VLEN';
class: 'H5T_ARRAY';
base: HsdsType;
dims?: number[];
dims: number[];
}

export interface HsdsCompoundType {
Expand Down
27 changes: 13 additions & 14 deletions packages/app/src/providers/hsds/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
intType,
strType,
unknownType,
vlenType,
} from '@h5web/shared/hdf5-utils';
import { describe, expect, it } from 'vitest';

Expand All @@ -18,6 +19,7 @@ import {
type HsdsEnumType,
type HsdsStringType,
type HsdsType,
type HsdsVLenType,
} from './models';
import { convertHsdsType } from './utils';

Expand Down Expand Up @@ -83,30 +85,27 @@ describe('convertHsdsType', () => {
expect(convertHsdsType(beFloat.hsds)).toStrictEqual(beFloat.hdf5);
});

it('should convert the base of Array type', () => {
const arr: HsdsArrayType = {
class: 'H5T_ARRAY',
it('should convert the base of VLen type', () => {
const vlen: HsdsVLenType = {
class: 'H5T_VLEN',
base: leInt.hsds,
dims: [4, 5],
};

expect(convertHsdsType(arr)).toStrictEqual(arrayType(leInt.hdf5, [4, 5]));
expect(convertHsdsType(vlen)).toStrictEqual(vlenType(leInt.hdf5));
});

it('should convert the base of VLen type', () => {
const vlen: HsdsArrayType = {
class: 'H5T_VLEN',
it('should convert the base of Array type', () => {
const arr: HsdsArrayType = {
class: 'H5T_ARRAY',
base: leInt.hsds,
dims: [4, 5],
};

expect(convertHsdsType(vlen)).toStrictEqual(arrayType(leInt.hdf5));
expect(convertHsdsType(arr)).toStrictEqual(arrayType(leInt.hdf5, [4, 5]));
});

it('should convert the field types of Compound type', () => {
const vlen: HsdsArrayType = {
class: 'H5T_VLEN',
base: leInt.hsds,
};
const vlen: HsdsVLenType = { class: 'H5T_VLEN', base: leInt.hsds };
const compound: HsdsCompoundType = {
class: 'H5T_COMPOUND',
fields: [
Expand All @@ -117,7 +116,7 @@ describe('convertHsdsType', () => {
expect(convertHsdsType(compound)).toStrictEqual(
compoundType({
f1: beFloat.hdf5,
f2: arrayType(leInt.hdf5),
f2: vlenType(leInt.hdf5),
}),
);
});
Expand Down
14 changes: 6 additions & 8 deletions packages/app/src/providers/hsds/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
type CompoundType,
type Dataset,
type DType,
DTypeClass,
type Entity,
type EnumType,
type Group,
Expand All @@ -17,13 +16,15 @@ import {
type Shape,
} from '@h5web/shared/hdf5-models';
import {
arrayType,
compoundType,
cplxType,
enumOrBoolType,
floatType,
intType,
strType,
unknownType,
vlenType,
} from '@h5web/shared/hdf5-utils';

import {
Expand Down Expand Up @@ -139,14 +140,11 @@ export function convertHsdsType(hsdsType: HsdsType): DType {
);
}

case 'H5T_ARRAY':
case 'H5T_VLEN':
return {
class:
hsdsType.class === 'H5T_ARRAY' ? DTypeClass.Array : DTypeClass.VLen,
base: convertHsdsType(hsdsType.base),
...(hsdsType.dims && { dims: hsdsType.dims }),
};
return vlenType(convertHsdsType(hsdsType.base));

case 'H5T_ARRAY':
return arrayType(convertHsdsType(hsdsType.base), hsdsType.dims);

case 'H5T_ENUM':
return convertHsdsEnumType(hsdsType);
Expand Down
5 changes: 3 additions & 2 deletions packages/app/src/providers/mock/mock-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
printableCompoundType,
strType,
unknownType,
vlenType,
} from '@h5web/shared/hdf5-utils';
import {
array,
Expand Down Expand Up @@ -87,9 +88,9 @@ export function makeMockFile(): GroupWithChildren {
],
}),
scalar('scalar_vlen', [1, 2, 3], {
type: arrayType(intType()),
type: vlenType(intType()),
attributes: [
scalarAttr('attr', [1, 2, 3], { type: arrayType(intType()) }),
scalarAttr('attr', [1, 2, 3], { type: vlenType(intType()) }),
],
}),
scalar('scalar_enum', 2, {
Expand Down
3 changes: 2 additions & 1 deletion packages/h5wasm/src/worker-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
strType,
timeType,
unknownType,
vlenType,
} from '@h5web/shared/hdf5-utils';
import {
type Dataset as H5WasmDataset,
Expand Down Expand Up @@ -279,7 +280,7 @@ function parseDType(metadata: Metadata): DType {
if (h5tClass === H5T_CLASS.VLEN) {
const { vlen_type } = metadata;
assertDefined(vlen_type);
return arrayType(parseDType(vlen_type));
return vlenType(parseDType(vlen_type));
}

if (h5tClass === H5T_CLASS.ARRAY) {
Expand Down
10 changes: 8 additions & 2 deletions packages/shared/src/hdf5-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export type DType =
| PrintableType
| CompoundType
| ArrayType
| VLenType
| TimeType
| BitfieldType
| OpaqueType
Expand Down Expand Up @@ -166,9 +167,14 @@ export interface CompoundType<T extends DType = DType> {
}

export interface ArrayType<T extends DType = DType> {
class: DTypeClass.Array | DTypeClass.VLen;
class: DTypeClass.Array;
base: T;
dims: number[];
}

export interface VLenType<T extends DType = DType> {
class: DTypeClass.VLen;
base: T;
dims?: number[];
}

export interface BooleanType {
Expand Down
13 changes: 7 additions & 6 deletions packages/shared/src/hdf5-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
type StringType,
type TimeType,
type UnknownType,
type VLenType,
} from './hdf5-models';

export function getChildEntity(
Expand Down Expand Up @@ -114,15 +115,15 @@ export function compoundOrCplxType<T extends DType>(
return compoundType(fields);
}

export function vlenType<T extends DType>(baseType: T): VLenType<T> {
return { class: DTypeClass.VLen, base: baseType };
}

export function arrayType<T extends DType>(
baseType: T,
dims?: number[],
dims: number[],
): ArrayType<T> {
return {
class: dims ? DTypeClass.Array : DTypeClass.VLen,
base: baseType,
...(dims && { dims }),
};
return { class: DTypeClass.Array, base: baseType, dims };
}

export function boolType(baseType: NumericType): BooleanType {
Expand Down

0 comments on commit 8f5eb7c

Please sign in to comment.