npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

structure-of-arrays

v0.9.2

Published

```sh npm install --save structure-of-arrays # or yarn add structure-of-arrays ```

Downloads

24

Readme

structure-of-arrays

Install

npm install --save structure-of-arrays
# or
yarn add structure-of-arrays

Usage

import { StructureOfArrays, float64 } from 'structure-of-arrays'

const structure = {
  x: float64
, y: float64
, vx: float64
, vy: float64
}

const Movable = new StructureOfArrays(structure)
const [player, enemy] = Movable.add([
  {
    x: 0
  , y: 0
  , vx: 0
  , vy: 0
  }
, {
    x: 100
  , y: 0
  , vx: -1
  , vy: 0
  }
])

for (const index of Movable.indexes()) {
  Movable.set(index, 'x', Movable.get(index, 'x') + Movable.get(index, 'vx'))
  Movable.set(index, 'y', Movable.get(index, 'y') + Movable.get(index, 'vy'))
}
// or
for (const index of Movable.indexes()) {
  Movable.arrays.x[index] += Movable.arrays.vx[index]
  Movable.arrays.y[index] += Movable.arrays.vy[index]
}

API

type Structure = Record<string, Type>

type MapTypesOfStructureToPrimitives<T extends Structure> = {
  [Key in keyof T]: PrimitiveOfType<T[Key]>
}

type MapTypesOfStructureToInternalArrays<T extends Structure> = {
  [Key in keyof T]: InternalArrayOfType<T[Key]>
}

type PrimitiveOfType<T extends Type> =
  T extends typeof int8 ? number
: T extends typeof uint8 ? number
: T extends typeof int16 ? number
: T extends typeof uint16 ? number
: T extends typeof int32 ? number
: T extends typeof uint32 ? number
: T extends typeof float32 ? number
: T extends typeof float64 ? number
: T extends typeof boolean ? boolean
: T extends typeof string ? string
: never

type InternalArrayOfType<T extends Type> =
  T extends typeof int8 ? Int8Array
: T extends typeof uint8 ? Uint8Array
: T extends typeof int16 ? Int16Array
: T extends typeof uint16 ? Uint16Array
: T extends typeof int32 ? Int32Array
: T extends typeof uint32 ? Uint32Array
: T extends typeof float32 ? Float32Array
: T extends typeof float64 ? Float64Array
: T extends typeof boolean ? boolean[]
: T extends typeof string ? string[]
: never

type Type =
| typeof int8
| typeof uint8
| typeof int16
| typeof uint16
| typeof int32
| typeof uint32
| typeof float32
| typeof float64
| typeof boolean
| typeof string

StructureOfArrays

type ValueOfContainer<T extends Container> =
  T extends DynamicTypedArray<TypedArrayConstructor> ? number
: T extends boolean[] ? boolean
: T extends string[] ? string
: never

type StructureContainers<T extends Structure> = {
  [Key in keyof T]: ContainerOfType<T[Key]>
}

class StructureOfArrays<T extends Stucture> {
  /**
   * Note that `StructureOfArrays` cannot respond to any operations on the internal arrays,
   * you must make sure that indexes being accessed are within bounds and not deleted.
   */
  readonly arrays: MapStructureToInternalArrays<T>
  readonly keys: string[]

  get length(): number
  get size(): number

  constructor(
    structure: T
  , defaultValuesOfStructure?: MapTypesOfStructureToPrimitives<T>
  )

  indexes(): Iterable<number>

  has(index: number): boolean

  /**
   * @throws {RangeError}
   */
  get<U extends keyof T>(index: number, key: U): PrimitiveOfType<T[U]>

  tryGet<U extends keyof T>(index: number, key: U): PrimitiveOfType<T[U]> | undefined

  addWithDefaultValues(size: number): number[]

  /**
   * Insert items that reuse deleted indexes, return indexes.
   */
  add(...structures: Array<MapStructureToPrimitive<T>>): number[]

  pushWithDefaultValues(size: number): number[]

  /**
   * Insert items at the end of the array, return indexes.
   */
  push(...structures: Array<MapStructureToPrimitive<T>>): number[]

  pop(): void

  /**
   * Insert or update an item based on index.
   */
  upsert(index: number, structure?: MapStructureToPrimitive<T>): void

  /**
   * @throws {RangeError}
   */
  update<U extends keyof T>(
    index: number
  , key: U
  , value: ValueOfContainer<StructureContainers<T>[U]>
  ): void

  tryUpdate<U extends keyof T>(
    index: number
  , key: U
  , value: ValueOfContainer<StructureContainers<T>[U]>
  ): boolean

  delete(index: number): void

  clear(): void
}

StructureOfSparseMaps

type ValueOfContainer<T extends Container> =
  T extends TypedSparseMap<TypedArrayConstructor> ? number
: T extends SparseMap<boolean> ? boolean
: T extends SparseMap<string> ? string
: never

type StructureContainers<T extends Structure> = {
  [Key in keyof T]: ContainerOfType<T[Key]>
}

class StructureOfSparseMaps<T extends Structure> {
  readonly arrays: MapStructureToInternalArrays<T>
  readonly keys: string[]

  get size(): number

  constructor(
    structure: T
  , defaultValuesOfStructure?: MapTypesOfStructureToPrimitives<T>
  )

  keys(): Iterable<string>

  indexes(): Iterable<number>

  has(index: number): boolean

  getInternalIndex(externalIndex: number): number

  tryGetInternalIndex(externalIndex: number): number | undefined

  /**
   * @throws {RangeError}
   */
  get<U extends keyof T>(index: number, key: U): PrimitiveOfType<T[U]>

  tryGet<U extends keyof T>(index: number, key: U): PrimitiveOfType<T[U]> | undefined

  addWithDefaultValues(size: number): number[]

  /**
   * Insert items that reuse deleted indexes, return indexes.
   */
  add(...structures: Array<MapStructureToPrimitive<T>>): number[]

  /**
   * Insert or update an item based on index.
   */
  upsert(index: number, structure?: MapStructureToPrimitive<T>): void

  /**
   * @throws {RangeError}
   */
  update<U extends keyof T>(
    index: number
  , key: U
  , value: ValueOfContainer<StructureContainers<T>[U]>
  ): void

  tryUpdate<U extends keyof T>(
    index: number
  , key: U
  , value: ValueOfContainer<StructureContainers<T>[U]>
  ): boolean

  delete(index: number): void

  clear(): void
}