Module: lib-tde.datastructure.set

Lua Implementation of a set

This module describes the api usage of a set (List with only unique elements) You can add items to the set or remove them

local set = require("lib-tde.datastructure.set")
local list = set()
list.add("first")
list.add("second")
list.remove("first") -- returns true, to indicate that it exists
list.add("second") -- returns false, to indicate it already exists

list.to_list() -- Returns a plain lua list with all elements
list.to_ordered_list() -- returns a plain lua list with all elements, ordered in the way you put them in

Time complexity:

  • Insert element O(1)
  • Remove element O(1)
  • To List O(1)
  • To Ordered List O(1)

Class Hierarchy

  • lib-tde.datastructure.set

Info:

  • Copyright: 2020 Tom Meyers
  • Author: Tom Meyers

Static module functions

lib-tde.datastrucuture.set () -> table Create a new Set (List with only unique values)
lib-tde.datastrucuture.set.add (value) -> () Add an element to the set, if it was successfully added return true (didn't exist yet)
lib-tde.datastrucuture.set.remove (value) -> () Remove an element from the set, if it was successfully removed return true
lib-tde.datastrucuture.set.to_ordered_list () -> () Convert the set to a regular lua list, in the order that you added the elements
lib-tde.datastrucuture.set.to_list () -> () Convert the set to a regular lua list
lib-tde.datastrucuture.set.iterate () -> () Iterate over the set with an iterator (much like ipairs())
lib-tde.datastrucuture.set.exists (value) -> () Check if an element exists in the set


Static module functions

lib-tde.datastrucuture.set () -> table
Create a new Set (List with only unique values)

Returns:

    table An empty set

Usage:

    -- This will create a new set
     lib-tde.datastrucuture.set()
lib-tde.datastrucuture.set.add (value) -> ()
Add an element to the set, if it was successfully added return true (didn't exist yet)

Parameters:

  • value object The value to put into the set

Returns:

    boolean Returns true if a new element was inserted, false if it already exists

Usage:

    -- Add the string 'tde' to the set
       set.add("tde")
lib-tde.datastrucuture.set.remove (value) -> ()
Remove an element from the set, if it was successfully removed return true

Parameters:

  • value object The value to remove from the set

Returns:

    boolean Returns true if the element was removed, false if it didn't exist

Usage:

    -- Remove the string 'tde' from the set
       set.remove("tde")
lib-tde.datastrucuture.set.to_ordered_list () -> ()
Convert the set to a regular lua list, in the order that you added the elements

Returns:

    table A regular lua list with elements in the order that you added them

Usage:

    local list = set.to_ordered_list()
lib-tde.datastrucuture.set.to_list () -> ()
Convert the set to a regular lua list

Returns:

    table A regular lua list

Usage:

    local list = set.to_list()
lib-tde.datastrucuture.set.iterate () -> ()
Iterate over the set with an iterator (much like ipairs())

Returns:

  1. fun(t: table, i?: integer):integer, any iterator
  2. table t
  3. integer i

Usage:

    for index, value in set.iterate() do
     print("Index: " .. index)
     print("Value: " .. value)
    end
lib-tde.datastrucuture.set.exists (value) -> ()
Check if an element exists in the set

Parameters:

  • value object The value to find in the set

Returns:

    boolean Returns true if the element exists

Usage:

    set.exists("tde")
generated by LDoc 1.4.6 Build with 💜 By F0xedb Last updated 2021-05-21 03:30:15