- Joined
- Jun 5, 2007
- Messages
- 73
EDIT: Revamped the old post.
This is a new version of my linked list. It allows you to.... well, store structs in a linked-fashion
It doesn't produce memory-leaks (if you use it right) and it's fast!
This is a new version of my linked list. It allows you to.... well, store structs in a linked-fashion
It doesn't produce memory-leaks (if you use it right) and it's fast!
JASS:
//LinkedList for vJass
//Copyright (c) 2008, E.Sandberg (Risc)
//Free for public use.
library LinkedList
function H2I takes handle h returns integer
return h
return 0
endfunction
function H2N takes handle h returns Node
return h
return 0
endfunction
function N2H takes Node n returns handle
return n
return null
endfunction
//Modify this struct to anything you like.
//Remember to modify the dispose-method to work with your struct (to prevent memory leaks)
struct Data
public string name
public unit u
public method dispose takes nothing returns nothing
//this is just an example
//change this to fit your fields
call RemoveUnit(this.u)
set this.u = null
set this.name = null
endmethod
endstruct
struct Node
public handle next
public Data data
public boolean IsTail = false
public method dispose takes nothing returns nothing
call this.data.dispose()
call this.data.destroy()
set this.data = 0
call this.destroy()
endmethod
endstruct
struct List
private Node head
private Node tail
private Node current
public integer count
static method create takes nothing returns List
local List ldummy = List.allocate()
set ldummy.head = Node.create()
set ldummy.tail = Node.create()
set ldummy.tail.IsTail = true
set ldummy.head.next = N2H(ldummy.tail)
return ldummy
endmethod
public method dispose takes nothing returns nothing
//clear list
call this.Clear()
call this.head.destroy()
call this.tail.destroy()
call this.current.destroy()
set this.head = 0
set this.tail = 0
set this.current = 0
set this = 0
endmethod
public method First takes nothing returns Node
return H2N(this.head.next)
endmethod
public method Add takes Data data returns Node
local Node n = Node.create()
local Node temp = this.head
//find last node
loop
exitwhen H2N(temp.next) == this.tail
set temp = H2N(temp.next)
endloop
set n.data = data
set n.next = N2H(this.tail)
set temp.next = N2H(n)
set this.count = this.count + 1
//--- Debug ---
//call BJDebugMsg("Handle to this object is " + I2S(H2I(N2H(n))))
//call BJDebugMsg("Handle to previous object is " + I2S(H2I(N2H(temp))))
return n
endmethod
public method RemoveAndEnumNext takes Node n returns Node
local Node temp = this.head
loop
exitwhen temp.next == N2H(n)
set temp = H2N(temp.next)
endloop
set temp.next = n.next
set this.current = H2N(this.current.next)
set this.count = this.count - 1
call n.dispose()
return this.current
endmethod
public method Clear takes nothing returns nothing
local Node temp = H2N(this.head.next)
//find last node
loop
exitwhen temp.IsTail == true
call temp.data.dispose()
set temp = this.RemoveAndEnumNext(temp)
endloop
set this.head.next = N2H(this.tail)
set this.count = 0
endmethod
public method Remove takes Node n returns nothing
local Node temp = this.head
loop
exitwhen temp.next == N2H(n)
set temp = H2N(temp.next)
endloop
set temp.next = n.next
if this.current == n then
set this.current = H2N(this.current.next)
endif
set this.count = this.count - 1
call n.dispose()
endmethod
public method Enum takes nothing returns Node
set this.current = H2N(this.head.next)
return this.current
endmethod
public method EnumNext takes nothing returns Node
set this.current = H2N(this.current.next)
return this.current
endmethod
endstruct
endlibrary
Last edited: