chore: refractor

This commit is contained in:
2026-01-08 22:58:58 +02:00
parent da7de7b08a
commit 2c41f40151
49 changed files with 6 additions and 6 deletions

View File

@@ -0,0 +1,38 @@
local Node = {}
Node.__index = Node
function Node.new(data)
local node = setmetatable({}, Node)
node.data = data
node.left = nil
node.right = nil
return node
end
-- return an iterator that traverses the tree in order
function Node:inorder()
local stack = {}
local current = {self, ""}
table.insert(stack, current)
return function()
while current[1].left do
local parent = current
current = {parent[1].left, parent[2] .. "0"}
table.insert(stack, current)
end
if #stack > 0 then
local node = table.remove(stack)
if node[1].right then
local parent = node
current = {parent[1].right, parent[2] .. "1"}
table.insert(stack, current)
end
return node[1], node[2]
end
end
end
return Node

View File

@@ -0,0 +1,232 @@
--[[
PriorityQueue - v1.0.1 - public domain Lua priority queue
implemented with indirect binary heap
no warranty implied; use at your own risk
based on binaryheap library (github.com/iskolbin/binaryheap)
author: Ilya Kolbin (iskolbin@gmail.com)
url: github.com/iskolbin/priorityqueue
See documentation in README file.
COMPATIBILITY
Lua 5.1, 5.2, 5.3, LuaJIT 1, 2
LICENSE
This software is dual-licensed to the public domain and under the following
license: you are granted a perpetual, irrevocable license to copy, modify,
publish, and distribute this file as you see fit.
--]]
local floor, setmetatable = math.floor, setmetatable
local function siftup( self, from )
local items, priorities, indices, higherpriority = self, self._priorities, self._indices, self._higherpriority
local index = from
local parent = floor( index / 2 )
while index > 1 and higherpriority( priorities[index], priorities[parent] ) do
priorities[index], priorities[parent] = priorities[parent], priorities[index]
items[index], items[parent] = items[parent], items[index]
indices[items[index]], indices[items[parent]] = index, parent
index = parent
parent = floor( index / 2 )
end
return index
end
local function siftdown( self, limit )
local items, priorities, indices, higherpriority, size = self, self._priorities, self._indices, self._higherpriority, self._size
for index = limit, 1, -1 do
local left = index + index
local right = left + 1
while left <= size do
local smaller = left
if right <= size and higherpriority( priorities[right], priorities[left] ) then
smaller = right
end
if higherpriority( priorities[smaller], priorities[index] ) then
items[index], items[smaller] = items[smaller], items[index]
priorities[index], priorities[smaller] = priorities[smaller], priorities[index]
indices[items[index]], indices[items[smaller]] = index, smaller
else
break
end
index = smaller
left = index + index
right = left + 1
end
end
end
local PriorityQueueMt
local PriorityQueue = {}
local function minishigher( a, b )
return a < b
end
local function maxishigher( a, b )
return a > b
end
function PriorityQueue.new( priority_or_array )
local t = type( priority_or_array )
local higherpriority = minishigher
if t == 'table' then
higherpriority = priority_or_array.higherpriority or higherpriority
elseif t == 'function' or t == 'string' then
higherpriority = priority_or_array
elseif t ~= 'nil' then
local msg = 'Wrong argument type to PriorityQueue.new, it must be table or function or string, has: %q'
error( msg:format( t ))
end
if type( higherpriority ) == 'string' then
if higherpriority == 'min' then
higherpriority = minishigher
elseif higherpriority == 'max' then
higherpriority = maxishigher
else
local msg = 'Wrong string argument to PriorityQueue.new, it must be "min" or "max", has: %q'
error( msg:format( tostring( higherpriority )))
end
end
local self = setmetatable( {
_priorities = {},
_indices = {},
_size = 0,
_higherpriority = higherpriority or minishigher
}, PriorityQueueMt )
if t == 'table' then
self:batchenq( priority_or_array )
end
return self
end
function PriorityQueue:enqueue( item, priority )
local items, priorities, indices = self, self._priorities, self._indices
if indices[item] ~= nil then
error( 'Item ' .. tostring(indices[item]) .. ' is already in the heap' )
end
local size = self._size + 1
self._size = size
items[size], priorities[size], indices[item] = item, priority, size
siftup( self, size )
return self
end
function PriorityQueue:remove( item )
local index = self._indices[item]
if index ~= nil then
local size = self._size
local items, priorities, indices = self, self._priorities, self._indices
indices[item] = nil
if size == index then
items[size], priorities[size] = nil, nil
self._size = size - 1
else
local lastitem = items[size]
items[index], priorities[index] = items[size], priorities[size]
items[size], priorities[size] = nil, nil
indices[lastitem] = index
size = size - 1
self._size = size
if size > 1 then
siftdown( self, siftup( self, index ))
end
end
return true
else
return false
end
end
function PriorityQueue:contains( item )
return self._indices[item] ~= nil
end
function PriorityQueue:update( item, priority )
local ok = self:remove( item )
if ok then
self:enqueue( item, priority )
return true
else
return false
end
end
function PriorityQueue:dequeue()
local size = self._size
assert( size > 0, 'Heap is empty' )
local items, priorities, indices = self, self._priorities, self._indices
local item, priority = items[1], priorities[1]
indices[item] = nil
if size > 1 then
local newitem = items[size]
items[1], priorities[1] = newitem, priorities[size]
items[size], priorities[size] = nil, nil
indices[newitem] = 1
size = size - 1
self._size = size
siftdown( self, 1 )
else
items[1], priorities[1] = nil, nil
self._size = 0
end
return item, priority
end
function PriorityQueue:peek()
return self[1], self._priorities[1]
end
function PriorityQueue:len()
return self._size
end
function PriorityQueue:empty()
return self._size <= 0
end
function PriorityQueue:batchenq( iparray )
local items, priorities, indices = self, self._priorities, self._indices
local size = self._size
for i = 1, #iparray, 2 do
local item, priority = iparray[i], iparray[i+1]
if indices[item] ~= nil then
error( 'Item ' .. tostring(indices[item]) .. ' is already in the heap' )
end
size = size + 1
items[size], priorities[size] = item, priority
indices[item] = size
end
self._size = size
if size > 1 then
siftdown( self, floor( size / 2 ))
end
end
PriorityQueueMt = {
__index = PriorityQueue,
__len = PriorityQueue.len,
}
return setmetatable( PriorityQueue, {
__call = function( _, ... )
return PriorityQueue.new( ... )
end
} )

