mirror of
https://github.com/appgurueu/modlib.git
synced 2024-08-19 04:44:11 +02:00
Add min-heap
This commit is contained in:
parent
61b48e09da
commit
2b881acc28
50
heap.lua
Normal file
50
heap.lua
Normal file
@ -0,0 +1,50 @@
|
||||
local metatable = {__index = getfenv(1)}
|
||||
|
||||
function default_less_than(a, b) return a < b end
|
||||
|
||||
--> empty min heap
|
||||
function new(less_than)
|
||||
return setmetatable({less_than = less_than or default_less_than}, metatable)
|
||||
end
|
||||
|
||||
function push(self, value)
|
||||
table.insert(self, value)
|
||||
local function heapify(index)
|
||||
if index == 1 then
|
||||
return
|
||||
end
|
||||
local parent = math.floor(index / 2)
|
||||
if self.less_than(self[index], self[parent]) then
|
||||
self[parent], self[index] = self[index], self[parent]
|
||||
heapify(parent)
|
||||
end
|
||||
end
|
||||
heapify(#self)
|
||||
end
|
||||
|
||||
function pop(self)
|
||||
local value = self[1]
|
||||
local last = #self
|
||||
if last == 1 then
|
||||
self[1] = nil
|
||||
return value
|
||||
end
|
||||
self[1], self[last] = self[last], nil
|
||||
last = last - 1
|
||||
local function heapify(index)
|
||||
local left_child = index * 2
|
||||
if left_child > last then
|
||||
return
|
||||
end
|
||||
local smallest_child = left_child + 1
|
||||
if smallest_child > last or self.less_than(self[left_child], self[smallest_child]) then
|
||||
smallest_child = left_child
|
||||
end
|
||||
if self.less_than(self[smallest_child], self[index]) then
|
||||
self[index], self[smallest_child] = self[smallest_child], self[index]
|
||||
heapify(smallest_child)
|
||||
end
|
||||
end
|
||||
heapify(1)
|
||||
return value
|
||||
end
|
3
init.lua
3
init.lua
@ -68,7 +68,8 @@ for _, component in ipairs{
|
||||
"text",
|
||||
"vector",
|
||||
"minetest",
|
||||
"trie"
|
||||
"trie",
|
||||
"heap"
|
||||
} do
|
||||
modlib[component] = loadfile_exports(get_resource(component .. ".lua"))
|
||||
end
|
||||
|
15
test.lua
15
test.lua
@ -54,4 +54,19 @@ if tests.liquid_raycast then
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
local n = 100
|
||||
local list = {}
|
||||
for index = 1, n do
|
||||
list[index] = index
|
||||
end
|
||||
modlib.table.shuffle(list)
|
||||
local heap = modlib.heap.new()
|
||||
for index = 1, #list do
|
||||
heap:push(list[index])
|
||||
end
|
||||
for index = 1, #list do
|
||||
local popped = heap:pop()
|
||||
assert(popped == index)
|
||||
end
|
Loading…
Reference in New Issue
Block a user