TypeScript-STL
STL (Standard Template Library) and Containers for TypeScript
References
Homepages
Projects using STL for TypeScript
Supports
Containers
Functions
Usage
Installation
npm install -g ts-stl
or download directly. (ts-stl.js
and ts-stl.d.ts
in master/lib
)
Node.JS or Require.JS
In JavaScript
var std = require("ts-stl");
var list = new std.List();
list.push_back("First element");
In TypeScript
global["std"] = require("ts-stl");
let list: std.List<string> = new std.List<string>();
list.push_back("First element");
Browser
<script src="ts-stl.js"></script>
<script>
var list = new std.List();
list.push_back("First element");
</script>
Example
std.HashMap
function hash_map(): void
{
let map: std.HashMap<number, string> = new std.HashMap<number, string>();
for (let i: number = 0; i < 10; i++)
map.set(i, "Its key is " + i);
let it = map.find(3);
it = map.erase(it);
console.log(it.first);
it = map.begin().advance(2)
it = map.insert(it, new std.Pair<number, string>(-1, "Its key is -1"));
console.log(it.next().first);
it = map.erase(map.begin().advance(6), map.begin().advance(9));
console.log("has 7:", map.has(7));
console.log("count 5:", map.count(5));
console.log("it is end():", it.equals(map.end()));
console.log("------------------------------");
for (let it = map.begin(); !it.equals(map.end()); it = it.next())
console.log(it.second);
}
std.TreeSet
function test_tree_set(): void
{
let intSet: std.TreeMultiSet<number> = new std.TreeMultiSet<number>();
for (let i = 0; i <= 10; i += 2)
for (let j = 0; j < 3; j++)
intSet.insert(i);
console.log("Matched node: 4");
console.log(" lower bound: " + intSet.lowerBound(4).value);
console.log(" upper bound: " + intSet.upperBound(4).value);
console.log(" ");
for (let i = 1; i <= 10; i += 2)
{
console.log("Mis-matched node: " + i);
console.log(" lower bound: " + intSet.lowerBound(i).value);
console.log(" upper bound: " + intSet.upperBound(i).value);
console.log(" ");
}
}
std.count_if (in <algorithm>)
function test_count_if(): void
{
let list: std.List<number> = new std.List();
for (let i: number = 0; i < 100; i++)
list.push_back(Math.random() * 100))
let count: number = std.count_if(list.begin(), list.end(),
function (val: number): boolean
{
return 50 <= val && val < 60;
}
);
console.log("Number of elements between 50 and 60 are: #" + count);
}
std.sort with customized sorting function
function test_sort(): void
{
let cubeList: std.List<Cube> = new std.List<Cube>();
for (let i: number = 0; i < 10; i++)
cubeList.pushBack(new Cube());
std.sort(cubeList.begin(), cubeList.end());
for (let it = cubeList.begin(); !it.equals(cubeList.end()); it = it.next())
it.value.debug_size();
console.log("------------------------------");
std.sort
(
cubeList.begin(), cubeList.end(),
function (left: Cube, right: Cube): boolean
{
if (left.x != right.x) return left.x < right.x;
else if (left.y != right.y) return left.y < right.y;
else return left.z < right.z;
}
);
for (let it = cubeList.begin(); !it.equals(cubeList.end()); it = it.next())
it.value.debug_position();
}
class Cube
{
public width: number;
public height: number;
public length: number;
public x: number;
public y: number;
public z: number;
public constructor()
{
this.width = Math.random() * 10;
this.height = Math.random() * 10;
this.length = Math.random() * 10;
this.x = Math.random() * 100 - 50;
this.y = Math.random() * 100 - 50;
this.z = Math.random() * 100 - 50;
}
public get volume(): number
{
return this.width * this.height * this.length;
}
public less(obj: Cube): boolean
{
return this.volume < obj.volume;
}
public debug_size(): void
{
console.log(this.width, this.height, this.length + " => " + this.volume);
}
public debug_position(): void
{
console.log(this.x, this.y, this.z);
}
}
License
BSD v3 License