katcipis / luanotify

Luanotify is a Lua package providing tools for implementing observer programming pattern
http://wiki.github.com/katcipis/luanotify
GNU Lesser General Public License v3.0
20 stars 4 forks source link

Use the set_up/tear_down functions themselves as keys #6

Closed katcipis closed 14 years ago

katcipis commented 14 years ago

Something like this:

function Signal:add_set_up(set_up_func)

    if (type(set_up_func) ~= "function") then 
        error("add_set_up: expected function...") 
    end

    if not self.set_up_funcs[set_up_func] then
        self.set_up_funcs[#self.set_up_funcs+1] = set_up_func
        self.set_up_funcs[set_up_func] = #self.set_up_funcs
    end
end

function Signal:remove_set_up(set_up_func)
    local pos = self.set_up_funcs[set_up_func]
    if pos then
        table.remove(self.set_up_funcs, pos)
        self.set_up_funcs[set_up_func] = nil
    end
end

The con is that we will have a larger table. The pro is faster lookup ( O(1) instead of O(n) ) of the position of the function (used to check if a function is already inserted and to remove it).

katcipis commented 14 years ago

This one seens to be neat to, lets implement it

ppizarro commented 14 years ago

lets do it

katcipis commented 14 years ago

-closed by d9d294228efde744dfefb37f57127147a558432b and extends the idea to connect too