- Joined
- Jun 26, 2020
- Messages
- 1,928
I don't know how necessary is this for that reason I don't public this, but I need it.
Requires:
Alloc: https://github.com/nestharus/JASS/blob/master/jass/Systems/Alloc/Standard/script.j
Table: https://www.hiveworkshop.com/threads/snippet-new-table.188084/
ErrorMessage: https://github.com/nestharus/JASS/blob/master/jass/Systems/ErrorMessage/main.j
Requires:
Alloc: https://github.com/nestharus/JASS/blob/master/jass/Systems/Alloc/Standard/script.j
Table: https://www.hiveworkshop.com/threads/snippet-new-table.188084/
ErrorMessage: https://github.com/nestharus/JASS/blob/master/jass/Systems/ErrorMessage/main.j
vJASS:
// This emulates the Lua lists
// Just do:
// <Type>List list = <Type>List.create()
// list.insert(value)
// value = list[index]
//
// To iterate do:
//
// set i = 1
// loop
// exitwhen i > list.length
// set value = list[i]
// // Do the stuff
// set i = i + 1
// endloop
library ArrayList requires Alloc, Table, ErrorMessage
//! runtextmacro LIST("Integer", "integer", "0")
//! runtextmacro LIST("String", "string", "null")
//! runtextmacro LIST("Unit", "unit", "null")
//! textmacro LIST takes NAME, TYPE, NULL
struct $NAME$List extends array
implement Alloc
private Table data
readonly integer length
method clear takes nothing returns nothing
call this.data.flush()
set this.length = 0
endmethod
method destroy takes nothing returns nothing
call this.clear()
call this.data.destroy()
call this.deallocate()
endmethod
// This is to do the classic "MyList[k] = v"
method operator [] takes integer k returns $TYPE$
return this.data.$TYPE$[k]
endmethod
method operator []= takes integer k, $TYPE$ value returns nothing
if k > this.length then
if k == this.length + 1 then
set this.length = k
else
debug call ThrowError(true, "ArrayList", "[]=", "integer", k, "You can't left null spaces in the list.")
return
endif
elseif k < 1 then
debug call ThrowError(true, "ArrayList", "[]=", "integer", k, "You can't use negative indexes in the list.")
return
endif
set this.data.$TYPE$[k] = value
endmethod
// Removes from specific position and move the values in the right 1 cell
method removeFrom takes integer k returns $TYPE$
local $TYPE$ value
local integer i
if k > this.length or k < 0 then
debug call ThrowError(true, "ArrayList", "removeFrom", "integer", k, "You are exceeding the limit of the list.")
return $NULL$
endif
set value = this.data.$TYPE$[this.length]
set i = k
set this.length = this.length - 1
loop
exitwhen i > this.length
set this.data.$TYPE$[i] = this.data.$TYPE$[i + 1]
set i = i + 1
endloop
call this.data.$TYPE$.remove(this.length + 1)
return value
endmethod
// Remove from the last position and return the saved value
method remove takes nothing returns $TYPE$
local $TYPE$ value = this.data.$TYPE$[this.length]
call this.data.$TYPE$.remove(this.length)
set this.length = this.length - 1
return value
endmethod
// Add to specific position and move the values in the right 1 cell
method insertTo takes $TYPE$ value, integer k returns nothing
local integer i
if k > this.length then
debug call ThrowError(true, "ArrayList", "insertTo", "integer", k, "You can't left null spaces in the list.")
return
endif
set i = k
loop
exitwhen i == this.length
set this.data.$TYPE$[i + 1] = this.data.$TYPE$[i]
set i = i + 1
endloop
set this.data.$TYPE$[k] = value
set this.length = this.length + 1
endmethod
// Add to the last position
method insert takes $TYPE$ value returns nothing
set this.length = this.length + 1
set this.data.$TYPE$[this.length] = value
endmethod
static method create takes nothing returns thistype
local thistype this = thistype.allocate()
set this.data = Table.create()
set this.length = 0
return this
endmethod
endstruct
//! endtextmacro
endlibrary
Last edited: