Class: Hat

Hat


new Hat(arrayBuffer)

A Sorting Hat

Parameters:
Name Type Description
arrayBuffer ArrayBuffer

allocate this Hat with some working memory

Source:
Throws:

if arrayBuffer is not an instance of ArrayBuffer

Type
TypeError

Methods


<static> create(byteSize)

Creates a new empty Hat from a given byte size

Parameters:
Name Type Description
byteSize Number

Allocate this Hat with a unsigned integer value (size in bytes)

Source:
Returns:
  • new instance of a Hat
Type
Hat

<static> fromString(str)

Class method to create a new Hat from a string (Could be used to retrieve from persistent storage)

Parameters:
Name Type Description
str string

Representing a new Hat instance

Source:
Throws:

if given argument is not an instance of string

Type
TypeError
Returns:
  • new instance of a Hat
Type
Hat

changePosition(position [, on])

Set value for a bit on position

Parameters:
Name Type Argument Description
position Number

unsigned integer value

on boolean <optional>

Optional set true or false (default : false)

Source:
Returns:
  • Return itself for chaining purposes
Type
Hat
Example
const hat = new Hat(ArrayBuffer(2));
hat.changePosition(1, true);

hat.isPosition(1); // returns true

clearPosition(position)

Set bit: false according to integer position in the Sorting Hat Note: Starting from 1

Parameters:
Name Type Description
position Number

unsigned integer value

Source:
Returns:
Type
Hat

compare(hat)

Compare two hat instances on data contents

Parameters:
Name Type Description
hat Hat

instance of a Hat

Source:
Throws:

if hat is not an instance of Hat

Type
TypeError
Returns:

if instance (or contents) are the same

Type
boolean

getPositionsArray()

This method analyzes every bit value in this hat and creates the corresponding
position array where bits are logical true.

Source:
Returns:

Which contains bit positions from this hat, which are logical set to true

Type
Array
Example
// Initialize a Hat with ArrayBuffer (size = 2 bytes)
  const ab = new ArrayBuffer(2);
  const hat = new Hat(ab);

  // Pre set some bit positions
  hat.setPosition(1)
  .setPosition(2)
  .setPosition(3)
  .setPosition(4)
  .setPosition(5)
  .setPosition(6)
  .setPosition(7)
  .setPosition(8)
  .setPosition(10);

  hat.getPositionsArray(); // returns: [1, 2, 3, 4, 5, 6, 7, 8, 10]

hasAllFromPositions(positionArray)

Compares all given positions

  • A positive position means this position should be set truthy
  • A negative position means this position should be set falsy

    When wants to check on bit positions outside the memory bounds (dataViewBounds),
    method wil return early with falsy result

Parameters:
Name Type Description
positionArray Array.<Number>

containing signed integer values (representing bit positions)

Source:
Throws:

if positionArray is not an instance of array

Type
TypeError
Returns:

true when all positions correspondent to the given indexes

Type
boolean
Examples
// Set 2 bit positions to logical '1'
 const hat = new Hat(new ArrayBuffer(2));
 hat.setPosition(1).setPosition(2);

 hat.hasAllFromPositions([1, 2]); // true
// Set 2 bit positions to logical '1' then the first bit position back to '0'
 const hat = new Hat(new ArrayBuffer(2));
 hat.setPosition(1).setPosition(2),togglePosition(1);

 hat.hasAllFromPositions([-1, 2]); // true

isPosition(position)

Checks if a value is true or false on a specific position

Parameters:
Name Type Description
position Number

unsigned integer value

Source:
Returns:

if membership is set

Type
boolean
Example
const hat = new Hat(ArrayBuffer(2));
hat.setPosition(1);

hat.isPosition(1); // returns true

setAllFromPositions(positionArray)

Able to manipulate bits according to an array of signed integers

Parameters:
Name Type Description
positionArray Array.<Number>

Array with integer values starting from 1.

Source:
Throws:

if positionArray is not an instance of array

Type
TypeError
Returns:
  • For chaining purposes
Type
Hat
Examples
// Set 2 bit positions to logical '1'
 const hat = Hat.create(2);
 hat.setAllFromPositions([1, 2]);
 hat.hasAllFromPositions([1, 2]); // returns true
// Set 1 bit positions to logical '1' and the second to '0'
 const hat = Hat.create(2);
 hat.setAllFromPositions([1, -2]);
 hat.hasAllFromPositions([1]); // returns true

setPosition(position)

Set bit: true according to integer position in the Sorting Hat
Note: Starting from 1

Parameters:
Name Type Description
position Number

unsigned integer value

Source:
Throws:

Position can't exceed data view bounds.

Type
Error
Returns:
  • For chaining purposes
Type
Hat

togglePosition(position)

Toggle bit value true => false, false => true

Parameters:
Name Type Description
position Number

unsigned integer value

Source:
Returns:
Type
Hat
Example
const hat = new Hat(ArrayBuffer(2));
hat.changePosition(1, true);

hat.isPosition(1); // true

toString()

Convert the whole ArrayBuffer to a string (Hint: Could be used to store a hat in persistent storage as a encoded string)

Source:
Returns:
  • Encoded string
Type
string