omdl  v0.9.5
OpenSCAD Mechanical Design Library

Map data type and operations. More...

+ Collaboration diagram for Maps:

Files

file  map.scad
 Map data structure and operations.
 

Functions

function map_get_index (m, k)
 Return the index of a map key. More...
 
function map_exists (m, k)
 Test if a key exists. More...
 
function map_get_value (m, k)
 Get the map value associated with a key. More...
 
function map_get_keys (m)
 Get a list of all map keys. More...
 
function map_get_values (m)
 Get a list of all map values. More...
 
function map_get_firstof2_or (m1, m2, k, d)
 Get the the first value associated with an existing key in one of two maps. More...
 
function map_get_size (m)
 Get the number of map entries. More...
 
function map_merge (m1, m2)
 Merge the unique key-value pairs of a second map with those of a first. More...
 
function map_errors (m)
 Perform basic format checks on a map and return errors. More...
 
module map_check (m, verbose=false)
 Perform basic format checks on a map and output errors to console. More...
 
module map_dump (m, sort=false, number=true, p=3)
 Dump each map entry to the console. More...
 
module map_write (m, ks, sort=false, number=false, fs="^", thn="idx", index_tags=empty_lst, key_tags=["b"], value_tags=empty_lst)
 Write formatted map entries to the console. More...
 

Detailed Description

Map data type and operations.

Requires:
include <omdl-base.scad>;

Map use script

include <omdl-base.scad>;
map =
[
["part1", ["screw10", [10, 11, 13]]],
["part2", ["screw12", [20, 21, 30]]],
["part3", ["screw10", [10, 10, -12]]],
["config", ["top", "front", "rear"]],
["version", [21, 5, 0]],
["runid", 10]
];
echo( "### map_check ###" );
map_check(map, true);
echo( "### map_exists ###" );
echo( str("is part0 = ", map_exists(map, "part0")) );
echo( str("is part1 = ", map_exists(map, "part1")) );
echo( "### map_get_value ###" );
p1 = map_get_value(map, "part1");
echo( c=second(p1) );
keys = map_get_keys(map);
parts = delete(keys, mv=["config", "version", "runid"]);
echo( "### map_delete ###" );
for ( p = parts )
echo
(
n=p,
p=first(map_get_value(map, p)),
l=second(map_get_value(map, p))
);
echo( "### map_dump ###" );
map_dump(map);
// end_include
function second(v)
Return the second element of an iterable value.
function first(v)
Return the first element of an iterable value.
function map_get_value(m, k)
Get the map value associated with a key.
function map_get_keys(m)
Get a list of all map keys.
module map_check(m, verbose=false)
Perform basic format checks on a map and output errors to console.
Definition: map.scad:431
module map_dump(m, sort=false, number=true, p=3)
Dump each map entry to the console.
Definition: map.scad:505
function map_exists(m, k)
Test if a key exists.

Map use script output

ECHO: "### map_check ###"
ECHO: "[ INFO ] map_check(); begin map check"
ECHO: "[ INFO ] map_check(); checking map format and keys."
ECHO: "[ INFO ] map_check(); map size: 6 entries."
ECHO: "[ INFO ] map_check(); end map check"
ECHO: "### map_exists ###"
ECHO: "is part0 = false"
ECHO: "is part1 = true"
ECHO: "### map_get_value ###"
ECHO: c = [10, 11, 13]
ECHO: "### map_delete ###"
ECHO: n = "part1", p = "screw10", l = [10, 11, 13]
ECHO: n = "part2", p = "screw12", l = [20, 21, 30]
ECHO: n = "part3", p = "screw10", l = [10, 10, -12]
ECHO: "### map_dump ###"
ECHO: "000: 'part1' = '["screw10", [10, 11, 13]]'"
ECHO: "001: 'part2' = '["screw12", [20, 21, 30]]'"
ECHO: "002: 'part3' = '["screw10", [10, 10, -12]]'"
ECHO: "003: 'config' = '["top", "front", "rear"]'"
ECHO: "004: 'version' = '[21, 5, 0]'"
ECHO: "005: 'runid' = '10'"
ECHO: "map size: 6 entries."

Function Documentation

◆ map_check()

module map_check ( ,
verbose  = false 
)

Perform basic format checks on a map and output errors to console.

Parameters
m<map> A list of N key-value map pairs.
verbose<boolean> Be verbose during check.

Check that: (1) each entry has key-value 2-tuple, (2) each key is a string, and (3) key identifiers are unique.

Definition at line 430 of file map.scad.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ map_dump()

module map_dump ( ,
sort  = false,
number  = true,
= 3 
)

Dump each map entry to the console.

Parameters
m<map> A list of N key-value map pairs.
sort<boolean> Sort the output by key.
number<boolean> Output index number.
p<integer> Number of places for zero-padded numbering.

Definition at line 504 of file map.scad.

