lua-users home
lua-l archive

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



On Jan 31, 2005, at 05:27, Javier Guerra wrote:

a crude, but working hack would be to write a function that returns a string that would be the same for any identical table. similar in concept to a hash code, but without collitions and the performance issues would be managed
transparently by the internal string hashing.

"Crudity" it is! Let the VM itself worry about all the hashing :)

        -- private instance method to convert a given key to a string
        local stringForKey = function( aKey )
                if aKey ~= nil and type( aKey ) ~= "string" then
                        if this.isKindOf( aKey ) == true then
                                aKey = aKey.toString()
                        else
                                aKey = tostring( aKey )
                        end
                end

                return aKey
        end

        -- instance method to return a value for a given key
        this.get = function( aKey )
                if aKey ~= nil then
                        aKey = stringForKey( aKey )

                        return ivars[ aKey ]
                end

                return nil
        end

        -- instance method to associate a given value with the given key
        this.put = function( aKey, aValue )
                if aKey ~= nil then
                        aKey = stringForKey( aKey )

                        ivars[ aKey ] = aValue
                end

                return this
        end


Cheers

--
PA, Onnay Equitursay
http://alt.textdrive.com/