Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

typescript array data structure

class DS_Array{
    private data: any = {};
    private length: number = 0;

    /*
    * Yields the element from the specified index from the array;
    */
    public Get(index: number){
        if(index > this.length) return "err -> index limit exceeded";
        return this.data[index];
    }

    /*
    * Removes the last element of the array
    */
    public Pop(){
        if(this.length <= 0) return "err -> no elements in the array"
        delete this.data[this.length - 1];
        this.length--;
    }

    /*
    * Adds an element to the end of the array
    */
    public Push(item: any){
        this.data[this.length] = item;
        this.length++;
    }

    /*
    * Deletes the element from the specified index from the array;
    */
    public Delete(index: number){
        if(index > this.length) return "err -> index limit exceeded"
        delete this.data[index];
        this.ShiftIndexes(index);
    }

    /*
    * Shifts items and their indexes accordingly;
    */
    private ShiftIndexes(index: number) {
        for (let x = index; x < this.length - 1; ++x) this.data[x] = this.data[x + 1];
        delete this.data[this.length - 1];
        this.length--;
    }

    /*
    * Renders the entire data structure clear
    */
    public Clear(){
        if(this.length <= 0) return "err -> no elements left";
        for (let x = 0; x < this.length; ++x) delete this.data[x];
        this.length = 0;
    }
}
 
PREVIOUS NEXT
Tagged: #typescript #array #data #structure
ADD COMMENT
Topic
Name
8+3 =