Unique Table

A unique table is an expanded table object allowing for easy management of a table where the desired usage of the table is to only store unique items.

Also allows easy addition, subtraction and various other functionality to manage the tables via multiple operators implemented through metamethods.

Loaded in Campaign loaded in campaign
Back to top

Metamethods - Usable Operators

Addition Operator (+)

  • Creates a new unique table adding the two unique tables together, the resulting unique table contains only unique items
  • Subtraction Operator (-)

  • Creates a new unique table removing all of the items from the second unique table from the first unique table
  • Multiplication Operator (*)

  • Creates a new unique table containing only items that exist in both tables
  • Division Operator (/)

  • Creates a new unique table containing only items that only exist in one of the two tables
  • Back to top

    Example Usages

    Example - Creating and Inserting:

    Only unique entires are added to the table, hence in the example below the second attempt to add "first_string" does not work
    local ut1 = unique_table:new();
    ut1:insert("first_string");
    ut1:insert("first_string");
    ut1:insert("second_string");

    -- ut1 Contents: "first_string", "second_string"

    Example - Adding two unique tables together:

    Only unique entires are added to both tables, and upon adding both tables again only unique entires remain in the final table
    local ut1 = unique_table:new();
    ut1:insert("first_string");
    ut1:insert("first_string");
    ut1:insert("second_string");

    local ut2 = unique_table:new();
    ut2:insert("second_string");
    ut2:insert("third_string");

    local ut3 = ut1 + ut2;

    -- ut3 Contents: "first_string", "second_string", "third_string"

    Example - Subracting one unique table from another:

    The entries from the second table are removed from the first and the result is a new unique table
    local ut1 = unique_table:new();
    ut1:insert("first_string");
    ut1:insert("second_string");

    local ut2 = unique_table:new();
    ut2:insert("second_string");

    local ut3 = ut1 - ut2;

    -- ut3 Contents: "first_string"

    Example - Extracting matching items:

    The resulting unique table from the multiplication operator only contains items that are in both of the unique tables supplied
    local ut1 = unique_table:new();
    ut1:insert("first_string");
    ut1:insert("second_string");

    local ut2 = unique_table:new();
    ut2:insert("second_string");
    ut2:insert("third_string");

    local ut3 = ut1 * ut2;

    -- ut3 Contents: "second_string"

    Example - Extracting unique items amongst multiple unique tables:

    The resulting unique table from the division operator only contains items that are not in both of the unique tables supplied
    local ut1 = unique_table:new();
    ut1:insert("first_string");
    ut1:insert("second_string");

    local ut2 = unique_table:new();
    ut2:insert("second_string");
    ut2:insert("third_string");

    local ut3 = ut1 / ut2;

    -- ut3 Contents: "first_string", "third_string"

    Example - Creating a unique table from existing data:

    Here two list interfaces are taken from the model, turned into unique tables and multiplied to get only the items in both lists
    local faction_obj = cm:model():world():faction_by_key(faction_key);
    local non_aggression_pacts = unique_table:faction_list_to_unique_table(faction_obj:factions_non_aggression_pact_with());
    local trading_partners = unique_table:faction_list_to_unique_table(faction_obj:factions_trading_with());

    local trading_partners_with_nap_pacts = non_aggression_pacts * trading_partners;

    -- Result: The final unique table contains only factions with whom the player has both a non-aggression pact and a trade treaty
    Back to top

    Creation

    unique_table:new([table o])

    Creates a new unique table object

    Parameters:

    1

    table

    optional, default value=nil

    Pass an object to the new function to use that instance of the object as this new one

    Returns:

    1. unique_table the new unique table object

    defined in ../working_data/script/_lib/lib_unique_table.lua, line 111

    Back to top

    Usage

    unique_table:insert(object item)

    Adds a new item into the unique table, but only if that item does not already exist

    Parameters:

    1

    object

    The item to add to the table

    Returns:

    1. nil

    defined in ../working_data/script/_lib/lib_unique_table.lua, line 180

    unique_table:remove(object item)

    Removes the given item from the table

    Parameters:

    1

    object

    The item to remove from the table

    Returns:

    1. nil

    defined in ../working_data/script/_lib/lib_unique_table.lua, line 190

    unique_table:contains(object item)

    Returns true if the given item exists in the table

    Parameters:

    1

    object

    The item to check

    Returns:

    1. boolean true if the item exists

    defined in ../working_data/script/_lib/lib_unique_table.lua, line 203

    unique_table:index_of(object item)

    Returns the index of the given item in the table, or 0 if it doesn't exist

    Parameters:

    1

    object

    The item to check

    Returns:

    1. number index of the given item

    defined in ../working_data/script/_lib/lib_unique_table.lua, line 211

    unique_table:to_table()

    Returns the unique table object as a normal table

    Returns:

    1. table the unique table as a normal table

    defined in ../working_data/script/_lib/lib_unique_table.lua, line 224

    Back to top

    Constructing From Existing Data

    unique_table:table_to_unique_table()

    Returns the table as a new unique table object

    Returns:

    1. unique_table the table as a new unique table

    defined in ../working_data/script/_lib/lib_unique_table.lua, line 232

    unique_table:faction_list_to_unique_table(object faction_list, boolean cqi_list)

    Returns a new unique table containing the items from the given faction list

    Parameters:

    1

    object

    The faction list to use

    2

    boolean

    Pass true to make the table use cqi's instead of faction objects

    Returns:

    1. unique_table the new unique table

    defined in ../working_data/script/_lib/lib_unique_table.lua, line 246

    unique_table:character_list_to_unique_table(object character_list)

    Returns a new unique table containing the items from the given character list

    Parameters:

    1

    object

    The character list to use

    Returns:

    1. unique_table the new unique table

    defined in ../working_data/script/_lib/lib_unique_table.lua, line 266

    unique_table:region_list_to_unique_table(object region_list)

    Returns a new unique table containing the items from the given region list

    Parameters:

    1

    object

    The region list to use

    Returns:

    1. unique_table the new unique table

    defined in ../working_data/script/_lib/lib_unique_table.lua, line 280

    Last updated 07/02/21 06:39:14