lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 12/17/2015 02:31 PM, Evandro Leopoldino Gonçalves wrote:
I'm implementing some classical algorithms and data structures in Lua.


Interesting and maybe useful if someone couldn't use any C, only Lua.

Your OO is broken however, at least for binary heap all instances share a common data array (although not size). This is one reason why I dislike "template" style OO, very prone to this kind of error.

local BinaryHeap = require("evandro-lg-binary-heap");

local heap1 = BinaryHeap:new();
local heap2 = BinaryHeap:new();

heap1:add(4);
heap1:add(8);
io.write( "heap1 size: ", heap1:size(),
    "\theap2 size: ", heap2:size(),
    "\theap1 minimum: ", heap1:get_minimum(), "\n" );

heap2:add(1);
io.write( "heap1 size: ", heap1:size(),
    "\theap2 size: ", heap2:size(),
    "\theap1 minimum: ", heap1:get_minimum(), "\n" );

Output, see the second "heap1 minimum" which reflects the number ostensibly inserted into heap2:

heap1 size: 2    heap2 size: 0    heap1 minimum: 4
heap1 size: 2    heap2 size: 1    heap1 minimum: 1

-- David

P.S., what's the license?