Skip to content

Commit

Permalink
MASK_EMPTY
Browse files Browse the repository at this point in the history
  • Loading branch information
fasttime committed Jul 11, 2024
1 parent d4ffb7c commit cbcdc4a
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 65 deletions.
18 changes: 9 additions & 9 deletions packages/~feature-hub/src/feature.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
Expand All @@ -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))
Expand All @@ -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;
}

Expand Down Expand Up @@ -267,7 +267,7 @@ FeatureConstructor

function featureArrayLikeToMask(features: ArrayLike<FeatureElement>): Mask
{
let mask = maskNew();
let mask = MASK_EMPTY;
const { length } = features;
for (let index = 0; index < length; ++index)
{
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -515,7 +515,7 @@ FeatureConstructor
}
else
{
mask = maskNew();
mask = MASK_EMPTY;
wrappedCheck = null;
}
{
Expand Down Expand Up @@ -663,7 +663,7 @@ FeatureConstructor
const featureNames = _Object_keys(featureInfos);
const includeSetMap = createMap<{ readonly [FeatureName in string]: null; }>();
const familiesMap = createMap<readonly string[]>();
let unionMask = maskNew();
let unionMask = MASK_EMPTY;

featureNames.forEach(completeFeature);
completeExclusions();
Expand Down Expand Up @@ -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;
}

Expand Down
6 changes: 3 additions & 3 deletions packages/~feature-hub/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
9 changes: 2 additions & 7 deletions packages/~feature-hub/src/mask-impl-52.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down
9 changes: 3 additions & 6 deletions packages/~feature-hub/src/mask-impl-64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
*
Expand Down
12 changes: 6 additions & 6 deletions packages/~feature-hub/test/spec/feature.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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');
Expand Down
12 changes: 6 additions & 6 deletions packages/~feature-hub/test/spec/mask-index.spec.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
17 changes: 8 additions & 9 deletions packages/~feature-hub/test/spec/mask.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import
{
MASK_EMPTY,
MASK_MAX_SIZE,
type Mask,
maskAreEqual,
maskIncludes,
maskIntersection,
maskNew,
maskNext,
maskUnion,
}
Expand All @@ -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`;
Expand Down Expand Up @@ -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`;
Expand All @@ -68,11 +68,10 @@ function formatMask(mask: Mask): string

it
(
'maskNew',
'MASK_EMPTY',
(): void =>
{
const newMask = maskNew();
assertMaskEmpty(newMask);
assertMaskEmpty(MASK_EMPTY);
},
);

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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)
Expand Down
32 changes: 15 additions & 17 deletions src/lib/debug.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand All @@ -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')
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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];
Expand Down
4 changes: 2 additions & 2 deletions src/lib/encoder/encoder-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit cbcdc4a

Please sign in to comment.