-
Notifications
You must be signed in to change notification settings - Fork 1
Encoding Maps
This tutorial show how to encode maps of values in the TIER format, The mapping we will be focusing on in this tutorial is standard.map which is located in the tier.standard module.
In TIER a map is a sequence of key value pairs, the keys are unique but the values can be the same. A map mapping is an aggregate mapping that uses two sub mappings. A key mapping for mapping keys and a value mapping for mapping values. The key mapping and the value mapping can be any kinds of mappings but in the usual case the keys are either strings or integers and values can be anything.
The standard.map mapping maps from lua maps to an encoded sequence of key value pairs. A lua table is already a lua map so it does not need to satisfy some specific format. However it's important that all keys can be encoded with the key mapping and that all values with the value mapping.
Creation
-- Creates a map mapping
-- key_mapping the mapping used by keys
-- in a lua table
-- value_mapping the mapping used by values
-- in the lua table
standard.map(key_mapping, value_mapping)Usage Example
local tier = require"tier"
local primitive = tier.primitive
local standard = tier.standard
local outfile = io.open("Maps.dat", "wb")
--Mapping of a map with strings and ints
local string_to_int_mapping = standard.map(primitive.string, primitive.int32)
local string_to_int_data =
{
this = 1, is = 32, a = 123,
mapping = 32, between = 3,
strings = 23, ["and"] = 123,
ints = 61
}
--Encode the string to int map
tier.encode(outfile, string_to_int_data, string_to_int_mapping)
--Mapping of a map
local string_to_string_mapping = standard.map(primitive.string, primitive.string)
local string_to_string_data =
{
A = "T", T = "A",
G = "C", C = "G",
a = "t", t = "a",
g = "c", c = "g"
}
tier.encode(outfile, string_to_string_data, string_to_string_mapping)
outfile:close()
--Reads back the values encoded
local infile = io.open("Maps.dat", "rb")
local string_to_int = tier.decode(infile, string_to_int_mapping)
local string_to_string = tier.decode(infile, string_to_string_mapping)
infile:close()This marks the end of this tutorial. The next tutorial Encoding Tuples is going to be very interesting. We will take a look at encoding lua tables as tuples and how we can use them to create some interesting objects.