+ Here is the call graph for this function:

◆ map_errors()

function map_errors ( )

Perform basic format checks on a map and return errors.

Parameters
m<map> A list of N key-value map pairs.
Returns
<list-N> A list of map format errors.

Check that: (1) each entry has key-value 2-tuple, (2) each key is a string, and (3) key identifiers are unique. When there are no errors, the empty_lst is returned.

◆ map_exists()

function map_exists ( ,
 
)

Test if a key exists.

Parameters
m<map> A list of N key-value map pairs.
k<string> A map key.
Returns
<boolean> true when the key exists and false otherwise.

◆ map_get_firstof2_or()

function map_get_firstof2_or ( m1  ,
m2  ,
,
 
)

Get the the first value associated with an existing key in one of two maps.

Parameters
m1<map> A list of N key-value map pairs.
m2<map> A list of N key-value map pairs.
k<string> A map key.
d<value> A default return value.
Returns
<value> The first value associated with key that exists in maps m1 or m2, otherwise return d when it does not exist in either.
+ Here is the caller graph for this function:

◆ map_get_index()

function map_get_index ( ,
 
)

Return the index of a map key.

Parameters
m<map> A list of N key-value map pairs.
k<string> A map key.
Returns
<integer> The index of the map entry if it exists. Returns undef if key is not a string or does not exists.
+ Here is the caller graph for this function:

◆ map_get_keys()

function map_get_keys ( )

Get a list of all map keys.

Parameters
m<map> A list of N key-value map pairs.
Returns
<string-list-N> A list of key strings for all N map entries.
+ Here is the caller graph for this function:

◆ map_get_size()

function map_get_size ( )

Get the number of map entries.

Parameters
m<map> A list of N key-value map pairs.
Returns
<integer> The number of map entries.
+ Here is the caller graph for this function:

◆ map_get_value()

function map_get_value ( ,
 
)

Get the map value associated with a key.

Parameters
m<map> A list of N key-value map pairs.
k<string> A map key.
Returns
<value> The value associated with key. Returns undef if key does not exists.
+ Here is the caller graph for this function:

◆ map_get_values()

function map_get_values ( )

Get a list of all map values.

Parameters
m<map> A list of N key-value map pairs.
Returns
<list-N> A list of values for all N map entries.

◆ map_merge()

function map_merge ( m1  ,
m2   
)

Merge the unique key-value pairs of a second map with those of a first.

Parameters
m1<map> A list of N key-value map pairs.
m2<map> A list of N key-value map pairs.
Returns
<value> The key value-pairs of m1 together with the unique key value-pairs of m2 that are absent in m1.

◆ map_write()

module map_write ( ,
ks  ,
sort  = false,
number  = false,
fs  = "^",
thn  = "idx",
index_tags  = empty_lst,
key_tags  = ["b"],
value_tags  = empty_lst 
)

Write formatted map entries to the console.

Parameters
m<map> A list of N key-value map pairs.
ks<string-list> A list of selected keys.
sort<boolean> Sort the output by key.
number<boolean> Output index number.
fs<string> A field separator.
thn<string> Column heading for numbered row output.
index_tags<string-list> List of html formatting tags.
key_tags<string-list> List of html formatting tags.
value_tags<string-list> List of html formatting tags.

Output map keys and values the console. To output only select keys, assign the desired key identifiers to ks. For example to output only 'key1' and 'key2', assign ks = ["key1", "key2"]. The output can then be processed to produce documentation tables as shown in the example below.

Map write script

include <omdl-base.scad>;
map =
[
["part1", ["screw10", [10, 11, 13]]],
["part2", ["screw12", [20, 21, 30]]],
["part3", ["screw10", [10, 10, -12]]],
["config", ["top", "front", "rear"]],
["version", [21, 5, 0]],
["runid", 10]
];
map_write(map, index_tags=["center","i"]);
// end_include
module map_write(m, ks, sort=false, number=false, fs="^", thn="idx", index_tags=empty_lst, key_tags=["b"], value_tags=empty_lst)
Write formatted map entries to the console.
Definition: map.scad:672

Map write script output

ECHO: "key^value"
ECHO: "<b>part1</b>^["screw10", [10, 11, 13]]^"
ECHO: "<b>part2</b>^["screw12", [20, 21, 30]]^"
ECHO: "<b>part3</b>^["screw10", [10, 10, -12]]^"
ECHO: "<b>config</b>^["top", "front", "rear"]^"
ECHO: "<b>version</b>^[21, 5, 0]^"
ECHO: "<b>runid</b>^10^"

Map write table

keyvalue
part1["screw10", [10, 11, 13]]
part2["screw12", [20, 21, 30]]
part3["screw10", [10, 10, -12]]
config["top", "front", "rear"]
version[21, 5, 0]
runid10

Definition at line 671 of file map.scad.

+ Here is the call graph for this function: