-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathTokenEventGraph.hpp
122 lines (98 loc) · 3.82 KB
/
TokenEventGraph.hpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#ifndef LIBSETREPLACE_TOKENEVENTGRAPH_HPP_
#define LIBSETREPLACE_TOKENEVENTGRAPH_HPP_
#include <memory>
#include <vector>
#include "AtomsIndex.hpp"
#include "IDTypes.hpp"
namespace SetReplace {
/** @brief Match is a potential event that has not actualized yet.
*/
struct Match {
/** @brief ID for the rule this match corresponds to.
*/
RuleID rule;
/** @brief Tokens matching the rule inputs.
*/
std::vector<TokenID> inputTokens;
};
using MatchPtr = std::shared_ptr<const Match>;
/** @brief Event is an instantiated replacement that has taken place in the system.
*/
struct Event {
/** @brief ID for the rule this event corresponds to.
*/
const RuleID rule;
/** @brief Tokens matching the rule inputs.
*/
const std::vector<TokenID> inputTokens;
/** @brief Tokens created from the rule outputs.
*/
const std::vector<TokenID> outputTokens;
/** @brief Layer of the causal graph this event belongs to.
*/
const Generation generation;
};
/** @brief Type of separation between tokens.
*/
enum class SeparationType {
Unknown, // Lookup not possible (i.e., SeparationTrackingMethod is None)
Identical, // The token IDs requested are the same
Timelike, // One token causally depends on the another
Spacelike, // Tokens are compatible (created by an event)
Branchlike // Tokens are incompatible (created from the same expression)
};
using GetTokenSeparationFunc = std::function<SeparationType(const TokenID&, const TokenID&)>;
/** @brief TokenEventGraph keeps track of causal relationships between events and tokens.
@details It does not care and does not know about atoms at all because they are only used for matching. Tokens are
only identified by IDs.
*/
class TokenEventGraph {
public:
/** @brief Whether and what kind of separation (timelike, spacelike, branchlike) between tokens should be
tracked.
@details This tracking is in general expensive, so it should be disabled if not needed. It is however much faster to
precompute it during evolution than compute it on demand. Only supported for spacelike systems.
*/
enum class SeparationTrackingMethod {
None, // lookup impossible
DestroyerChoices // O(events * tokens) in memory and time, O(tokens) lookup
};
/** @brief Creates a new TokenEventGraph with a given number of initial tokens.
*/
explicit TokenEventGraph(int initialTokenCount, SeparationTrackingMethod separationTrackingMethod);
/** @brief Adds a new event, names its output tokens, and returns their IDs.
*/
std::vector<TokenID> addEvent(RuleID ruleID, const std::vector<TokenID>& inputTokens, int outputTokenCount);
/** @brief Yields a vector of all events throughout history.
@details This includes the initial event, so the size of the result is one larger than eventsCount().
*/
const std::vector<Event>& events() const;
/** @brief Total number of events.
*/
size_t eventsCount() const;
/** @brief Yields a vector of IDs for all tokens in the causal graph.
*/
std::vector<TokenID> allTokenIDs() const;
/** @brief Total number of tokens.
*/
size_t tokenCount() const;
/** @brief Generation for a given token.
* @details This is the same as the generation of its creator event.
*/
Generation tokenGeneration(TokenID id) const;
/** @brief Largest generation of any event.
*/
Generation largestGeneration() const;
/** @brief Computes the separation type between tokens (timelike, spacelike or branchlike).
@details Fails if SeparationTrackingMethod is disabled.
*/
SeparationType tokenSeparation(TokenID first, TokenID second) const;
/** @brief Number of destroyer events per token.
*/
uint64_t destroyerEventsCount(TokenID id) const;
private:
class Implementation;
std::shared_ptr<Implementation> implementation_;
};
} // namespace SetReplace
#endif // LIBSETREPLACE_TOKENEVENTGRAPH_HPP_