ts-structures
Advanced tools
Comparing version 0.4.1 to 0.5.0
function guardOverflow(throwError, returnValue) { | ||
return function (target, methodName, descriptor) { | ||
var method = descriptor.value; | ||
descriptor.value = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
return (target, methodName, descriptor) => { | ||
const method = descriptor.value; | ||
descriptor.value = function (...args) { | ||
if (!this.capacity || | ||
@@ -14,3 +10,3 @@ this.capacity < 0 || | ||
if (throwError) | ||
throw new Error(target + "." + methodName + ": insertion aborted (limit of " + this.length + " reached)"); | ||
throw new Error(`${target.constructor.name}.${methodName}: insertion aborted (limit of ${this.length} reached)`); | ||
return returnValue; | ||
@@ -17,0 +13,0 @@ }; |
@@ -14,18 +14,18 @@ function compare(a, b) { | ||
function infOrEqual(a, b) { | ||
var value = this.compare(a, b); | ||
const value = this.compare(a, b); | ||
return value === -1 || value === 0; | ||
} | ||
function supOrEqual(a, b) { | ||
var value = this.compare(a, b); | ||
const value = this.compare(a, b); | ||
return value === 1 || value === 0; | ||
} | ||
var compareMethods = { | ||
compare: compare, | ||
inf: inf, | ||
sup: sup, | ||
equal: equal, | ||
infOrEqual: infOrEqual, | ||
supOrEqual: supOrEqual, | ||
const compareMethods = { | ||
compare, | ||
inf, | ||
sup, | ||
equal, | ||
infOrEqual, | ||
supOrEqual, | ||
}; | ||
export { compareMethods, }; | ||
//# sourceMappingURL=comparer.js.map |
import ListGraph from './list-graph'; | ||
interface Graph<T> { | ||
addVertex(vertex: T): Graph<T>; | ||
removeVertex(vertex: T): Graph<T>; | ||
addEdge(vertexA: T, vertexB: T, weight: number): Graph<T>; | ||
removeEdge(vertexA: T, vertexB: T): Graph<T>; | ||
interface Graph { | ||
addVertex(vertex: string): void; | ||
removeVertex(vertex: string): void; | ||
addEdge(vertexA: string, vertexB: string, weight: number): void; | ||
removeEdge(vertexA: string, vertexB: string): void; | ||
addDirectedEdge(from: string, to: string, weight: number): void; | ||
removeDirectedEdge(from: string, to: string): void; | ||
} | ||
export { Graph, ListGraph, }; |
@@ -1,15 +0,29 @@ | ||
import { Graph } from './'; | ||
declare class Edge<T> { | ||
vertex: T; | ||
import { Graph } from '.'; | ||
declare type Vertex<T extends string | number> = T; | ||
declare class Edge { | ||
vertex: Vertex<string>; | ||
weight: number; | ||
constructor(vertex: T, weight: number); | ||
constructor(vertex: Vertex<string>, weight: number); | ||
setWeight(weight: number): boolean; | ||
} | ||
declare type AdjacencyList<T> = Map<T, Set<Edge<T>>>; | ||
declare class ListGraph<T> implements Graph<T> { | ||
data: AdjacencyList<T>; | ||
addVertex(vertex: T): Graph<T>; | ||
removeVertex(vertex: T): Graph<T>; | ||
addEdge(vertexA: T, vertexB: T, weight: number): Graph<T>; | ||
removeEdge(vertexA: T, vertexB: T): Graph<T>; | ||
declare class EdgeSet extends Set<Edge> { | ||
hasVertex(vertex: Vertex<string>): boolean; | ||
getEdge(vertex: Vertex<string>): Edge | undefined; | ||
deleteVertex(vertex: Vertex<string>): boolean; | ||
private actionOnMatch; | ||
} | ||
declare class ListGraph implements Graph { | ||
data: Map<string, EdgeSet>; | ||
addVertex(vertex: Vertex<string>): boolean; | ||
removeVertex(vertex: Vertex<string>): boolean; | ||
resetVertex(vertex: Vertex<string>): boolean; | ||
addEdge(vertexA: Vertex<string>, vertexB: Vertex<string>, weight?: number): boolean; | ||
removeEdge(vertexA: Vertex<string>, vertexB: Vertex<string>): boolean; | ||
addDirectedEdge(from: Vertex<string>, to: Vertex<string>, weight?: number): boolean; | ||
removeDirectedEdge(from: Vertex<string>, to: Vertex<string>): boolean; | ||
getEdgeWeight(from: Vertex<string>, to: Vertex<string>): number | undefined; | ||
setEdgeWeight(from: Vertex<string>, to: Vertex<string>, weight: number): boolean; | ||
private _addEdge; | ||
private _removeEdge; | ||
} | ||
export default ListGraph; |
@@ -1,42 +0,99 @@ | ||
var Edge = (function () { | ||
function Edge(vertex, weight) { | ||
class Edge { | ||
constructor(vertex, weight) { | ||
this.vertex = vertex; | ||
this.weight = weight; | ||
} | ||
return Edge; | ||
}()); | ||
var ListGraph = (function () { | ||
function ListGraph() { | ||
setWeight(weight) { | ||
this.weight = weight; | ||
return true; | ||
} | ||
} | ||
class EdgeSet extends Set { | ||
hasVertex(vertex) { | ||
var _a; | ||
return (_a = this.actionOnMatch(vertex, () => true)) !== null && _a !== void 0 ? _a : false; | ||
} | ||
getEdge(vertex) { | ||
return this.actionOnMatch(vertex, (edge) => edge); | ||
} | ||
deleteVertex(vertex) { | ||
var _a; | ||
return (_a = this.actionOnMatch(vertex, (edge) => this.delete(edge))) !== null && _a !== void 0 ? _a : false; | ||
} | ||
actionOnMatch(vertex, action) { | ||
for (const edge of this) | ||
if (edge.vertex === vertex) | ||
return action(edge); | ||
return undefined; | ||
} | ||
} | ||
class ListGraph { | ||
constructor() { | ||
this.data = new Map(); | ||
} | ||
ListGraph.prototype.addVertex = function (vertex) { | ||
if (!this.data.has(vertex)) | ||
this.data.set(vertex, new Set()); | ||
return this; | ||
}; | ||
ListGraph.prototype.removeVertex = function (vertex) { | ||
if (!this.data.has(vertex)) | ||
return this; | ||
this.data.delete(vertex); | ||
return this; | ||
}; | ||
ListGraph.prototype.addEdge = function (vertexA, vertexB, weight) { | ||
var edgesA = this.data.get(vertexA); | ||
var edgesB = this.data.get(vertexB); | ||
if (!edgesA || !edgesB) | ||
return this; | ||
edgesA.add(new Edge(vertexB, weight)); | ||
edgesB.add(new Edge(vertexA, weight)); | ||
return this; | ||
}; | ||
ListGraph.prototype.removeEdge = function (vertexA, vertexB) { | ||
var edgesA = this.data.get(vertexA); | ||
var edgesB = this.data.get(vertexB); | ||
if (!edgesA || !edgesB) | ||
return this; | ||
return this; | ||
}; | ||
return ListGraph; | ||
}()); | ||
addVertex(vertex) { | ||
if (this.data.has(vertex)) | ||
return false; | ||
this.data.set(vertex, new EdgeSet()); | ||
return true; | ||
} | ||
removeVertex(vertex) { | ||
const vertexIsDeleted = this.data.delete(vertex); | ||
if (!vertexIsDeleted) | ||
return false; | ||
this.data.forEach((edgeSet) => edgeSet.deleteVertex(vertex)); | ||
return true; | ||
} | ||
resetVertex(vertex) { | ||
return this.removeVertex(vertex) && this.addVertex(vertex); | ||
} | ||
addEdge(vertexA, vertexB, weight = 1) { | ||
return this._addEdge(false, vertexA, vertexB, weight); | ||
} | ||
removeEdge(vertexA, vertexB) { | ||
return this._removeEdge(false, vertexA, vertexB); | ||
} | ||
addDirectedEdge(from, to, weight = 1) { | ||
return this._addEdge(true, from, to, weight); | ||
} | ||
removeDirectedEdge(from, to) { | ||
return this._removeEdge(true, from, to); | ||
} | ||
getEdgeWeight(from, to) { | ||
var _a, _b; | ||
return (_b = (_a = this.data.get(from)) === null || _a === void 0 ? void 0 : _a.getEdge(to)) === null || _b === void 0 ? void 0 : _b.weight; | ||
} | ||
setEdgeWeight(from, to, weight) { | ||
var _a, _b; | ||
return !!((_b = (_a = this.data.get(from)) === null || _a === void 0 ? void 0 : _a.getEdge(to)) === null || _b === void 0 ? void 0 : _b.setWeight(weight)); | ||
} | ||
_addEdge(isDirected, from, to, weight) { | ||
const srcEdges = this.data.get(from); | ||
const dstEdges = this.data.get(to); | ||
if (!srcEdges || !dstEdges) | ||
return false; | ||
if (srcEdges.hasVertex(to)) | ||
return false; | ||
if (!isDirected && dstEdges.hasVertex(from)) | ||
return false; | ||
srcEdges.add(new Edge(to, weight)); | ||
if (!isDirected) | ||
dstEdges.add(new Edge(from, weight)); | ||
return true; | ||
} | ||
_removeEdge(isDirected, from, to) { | ||
const srcEdges = this.data.get(from); | ||
const dstEdges = this.data.get(to); | ||
if (!srcEdges || !dstEdges) | ||
return false; | ||
if (!srcEdges.hasVertex(to)) | ||
return false; | ||
if (!isDirected && !dstEdges.hasVertex(from)) | ||
return false; | ||
const vertexIsDeleted = srcEdges.deleteVertex(to); | ||
const otherIsDeleted = isDirected ? true : dstEdges.deleteVertex(from); | ||
return vertexIsDeleted && otherIsDeleted; | ||
} | ||
} | ||
export default ListGraph; | ||
//# sourceMappingURL=list-graph.js.map |
import { compareMethods, } from '../_shared'; | ||
var BinaryHeap = (function () { | ||
function BinaryHeap() { | ||
class BinaryHeap { | ||
constructor() { | ||
this.values = []; | ||
@@ -12,36 +12,27 @@ this.compare = compareMethods.compare.bind(this); | ||
} | ||
BinaryHeap.prototype.setCompareFunction = function (compareFunction) { | ||
setCompareFunction(compareFunction) { | ||
this.compare = compareFunction; | ||
var values = this.toArray(); | ||
const values = this.toArray(); | ||
this.clear(); | ||
this.insert.apply(this, values); | ||
this.insert(...values); | ||
return this; | ||
}; | ||
Object.defineProperty(BinaryHeap.prototype, "length", { | ||
get: function () { | ||
return this.values.length; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
BinaryHeap.prototype.toArray = function () { | ||
} | ||
get length() { | ||
return this.values.length; | ||
} | ||
toArray() { | ||
return this.values; | ||
}; | ||
BinaryHeap.prototype.insert = function () { | ||
var _this = this; | ||
var values = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
values[_i] = arguments[_i]; | ||
} | ||
values.forEach(function (value) { | ||
_this.values.push(value); | ||
_this.bubbleUp(); | ||
} | ||
insert(...values) { | ||
values.forEach((value) => { | ||
this.values.push(value); | ||
this.bubbleUp(); | ||
}); | ||
return values[values.length - 1]; | ||
}; | ||
BinaryHeap.prototype.shift = function () { | ||
var first = this.values[0]; | ||
} | ||
shift() { | ||
const first = this.values[0]; | ||
if (first === undefined) | ||
return undefined; | ||
var last = this.values.pop(); | ||
const last = this.values.pop(); | ||
if (this.length === 0) | ||
@@ -52,10 +43,10 @@ return first; | ||
return first; | ||
}; | ||
BinaryHeap.prototype.clear = function () { | ||
} | ||
clear() { | ||
this.values = []; | ||
}; | ||
BinaryHeap.prototype.bubbleUp = function () { | ||
var currentIndex = this.values.length - 1; | ||
} | ||
bubbleUp() { | ||
let currentIndex = this.values.length - 1; | ||
while (currentIndex > 0) { | ||
var parentIndex = Math.floor((currentIndex - 1) / 2); | ||
const parentIndex = Math.floor((currentIndex - 1) / 2); | ||
if (this.infOrEqual(this.values[currentIndex], this.values[parentIndex])) | ||
@@ -66,13 +57,8 @@ break; | ||
} | ||
}; | ||
BinaryHeap.prototype.bubbleDown = function () { | ||
var _this = this; | ||
var maxValueIndex = function () { | ||
var indexes = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
indexes[_i] = arguments[_i]; | ||
} | ||
var iMax = indexes[0]; | ||
indexes.forEach(function (i) { | ||
if (_this.supOrEqual(_this.values[i], _this.values[iMax])) | ||
} | ||
bubbleDown() { | ||
const maxValueIndex = (...indexes) => { | ||
let iMax = indexes[0]; | ||
indexes.forEach((i) => { | ||
if (this.supOrEqual(this.values[i], this.values[iMax])) | ||
iMax = i; | ||
@@ -82,36 +68,28 @@ }); | ||
}; | ||
var i = 0; | ||
var _loop_1 = function () { | ||
var iLchild = 2 * i + 1; | ||
var iRchild = iLchild + 1; | ||
var iMax = (function () { | ||
if (!_this.hasIndex(iLchild)) | ||
let i = 0; | ||
while (i < this.values.length) { | ||
const iLchild = 2 * i + 1; | ||
const iRchild = iLchild + 1; | ||
const iMax = (() => { | ||
if (!this.hasIndex(iLchild)) | ||
return i; | ||
if (!_this.hasIndex(iRchild)) | ||
if (!this.hasIndex(iRchild)) | ||
return maxValueIndex(i, iLchild); | ||
return maxValueIndex(i, iLchild, iRchild); | ||
})(); | ||
var next = i === iMax ? 0 : iMax; | ||
const next = i === iMax ? 0 : iMax; | ||
if (!next) | ||
return "break"; | ||
this_1.swap(i, next); | ||
break; | ||
this.swap(i, next); | ||
i = next; | ||
}; | ||
var this_1 = this; | ||
while (i < this.values.length) { | ||
var state_1 = _loop_1(); | ||
if (state_1 === "break") | ||
break; | ||
} | ||
}; | ||
BinaryHeap.prototype.swap = function (i, j) { | ||
var _a; | ||
_a = [this.values[j], this.values[i]], this.values[i] = _a[0], this.values[j] = _a[1]; | ||
}; | ||
BinaryHeap.prototype.hasIndex = function (i) { | ||
} | ||
swap(i, j) { | ||
[this.values[i], this.values[j]] = [this.values[j], this.values[i]]; | ||
} | ||
hasIndex(i) { | ||
return i < this.values.length; | ||
}; | ||
return BinaryHeap; | ||
}()); | ||
} | ||
} | ||
export default BinaryHeap; | ||
//# sourceMappingURL=binary-heap.js.map |
@@ -8,3 +8,4 @@ import type { CompareFunction, FilterFunction, MapFunction, ReduceFunction, TraverseCallback } from './_shared'; | ||
import { BinaryHeap } from './heap'; | ||
import { ListGraph } from './graph'; | ||
export type { CompareFunction, FilterFunction, MapFunction, ReduceFunction, TraverseCallback, }; | ||
export { DoublyLinkedList, Queue, PriorityQueue, Stack, BinarySearchTree, TraverseMethod, BinaryHeap, }; | ||
export { DoublyLinkedList, Queue, PriorityQueue, Stack, BinarySearchTree, TraverseMethod, BinaryHeap, ListGraph, }; |
@@ -7,3 +7,4 @@ import { TraverseMethod } from './_shared'; | ||
import { BinaryHeap } from './heap'; | ||
export { DoublyLinkedList, Queue, PriorityQueue, Stack, BinarySearchTree, TraverseMethod, BinaryHeap, }; | ||
import { ListGraph } from './graph'; | ||
export { DoublyLinkedList, Queue, PriorityQueue, Stack, BinarySearchTree, TraverseMethod, BinaryHeap, ListGraph, }; | ||
//# sourceMappingURL=index.js.map |
@@ -1,3 +0,3 @@ | ||
var DoublyLinkedListNode = (function () { | ||
function DoublyLinkedListNode(list, value, prev, next) { | ||
class DoublyLinkedListNode { | ||
constructor(list, value, prev, next) { | ||
this.list = list; | ||
@@ -8,48 +8,47 @@ this.value = value; | ||
} | ||
return DoublyLinkedListNode; | ||
}()); | ||
var DoublyLinkedList = (function () { | ||
function DoublyLinkedList() { | ||
} | ||
class DoublyLinkedList { | ||
constructor() { | ||
this.length = 0; | ||
} | ||
DoublyLinkedList.prototype.push = function (value) { | ||
push(value) { | ||
return this.insertValue(value, this.tail, undefined); | ||
}; | ||
DoublyLinkedList.prototype.pop = function () { | ||
} | ||
pop() { | ||
return this.removeNode(this.tail); | ||
}; | ||
DoublyLinkedList.prototype.unshift = function (value) { | ||
} | ||
unshift(value) { | ||
return this.insertValue(value, undefined, this.head); | ||
}; | ||
DoublyLinkedList.prototype.shift = function () { | ||
} | ||
shift() { | ||
return this.removeNode(this.head); | ||
}; | ||
DoublyLinkedList.prototype.insertBefore = function (value, before) { | ||
} | ||
insertBefore(value, before) { | ||
if (before.list !== this) | ||
return undefined; | ||
return this.insertValue(value, before.prev, before); | ||
}; | ||
DoublyLinkedList.prototype.insertAfter = function (value, after) { | ||
} | ||
insertAfter(value, after) { | ||
if (after.list !== this) | ||
return undefined; | ||
return this.insertValue(value, after, after.next); | ||
}; | ||
DoublyLinkedList.prototype.has = function (value) { | ||
} | ||
has(value) { | ||
return !!this.get(value); | ||
}; | ||
DoublyLinkedList.prototype.get = function (value) { | ||
for (var curr = this.head; curr; curr = curr.next) | ||
} | ||
get(value) { | ||
for (let curr = this.head; curr; curr = curr.next) | ||
if (curr.value === value) | ||
return curr; | ||
return undefined; | ||
}; | ||
DoublyLinkedList.prototype.getAll = function (value) { | ||
var results = []; | ||
for (var curr = this.head; curr; curr = curr.next) | ||
} | ||
getAll(value) { | ||
const results = []; | ||
for (let curr = this.head; curr; curr = curr.next) | ||
if (curr.value === value) | ||
results.push(curr); | ||
return results; | ||
}; | ||
DoublyLinkedList.prototype.insertValue = function (value, prev, next) { | ||
var node = new DoublyLinkedListNode(this, value, prev, next); | ||
} | ||
insertValue(value, prev, next) { | ||
const node = new DoublyLinkedListNode(this, value, prev, next); | ||
!prev ? this.head = node : prev.next = node; | ||
@@ -59,4 +58,4 @@ !next ? this.tail = node : next.prev = node; | ||
return node; | ||
}; | ||
DoublyLinkedList.prototype.removeNode = function (node) { | ||
} | ||
removeNode(node) { | ||
if (!node || !this.head) | ||
@@ -74,12 +73,12 @@ return undefined; | ||
return node; | ||
}; | ||
DoublyLinkedList.prototype.map = function (callback) { | ||
var newList = new DoublyLinkedList(); | ||
for (var curr = this.head; curr; curr = curr.next) | ||
} | ||
map(callback) { | ||
const newList = new DoublyLinkedList(); | ||
for (let curr = this.head; curr; curr = curr.next) | ||
newList.push(callback(curr.value, curr, this)); | ||
return newList; | ||
}; | ||
DoublyLinkedList.prototype.filter = function (callback) { | ||
var newList = new DoublyLinkedList(); | ||
for (var curr = this.head; curr; curr = curr.next) { | ||
} | ||
filter(callback) { | ||
const newList = new DoublyLinkedList(); | ||
for (let curr = this.head; curr; curr = curr.next) { | ||
if (callback(curr.value, curr, this)) | ||
@@ -89,18 +88,17 @@ newList.push(curr.value); | ||
return newList; | ||
}; | ||
DoublyLinkedList.prototype.reduce = function (callback, initialValue) { | ||
var acc = initialValue; | ||
for (var curr = this.head; curr; curr = curr.next) | ||
} | ||
reduce(callback, initialValue) { | ||
let acc = initialValue; | ||
for (let curr = this.head; curr; curr = curr.next) | ||
acc = callback(acc, curr.value); | ||
return acc; | ||
}; | ||
DoublyLinkedList.prototype.toArray = function () { | ||
var arr = []; | ||
for (var curr = this.head; curr; curr = curr.next) | ||
} | ||
toArray() { | ||
const arr = []; | ||
for (let curr = this.head; curr; curr = curr.next) | ||
arr.push(curr.value); | ||
return arr; | ||
}; | ||
return DoublyLinkedList; | ||
}()); | ||
} | ||
} | ||
export default DoublyLinkedList; | ||
//# sourceMappingURL=doubly-linked-list.js.map |
{ | ||
"name": "ts-structures", | ||
"version": "0.4.1", | ||
"version": "0.5.0", | ||
"description": "TypeScript implementation of common data structures", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -12,13 +12,12 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
import { BinaryHeap } from '../heap'; | ||
var PriorityQueueNode = (function () { | ||
function PriorityQueueNode(value, priority) { | ||
class PriorityQueueNode { | ||
constructor(value, priority) { | ||
this.value = value; | ||
this.priority = priority; | ||
} | ||
return PriorityQueueNode; | ||
}()); | ||
var PriorityQueue = (function () { | ||
function PriorityQueue(capacity) { | ||
} | ||
class PriorityQueue { | ||
constructor(capacity) { | ||
this.capacity = -1; | ||
var compareFunction = function (a, b) { | ||
const compareFunction = (a, b) => { | ||
return a.priority < b.priority ? 1 : a.priority > b.priority ? -1 : 0; | ||
@@ -30,25 +29,20 @@ }; | ||
} | ||
Object.defineProperty(PriorityQueue.prototype, "length", { | ||
get: function () { | ||
return !this.values ? 0 : this.values.length; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
PriorityQueue.prototype.enqueue = function (value, priority) { | ||
var node = new PriorityQueueNode(value, priority); | ||
get length() { | ||
return !this.values ? 0 : this.values.length; | ||
} | ||
enqueue(value, priority) { | ||
const node = new PriorityQueueNode(value, priority); | ||
return this.values.insert(node); | ||
}; | ||
PriorityQueue.prototype.dequeue = function () { | ||
} | ||
dequeue() { | ||
return this.values.shift(); | ||
}; | ||
__decorate([ | ||
guardOverflow(false, undefined), | ||
__metadata("design:type", Function), | ||
__metadata("design:paramtypes", [Object, Number]), | ||
__metadata("design:returntype", Object) | ||
], PriorityQueue.prototype, "enqueue", null); | ||
return PriorityQueue; | ||
}()); | ||
} | ||
} | ||
__decorate([ | ||
guardOverflow(false, undefined), | ||
__metadata("design:type", Function), | ||
__metadata("design:paramtypes", [Object, Number]), | ||
__metadata("design:returntype", Object) | ||
], PriorityQueue.prototype, "enqueue", null); | ||
export default PriorityQueue; | ||
//# sourceMappingURL=priority-queue.js.map |
@@ -11,10 +11,9 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
import { guardOverflow, } from '../_shared'; | ||
var QueueNode = (function () { | ||
function QueueNode(value) { | ||
class QueueNode { | ||
constructor(value) { | ||
this.value = value; | ||
} | ||
return QueueNode; | ||
}()); | ||
var Queue = (function () { | ||
function Queue(capacity) { | ||
} | ||
class Queue { | ||
constructor(capacity) { | ||
this.length = 0; | ||
@@ -25,10 +24,10 @@ this.capacity = -1; | ||
} | ||
Queue.prototype.first = function () { | ||
first() { | ||
return this._first; | ||
}; | ||
Queue.prototype.last = function () { | ||
} | ||
last() { | ||
return this._last; | ||
}; | ||
Queue.prototype.enqueue = function (value) { | ||
var node = new QueueNode(value); | ||
} | ||
enqueue(value) { | ||
const node = new QueueNode(value); | ||
if (!this._first) | ||
@@ -40,4 +39,4 @@ this._first = node; | ||
return ++this.length; | ||
}; | ||
Queue.prototype.dequeue = function () { | ||
} | ||
dequeue() { | ||
if (!this._first) | ||
@@ -47,16 +46,15 @@ return undefined; | ||
this._last = undefined; | ||
var value = this._first.value; | ||
const value = this._first.value; | ||
this._first = this._first.next; | ||
this.length--; | ||
return value; | ||
}; | ||
__decorate([ | ||
guardOverflow(false, -1), | ||
__metadata("design:type", Function), | ||
__metadata("design:paramtypes", [Object]), | ||
__metadata("design:returntype", Number) | ||
], Queue.prototype, "enqueue", null); | ||
return Queue; | ||
}()); | ||
} | ||
} | ||
__decorate([ | ||
guardOverflow(false, -1), | ||
__metadata("design:type", Function), | ||
__metadata("design:paramtypes", [Object]), | ||
__metadata("design:returntype", Number) | ||
], Queue.prototype, "enqueue", null); | ||
export default Queue; | ||
//# sourceMappingURL=queue.js.map |
@@ -15,3 +15,3 @@ # TypeScript Datastructures | ||
This project is also a pretext for the student developer I am to learn or practice many aspects of the development process: | ||
This project is also a pretext for the student developer I am to learn and practice many aspects of the development process: | ||
@@ -51,7 +51,12 @@ - :dna: Understanding data structures | ||
- [Binary Heap](https://gregoryalbouy-ts-datastructures.netlify.app/classes/_heap_binary_heap_.binaryheap.html) | ||
- Graph *in progress* | ||
- [Graph](https://gregoryalbouy-ts-datastructures.netlify.app/interfaces/_graph_index_.graph.html) | ||
- [List Graph](https://gregoryalbouy-ts-datastructures.netlify.app/classes/_graph_list_graph_.listgraph.html) (Adjacency list based graph, optionnally directed or weighted): still needs massive refacto & documentation | ||
- Matrix Graph (Adjacency matrix based graph): *in progress* | ||
## Todo | ||
- More data structures | ||
- Benchmarks | ||
- Graph implementation | ||
- Refacto | ||
- Documentation | ||
- MatrixGraph | ||
- More data structures |
@@ -11,11 +11,10 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
import { guardOverflow, } from '../_shared'; | ||
var StackNode = (function () { | ||
function StackNode(value, next) { | ||
class StackNode { | ||
constructor(value, next) { | ||
this.value = value; | ||
this.next = next; | ||
} | ||
return StackNode; | ||
}()); | ||
var Stack = (function () { | ||
function Stack(capacity) { | ||
} | ||
class Stack { | ||
constructor(capacity) { | ||
this.length = 0; | ||
@@ -26,24 +25,23 @@ this.capacity = -1; | ||
} | ||
Stack.prototype.push = function (value) { | ||
var node = new StackNode(value, this.front); | ||
push(value) { | ||
const node = new StackNode(value, this.front); | ||
this.front = node; | ||
return ++this.length; | ||
}; | ||
Stack.prototype.pop = function () { | ||
} | ||
pop() { | ||
if (!this.front) | ||
return undefined; | ||
var value = this.front.value; | ||
const value = this.front.value; | ||
this.front = this.front.next; | ||
this.length--; | ||
return value; | ||
}; | ||
__decorate([ | ||
guardOverflow(false, -1), | ||
__metadata("design:type", Function), | ||
__metadata("design:paramtypes", [Object]), | ||
__metadata("design:returntype", Number) | ||
], Stack.prototype, "push", null); | ||
return Stack; | ||
}()); | ||
} | ||
} | ||
__decorate([ | ||
guardOverflow(false, -1), | ||
__metadata("design:type", Function), | ||
__metadata("design:paramtypes", [Object]), | ||
__metadata("design:returntype", Number) | ||
], Stack.prototype, "push", null); | ||
export default Stack; | ||
//# sourceMappingURL=stack.js.map |
import { compareMethods, TraverseMethod, } from '../_shared'; | ||
import { Queue } from '../queue'; | ||
var BinarySearchTreeNode = (function () { | ||
function BinarySearchTreeNode(value) { | ||
class BinarySearchTreeNode { | ||
constructor(value) { | ||
this.value = value; | ||
} | ||
BinarySearchTreeNode.prototype.hasChild = function (direction) { | ||
hasChild(direction) { | ||
return !!this.getChild(direction); | ||
}; | ||
BinarySearchTreeNode.prototype.getChild = function (direction) { | ||
} | ||
getChild(direction) { | ||
return direction === -1 ? this.left : this.right; | ||
}; | ||
BinarySearchTreeNode.prototype.setChild = function (direction, newNode) { | ||
} | ||
setChild(direction, newNode) { | ||
direction === -1 ? this.left = newNode : this.right = newNode; | ||
}; | ||
return BinarySearchTreeNode; | ||
}()); | ||
var BinarySearchTree = (function () { | ||
function BinarySearchTree(compareFunction) { | ||
} | ||
} | ||
class BinarySearchTree { | ||
constructor(compareFunction) { | ||
this.compare = compareMethods.compare.bind(this); | ||
@@ -36,17 +35,17 @@ this.inf = compareMethods.inf.bind(this); | ||
} | ||
BinarySearchTree.prototype.root = function () { | ||
root() { | ||
return this._root; | ||
}; | ||
BinarySearchTree.prototype.setCompareFunction = function (compareFunction) { | ||
} | ||
setCompareFunction(compareFunction) { | ||
this.compare = compareFunction; | ||
var values = this.toArray(TraverseMethod.DFSPreOrder); | ||
const values = this.toArray(TraverseMethod.DFSPreOrder); | ||
this.clear(); | ||
values.forEach(this.insert.bind(this)); | ||
return this; | ||
}; | ||
BinarySearchTree.prototype.setDefaultTraverseMethod = function (traverseMethod) { | ||
} | ||
setDefaultTraverseMethod(traverseMethod) { | ||
this.defaultTraverseMethod = traverseMethod; | ||
}; | ||
BinarySearchTree.prototype.insert = function (value) { | ||
var newNode = new BinarySearchTreeNode(value); | ||
} | ||
insert(value) { | ||
const newNode = new BinarySearchTreeNode(value); | ||
if (!this._root) { | ||
@@ -56,4 +55,4 @@ this._root = newNode; | ||
} | ||
for (var currentNode = this._root; currentNode;) { | ||
var direction = this.compare(value, currentNode.value); | ||
for (let currentNode = this._root; currentNode;) { | ||
const direction = this.compare(value, currentNode.value); | ||
if (direction === 0) | ||
@@ -68,32 +67,29 @@ break; | ||
return false; | ||
}; | ||
BinarySearchTree.prototype.clear = function () { | ||
} | ||
clear() { | ||
this._root = undefined; | ||
}; | ||
BinarySearchTree.prototype.toArray = function (traverseMethod) { | ||
if (traverseMethod === void 0) { traverseMethod = this.defaultTraverseMethod; } | ||
var reduceFunction = function (values, value) { | ||
} | ||
toArray(traverseMethod = this.defaultTraverseMethod) { | ||
const reduceFunction = (values, value) => { | ||
values.push(value); | ||
return values; | ||
}; | ||
var initialValue = []; | ||
const initialValue = []; | ||
return this.reduce(reduceFunction, initialValue, traverseMethod); | ||
}; | ||
BinarySearchTree.prototype.map = function (mapFunction, traverseMethod, newCompareFunction) { | ||
if (traverseMethod === void 0) { traverseMethod = this.defaultTraverseMethod; } | ||
var newTree = new BinarySearchTree(newCompareFunction); | ||
} | ||
map(mapFunction, traverseMethod = this.defaultTraverseMethod, newCompareFunction) { | ||
const newTree = new BinarySearchTree(newCompareFunction); | ||
if (!this._root) | ||
return newTree; | ||
var traverse = this.traverseMethods[traverseMethod]; | ||
var callback = function (value) { return newTree.insert(mapFunction(value)); }; | ||
const traverse = this.traverseMethods[traverseMethod]; | ||
const callback = (value) => newTree.insert(mapFunction(value)); | ||
traverse(this._root, callback); | ||
return newTree; | ||
}; | ||
BinarySearchTree.prototype.filter = function (filterFunction, traverseMethod) { | ||
if (traverseMethod === void 0) { traverseMethod = this.defaultTraverseMethod; } | ||
var newTree = new BinarySearchTree(this.compare); | ||
} | ||
filter(filterFunction, traverseMethod = this.defaultTraverseMethod) { | ||
const newTree = new BinarySearchTree(this.compare); | ||
if (!this._root) | ||
return newTree; | ||
var traverse = this.traverseMethods[traverseMethod]; | ||
var callback = function (value) { | ||
const traverse = this.traverseMethods[traverseMethod]; | ||
const callback = (value) => { | ||
if (filterFunction(value)) | ||
@@ -104,22 +100,21 @@ newTree.insert(value); | ||
return newTree; | ||
}; | ||
BinarySearchTree.prototype.reduce = function (reduceFunction, initialValue, traverseMethod) { | ||
if (traverseMethod === void 0) { traverseMethod = this.defaultTraverseMethod; } | ||
} | ||
reduce(reduceFunction, initialValue, traverseMethod = this.defaultTraverseMethod) { | ||
if (!this._root) | ||
return initialValue; | ||
var accumulator = initialValue; | ||
var callback = function (value) { | ||
let accumulator = initialValue; | ||
const callback = (value) => { | ||
accumulator = reduceFunction(accumulator, value); | ||
}; | ||
var traverse = this.traverseMethods[traverseMethod]; | ||
const traverse = this.traverseMethods[traverseMethod]; | ||
traverse(this._root, callback); | ||
return accumulator; | ||
}; | ||
BinarySearchTree.prototype.traverseBFS = function (root, callback) { | ||
} | ||
traverseBFS(root, callback) { | ||
if (!root) | ||
return; | ||
var queue = new Queue(); | ||
const queue = new Queue(); | ||
queue.enqueue(root); | ||
while (queue.first()) { | ||
var currentTreeNode = queue.dequeue(), left = currentTreeNode.left, right = currentTreeNode.right, value = currentTreeNode.value; | ||
const currentTreeNode = queue.dequeue(), left = currentTreeNode.left, right = currentTreeNode.right, value = currentTreeNode.value; | ||
if (left) | ||
@@ -131,4 +126,4 @@ queue.enqueue(left); | ||
} | ||
}; | ||
BinarySearchTree.prototype.traverseDFS = function (order, currentNode, callback) { | ||
} | ||
traverseDFS(order, currentNode, callback) { | ||
if (order === TraverseMethod.DFSPreOrder) | ||
@@ -144,16 +139,15 @@ callback(currentNode.value); | ||
callback(currentNode.value); | ||
}; | ||
BinarySearchTree.prototype.traversePreOrder = function (root, callback) { | ||
} | ||
traversePreOrder(root, callback) { | ||
this.traverseDFS(TraverseMethod.DFSPreOrder, root, callback); | ||
}; | ||
BinarySearchTree.prototype.traverseInOrder = function (root, callback) { | ||
} | ||
traverseInOrder(root, callback) { | ||
this.traverseDFS(TraverseMethod.DFSInOrder, root, callback); | ||
}; | ||
BinarySearchTree.prototype.traversePostOrder = function (root, callback) { | ||
} | ||
traversePostOrder(root, callback) { | ||
this.traverseDFS(TraverseMethod.DFSPostOrder, root, callback); | ||
}; | ||
return BinarySearchTree; | ||
}()); | ||
} | ||
} | ||
export default BinarySearchTree; | ||
export { TraverseMethod }; | ||
//# sourceMappingURL=binary-search-tree.js.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
60381
929
60