Version: 1.0
condition_action.h
Go to the documentation of this file.
1 // Copyright (c) 2020, the GRAPHGEN contributors, as
2 // shown by the AUTHORS file. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6 
7 #ifndef GRAPHGEN_CONDITION_ACTION_H_
8 #define GRAPHGEN_CONDITION_ACTION_H_
9 
10 #include <string>
11 #include <vector>
12 #include <bitset>
13 
14 
15 using uint = uint32_t;
16 
17 // Condition or action
18 struct conact {
19  enum class type { CONDITION, ACTION };
20 
22  // CONDITION
23  std::string condition;
24  // ACTION
25  std::bitset<131/*CTBE needs 131 bits*/> action = 0; // List of actions (bitmapped)
26  size_t next = 0;
27 
28  conact() {}
29  conact(std::string c) : t(type::CONDITION), condition(std::move(c)) {}
30  conact(uint a, uint n) : t(type::ACTION), action(a), next(n) {}
31 
32  std::vector<uint> actions() const {
33  std::vector<uint> a;
34  auto uAction = action;
35  uint nAction = 1;
36  while (uAction != 0) {
37  if (uAction[0])
38  a.push_back(nAction);
39  uAction >>= 1;
40  nAction++;
41  }
42  return a;
43  }
44 
45  // To check if two conact are equal (exactly the same)
46  bool operator==(const conact& other) const {
47  if (t != other.t)
48  return false;
49  if (t == type::CONDITION)
50  return condition == other.condition;
51  else
52  return ((action == other.action) && (next == other.next));
53  }
54  // To check if two conact are not equal
55  bool operator!=(const conact& other) const {
56  return !(*this == other);
57  }
58 
59  // To check if two conact are equivalent (the leaves actions has non empty intersection)
60  bool eq(const conact& other) const {
61  if (t != other.t)
62  return false;
63  if (t == type::CONDITION)
64  return condition == other.condition;
65  else
66  return (action & other.action) != 0 && next == other.next;
67  }
68  // To check if two conact are not equivalent (the leaves actions has empty intersection)
69  bool neq(const conact& other) const {
70  return !((*this).eq(other));
71  }
72 };
73 
74 #endif // !GRAPHGEN_CONDITION_ACTION_H_
75 
uint32_t uint
bool operator!=(const conact &other) const
std::vector< uint > actions() const
bool neq(const conact &other) const
bool operator==(const conact &other) const
std::bitset< 131 > action
conact(std::string c)
std::string condition
bool eq(const conact &other) const
size_t next
conact(uint a, uint n)