Skip to content
TheFlyingFiddle edited this page Jul 11, 2015 · 5 revisions

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

In TIER an array is a fixed size sequence of items. With the exception that it has a fixed number of elements it is identical to the list mapping.

Creation

--Creates an array mapping. 
--elment_mapping is the mapping used for the
--               elements of the array.
--size is the fixed number of items in an array.
standard.array(element_mapping, size)

Usage Examples

--We start by fetching the standard libraries.
local tier  = require"tier"
local primitive = tier.primitive
local standard  = tier.standard

--An output file. 
local file = io.open("Arrays.dat", "wb")

--A mapping from a table with 5 integer numbers.
local int_array_mapping = standard.array(primitive.int32, 5)
local int_array_data = { 0, 1, 2, 3, 4 }

--Encodes the integer array
tier.encode(file, int_array_data, int_array_mapping)

--Another mapping this time a table with 3 strings.
local string_array_mapping = standard.array(primitive.string, 3)
local string_array_data = { "First", "Second", "Third" }

--Encodes the string array
tier.encode(file, string_array_data, string_array_mapping)

--A 4x4 matrix of floats.
local matrix_mapping = standard.array(standard.array(primitive.float, 4), 4)
local matrix_data = 
{
   { 1, 0, 0, 0 },
   { 0, 1, 0, 0 },
   { 0, 0, 1, 0 },
   { 0, 0, 0, 1 }
}

--Encodes the matrix
tier.encode(file, matrix_data, matrix_mapping)
file:close()

--We can now decode the values we wrote. 
local infile = io.open("Arrays.dat", "rb")

--Notice that we supply the mappings here. We do not have to do this
--It will work either way, but supplying the mappings will indicate
--What type we want and provide some type checking.
local int_array    = tier.decode(infile, int_array_mapping)
local string_array = tier.decode(infile, string_array_mapping)
local float_matrix = tier.decode(infile, matrix_mapping)
infile:close()

This marks the end of this tutorial. In the next tutorial Encoding Sets we will look at encoding lua tables as sets of of values.

Clone this wiki locally