- Joined
- May 9, 2014
- Messages
- 1,820
I'm coding a Binary Tree Binary Heap down below.
Any ideas as to how I can improve certain things about it?
JASS:
library BinaryAssortment
module MyAlloc
private static thistype instance = 0
private thistype recNext
static method allocate takes nothing returns thistype
local thistype this
if thistype(0).recNext == 0 then
set instance = instance + 1
return instance
endif
set this = thistype(0).recNext
set thistype(0).recNext = this.recNext
return this
endmethod
method deallocate takes nothing returns nothing
set this.recNext = thistype(0).recNext
set thistype(0).recNext = this
endmethod
endmodule
module PrevNext
thistype prev
thistype next
method push takes nothing returns nothing
set this.next = 0
set this.prev = thistype(0).prev
set this.prev.next = this
set this.next.prev = this
endmethod
method pop takes nothing returns nothing
set this.prev.next = this.next
set this.next.prev = this.prev
endmethod
endmodule
private struct Interface extends array
//has no method whatsoever
endstruct
private struct Node extends array
implement MyAlloc
implement PrevNext
integer value
integer index
Interface Id
static method getNodeUnknown takes integer index, Interface indexId returns thistype
local thistype this = thistype(0).next
loop
exitwhen this == 0 or (this.index == index and this.Id == indexId)
set this = this.next
endloop
return this
endmethod
static method getNode takes integer value, integer index, Interface indexId returns thistype
local thistype this = thistype(0).next
loop
exitwhen this == 0 or (this.value == value and this.index == index and this.Id == indexId)
set this = this.next
endloop
return this
endmethod
static method create takes integer value, integer index, Interface indexId returns thistype
local thistype this = getNode(value, index, indexId)
if this == 0 then
set this = allocate()
set this.index = index
set this.Id = indexId
call this.push()
endif
return this
endmethod
method destroy takes nothing returns nothing
set this.value = 0
set this.index = 0
set this.Id = 0
call this.pop()
call this.deallocate()
endmethod
endstruct
private struct Heap extends array
implement MyAlloc
implement PrevNext
private Node root
private integer index
private Interface interface
static method create takes integer root returns thistype
local thistype this = allocate()
set this.index = this.index + 1
set this.interface = this
set this.root = Node.create(root, this.index, this.interface)
call this.push()
return this
endmethod
method search takes integer value returns Node
local integer i = 1
loop
exitwhen Node.getNodeUnknown(i, this.interface).value == value or i == this.index + 1
set i = i + 1
endloop
if i == this.index + 1 then
return 0
endif
return Node.getNodeUnknown(i, this.interface)
endmethod
method sortAdd takes Node whichNode returns nothing
local Node curr = whichNode
local Node head = Node.getNodeUnknown(curr.index / 2, this.interface)
local integer index = 0
loop
exitwhen curr.value < head.value or head == 0
set index = head.index
set head.index = curr.index
set curr.index = index
set index = curr.value
set curr.value = head.value
set head.value = index
set head = Node.getNodeUnknown(head.index / 2, this.interface)
endloop
endmethod
method add takes integer value returns nothing
local Node temp = this.root
local Node new = Node.getNode(value, this.index, this.interface)
if new == 0 then
set this.index = this.index + 1
set new = Node.create(value, this.index, this.interface)
call this.sortAdd(new)
endif
endmethod
endstruct
endlibrary
Any ideas as to how I can improve certain things about it?
Last edited: