Documentation of MARTY
A Modern ARtificial Theoretical phYsicist
sglalgo.h
Go to the documentation of this file.
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 
23 #pragma once
24 
25 #include "abstractgammasym.h"
26 #include "typecast.h"
27 
28 namespace sgl {
29 
30  template<typename Type>
31  void for_each(
32  GExpr &expr,
33  std::function<void(GExpr &)> f
34  )
35  {
36  const size_t sz = expr->size();
37  for (size_t i = 0; i != sz; ++i)
38  sgl::for_each<Type>(expr->argument(i), f);
39  if (IsType<Type>(expr))
40  f(expr);
41  }
42 
43  template<typename Type>
44  bool transform(
45  GExpr &expr,
46  std::function<bool(GExpr &)> f
47  )
48  {
49  const size_t sz = expr->size();
50  bool transformed = false;
51  for (size_t i = 0; i != sz; ++i)
52  if (sgl::transform<Type>(expr->argument(i), f))
53  transformed = true;
54  if (IsType<Type>(expr)) {
55  transformed = f(expr) || transformed;
56  }
57  if (transformed)
58  expr = expr->refresh();
59  return transformed;
60  }
61 }
Definition: abstractgammasym.h:32
Contains the base class for the Symbolic Gamma Library (SGL) and the GExpr definition.
RTTI utilities for SGL (run-time type information).