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

multi-array-view

v0.1.7

Published

A small JavaScript library for efficient work with multidimensional arrays

Downloads

20

Readme

MultiArrayView NPM Build Status

A small JavaScript library for efficient work with multidimensional arrays.

When working with a large data, (for example, in 3d-graphics), working with nested arrays in JavaScript can affect the performance of the code. The more productive analogy of nested arrays is strided arrays—multidimensional data placed in the plane of a one-dimensional array. Unfortunately, working with strided arrays can lead to code repetition, complex operations, and syntactic noise in the code. The MultiArrayView library serves to simplify the work with strided arrays as if it were an ordinary multidimensional array.

A simple 3x3 matrix:

const array = [
  [0, 0, 0],
  [0, 0, 0],
  [0, 0, 1]
]

const item = arr[2][2] // 1

And a similar 3x3 matrix in the form of a one-dimensional strided array wrapped in MultiArrayView:

const array = [
  0, 0, 0,
  0, 0, 0,
  0, 0, 1
]

// Wrapping the strided array
const multiArray = MultiArrayView.wrap(arr, [3, 3])

const unit = multiArray.get(2, 2) // 1

Getting Started

Installation using npm:

$ npm install multi-array-view

Or using yarn:

$ yarn add multi-array-view

Usage

You can import the package in your node.js-project the following way:

const MultiArrayView = require('multi-array-view')

If your project has ES6 or TypeScript support, use the following import method. The MultiArrayView has full typing support.

import MultiArrayView from 'multi-array-view'

API reference

Static methods

MultiArrayView.create(shape[, constructor, offset, order])

Creates a new array using the specified constructor. If the constructor is not specified, the Array is used as the constructor. Returns instance of MultiArrayView class.

  • shape
    List of array dimensions.

  • constructor Optional
    Constructor for creating the source one-dimensional array.
    Defaults to Array.

  • offset Optional
    The starting offset of the array view.
    Defaults to 0.

  • order Optional
    Ordering method for storing data. See ordering methods section for more information.
    Available values:

    • MultiArray.C_ORDER — Row-major order (C-style)
    • MultiArray.F_ORDER — Column-major (Fortran-style)

A minimal example

// Creates empty 4x4 matrix
const multiArray = MultiArrayView.create([4, 4])

console.log(multiArray.array) // (16) [empty × 16] 

With specified constructor and offset

const multiArray = MultiArrayView.create([4, 4], Uint8Array, 2)

console.log(multiArray.array)
// Uint8Array(18) [
//   0, 0,          <— 2 offsetted items
//   0, 0, 0, 0,    <— matrix start
//   0, 0, 0, 0,
//   0, 0, 0, 0,
//   0, 0, 0, 0
// ]

MultiArrayView.wrap(array, shape[, offset, order])

Wraps the readymade strided array in the MultiArrayView class for multi-dimensional viewing. Returns instance of MultiArrayView class.

  • array
    The 1d array to wrap.

  • shape
    List of array dimensions.

  • offset Optional
    The starting offset of the array view.
    Defaults to 0.

  • order Optional
    Ordering method for storing data. See ordering methods section for more information.
    Available values:

    • MultiArray.C_ORDER — Row-major order (C-style)
    • MultiArray.F_ORDER — Column-major (Fortran-style)

Simple example

const shape = [128, 256, 512]
const length = shape.reduce((a, b) => a * b) // Calculation the length of the array
const typedArray = new Int32Array(length) 

// Wrapping the array
const multiArray = MultiArrayView.wrap(typedArray, shape) 

console.log(multiArray.array) // Int32Array(16777216) [0, 0, 0, 0, 0, ...

Instance methods

.get(x1 [, x2, ..., xN])

Returns the value of the item by the specified path.

  • x1 ... xN
    List of indices for each dimension to obtain an item. The same as indices in a simple multidimensional array.

.set(value, x1 [, x2, ..., xN])

Sets the item value by the specified path.

  • value
    The value to set

  • x1 ... xN
    List of indices for each dimension to set a value to the item. The same as indices in a simple multidimensional array.

Example

const multiArray = MultiArrayView.create([100, 100])

const value = 1337
multiArray.set(value, 90, 10)

console.log(multiArray.get(90, 10)) // 1337

.getIndex(x1 [, x2, ..., xN])

Returns the real index of source array by the specified path.

  • x1 ... xN
    List of indices for each dimension to get the real index. The same as indices in a simple multidimensional array.

.array

The source array.

.shape

Shape of the array that was putted when the MultiArrayView instance was created.

.length

Length of the source array computed by the shape.

Ordering methods

There are two values for the sorting method:

  • MultiArray.C_ORDER — Row-major order (C-style)
  • MultiArray.F_ORDER — Column-major (Fortran-style)

They differ in the way of storing data in the source array.

Simple 3x3 matrix:

const matrix3x3 = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
]

And flat representation of the matrix above after applying different-ordered arrays:

// Default row-major ordered array
const cOrdered = MultiArrayView.create([3, 3], Uint8Array, 0, MultiArrayView.C_ORDER)

// Column-major ordered array
const fOrdered = MultiArrayView.create([3, 3], Uint8Array, 0, MultiArrayView.F_ORDER)

// Applying matrix to arrays
for (let x = 0; x < matrix3x3.length; x++) {
  for (let y = 0; y < matrix3x3[x].length; y++) {
    const value = matrix3x3[x][y]
    
    cOrdered.set(value, x, y)
    fOrdered.set(value, x, y)
  }
}

// Row-major source array representation
console.log(cOrdered.array)
// [1, 2, 3, 4, 5, 6, 7, 8, 9]


// Column-major source array representation
console.log(fOrdered.array)
// [1, 4, 7, 2, 5, 8, 3, 6, 9]