View File

@@ -0,0 +1,114 @@
--Huffman.lua
--iiau, Sat May 18 2024
--Implementation of huffman coding algorithm for use in Roblox
local Huffman = {}
local PriorityQueue = require(script.PriorityQueue)
local Node = require(script.Node)
local BitBuffer = require(script.BitBuffer)
local CHUNK_SIZE = 256
--thanks to https://stackoverflow.com/a/32220398 for helping me with this
local function to_bin(n)
local t = {}
for _ = 1, 32 do
n = bit32.rrotate(n, -1)
table.insert(t, bit32.band(n, 1))
end
return table.concat(t)
end
-- Encode a string to huffman coded string. Limitation is that the data should not be more than 16777215 bytes.
-- @param data The string to encode
-- @return The encoded string
Huffman.encode = function(data: string) : string
assert(#data > 0, "Data must not be empty")
local buffer = BitBuffer()
-- get the frequency of each character in the string
local freq, dict, size = {}, {}, 0
for c in data:gmatch(".") do
freq[c] = (freq[c] or 0) + 1
end
for _ in pairs(freq) do
size += 1
end
local q = PriorityQueue.new 'min'
for k: string, v: number in pairs(freq) do
local leaf = Node.new(string.byte(k))
q:enqueue(leaf, v)
end
while q:len() > 1 do
local left, freq_l = q:dequeue()
local right, freq_r = q:dequeue()
local parent = Node.new()
parent.left = left
parent.right = right
q:enqueue(parent, freq_l + freq_r)
end
local tree = q:dequeue()
buffer.writeUInt8(size-1)
buffer.writeUnsigned(24, #data)
for node, bits: string in tree:inorder() do
if not node.data then
continue
end
local number = tonumber(bits, 2)
local bit_array = string.split(bits, "")
for i = 1, #bit_array do
bit_array[i] = tonumber(bit_array[i])
end
dict[string.char(node.data)] = bit_array
buffer.writeUInt8(node.data) -- char
buffer.writeUnsigned(5, #bits) -- number of bits
buffer.writeUnsigned(#bits, number) -- bits
end
for c in data:gmatch(".") do
buffer.writeBits(table.unpack(dict[c]))
end
-- to avoid the dreaded too many results to unpack error
local chunks = {}
for _, chunk in buffer.exportChunk(CHUNK_SIZE) do
table.insert(chunks, chunk)
end
return table.concat(chunks)
end
-- Decode a string from huffman coded string
-- @param data The string to decode
-- @return The decoded string
Huffman.decode = function(data: string) : string
assert(#data > 0, "Data must not be empty")
local buffer = BitBuffer(data)
local dict_size = buffer.readUInt8()+1
local len_data = buffer.readUnsigned(24)
local dict, read = {}, 0
for i = 1, dict_size do
local char = buffer.readUInt8()
local digits = buffer.readUnsigned(5)
local bits = buffer.readUnsigned(digits)
dict[to_bin(bits):sub(-digits)] = char
end
local decoded = {}
local bits = ""
while read < len_data do
bits ..= buffer.readBits(1)[1]
local char = dict[bits]
if char then
table.insert(decoded, string.char(char))
bits = ""
read += 1
end
end
return table.concat(decoded)
end
return Huffman