From cbcdc4ace6d6af4d75a16b2f3e248e34dfdcd2f8 Mon Sep 17 00:00:00 2001 From: Francesco Trotta Date: Fri, 12 Jul 2024 00:12:40 +0200 Subject: [PATCH] `MASK_EMPTY` --- packages/~feature-hub/src/feature.ts | 18 +++++------ packages/~feature-hub/src/index.ts | 6 ++-- packages/~feature-hub/src/mask-impl-52.ts | 9 ++---- packages/~feature-hub/src/mask-impl-64.ts | 9 ++---- .../~feature-hub/test/spec/feature.spec.ts | 12 +++---- .../~feature-hub/test/spec/mask-index.spec.ts | 12 +++---- packages/~feature-hub/test/spec/mask.spec.ts | 17 +++++----- src/lib/debug.js | 32 +++++++++---------- src/lib/encoder/encoder-base.js | 4 +-- 9 files changed, 54 insertions(+), 65 deletions(-) diff --git a/packages/~feature-hub/src/feature.ts b/packages/~feature-hub/src/feature.ts index 476765fd..aed8264d 100644 --- a/packages/~feature-hub/src/feature.ts +++ b/packages/~feature-hub/src/feature.ts @@ -1,6 +1,6 @@ import type util from 'node:util'; -import { type Mask, maskAreEqual, maskIncludes, maskIntersection, maskNew, maskNext, maskUnion } +import { MASK_EMPTY, type Mask, maskAreEqual, maskIncludes, maskIntersection, maskNext, maskUnion } from './mask-impl'; import { MaskSet } from './mask-index'; @@ -138,7 +138,7 @@ FeatureConstructor function Feature(this: Feature, ...features: FeatureElementOrCompatibleArray[]): Feature { - let mask = maskNew(); + let mask = MASK_EMPTY; for (const feature of features) { const otherMask = validMaskFromArrayOrStringOrFeature(feature); @@ -156,7 +156,7 @@ FeatureConstructor { if (isMaskCompatible(mask)) { - let includedMask = maskNew(); + let includedMask = MASK_EMPTY; for (const { mask: featureMask } of ELEMENTARY) { if (maskIncludes(mask, featureMask)) @@ -174,7 +174,7 @@ FeatureConstructor function _getMask(feature?: FeatureElementOrCompatibleArray): Mask { const mask = - feature !== undefined ? validMaskFromArrayOrStringOrFeature(feature) : maskNew(); + feature !== undefined ? validMaskFromArrayOrStringOrFeature(feature) : MASK_EMPTY; return mask; } @@ -267,7 +267,7 @@ FeatureConstructor function featureArrayLikeToMask(features: ArrayLike): Mask { - let mask = maskNew(); + let mask = MASK_EMPTY; const { length } = features; for (let index = 0; index < length; ++index) { @@ -382,7 +382,7 @@ FeatureConstructor { const { mask } = this; const names: string[] = []; - let includedMask = maskNew(); + let includedMask = MASK_EMPTY; for (let index = PRISTINE_ELEMENTARY.length; index--;) { const featureObj = PRISTINE_ELEMENTARY[index]; @@ -515,7 +515,7 @@ FeatureConstructor } else { - mask = maskNew(); + mask = MASK_EMPTY; wrappedCheck = null; } { @@ -663,7 +663,7 @@ FeatureConstructor const featureNames = _Object_keys(featureInfos); const includeSetMap = createMap<{ readonly [FeatureName in string]: null; }>(); const familiesMap = createMap(); - let unionMask = maskNew(); + let unionMask = MASK_EMPTY; featureNames.forEach(completeFeature); completeExclusions(); @@ -695,7 +695,7 @@ function esToString(name: unknown): string export function featuresToMask(featureObjs: readonly Feature[]): Mask { const mask = - featureObjs.reduce((mask, featureObj): Mask => maskUnion(mask, featureObj.mask), maskNew()); + featureObjs.reduce((mask, featureObj): Mask => maskUnion(mask, featureObj.mask), MASK_EMPTY); return mask; } diff --git a/packages/~feature-hub/src/index.ts b/packages/~feature-hub/src/index.ts index 5bdc1308..19fb6d89 100644 --- a/packages/~feature-hub/src/index.ts +++ b/packages/~feature-hub/src/index.ts @@ -1,3 +1,3 @@ -export * from './feature'; -export { maskAreEqual, maskIncludes, maskIntersection, maskNew, maskUnion } from './mask-impl'; -export * from './mask-index'; +export * from './feature'; +export { MASK_EMPTY, maskAreEqual, maskIncludes, maskIntersection, maskUnion } from './mask-impl'; +export * from './mask-index'; diff --git a/packages/~feature-hub/src/mask-impl-52.ts b/packages/~feature-hub/src/mask-impl-52.ts index d873e8a1..8edc88ba 100644 --- a/packages/~feature-hub/src/mask-impl-52.ts +++ b/packages/~feature-hub/src/mask-impl-52.ts @@ -5,7 +5,8 @@ const BIN_POW_32 = 0x1_0000_0000; const BIN_POW_51 = 0x8_0000_0000_0000; const BIT_MASK_31 = 0x7fff_ffff; -const EMPTY_MASK: Mask = 0 as never; +/** An empty mask. */ +export const MASK_EMPTY: Mask = 0 as never; /** The maximum number of disjoint, non-empty masks supported by this implementation. */ export const MASK_MAX_SIZE = 52 as number; @@ -46,12 +47,6 @@ export function maskIntersection(mask1: Mask, mask2: Mask): Mask return intersectionMask; } -/** Returns a new empty mask. */ -export function maskNew(): Mask -{ - return EMPTY_MASK; -} - /** * Returns a new non-empty mask that does not intersect the specified mask. * diff --git a/packages/~feature-hub/src/mask-impl-64.ts b/packages/~feature-hub/src/mask-impl-64.ts index 79414558..d45a1197 100644 --- a/packages/~feature-hub/src/mask-impl-64.ts +++ b/packages/~feature-hub/src/mask-impl-64.ts @@ -7,6 +7,9 @@ type LoHi = readonly [number, number]; const freezeMask = (_LoHi: LoHi): Mask => Object.freeze(_LoHi) as unknown as Mask; +/** An empty mask. */ +export const MASK_EMPTY = freezeMask([0, 0]); + /** The maximum number of disjoint, non-empty masks supported by this implementation. */ export const MASK_MAX_SIZE = 64 as number; @@ -42,12 +45,6 @@ export function maskIntersection(mask1: Mask, mask2: Mask): Mask return freezeMask(intersectionLoHi); } -/** Returns a new empty mask. */ -export function maskNew(): Mask -{ - return freezeMask([0, 0]); -} - /** * Returns a new non-empty mask that does not intersect the specified mask. * diff --git a/packages/~feature-hub/test/spec/feature.spec.ts b/packages/~feature-hub/test/spec/feature.spec.ts index 27fd8f86..d7d47cd3 100644 --- a/packages/~feature-hub/test/spec/feature.spec.ts +++ b/packages/~feature-hub/test/spec/feature.spec.ts @@ -10,7 +10,7 @@ import } from '../../src/feature'; -import { type Mask, maskAreEqual, maskIntersection, maskNew, maskNext, maskUnion } +import { MASK_EMPTY, type Mask, maskAreEqual, maskIntersection, maskNext, maskUnion } from '../../src/mask-impl'; import assert from 'assert'; @@ -54,10 +54,10 @@ it ); const { RED, RED1, RED2, GREEN, FOO, BAR } = Feature.ALL; assert(maskAreEqual(RED.mask, featuresToMask([RED1, RED2]))); - assert(maskAreEqual(maskIntersection(RED1.mask, RED2.mask), maskNew())); - assert(!maskAreEqual(RED1.mask, maskNew())); - assert(!maskAreEqual(RED2.mask, maskNew())); - assert(maskAreEqual(maskIntersection(RED.mask, GREEN.mask), maskNew())); + assert(maskAreEqual(maskIntersection(RED1.mask, RED2.mask), MASK_EMPTY)); + assert(!maskAreEqual(RED1.mask, MASK_EMPTY)); + assert(!maskAreEqual(RED2.mask, MASK_EMPTY)); + assert(maskAreEqual(maskIntersection(RED.mask, GREEN.mask), MASK_EMPTY)); assert(maskAreEqual(FOO.mask, featuresToMask([RED1, GREEN]))); assert(maskAreEqual(BAR.mask, featuresToMask([RED2, GREEN]))); assert.strictEqual(RED.elementary, true); @@ -383,7 +383,7 @@ it const Feature = createFeatureClass({ FOO: { check: noop }, BAR: { check: noop } }); { const actual = Feature._getMask(undefined); - assert(maskAreEqual(actual, maskNew()), `Actual value is:\n${actual as unknown}`); + assert(maskAreEqual(actual, MASK_EMPTY), `Actual value is:\n${actual as unknown}`); } { const actual = Feature._getMask('FOO'); diff --git a/packages/~feature-hub/test/spec/mask-index.spec.ts b/packages/~feature-hub/test/spec/mask-index.spec.ts index 18965be8..971e90ad 100644 --- a/packages/~feature-hub/test/spec/mask-index.spec.ts +++ b/packages/~feature-hub/test/spec/mask-index.spec.ts @@ -1,4 +1,4 @@ -import { maskNew } from '../../src/mask-impl'; +import { MASK_EMPTY } from '../../src/mask-impl'; import { MaskMap, MaskSet } from '../../src/mask-index'; import assert from 'assert'; @@ -18,7 +18,7 @@ describe (): void => { const map = new MaskMap(); - const mask = maskNew(); + const mask = MASK_EMPTY; map.set(mask, 42); const actualValue = map.get(mask); assert.strictEqual(actualValue, 42); @@ -31,7 +31,7 @@ describe (): void => { const map = new MaskMap(); - const mask = maskNew(); + const mask = MASK_EMPTY; map.set(mask, 'foo'); map.set(mask, 'bar'); const actualValue = map.get(mask); @@ -47,7 +47,7 @@ describe (): void => { const map = new MaskMap(); - const mask = maskNew(); + const mask = MASK_EMPTY; assert.strictEqual(map.size, 0); map.set(mask, true); map.set(mask, undefined); @@ -68,7 +68,7 @@ describe (): void => { const set = new MaskSet(); - const mask = maskNew(); + const mask = MASK_EMPTY; set.add(mask); const actualValue = set.has(mask); assert.strictEqual(actualValue, true); @@ -81,7 +81,7 @@ describe (): void => { const set = new MaskSet(); - const mask = maskNew(); + const mask = MASK_EMPTY; assert.strictEqual(set.size, 0); set.add(mask); set.add(mask); diff --git a/packages/~feature-hub/test/spec/mask.spec.ts b/packages/~feature-hub/test/spec/mask.spec.ts index fda25ba9..20ba8a35 100644 --- a/packages/~feature-hub/test/spec/mask.spec.ts +++ b/packages/~feature-hub/test/spec/mask.spec.ts @@ -1,11 +1,11 @@ import { + MASK_EMPTY, MASK_MAX_SIZE, type Mask, maskAreEqual, maskIncludes, maskIntersection, - maskNew, maskNext, maskUnion, } @@ -24,7 +24,7 @@ never function assertMaskEmpty(actual: Mask): void { - const expected = maskNew(); + const expected = MASK_EMPTY; if (!maskAreEqual(actual, expected)) { const message = `Expected ${formatMask(actual)} to be empty`; @@ -52,7 +52,7 @@ function assertMaskInclude(actual: Mask, includedMask: Mask): void function assertMaskNotEmpty(actual: Mask): void { - const expected = maskNew(); + const expected = MASK_EMPTY; if (maskAreEqual(actual, expected)) { const message = `Expected ${formatMask(actual)} to be non-empty`; @@ -68,11 +68,10 @@ function formatMask(mask: Mask): string it ( - 'maskNew', + 'MASK_EMPTY', (): void => { - const newMask = maskNew(); - assertMaskEmpty(newMask); + assertMaskEmpty(MASK_EMPTY); }, ); @@ -81,7 +80,7 @@ it 'maskNext', (): void => { - let prevMask = maskNew(); + let prevMask = MASK_EMPTY; for (let count = 0; count < MASK_MAX_SIZE; ++count) { const nextMask = maskNext(prevMask); @@ -100,7 +99,7 @@ it 'maskUnion', (): void => { - let prevMask = maskNew(); + let prevMask = MASK_EMPTY; for (let count = 0; count < 52; ++count) { const nextMask = maskNext(prevMask); @@ -117,7 +116,7 @@ it 'maskIntersection', (): void => { - const mask0 = maskNew(); + const mask0 = MASK_EMPTY; let prevHighMask = mask0; let lowMask = maskNext(mask0); for (let count = 0; count < 51; ++count) diff --git a/src/lib/debug.js b/src/lib/debug.js index e1f3449b..652be020 100644 --- a/src/lib/debug.js +++ b/src/lib/debug.js @@ -1,7 +1,7 @@ /* global DEBUG */ -import createClusteringPlan from './clustering-plan'; -import { define } from './definers'; +import createClusteringPlan from './clustering-plan'; +import { define } from './definers'; import { BASE64_ALPHABET_HI_4, @@ -17,11 +17,11 @@ import OPTIMAL_RETURN_STRING, } from './definitions'; -import { Encoder } from './encoder/encoder-base'; -import { STRATEGIES, createReindexMap } from './encoder/encoder-ext'; -import { Feature } from './features'; -import createFigurator from './figurator'; -import { JScrewIt, isEncoderInCache } from './jscrewit-base'; +import { Encoder } from './encoder/encoder-base'; +import { STRATEGIES, createReindexMap } from './encoder/encoder-ext'; +import { Feature } from './features'; +import createFigurator from './figurator'; +import { JScrewIt, isEncoderInCache } from './jscrewit-base'; import { _Array_isArray, @@ -35,11 +35,11 @@ import esToString, } from './obj-utils'; -import { ScrewBuffer, optimizeSolutions } from './screw-buffer'; -import { DynamicSolution, SimpleSolution } from './solution'; -import trimJS from './trim-js'; -import { MaskMap, MaskSet, maskIncludes, maskNew, maskUnion } from '~feature-hub'; -import { SolutionType, calculateSolutionType } from '~solution'; +import { ScrewBuffer, optimizeSolutions } from './screw-buffer'; +import { DynamicSolution, SimpleSolution } from './solution'; +import trimJS from './trim-js'; +import { MASK_EMPTY, MaskMap, MaskSet, maskIncludes, maskUnion } from '~feature-hub'; +import { SolutionType, calculateSolutionType } from '~solution'; if (typeof NO_DEBUG === 'undefined') { @@ -72,15 +72,15 @@ if (typeof NO_DEBUG === 'undefined') if (_Array_isArray(inputEntries)) outputEntries = inputEntries.map(cloneEntry); else - outputEntries = [createEntryClone(inputEntries, EMPTY_MASK)]; + outputEntries = [createEntryClone(inputEntries, MASK_EMPTY)]; } return outputEntries; } function cloneEntry(inputEntry) { - var entry = createEntryClone(inputEntry.definition, inputEntry.mask); - return entry; + var outputEntry = createEntryClone(inputEntry.definition, inputEntry.mask); + return outputEntry; } function createEncoder(features) @@ -163,8 +163,6 @@ if (typeof NO_DEBUG === 'undefined') return STRATEGIES; } - var EMPTY_MASK = maskNew(); - // Miscellaneous entries var ENTRIES = createEmpty(); ENTRIES['BASE64_ALPHABET_HI_4:0'] = BASE64_ALPHABET_HI_4[0]; diff --git a/src/lib/encoder/encoder-base.js b/src/lib/encoder/encoder-base.js index 60e2c987..2c06ee39 100644 --- a/src/lib/encoder/encoder-base.js +++ b/src/lib/encoder/encoder-base.js @@ -35,14 +35,14 @@ import replaceCharByAtob from './replace- import replaceCharByCharCode from './replace-char-by-char-code'; import replaceCharByEscSeq from './replace-char-by-esc-seq'; import replaceCharByUnescape from './replace-char-by-unescape'; -import { maskIncludes, maskNew } from '~feature-hub'; +import { MASK_EMPTY, maskIncludes } from '~feature-hub'; import { SolutionType } from '~solution'; var ATOB_MASK = Feature.ATOB.mask; var STATIC_CHAR_CACHE = createEmpty(); var STATIC_CONST_CACHE = createEmpty(); -var STATIC_ENCODER = new Encoder(maskNew()); +var STATIC_ENCODER = new Encoder(MASK_EMPTY); var BOND_STRENGTH_NONE = 0; var BOND_STRENGTH_WEAK = 1;