SecreC 2 language  2.8.0 (2023.09)
Language and standard library reference
Vector map

Vector maps in SecreC

Vector map

The table database module includes a data structure called vector map which can be used for heterogeneous vectors, for dynamically typed data or as an associative data structure (similar to Python dictionaries). The table database page describes how vector maps are used for working with databases.

A vector in a vector map contains SecreC vectors. Vector map string vectors are an exception as they can only contain SecreC strings since SecreC doesn't support string vectors.

Listing 1: Examples

import shared3p;
import shared3p_table_database;
import stdlib;
import table_database;
domain pd_shared3p shared3p;
void main() {
uint vmap = tdbVmapNew();
// We can add values of different type to the same vector so this
// can be used as a heterogeneous collection.
uint[[1]] x = {1, 2};
tdbVmapAddValue(vmap, "key", x);
float[[1]] y = {30};
tdbVmapAddValue(vmap, "key", y);
uint[[1]] xres = tdbVmapGetValue(vmap, "key", 0 :: uint);
print("First element of 'key':");
printVector(xres);
float[[1]] yres = tdbVmapGetValue(vmap, "key", 1 :: uint);
print("Second element of 'key':");
printVector(yres);
// We have different functions for string types. Strings and
// primitive types can't be in the same vector so we use a
// different key in the vector map.
tdbVmapAddString(vmap, "strings", "foobar");
print("First element of 'strings': ", tdbVmapGetString(vmap, "strings", 0 :: uint));
// Count the number of elements in a vector
print("Number of elements of 'key': ", tostring(tdbVmapValueVectorSize(vmap, "key")));
// Count the number of vectors in a vector map with a specific
// key. This is useful to test if the map contains the key.
print("Map contains 'baz'? ", tdbVmapCount(vmap, "baz") > 0);
}
void printVector(T[[1]] vec)
Definition: stdlib.sc:389
void tdbVmapAddValue(uint64 id, string paramname, D T value)
Definition: shared3p_table_database.sc:92
D T tdbVmapGetValue(uint64 id, string paramname, uint64 index)
Definition: shared3p_table_database.sc:157
void tdbVmapAddString(uint64 id, string paramname, string str)
Definition: table_database.sc:180
uint64 tdbVmapCount(uint64 id, string paramname)
Definition: table_database.sc:124
void tdbVmapDelete(uint64 id)
Definition: table_database.sc:112
string tdbVmapGetString(uint64 id, string paramname, uint64 index)
Definition: table_database.sc:580
uint64 tdbVmapNew()
Definition: table_database.sc:100
uint64 tdbVmapValueVectorSize(uint64 id, string paramname)
Definition: table_database.sc:152