Mapped data access via key-value pairs.
More...
|
file | map.scad |
| Mapped key-value pair data access.
|
|
|
function | map_get_idx (map, key) |
| Return the index for the storage location of a map key-value pair. More...
|
|
function | map_exists (map, key) |
| Test if a key exists in a map. More...
|
|
function | map_get (map, key) |
| Get the value associated with a map key. More...
|
|
function | map_get_keys (map) |
| Get a vector of the map entry identifier keys. More...
|
|
function | map_get_values (map) |
| Get a vector of the map entry values. More...
|
|
function | map_size (map) |
| Get the number of key-value pairs stored in a map. More...
|
|
module | map_check (map, verbose=false) |
| Perform some basic validation/checks on a map. More...
|
|
module | map_dump (map, sort=true, number=true, p=3) |
| Dump each map key-value pair to the console. More...
|
|
Mapped data access via key-value pairs.
Example
use <map.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( str(
"is part0 = ",
map_exists(map,
"part0")) );
echo( str(
"is part1 = ",
map_exists(map,
"part1")) );
parts = delete(keys, mv=["config", "version", "runid"]);
for ( p = parts )
echo
(
n=p,
);
Result
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: "is part0 = false"
ECHO: "is part1 = true"
ECHO: c = [10, 11, 13]
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: "003: 'config' = '["top", "front", "rear"]'"
ECHO: "000: 'part1' = '["screw10", [10, 11, 13]]'"
ECHO: "001: 'part2' = '["screw12", [20, 21, 30]]'"
ECHO: "002: 'part3' = '["screw10", [10, 10, -12]]'"
ECHO: "005: 'runid' = '10'"
ECHO: "004: 'version' = '[21, 5, 0]'"
ECHO: "map size: 6 entries."
module map_check |
( |
map |
, |
|
|
verbose |
= false |
|
) |
| |
Perform some basic validation/checks on a map.
- Parameters
-
map | <2d-vector> A two dimensional vector (2-tuple x n-tuple) containing an associative map with n elements. |
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 169 of file map.scad.
module map_dump |
( |
map |
, |
|
|
sort |
= true , |
|
|
number |
= true , |
|
|
p |
= 3 |
|
) |
| |
Dump each map key-value pair to the console.
- Parameters
-
map | <2d-vector> A two dimensional vector (2-tuple x n-tuple) containing an associative map with n elements. |
sort | <boolean> Sort the output by key. |
number | <boolean> Output index number. |
p | <integer> Number of places for zero-padded numbering. |
Definition at line 245 of file map.scad.
function map_exists |
( |
map |
, |
|
|
key |
|
|
) |
| |
Test if a key exists in a map.
- Parameters
-
map | <2d-vector> A two dimensional vector (2-tuple x n-tuple) containing an associative map with n elements. |
key | <string> A map entry identifier. |
- Returns
- <boolean> true when the key exists and false otherwise.
function map_get |
( |
map |
, |
|
|
key |
|
|
) |
| |
Get the value associated with a map key.
- Parameters
-
map | <2d-vector> A two dimensional vector (2-tuple x n-tuple) containing an associative map with n elements. |
key | <string> A map entry identifier. |
- Returns
- The map value associated with
key
. Returns undef if key
does not exists.
function map_get_idx |
( |
map |
, |
|
|
key |
|
|
) |
| |
Return the index for the storage location of a map key-value pair.
- Parameters
-
map | <2d-vector> A two dimensional vector (2-tuple x n-tuple) containing an associative map with n elements. |
key | <string> A map entry identifier. |
- Returns
- <integer> The index of the value associated
key
in the map. Returns undef if key
is not a string or does not exists.
function map_get_keys |
( |
map |
| ) |
|
Get a vector of the map entry identifier keys.
- Parameters
-
map | <2d-vector> A two dimensional vector (2-tuple x n-tuple) containing an associative map with n elements. |
- Returns
- <vector> A vector of keys that exist in the associative map.
- Note
- Uses function eselect to select the first column of the vector defining the map.
function map_get_values |
( |
map |
| ) |
|
Get a vector of the map entry values.
- Parameters
-
map | <2d-vector> A two dimensional vector (2-tuple x n-tuple) containing an associative map with n elements. |
- Returns
- <vector> A vector of values stored in the associative map.
- Note
- Uses function eselect to select the last column of the vector defining the map.
function map_size |
( |
map |
| ) |
|
Get the number of key-value pairs stored in a map.
- Parameters
-
map | <2d-vector> A two dimensional vector (2-tuple x n-tuple) containing an associative map with n elements. |
- Returns
- <integer> The number of key-value pairs stored in the map.