Skip to content

Commit

Permalink
Swap internal enum mapping for efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
axelboc committed Aug 1, 2024
1 parent 6dde819 commit a41b681
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1720,8 +1720,8 @@ exports[`test file matches snapshot 1`] = `
},
"class": "Enumeration",
"mapping": {
"A": 0,
"B": 1,
"0": "A",
"1": "B",
},
},
"value": 1,
Expand Down Expand Up @@ -1753,8 +1753,8 @@ exports[`test file matches snapshot 1`] = `
},
"class": "Enumeration",
"mapping": {
"A": 256,
"B": 257,
"256": "A",
"257": "B",
},
},
"value": 256,
Expand Down Expand Up @@ -1789,9 +1789,9 @@ exports[`test file matches snapshot 1`] = `
},
"class": "Enumeration",
"mapping": {
"A": 0,
"B": 1,
"C": 2,
"0": "A",
"1": "B",
"2": "C",
},
},
"value": Uint8Array [
Expand Down Expand Up @@ -1838,9 +1838,9 @@ exports[`test file matches snapshot 1`] = `
},
"class": "Enumeration",
"mapping": {
"A": 0,
"B": 1,
"C": 2,
"0": "A",
"1": "B",
"2": "C",
},
},
"value": Uint8Array [
Expand Down
8 changes: 2 additions & 6 deletions packages/app/src/providers/hsds/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ import type {
} from '@h5web/shared/hdf5-models';
import { DTypeClass } from '@h5web/shared/hdf5-models';
import {
boolType,
compoundType,
cplxType,
enumType,
enumOrBoolType,
floatType,
intType,
isBoolEnumType,
strType,
uintType,
unknownType,
Expand Down Expand Up @@ -123,9 +121,7 @@ function convertHsdsCompoundType(
function convertHsdsEnumType(hsdsType: HsdsEnumType): EnumType | BooleanType {
const { base, mapping } = hsdsType;
assertHsdsNumericType(base);

const type = enumType(convertHsdsNumericType(base), mapping);
return isBoolEnumType(type) ? boolType() : type; // booleans stored as enums by h5py
return enumOrBoolType(convertHsdsNumericType(base), mapping);
}

export function convertHsdsType(hsdsType: HsdsType): DType {
Expand Down
20 changes: 10 additions & 10 deletions packages/h5wasm/src/__snapshots__/h5wasm-api.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1966,8 +1966,8 @@ exports[`test file matches snapshot 1`] = `
},
"class": "Enumeration",
"mapping": {
"A": 0,
"B": 1,
"0": "A",
"1": "B",
},
},
"value": 1,
Expand Down Expand Up @@ -1999,8 +1999,8 @@ exports[`test file matches snapshot 1`] = `
},
"class": "Enumeration",
"mapping": {
"A": 256,
"B": 257,
"256": "A",
"257": "B",
},
},
"value": 256,
Expand Down Expand Up @@ -2035,9 +2035,9 @@ exports[`test file matches snapshot 1`] = `
},
"class": "Enumeration",
"mapping": {
"A": 0,
"B": 1,
"C": 2,
"0": "A",
"1": "B",
"2": "C",
},
},
"value": Uint8Array [
Expand Down Expand Up @@ -2084,9 +2084,9 @@ exports[`test file matches snapshot 1`] = `
},
"class": "Enumeration",
"mapping": {
"A": 0,
"B": 1,
"C": 2,
"0": "A",
"1": "B",
"2": "C",
},
},
"value": Uint8Array [
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/hdf5-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export interface ArrayType<T extends DType = DType> {
export interface EnumType {
class: DTypeClass.Enum;
base: NumericType; // technically, only int/uint
mapping: Record<string, number>;
mapping: Record<number, string>;
}

export interface TimeType {
Expand Down
30 changes: 16 additions & 14 deletions packages/shared/src/hdf5-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,29 +143,31 @@ export function arrayType<T extends DType>(

export function enumType(
baseType: NumericType,
mapping: Record<string, number>,
hdf5Mapping: Record<string, number>,
): EnumType {
return { class: DTypeClass.Enum, base: baseType, mapping };
}

export function isBoolEnumType(type: EnumType): boolean {
const { mapping } = type;
return (
Object.keys(mapping).length === 2 &&
mapping.FALSE === 0 &&
mapping.TRUE === 1
);
return {
class: DTypeClass.Enum,
base: baseType,
// Swap mapping to optimise retrieval of enum keys from numeric values
mapping: Object.fromEntries(
Object.entries(hdf5Mapping).map(([k, v]) => [v, k]),
),
};
}

export function enumOrBoolType(
baseType: NumericType,
mapping: Record<string, number>,
hdf5Mapping: Record<string, number>,
): EnumType | BooleanType {
if (mapping.FALSE === 0 && mapping.TRUE === 1) {
if (
Object.keys(hdf5Mapping).length === 2 &&
hdf5Mapping.FALSE === 0 &&
hdf5Mapping.TRUE === 1
) {
return boolType();
}

return enumType(baseType, mapping);
return enumType(baseType, hdf5Mapping);
}

export function timeType(): TimeType {
Expand Down
7 changes: 2 additions & 5 deletions packages/shared/src/vis-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,9 @@ export function createComplexFormatter(
}

export function createEnumFormatter(
mapping: Record<string, number>,
mapping: Record<number, string>,
): ValueFormatter<EnumType> {
return (value) => {
const entry = Object.entries(mapping).find(([, v]) => value === v);
return entry?.[0] || value.toString();
};
return (value) => (value in mapping ? mapping[value] : value.toString());
}

export function getValues(arr: AnyNumArray): NumArray {
Expand Down

0 comments on commit a41b681

Please sign in to comment.