1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#ifndef __macrovsa_AssociativeNetwork__
#define __macrovsa_AssociativeNetwork__

#include "Symbol.hpp"
#include "AssociativeMap.hpp"

namespace macrovsa {
  /**
   * @class AssociativeNetwork
   * @description Implements a macroscopic ersatz of a VSA associative network.
   * - An associative network is created adding new key/value pairs of symbol.
   * - It is implemented via a matrix of the form `>_i value_i key_i^T`
   * @param {string} [name] An optional name. By default, a `#index` unique name is generated.
   * @extends AssociativeMap
   */
  class AssociativeNetwork: public Symbol {
    // Non assignable
    AssociativeMap& operator = (const AssociativeMap&) = delete;
    AssociativeNetwork(const AssociativeNetwork&) = delete;
    // Double indexing mechanism
    AssociativeMap keys, values;
public:
    AssociativeNetwork() {}
    AssociativeNetwork(String name) : Symbol(name) {}

    /**
     * @function add
     * @memberof AssociativeNetwork
     * @instance
     * @description Adds a new symbol pair to the container.
     * @param {Symbol} key The symbol key to add.
     * @param {Symbol} value The symbol value to add.
     */
    void add(const Symbol& key, const Symbol& value)
    {
      values.add(key, value);
      keys.add(value, key);
    }
    /**
     * @function getValue
     * @memberof AssociativeMap
     * @instance
     * @description Returns an approximate value of the associative map by unbinding.
     * @param {Symbol|String} key The symbol key `key_i`.
     * @return {Symbol} value A reference to the stored values as a bundling, available until program end.
     */
    const Symbol& getValue(const Symbol& key) const
    {
      return values.get(key);
    }
    const Symbol& getValue(String key) const
    {
      Symbol *skey = new Symbol(key);
      const Symbol& result = values.get(*skey);
      delete skey;
      return result;
    }
    /**
     * @function getKey
     * @memberof AssociativeMap
     * @instance
     * @description Returns an approximate key of the associative map by unbinding.
     * @param {Symbol|String} value The symbol value `value_i`.
     * @return {Symbol} key A reference to the stored keys as a bundling, available until program end.
     */
    const Symbol& getKey(const Symbol& value) const
    {
      return keys.get(value);
    }
    const Symbol& getKey(String value) const
    {
      Symbol *svalue = new Symbol(value);
      const Symbol& result = values.get(*svalue);
      delete svalue;
      return result;
    }
  };
}

#endif