Dirac
22710180
- Reaction score
- 147
JASS:
library MergeList
//*********************************************************************************
// MergeList
//*********************************************************************************
// MergeList is an efficient method to order your instances
// depending on a number each one has assigned.
//*********************************************************************************
// API:
/*
method insert takes nothing value returns nothing
*/
// -This method allocates your instance in the struct, you must initialize it's
// "sort" value before calling Sort
/*
method delete takes nothing returns nothing
*/
// -Removes the allocated instance from the list, this completly scrambles
// the sorting
/*
method Sort takes nothing returns nothing
*/
// -Sorts all the instances according to their value, you have to call this
// manually because it's faster than calling it everytime an instance is
// allocated
/*
this<i>
*/
// -Use this to reffer to instance number <i> on the list of instances.
// Ex: head<i>.unit
/*
.position
*/
// -Returns the position of that instance in the list
//*********************************************************************************
// How to use:
//
// -Implement ML at the top of you struct
// -You must have this method somewhere in your struct, it determines wether
// it orders the instance by the highest number or the lowest
/*
private static method compare takes thistype v1,thistype v2 returns boolean
return v1.yourVariable<v2.yourVariable
endmethod
*/
// -Use < for lowest and > for highest
//*********************************************************************************
globals
private constant integer ARRAY_SIZE=100
endglobals
module ML
private thistype array node[ARRAY_SIZE]
private integer size=0
private thistype head
readonly integer position
private method Merge takes integer start,integer split,integer max returns nothing
local integer k=start
local integer i=start
local integer j=split+1
local thistype array t
loop
exitwhen (i>split) or (j>max)
if compare(.node<i>,.node[j]) then
set t[k]=.node<i>
set i=i+1
else
set t[k]=.node[j]
set j=j+1
endif
set k=k+1
endloop
loop
exitwhen (i>split)
set t[k]=.node<i>
set i=i+1
set k=k+1
endloop
loop
exitwhen (j>max)
set t[k]=.node[j]
set j=j+1
set k=k+1
endloop
loop
exitwhen (k==start)
set k=k-1
set thistype(t[k]).position=k
set .node[k]=t[k]
endloop
endmethod
method Sort takes nothing returns nothing
local integer i=1
local integer j=1
local integer n=0
loop
exitwhen (i>.size)
set i=i*2
loop
exitwhen (n*i>.size)
set n=n+1
if n*i<=.size then
call .Merge(j,(n*i+j)/2,n*i)
else
call .Merge(j,(n*i+j)/2,.size)
endif
set j=n*i+1
endloop
set j=1
set n=0
endloop
endmethod
method delete takes integer whichOne returns nothing
set .head.node[whichOne]=.head.node[.size]
set .head.size=.head.size-1
call .destroy()
endmethod
method insert takes nothing returns thistype
local thistype i=.size+1
local thistype a=thistype.allocate()
set a.head=this
set .size=i
set .node<i>=a
return a
endmethod
method operator[] takes integer i returns thistype
return .node<i>
endmethod
endmodule
endlibrary</i></i></i></i></i></i></i></i>
JASS:
scope TEST
struct someStruct
private integer value
private static method compare takes thistype v1,thistype v2 returns boolean
return v1.value<v2.value
endmethod
implement ML
private static method AfterWait takes nothing returns nothing
local integer i=1
local thistype array head
local thistype array d
set head[1]=thistype.create()
set d[1]=head[1].insert()
set d[1].value=982
set d[2]=head[1].insert()
set d[2].value=594
set d[3]=head[1].insert()
set d[3].value=114
set d[4]=head[1].insert()
set d[4].value=435
set d[5]=head[1].insert()
set d[5].value=84
set d[6]=head[1].insert()
set d[6].value=13
set head[2]=thistype.create()
set d[1]=head[2].insert()
set d[1].value=1409
set d[2]=head[2].insert()
set d[2].value=7655
set d[3]=head[2].insert()
set d[3].value=2450
set d[4]=head[2].insert()
set d[4].value=2309
set d[5]=head[2].insert()
set d[5].value=1943
call head[1].Sort()
call head[2].Sort()
loop
exitwhen i>6
call BJDebugMsg(I2S(head[1]<i>.value))
set i=i+1
endloop
set i=1
loop
exitwhen i>5
call BJDebugMsg(I2S(head[2]<i>.value))
set i=i+1
endloop
//13 - 84 - 114 - 435 - 594 - 982 - 1409 - 1943 - 2309 - 2450 - 7655
endmethod
//i add this so the debug mesages show up on the message log
private static method onInit takes nothing returns nothing
call TimerStart(CreateTimer(),0,false,function thistype.AfterWait)
endmethod
endstruct
endscope</i></i>
Changelog
Version 0.02
-Allows multiple lists for the same struct
-No longer requires the struct to extend array