Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
67 lines (56 sloc) 1.4 KB
# min heap
data owner
data size
data elements[2^256]
def init():
self.owner = msg.sender
def push(item):
if self.owner != msg.sender:
stop
sz = self.size
self.elements[sz + 1] = item
k = sz + 1
while k > 1:
bottom = self.elements[k]
top = self.elements[k/2]
if bottom < top:
self.elements[k] = top
self.elements[k/2] = bottom
k /= 2
else:
k = 0
self.size = sz + 1
def pop():
if self.owner != msg.sender:
stop
sz = self.size
if !sz:
return(0)
prevtop = self.elements[1]
self.elements[1] = self.elements[sz]
self.elements[sz] = 0
top = self.elements[1]
k = 1
while k * 2 < sz:
bottom1 = self.elements[k * 2]
bottom2 = self.elements[k * 2 + 1]
if bottom1 < top and (bottom1 < bottom2 or k * 2 + 1 >= sz):
self.elements[k] = bottom1
self.elements[k * 2] = top
k = k * 2
elif bottom2 < top and bottom2 < bottom1 and k * 2 + 1 < sz:
self.elements[k] = bottom2
self.elements[k * 2 + 1] = top
k = k * 2 + 1
else:
k = sz
self.size = sz - 1
return(prevtop)
def top():
return(self.elements[1])
def size():
return(self.size)
def set_owner(new_owner):
if self.owner != msg.sender:
stop
self.owner = new_owner