Documentation of CSL
patternMatch.h
1 // This file is part of MARTY.
2 //
3 // MARTY is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // MARTY is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with MARTY. If not, see <https://www.gnu.org/licenses/>.
15 
16  #pragma once
17 
18 #include "abstract.h"
19 
20 namespace csl::matcher {
21 
22 
23 struct Node;
24 struct Tree;
25 size_t dichoFinder(
26  csl::Expr const &expr,
27  std::vector<Node*> const &v
28  );
29 
30 void compress_impl(
31  std::vector<csl::Expr> &expr,
32  size_t nIter = 1
33  );
34 void compress(
35  std::vector<csl::Expr> &expr,
36  size_t nIter = 1
37  );
38 
39 inline void compress(
40  csl::Expr &expr,
41  size_t nIter = 1
42  )
43 {
44  std::vector<csl::Expr> vec { expr };
45  compress(vec, nIter);
46  expr = vec[0];
47 }
48 
49 inline void compress_impl(
50  csl::Expr &expr,
51  size_t nIter = 1
52  )
53 {
54  std::vector<csl::Expr> vec { expr };
55  compress_impl(vec, nIter);
56  expr = vec[0];
57 }
58 
59 struct Node {
60 
61  using Container = std::vector<Node*>;
62  using iterator = Container::iterator;
63  using const_iterator = Container::const_iterator;
64 
65  enum class ExprType {
66  Sum,
67  Prod
68  };
69 
70  static inline bool useDifferedStart = false;
71  static inline size_t maxLeaf = 10;
72 
73  bool empty() const { return children.empty(); }
74  auto size() const { return children.size(); }
75  auto begin() { return children.begin(); }
76  auto end() { return children.end(); }
77  auto begin() const { return children.begin(); }
78  auto end() const { return children.end(); }
79 
80  bool isRoot() const { return !parent; }
81  bool isAbbreviated() const;
82 
83  static csl::Expr makeAbbreviation(
84  csl::Expr const &expr,
85  ExprType type
86  );
87 
88  static csl::Expr makeExpression(
89  std::vector<csl::Expr> const &args,
90  ExprType type
91  );
92 
93  static size_t distance(
94  Node const *first,
95  Node const *last
96  );
97  static size_t nLeafs(csl::Expr const &expr);
98 
99  iterator insert(csl::Expr const &t_expr);
100 
101  std::pair<Node*, std::vector<csl::Expr>::const_iterator>
102  findBestMatch(
103  std::vector<csl::Expr>::const_iterator first,
104  std::vector<csl::Expr>::const_iterator last
105  );
106 
107  std::vector<csl::Expr> getArgs() const;
108  static csl::Expr getChainExpr(
109  std::vector<csl::Expr> const &args,
110  ExprType type
111  );
112  csl::Expr getChainExpr(ExprType type) const;
113  csl::Expr getAbbreviation() const;
114  csl::Expr getChainAbbreviation() const;
115 
116  void setAbbreviation(
117  std::vector<Tree*> &trees,
118  ExprType type
119  );
120 
121  void parse(
122  std::vector<csl::Expr>::const_iterator first,
123  std::vector<csl::Expr>::const_iterator last
124  );
125 
126  static Node *build(
127  csl::Expr const &t_expr,
128  Node *t_parent = nullptr
129  );
130 
131  static void destroy(Node *&node);
132  static void removeSingle(Node *&node);
133 
134  void print(int indent = 0) const;
135 
136  csl::Expr expr;
137  Node *parent;
138  csl::Expr abbreviation { nullptr };
139  csl::Expr chainAbbreviation { nullptr };
140  size_t nOccurences {1};
141  std::vector<Node*> children {};
142 };
143 
144 struct Tree {
145 
146  Tree(Node::ExprType t_type);
147  ~Tree();
148 
149  std::pair<Node*, std::vector<csl::Expr>::const_iterator>
150  findBestMatch(
151  std::vector<csl::Expr> const &vec,
152  size_t minElements = 2
153  );
154 
155  static std::optional<csl::Expr> getChainAbbreviationFor(
156  csl::Expr const &init,
157  std::vector<Tree*> &trees
158  );
159 
160  static void findAllAbbreviations(
161  std::vector<Tree*> &trees
162  );
163 
164  static void findAllAbbreviations(
165  Node *node,
166  Tree *t,
167  std::vector<Tree*> &trees
168  );
169 
170  void parse(std::vector<csl::Expr> const &vec);
171  void removeSingle();
172  void print();
173 
174  Node::ExprType type;
175  Node *root;
176 };
177 
178 
179 } // namespace csl::matcher
Definition: patternMatch.h:20
Handles a sum, function of multiple arguments.
Definition: operations.h:33
Handles a product, function of multiple arguments.
Definition: operations.h:289
Definition: patternMatch.h:59
Definition: patternMatch.h:144
Base classes for all exprs in the program.
Expression type/.
Definition: abstract.h:1573