lua-users home
lua-l archive

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

Why exactly don't you like replacing table.insert You can still use the original ones, somewhat like this:

local table_insert= table.insert

table.insert= function( ... )		-- ( table, [pos,] value )
	... your code here ...


Norman Ramsey kirjoitti 20.11.2007 kello 22:50:

I have an application for which I am given a list of strings, and I
want to track any additions to the list as well as any mutations of
the strings that are there.  Given a list l, let me write l'
(ell-prime) for the original contents of the list.  Then for each i in
the range from 1 to #l, I'd like to know that exactly one of the
following holds:

  l[i] was inserted into the list by table.insert
  l[i] was obtained by an assignment l[i] = ...
  l[i] is identical to l'[j], where j-i is the number of insertions
       that took place in the appropriate spot

I was hoping to achieve all this with metamethods and a proxy table
(taking the crippling hit that I won't be able to use ipairs with l),
but I can't figure out how table.insert is going to manifest itself to
the metamethods.  I fear what may happen is that every call to
table.insert, no matter what the arguments, is going to appear as a
call to the __newindex(l, #l+1, l[#l]).  This is not much use to me.
The alternative of providing my own versions of table.insert and
table.remove is also not very palatable.

One alternative would be to try to discover changes after the fact by
running some kind of algorithm for computing minimum edit distance.
Does anybody have any suggestions along those lines, or better yet,