Skip to content

TypeScript-STL v1.5.6

Compare
Choose a tag to compare
@samchon samchon released this 13 Aug 09:48
· 497 commits to master since this release

New Features

For ... of iteration

Until the v1.4, there had been only a generic iterator pattern using the begin() and end() methods. However, since this v1.5 update, you also can utilize the for ... of iteration statement.

When you need to iterate full elements of a STL-container, then the for ... of iteration would be much convenient.

function old_iteration(): void
{
    let v = new std.Vector<number>();
    for (let i: number = 0; i < 10; ++i)
        v.push_back(i);

    // NEW FEATURE - FOR OF ITERATION
    for (let it = v.begin(); !it.equal_to(v.end()); it = it.next())
        console.log(it.value);
}

function for_of_vector(): void
{
    let v = new std.Vector<number>();
    for (let i: number = 0; i < 10; ++i)
        v.push_back(i);

    // NEW FEATURE - FOR OF ITERATION
    for (let elem of v)
        console.log(elem);
}

function for_of_map(): void
{
    let m = new std.Map<string, number>();
    m.emplace("First", 1);
    m.emplace("Second", 2);
    m.emplace("Third", 3);

    for (let pair of m)
        console.log(pair.first, pair.second);
}

<thread>

Since this TypeScript-STL v1.5 update, the <thread> has implemented in. However, notice that; JavaScript code runs in only a thread. The function and classes in the <thread> are all fake instances, based on the Promise. They just imitate the <thread>'s behaviors.

async function test_sleep_for(): Promise<void>
{
    await std.sleep_for(2000); // sleep for 2 seconds.
}

When you call the std.sleep_until() function, then it doesn't lock your program. It just returns a Promise object that calling the call-back function at the specified datetime.

<thread> features implemented in the TypeScript-STL v1.5 are such below:

global functions

namespace std
{
    export function sleep_for(ms: number): Promise<void>;
    export function sleep_until(at: Date): Promise<void>;
    
    export function lock(...items: ILockable[]): Promise<void>;
    export function try_lock(...items: ILockable[]): number;

    export interface ILockable
    {
        public lock(): Promise<void>;
        public try_lock(): boolean;
    }
}

mutex & timed_mutex

namespace std
{
    export class Mutex implements ILockable
    {
        public lock(): Promise<void>;
        public try_lock(): boolean;
        public unlock(): void;
    }

    export class TimedMutex implements Mutex
    {
        public try_lock_for(ms: number): Promise<boolean>;
        public try_lock_until(at: Date): Promise<boolean>;
    }
}

shared_mutex

namespace std
{
    export class SharedMutex implements Mutex
    {
        public lock_shared(): Promise<void>;
        public try_lock_shared(): boolean;
        public unlock_shared(): void;
    }

    export class SharedTimedMutex implements SharedMutex, TimedMutex
    {
        public try_lock_shared_for(ms: number): Promise<boolean>;
        public try_lock_shared_until(at: Date): Promise<boolean>;
    }
}

Break Changes

Exception extends Error

Due to the transpiler problem (Extending built-ins like Error, Array, and Map may no longer work), I determined Exception not to extends the Error class, the built-in-class of JavaScript.

However, not extending the Error class, tracing call stack had been difficult such below:

So I determined to roll back the Exception class to extends the Error again.

namespace std
{
    export class Exception extends Error;

    export class LogicError extends Exception;
    export class RuntimeError extends Exception;
}

Container.swap

Until the v1.5 update, calling Container.swap() function between heterogeneous containers was possible. However, since this v1.5 update, the Container.swap() function works only for the homogeneous containers.

Additionally, until the v1.5 update, when the Container.swap() is called, Iterator.source() doesn't point the swapped container, but the old container. Such bug also has been fixed since this v1.5 update.

namespace std
{
    export class List<T>
    {
        public swap(obj: List<T>); // ONLY SAME TYPE IS ALLOWED
        // public swap(obj: base.Container<T>); -> HETEROGENEOUS -> IT IS TRUNCATED
    }
}