BitSpace represents a single set of mutually exclusive (or orthogonal) properties.

Each property has a numeric value equal to 1 bit-shifted by a certain number of bits.

Created using BitSpaces.create

interface BitSpace<T extends BitPropName> {
    bit: { [key in string]: number };
    bitmask: number;
    bitmaskFor: (pStart?: null | T, pEnd?: null | T) => number;
    end: number;
    has: (p: string) => p is T;
    nameOf: (val: number) => null | T;
    start: number;
}

Type Parameters

Properties

bit: { [key in string]: number }

Holds properties whose numeric values are non-overlapping binary values, suitable for bitmasking.

The given properties are set under the "bit" key in the object and hold the numeric value.

const space = new BitSpaces().create("up", "down", "left", "right");

// {
// bit: {
// up: 1, // at bit 0, i.e. 1 << 0
// down: 2, // at bit 1, i.e. 1 << 1
// left: 4, // at bit 2, i.e. 1 << 2
// right: 8, // at bit 3, i.e. 1 << 3
// },
// start: 0,
// end: 3,
// bitmask: 15, // 1 << 0 | 1 << 1 | 1 << 2 | 1 << 3
// has: (p) => p === "up" || p === "down" || p === "left" || p === "right",
// bitmaskFor: (pStart, pEnd) => ...
// nameOf: (v) => v === 1 ? "up" : v === 2 ? "down" : v === 4 ...
// }

space.bitmaskFor(); // => space.bitmask (15)
space.bitmaskFor("left"); // => space.bit.left | space.bit.right (12)
space.bitmaskFor(null, "down"); // => space.bit.up | space.bit.down (3)
bitmask: number

A bitmask of all values in the space.

bitmaskFor: (pStart?: null | T, pEnd?: null | T) => number

Takes the names of two properties within the space and returns a bitmask that covers all values between them including the starting and ending one.*

If pStart > pEnd, they are reversed.

  • The numeric values of the properties are guaranteed to be in the same order, increasing in value, as the keys passed to the BitSpaces.create, function.

Type declaration

    • (pStart?: null | T, pEnd?: null | T): number
    • Parameters

      • OptionalpStart: null | T

        The name of the property that holds the start value. If null the bitmask will cover from the lowest property.

      • OptionalpEnd: null | T

        The name of the property that holds the end cut-off value for the bitmask. The bitmask with not include pEnd's value. If null the bitmask will cover to the highest property, including.

      Returns number

      Returns a non-0 bitmask containing all values in the space between the given ones. Returns 0 if one or both of the given properties do not exist in the space.

end: number

The ending bit of the space. It's always equal to start + (# of properties in space) - 1

has: (p: string) => p is T

Returns true if the given name is one of the properties in the space. It is case-sensitive.

nameOf: (val: number) => null | T

Returns the name of the property with the given value, or null if the value does not correspond to one of the space properties.

start: number

The starting bit of the space. It's 0 for the first space created in a given set of BitSpaces.