Commit Graph

8 Commits

Author SHA1 Message Date
Max Cahill
de5c803389 fixed set not specifying deep or shallow copy 2022-03-07 11:59:23 +11:00
David Briscoe
66a6c5a50e Add comments for things that surprised me
As a new user, there were things I was skeptical about and after digging
in, these were my conclusions.

Compared to the simple and obvious lua wiki solutions, batteries' string
functions are slightly faster. GC is the same.

Test
    local str = "hello world"
    local fn = function()
        local x = 0
        if stringx.ends_with(str, "h") then
            x = x + 1
        end
        if stringx.ends_with(str, "helll") then
            x = x + 1
        end
        if stringx.ends_with(str, "helicopter") then
            x = x + 1
        end
    end
    local pretty = require "inspect"
    print("stringx =", pretty({
                time_taken = {measure.time_taken(fn, 10000)},
                memory_taken = {measure.memory_taken(fn, 10000)}
        }))
    local function starts_with(str, prefix)
        return str:find(prefix, 1, true) == 1
    end
    local function ends_with(str, ending)
        return ending == "" or str:sub(-#ending) == ending
    end
    local fn = function()
        local x = 0
        if ends_with(str, "h") then
            x = x + 1
        end
        if ends_with(str, "helll") then
            x = x + 1
        end
        if ends_with(str, "helicopter") then
            x = x + 1
        end
    end
    print("find =", pretty({
                time_taken = {measure.time_taken(fn, 10000)},
                memory_taken = {measure.memory_taken(fn, 10000)}
        }))

starts_with
===========

stringx =       {
  memory_taken = { 0, 0, 0 },
  time_taken = { 1.5098012518138e-007, 9.988434612751e-008, 2.1699932403862e-005 }
}
find =  {
  memory_taken = { 0, 0, 0 },
  time_taken = { 2.7349997544661e-007, 1.9988510757685e-007, 9.1999536380172e-006 }
}

ends_with
=========

stringx =       {
  memory_taken = { 0, 0, 0 },
  time_taken = { 9.0479978825897e-008, 0, 2.5199959054589e-005 }
}
find =  {
  memory_taken = { 0, 0, 0 },
  time_taken = { 2.1833006758243e-007, 1.9988510757685e-007, 6.1000464484096e-006 }
}
2022-03-03 10:17:34 -08:00
Max Cahill
3cc177a0c0 BREAKING class interface refactor - all classes will need a minor update, see class.lua
tl;dr is that new no longer needs to call init, calling :new() directly in user code is not allowed, properties are copied, metamethods work, and a config table is needed rather than a class to extend from, so use {extends = superclass} if you want a minimal fix
2021-07-15 16:15:27 +10:00
Max Cahill
c2a9d39bcf [added] set.clear which does what you'd expect 2021-05-04 10:38:07 +10:00
Max Cahill
699affd1ad [added] set:size and set:get(index) 2020-08-29 21:12:56 +10:00
Max Cahill
028b21ef9a [added] set:to_table which returns the ordered contents unchanged and intentionally disables the set object itself 2020-08-24 20:54:24 +10:00
Max Cahill
6f0945a675 [added] set:values_readonly() to avoid a copy if you only need readonly access, with a note about safety 2020-05-02 20:12:59 +10:00
Max Cahill
e5be0df492 [added] set module with full (-ish?) suite of set operations and efficient (non-linear) membership testing
closes #4
2020-05-02 19:59:02 +10:00