lua-users home
lua-l archive

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


> Wouldn't it be easy, and very low-overhead, to increment and decrement a
> count for every add/remove? My countpairs() is O(n) :-(

If you really, really need to know the number of key-value pairs in your table
(which is probably not the case, as Javier suggested), you can try this:

-- hash.lua
local new = function()
  local t = {}
  local n = 0
  local u = newproxy(true)
  local mt = getmetatable(u)
  mt.__index = t
  mt.__newindex = function(o, k, v)
    if t[k] == nil and v ~= nil then -- new key?
      n = n + 1
    elseif t[k] ~= nil and v == nil then -- remove key?
      n = n - 1
    end
    t[k] = v
  end
  mt.__len = function() return n end
  mt.__call = function() return next, t, nil end -- pairs
  return u
end

module(..., function(mod) setmetatable(mod, {__call = new}) end)


$ lua -i -lhash
Lua 5.1.1  Copyright (C) 1994-2006 Lua.org, PUC-Rio
> h = hash()
> h.k1 = 10
> h.k2 = "hash"
> = #h
2
> h.k2 = nil
> for k, v in h() do print(k,v) end
k1      10
> = #h
1


It's simple and pure Lua, but I'm not sure it's right. :) I hope you never
need to keep counts on weak tables, as this could get trickier.

Cheers,
Luis.

-- 
A mathematician is a device for turning coffee into theorems.
        -- P. Erdos 

-- 
Luis Carvalho
Applied Math PhD Student - Brown University
PGP Key: E820854A <carvalho@dam.brown.edu>