Home | Libraries | People | FAQ | More |
The dynamic variable is accompanied by a number of algorithms, but also works with most standard C++ algorithms.
Counts the number of matching keys or values in a dynamic variable.
Note | |
---|---|
|
There are two count algorithms. One counts matching keys, and the other
counts matching values. They are located in the dynamic::key
and dynamic::value
namespaces respectively.
Expression |
Return type |
Semantics |
Conditions |
---|---|---|---|
|
|
Counts elements with key |
Requires:
Effects:
Returns:
Number of elements in |
|
|
Counts elements with value |
Requires:
Effects:
Returns:
Number of elements in |
Finds an element by key or by value in a dynamic variable.
Note | |
---|---|
|
Expression |
Return type |
Semantics |
Conditions |
---|---|---|---|
|
|
Finds element with key in dynamic variable. |
Requires:
Effects:
Returns:
Iterator pointing to element in |
|
|
Finds element with value in dynamic variable. |
Requires:
Effects:
Returns:
Iterator pointing to element in |
Invokes a function call operator on a visitor object with the stored value of the dynamic variable as the function parameter.
Note | |
---|---|
|
In addition to various ways of doing type checking, we can also invoke a typed callback on a customized visitor object. The function call operator always takes a single input parameter whose type is one of the supported types. The normal C++ function overloading rules applies when selecting which function call operator to invoke.
struct my_visitor { template <typename T> void operator()(T value) { std::cout << value << std::end; } }; int main() { dynamic::variable data = { true, 2, 3.0, "alpha" }; my_visitor visitor; dynamic::visit(vistor, data); return 0; }
The function call operator may return a value. All function call operators
must use the same return type, which is also the return type of the dynamic::visit
algorithm.
struct my_returning_visitor { template <typename T> dynamic::symbol::value operator()(T value) { dynamic::variable tmp(value); return tmp.symbol(); } }; int main() { dynamic::variable data = { true, 2, 3.0, "alpha" }; my_returning_visitor visitor; auto symbol = dynamic::visit(vistor, data); assert(symbol == dynamic::symbol::array); return 0; }
The dynamic variable works with standard C++ algorithms that require at most bi-directional iterators.
Some algorithms assume that if they take two ranges, then the second range
is at least as long as the first. dynamic::nullable
is an empty container, so it cannot be used as the second range.
The algorithms listed in the table below have been verified. Excluded are
sorting algorithms and algorithms requiring special operators apart from
operator+
(e.g. std::inner_product
without binary predicates.)
Algorithm |
Caveat |
---|---|
|
None. |
|
None. |
|
None. |
|
None. |
|
None. |
|
None. |
|
None. |
|
None. |
|
None. |
|
Using
Using |
|
None. |
|
Using |
|
Using |
|
None. |
|
Only arithmetic types can be inserted. |
|
None. |
|
None. |
|
None. |
|
None. |
|
None. |
|
Using |
|
None. |
|
None. |
|
None. |
|
None. |
|
None. |
|
None. |
|
In associated arrays entries are removed by value but the key order is kept. |
|
Cannot insert container as new value because iterators will be changed during replacement. |
|
None. |
|
No effect on singular values. |
|
Using |
|
None. |
|
Using
Using |
|
None. |
|
None. |
|
None. |