https://gitlab.inria.fr/line/aide-group/macrovsa
Tip revision: 31a87d848f8ab28a06ccf77d0b359fc966974138 authored by vthierry on 15 December 2025, 21:31:50 UTC
sync from makefile
sync from makefile
Tip revision: 31a87d8
AssociativeMap.hpp
#ifndef __macrovsa_AssociativeMap__
#define __macrovsa_AssociativeMap__
#include "Symbol.hpp"
#include "Bundling.hpp"
#include "Binding.hpp"
#include <map>
namespace macrovsa {
/**
* @class AssociativeMap
* @description Implements a macroscopic ersatz of a VSA associative map.
* - An associative map is created adding new key/value pairs of symbol.
* - It is implemented via [Bundling](./Bundling.html) and [Binding](./Binding.html), without using a C++ `std::map`.
* @extends Bundling
* @param {string} [name] An optional name. By default, a `#index` unique name is generated.
*/
class AssociativeMap: public Symbol {
// Non assignable
AssociativeMap& operator = (const AssociativeMap&) = delete;
// Binding map to accelerate the get function with remanence mechanism
std::unordered_map < unsigned int, std::pair < const Symbol *, std::unordered_map < unsigned int, Symbol * >> > values;
// Symbol remamence
mutable Symbols symbols;
// Bundling mirror
mutable Bundling bundling;
mutable bool values_changed = false;
public:
AssociativeMap() : Symbol("", associativemap) {}
AssociativeMap(String name) : Symbol(name, associativemap) {}
AssociativeMap(const AssociativeMap&);
virtual ~AssociativeMap() {}
virtual const Belief& getBelief() const;
virtual void setBelief(double tau, double sigma);
virtual bool equals(const Symbol& symbol, char what = 'i') const;
virtual void setVector(double *vector) const;
/**
* @function add
* @memberof AssociativeMap
* @instance
* @description Adds a new symbol pair to the container.
* - Adding twice the same key/value pair corresponds to adding their belief level `tau`.
* @param {Symbol} key The symbol key to add or modify.
* @param {Symbol} value The symbol value to add.
*/
void add(const Symbol& key, const Symbol& value);
/**
* @function erase
* @memberof AssociativeMap
* @instance
* @description Erases a symbol in the container.
* @param {Symbol} [key] The symbol key to add or modify.
* - Without key argument erases all keys.
*/
virtual void erase(const Symbol& key);
virtual void clear();
/**
* @function get
* @memberof AssociativeMap
* @instance
* @description Returns an approximate value of the associative map by unbinding.
* - For a map with `>_i B_(y_i) x_i` returns
* - `x_i` if `y_i` is a map key mapping only one symbol.
* - `>_(i_j) x_(i_j)` if `y_i` is a map key mapping several symbols `x_(i_j)`.
* - `B(y_i~) >_j B_(y_j) x_j` if `y_i` is not a map key.
* @param {Symbol|String} key The symbol key `y_i`.
* @return {Symbol} value A reference to the stored value, available until program end.
* - If several values associated to a key, it is returned as a bundling.
*/
const Symbol& get(const Symbol& key) const;
const Symbol& get(String key) const
{
Symbol *skey = new Symbol(key);
const Symbol& result = get(*skey);
delete skey;
return result;
}
/**
* @function get
* @memberof AssociativeMap
* @instance
* @description Defines an iterator over the associative map symbols, used in a construct of the form:
* ```
* for(auto it = associativeMap.get().cbegin(); it != associativeMap.get().cend(); it++) {
* const Symbol& key = *(it->second.first);
* for(auto jt = it->second.second.cbegin(); jt != it->second.second.cend(); jt++) {
* const Symbol& value = *(jt->second);
* ../..
* }
* ```
* - This is NOT a VSA plausible operation (unless another List data structure is used) by a debugging function.
* - A map can have several values for one key, combined in the values bundling.
* @return A `const std::unordered_map < unsigned int, std::pair < const Symbol *, std::unordered_map < unsigned int, Symbol * >>> &` reference for associative map iteration.
*/
const std::unordered_map < unsigned int, std::pair < const Symbol *, std::unordered_map < unsigned int, Symbol * >> >& get() const {
return values;
}
/**
* @function getBundling
* @memberof AssociativeMap
* @instance
* @description Returns the bundling representation of this associative map.
* @return {Bundling} A reference to the bundling representation.
*/
const Bundling& getBundling() const;
/**
* @function getSize
* @memberof AssociativeMap
* @instance
* @description Returns the number of key-value pairs this associative map.
* @return {uint} The number of key-value pairs.
*/
unsigned int getSize() const;
/**
* @function asString
* @memberof AssociativeMap
* @instance
* @description Returns the value as a string.
* @return {String} A string of the form `{ key: value ...}_<belief>`, omitting the belief if `tau=1, sigma=0`.
*/
virtual std::string asString() const;
};
}
#endif
