Skip to content
TheFlyingFiddle edited this page Jan 16, 2017 · 5 revisions

This tutorial shows how to encode sets of values in the TIER format. The mapping we will be focusing on in this tutorial is standard.set which is located in the tier.standard module.

In TIER a set is a sequence of unique elements, that is no element is repeated in the sequence. A set mapping is an aggregate mapping that uses a single element mapping to encode and decode the elements in a sequence. Like the other aggregate types the set is composable so sets within sets is possible.

The set mapping maps from lua sets to an encoded sequence of elements. For the purposes of the standard.set mapping a lua set is a lua table where the keys are used to contain the elements with a value of true.

Lua set examples

local string_set = 
{
   this = true,   is   = true,
   a    = true,   set  = true,
   of   = true,   strings = true
}

local number_set = 
{
   [123] = true, [12] = true,
   [56]  = true, [132] = true
} 

Creation

--Creates a set mapping
--element_mapping the mapping used by the 
--                elements in the set.
standard.set(element_mapping)

Usage Examples

local tier  = require"tier"
local primitive = tier.primitive
local standard  = tier.standard

local outfile = io.open("Sets.dat", "wb")

--Mapping of a lua set of integers
local int_set_mapping = standard.set(primitive.int32)
local int_set_data =
{
  [2] = true, [3] = true,
  [5] = true, [7] = true,
  [11] = true, [13] = true
}

tier.encode(outfile, int_set_data, int_set_mapping)
outfile:close()

--We can read the data back like this
local infile = io.open("Sets.dat", "rb")
local int_set = tier.decode(infile, int_set_mapping)
infile:close()

This marks the end of this tutorial. In the next tutorial Encoding Maps we will take a look at how we can encode lua tables as maps.

Clone this wiki locally