Hiho,
this is my creation for a LinkedList object.
Until now I only noticed that there are LinkedList modules which you have to implement in your struct, scope or library but I thought that some people may need an object of a LinkedList and that's why I made this small snippet.
I hope you like it and that nobody else already made something very similar to this.
Robbepop
this is my creation for a LinkedList object.
Until now I only noticed that there are LinkedList modules which you have to implement in your struct, scope or library but I thought that some people may need an object of a LinkedList and that's why I made this small snippet.
I hope you like it and that nobody else already made something very similar to this.
JASS:
library LinkedList /* v1.0.0
**************************************************************************************************
* A library which gives user the possibility to create LinkedList objects with all
* basic functionalities. It also provides an easy way to define custom LinkedList
* of non-native objects data.
**************************************************************************************************
*
* As LinkedList objects are very flexible all examples are made with the unit data type!
*
* Please give me credits if you use this in your maps.
*
**************************************************************************************************
* API:
* - create
* Creates a new LinkedList object
*
* - destroy
* Destroyes this LinkedList object and all its LinkedListElements.
* Runtime: O(n)
*
* - add
* Adds a new element to this LinkedList object
* Runtime: O(1)
*
* - remove
* Removes an element from this LinkedList object
* Runtime: O(n)
*
* - has
* Checks if this LinkedList object contains the given element
* Runtime: O(n)
*
* - get
* Returns the LinkedListElement object of the given element
* Runtime: O(n)
*
* - getHead
* Returns the first element of this LinkedList object
* Runtime: O(1)
*
* - getSize
* Returns the total amount of elements stored in this LinkedList object
* Runtime: O(1)
*
* - isEmpty
* Checks if this LinkedList object contains no elements
* Runtime: O(1)
**************************************************************************************************
*
**************************************************************************************************
* If you want to create a new LinkedUnitList object you have to type in the following:
* call LinkedUnitList list = LinkedUnitList.create()
*
* Adding a new unit to the list is also very simpel:
* call list.add(udg_myUnit)
* Note: udg_myUnit is just a default variable of type 'unit'!
*
* To check if you LinkedUnitList is empty just type in the following:
* local boolean empty = list.isEmpty()
*
* The rest of the API should be self-explanatory.
**************************************************************************************************
*
**************************************************************************************************
* You can add new LinkedList types (for example for your own object types)
* by extending the text macro in the end of this code snippet.
* Be sure that the FINALIZE parameter is set to "null" if you aren't using native types!
**************************************************************************************************
*/
//! textmacro LinkedListE takes TYPE, NAME, FINALIZE
private struct Linked$NAME$ListElement
public $TYPE$ element
private thistype previous
private thistype next
method destroy takes nothing returns nothing
set .element = $FINALIZE$
set .previous = 0
set .next = 0
call .deallocate()
endmethod
public method hasNext takes nothing returns boolean
return .next != null
endmethod
public method hasPrevious takes nothing returns boolean
return .previous != null
endmethod
public method getNext takes nothing returns thistype
return .next
endmethod
public method getPrevious takes nothing returns thistype
return .previous
endmethod
method setNext takes thistype n returns nothing
set .next = n
endmethod
method setPrevious takes thistype p returns nothing
set .previous = p
endmethod
static method create takes $TYPE$ h returns thistype
local thistype this = thistype.allocate()
set .element = h
set .previous = 0
set .next = 0
return this
endmethod
endstruct
struct Linked$NAME$List
private Linked$NAME$ListElement head
private integer size
public method destroy takes nothing returns nothing
local Linked$NAME$ListElement cur = .head
loop
exitwhen cur == 0
call cur.destroy()
set cur = cur.getNext()
endloop
set .head = 0
set .size = 0
call .deallocate()
endmethod
public method getHead takes nothing returns Linked$NAME$ListElement
return .head
endmethod
public method has takes $TYPE$ h returns boolean
local Linked$NAME$ListElement cur
if h != null and not .isEmpty() then
set cur = .head
loop
exitwhen cur == 0
if cur.element == h then
return true
endif
set cur = cur.getNext()
endloop
endif
return false
endmethod
public method get takes $TYPE$ h returns Linked$NAME$ListElement
local Linked$NAME$ListElement cur
if h != null then
set cur = .head
loop
exitwhen cur == 0
if cur.element == h then
return cur
endif
set cur = cur.getNext()
endloop
endif
return 0
endmethod
public method add takes $TYPE$ h returns boolean
local Linked$NAME$ListElement e
local Linked$NAME$ListElement cur
if h != null and not .has(h) then
set e = Linked$NAME$ListElement.create(h)
if .isEmpty() then
set .head = e
else
set cur = .head
set .head = e
call .head.setNext(cur)
endif
set .size = .size + 1
return true
endif
return false
endmethod
public method remove takes $TYPE$ h returns boolean
local Linked$NAME$ListElement e
local Linked$NAME$ListElement prv
local Linked$NAME$ListElement nxt
if h != null and .has(h) then
set e = .get(h)
set prv = e.getPrevious()
set nxt = e.getNext()
call prv.setNext(nxt)
call nxt.setPrevious(prv)
call e.destroy()
set .size = .size - 1
return true
endif
return false
endmethod
public method getSize takes nothing returns integer
return .size
endmethod
public method isEmpty takes nothing returns boolean
return .size == 0
endmethod
public static method create takes nothing returns thistype
local thistype this = thistype.allocate()
set .head = 0
set .size = 0
return this
endmethod
endstruct
//! endtextmacro
//! runtextmacro LinkedListE("integer", "Integer", "0")
//! runtextmacro LinkedListE("real", "Real", "0")
//! runtextmacro LinkedListE("handle", "Handle", "null")
//! runtextmacro LinkedListE("player", "Player", "null")
//! runtextmacro LinkedListE("unit", "Unit", "null")
//! runtextmacro LinkedListE("item", "Item", "null")
//! runtextmacro LinkedListE("destructable", "Destructable", "null")
//! runtextmacro LinkedListE("location", "Location", "null")
//! runtextmacro LinkedListE("trigger", "Trigger", "null")
//! runtextmacro LinkedListE("effect", "Effect", "null")
//! runtextmacro LinkedListE("rect", "Rect", "null")
//! runtextmacro LinkedListE("region", "Region", "null")
//! runtextmacro LinkedListE("texttag", "Texttag", "null")
//! runtextmacro LinkedListE("trackable", "Trackable", "null")
//! runtextmacro LinkedListE("timer", "Timer", "null")
//! runtextmacro LinkedListE("timerdialog", "Timerdialog", "null")
//! runtextmacro LinkedListE("group", "Group", "null")
//! runtextmacro LinkedListE("force", "Force", "null")
endlibrary
Robbepop