• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!

[vJass] integer-list

Level 3
Joined
Sep 11, 2008
Messages
58
Integer-List v1.1

Documentation within code-block.

Change history

V1.01 - 27.04.2010
  • Added a clear method (to clear the integer-list)
  • A view changes in the documentation

JASS:
// Integer-List V1.01
// =========================================================================
//
// Description
// ===========
// The integer-list is a construct around an integer array with some helpful
// methods for the manipulation of the array, including:
//
// * insertion of an integer at a certain index
// * removing an integer at a certain index without leaving a gap
// * get the array-elements count
// * get the proper index of an integer-value
// * get the x-th index of an integer-value
// * sort up- or downwards using the merge-sort algorithm
//
// Usage example:
// Add Unit-Type-Ids to the list and use them in triggers, respectively
// boolean expressions:
// if (il_darkHeroes.GetIndex(GetUnitTypeId(GetFilterUnit())) > -1) then ....
//
// Pros:
// * comfortable
// * fast sort algorithm
//
// Cons:
// * Using this struct is slower than accessing the array's indexes directly
// * The ways to manipulate the array are limited to the IntegerLists
//   methods (unless you set the array to public and access it directly)
//
// Feel free to manipulate or use only 
// parts of the code.
//
// Default array-size is 100, allowing 81 instances of the struct at maximum.
//
// Importing instructions
// ======================
//
// To import this struct into your code open the trigger editor and create a 
// new trigger. Then convert the trigger into text in the edit-menu, then 
// paste this script into the empty trigger-page. For more details or if 
// you're new to JASS, consult some of the excellent JASS-Tutorials on 
// [url]http://www.hiveworkshop.com[/url]
// respectively [url]http://www.hiveworkshop.com/forums/tutorials/[/url]
//
// Methods Overview
// ================
//
// * create => creates the instance, takes no parameters
// * add => adds an integer to the end of the array, takes an integer, 
//   returns nothing.
// * insert => inserts an integer at a certain position into the array, 
//   takes index (integer) and the integer-value.
// * replace => replaces an integer at a certain position in the array, 
//   takes index and the integer-value.
// * remove => removes an integer at a certain position in the array and 
//   closes the gap, takes index.
// * clear => clears the integer-list
// * value => returns the integer-value of a certain index, takes index, 
//   returns the integer-value
// * count => returns the number of elements in the array, takes nothing, 
//   returns the number of elements (integer)
// * getIndex => returns the index of the first given integer-value, 
//   takes integer, returns index
// * getIndexX => returns the index of the by X given occurence of the given 
//   integer-value, takes integer-value, occurrence (integer), returns index
// * occurrenceCount => counts the occurrence of the given integer-value, 
//   takes integer-value, returns occurence-count (integer)
// * sort => sorts the elements in the array up- or downwards, 
//   takes true for sortorder upwards, false for downwards

struct integerList
    private integer array avalue[100] //max struct instances: 81
    private integer acount
    static method create takes nothing returns integerList
        local integerList il = integerList.allocate()
        set il.acount = 0
        return il
    endmethod
    public method add takes integer int returns nothing
        //adds an integer to the list
        set avalue[acount] = int
        set acount = acount + 1
    endmethod
    public method insert takes integer int, integer index returns nothing
        //inserts an integer-value at the given index
        local integer i = this.acount
        if (index >= i) then
            set this.avalue[this.acount] = int
            set this.acount = this.acount + 1
            return
        endif
        loop
            set i = i - 1
            set this.avalue[i+1] = this.avalue[i]
            exitwhen i == index
        endloop
        set this.avalue[index] = int
        set this.acount = this.acount + 1
    endmethod
    public method replace takes integer index, integer int returns nothing
        //replaces the integer-value at the given index
        set this.avalue[index] = int
    endmethod
    public method remove takes integer index returns nothing
        //removes an integer-value from the list
        local integer i = index
        local integer lastIndex = this.acount - 1

        if (i >= lastIndex) then
            set this.acount = lastIndex
            return
        else
            loop
                exitwhen i == lastIndex
                set this.avalue[i] = this.avalue[i+1]
                set i = i + 1
            endloop
            set this.acount = lastIndex
        endif
    endmethod
    public method clear takes nothing returns nothing
        set this.acount = 0
    endmethod
    public method value takes integer index returns integer
        //returns the integer-value of a certain index
        return avalue[index]
    endmethod
    public method count takes nothing returns integer
        return this.acount
    endmethod
    public method getIndex takes integer int returns integer
        //returns the index of the first occurence of an integer
        //returns -1 if that integer isn't in the list
        local integer i = 0
        loop
            exitwhen i == this.acount
            if (int == this.avalue[i]) then
                return i
            endif
            set i = i + 1
        endloop
        return -1
    endmethod
    public method getIndexX takes integer int, integer occurrence returns integer
        //returns the index of the by [occurence] given occurence of an integer
        //returns -1 if that integer occures less than [occurence] times
        local integer i = 0
        local integer count = 0
        loop
            exitwhen i == this.acount
            if (int == this.avalue[i]) then
                set count = count + 1
                if (count == occurrence) then
                    return i
                endif
            endif
            set i = i + 1
        endloop
        return -1
    endmethod
    public method occurrenceCount takes integer int returns integer
        //returns the number of occurences of an integer in this list
        local integer i = 0
        local integer count = 0
        loop
            exitwhen i == this.acount
            if (int == this.avalue[i]) then
                set count = count + 1
            endif
            set i = i + 1
        endloop
        return count
    endmethod
    private method mergeUp takes integer lo, integer hi returns nothing
        local integer i = lo
        local integer j = hi
        local integer k = 0
        local integer mid = (lo+hi)/2
        local integer length = hi-lo+1
        local integer array temp
        loop
            exitwhen i>mid
            set temp[k]=this.avalue[i]
            set k = k + 1
            set i = i + 1
        endloop
        loop
            exitwhen j<mid+1
            set temp[k]=this.avalue[j]
            set k = k + 1
            set j = j -1
        endloop
        set i = 0
        set j=length-1
        set k = lo
        loop
            exitwhen i>j
            if (temp[i]<=temp[j]) then
                set this.avalue[k]=temp[i]
                set k = k + 1
                set i = i + 1
            else
                set this.avalue[k]=temp[j]
                set k = k + 1
                set j = j - 1
            endif
        endloop
    endmethod
    private method mergeDown takes integer lo, integer hi returns nothing
        local integer i = lo
        local integer j = hi
        local integer k =0 
        local integer mid = (lo+hi)/2
        local integer length = hi-lo+1
        local integer array temp
        loop
            exitwhen i>mid
            set temp[k]=this.avalue[i]
            set k = k + 1
            set i = i + 1
        endloop
        loop
            exitwhen j<mid+1
            set temp[k]=this.avalue[j]
            set k = k + 1
            set j = j -1
        endloop
        set i = 0
        set j=length-1
        set k = lo
        loop
            exitwhen i>j
            if (temp[i]>=temp[j]) then
                set this.avalue[k]=temp[i]
                set k = k + 1
                set i = i + 1
            else
                set this.avalue[k]=temp[j]
                set k = k + 1
                set j = j - 1
            endif
        endloop
    endmethod
    private method mergeSortUp takes integer lo, integer hi returns nothing
        local integer mid
        if (lo < hi) then
            set mid = (lo + hi)/2
            call mergeSortUp(lo, mid)
            call mergeSortUp(mid+1, hi)
            call mergeUp(lo, hi)
        endif
    endmethod
    private method mergeSortDown takes integer lo, integer hi returns nothing
        local integer mid
        if (lo < hi) then
            set mid = (lo + hi)/2
            call mergeSortDown(lo, mid)
            call mergeSortDown(mid+1, hi)
            call mergeDown(lo, hi)
        endif
    endmethod
    public method sort takes boolean sortOrderUpwards returns nothing
        //Sorts the elements in the array up or downwards, using the merge sort algorithm.
        if (sortOrderUpwards) then
            call mergeSortUp(0, this.acount-1) //calls merge sort algorithm, code-template (java) from [url]http://www.hermann-gruber.com/[/url]
        else
            call mergeSortDown(0, this.acount-1) //calls merge sort algorithm, code-template (java) from [url]http://www.hermann-gruber.com/[/url]
        endif
    endmethod    
endstruct
 
Last edited:
Level 8
Joined
Oct 3, 2008
Messages
367
I must say that I don't find this useful at all. A much more versatile list module pack would be infinitely more useful than a single struct with an instance limit of only 81(!!). And, we have plenty of list module things already. Why use this?
 
Top