codybonney / ts-data-structures

A collection of common data structures written in TypeScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ts-data-structures

Build Status Coverage Status npm version

Description:

A collection of common data structures written in TypeScript

Installation:

npm install ts-data-structures

Hash Table

HashTable<Key, Value>

Creates a hash table of key and value pairs. Defaults to a size of 16 buckets. Each bucket represents a Singly Linked List of entries.

import { HashTable } from 'ts-data-structures'

// create a HashTable that takes keys of type string and values of type number
const table = new HashTable<string, number>();

// create a HashTable that has a size of 64 buckets
const biggerTable = new HashTable(64);

HashTable.size

Get the number of buckets in the table.

table.size // 16

HashTable.entries

Get the number of entries in the table.

table.entries // 0

HashTable.add(key, value)

Add a new entry to the table. The table will be resized if it contains too many entries. If an entry with the given key already exists, it's value will be updated.

table.add('foo', 42)
table.add('bar', 8)

HashTable.find(key)

Find the value associated with a given key.

table.find('foo') // 42
table.find('zap') // undefined

HashTable.resize(size)

Resize the table to a given number of buckets.

table.resize(32)
table.size // 32

HashTable.remove(key)

Remove an entry from the table

table.entries // 2
table.remove('foo')
table.entries // 1

Singly Linked List

SinglyLinkedList<Value>

Creates a Singly Linked List of values.

import { SinglyLinkedList } from 'ts-data-structures'

// create a Singly Linked List values of type number
const list = new SinglyLinkedList<number>();

// create a Singly Linked List with three initial nodes
const populatedList = new SinglyLinkedList(1, 2, 3);

SinglyLinkedList.add(...values)

Add new entries to the singly linked list

list.add(1, 2, 3)
list.toArray() // [1, 2, 3]

SinglyLinkedList.remove(...values)

Remove entries from the singly linked list

list.remove(2, 3)
list.toArray() // [1]

About

A collection of common data structures written in TypeScript

License:MIT License


Languages

Language:TypeScript 100.0%