Cadabra
Computer algebra system for field theory problems
Loading...
Searching...
No Matches
Functional.hh
Go to the documentation of this file.
1
2#pragma once
3
4#include <functional>
5#include "Storage.hh"
6
7namespace cadabra {
8
16
17 void do_list(const Ex& tr, Ex::iterator it, std::function<bool(Ex::iterator)> f);
18
24
25 void do_sum(const Ex& sum, Ex::iterator it, std::function<bool(Ex::iterator)> f);
26
31
32 int list_size(const Ex& tr, Ex::iterator it);
33
37
38 Ex::iterator find_in_list(const Ex& tr, Ex::iterator it, std::function<Ex::iterator(Ex::iterator)> f);
39
43
44 Ex::iterator find_in_subtree(const Ex& tr, Ex::iterator it, std::function<bool(Ex::iterator)> f, bool including_head=true);
45
47
48 std::vector<Ex::iterator> find_in_sum_multiple();
49
53
55
61
62 template<typename T>
63 typename T::iterator do_subtree(const T& tr, typename T::iterator it, std::function<typename T::iterator(typename T::iterator)> f)
64 {
65 if(it==tr.end()) return it;
66
67 class T::post_order_iterator walk=it, last=it;
68 ++last;
69 walk.descend_all();
70
71 do {
72 auto nxt=walk;
73 ++nxt;
74
75 bool cpy=false;
76 if(walk==it) cpy=true;
77 walk = f(walk);
78 if(cpy) it=walk;
79
80 walk=nxt;
81 }
82 while(walk!=last);
83
84 return it;
85 }
86
87 // Iterate over the children of 'it' if the node is named 'delim', otherwise only
88 // iterate over the single node. Note: this yields iterators, not str_nodes.
89 struct split_it
90 {
91 struct iterator {
92 using value_type = Ex::sibling_iterator;
97 using const_pointer = const value_type*;
98 using iterator_category = std::input_iterator_tag;
99
101 iterator(Ex::sibling_iterator it) : it(it) {}
102
103 bool operator == (const iterator& other) { return it == other.it; }
104 bool operator != (const iterator& other) { return !(*this == other); }
105 reference operator* () { return it; }
106 pointer operator-> () { return &it; }
107 reference operator ++ () { return ++it; }
108 value_type operator ++ (int) { return it++; }
109
110
111 Ex::sibling_iterator it;
112 };
113
114 split_it(Ex::iterator it, const std::string& delim = "")
115 {
116 if (delim == "" || *it->name == delim) {
117 begin_ = it.begin();
118 end_ = it.end();
119 }
120 else {
121 begin_ = it;
122 end_ = it;
123 ++end_;
124 }
125 }
126
128 iterator end() { return iterator(end_); }
129
130 private:
131 Ex::sibling_iterator begin_, end_;
132 };
133
134 };
Definition Storage.hh:170
T::iterator do_subtree(const T &tr, typename T::iterator it, std::function< typename T::iterator(typename T::iterator)> f)
Apply a function on every node in the tree at and below the given node, depth-first.
Definition Functional.hh:63
int list_size(const Ex &tr, Ex::iterator it)
For lists as defined above for 'do_list', return their size (in case you really need to know the size...
Definition Functional.cc:44
Ex::iterator find_in_list(const Ex &tr, Ex::iterator it, std::function< Ex::iterator(Ex::iterator)> f)
Returns an iterator to the first element for which 'f' does not return tr.end().
Definition Functional.cc:78
void do_list(const Ex &tr, Ex::iterator it, std::function< bool(Ex::iterator)> f)
Apply a function to every element of a list, or if the iterator 'it' does not point to a list,...
Definition Functional.cc:6
void do_sum(const Ex &tr, Ex::iterator it, std::function< bool(Ex::iterator)> f)
Apply a function to every term in a sum, or if the iterator does not point to a sum,...
Definition Functional.cc:25
Ex::iterator find_in_subtree(const Ex &tr, Ex::iterator it, std::function< bool(Ex::iterator)> f, bool including_head)
Returns an iterator to the first element for which 'f' returns 'true', or 'tr.end()'.
Definition Functional.cc:52
Ex make_list(Ex el)
Ensure that the tree is a list, even if it contains only a single element.
Definition Functional.cc:95
Functions to handle the exchange properties of two or more symbols in a product.
Definition Adjform.cc:83
std::vector< Ex::iterator > find_in_sum_multiple()
void set(rset_t::iterator &num, multiplier_t fac)
Definition Storage.cc:1179
Definition Functional.hh:91
pointer operator->()
Definition Functional.hh:106
bool operator!=(const iterator &other)
Definition Functional.hh:104
iterator()
Definition Functional.hh:100
value_type * pointer
Definition Functional.hh:96
ptrdiff_t difference_type
Definition Functional.hh:93
value_type & reference
Definition Functional.hh:94
Ex::sibling_iterator it
Definition Functional.hh:111
reference operator*()
Definition Functional.hh:105
Ex::sibling_iterator value_type
Definition Functional.hh:92
iterator(Ex::sibling_iterator it)
Definition Functional.hh:101
reference operator++()
Definition Functional.hh:107
const value_type * const_pointer
Definition Functional.hh:97
std::input_iterator_tag iterator_category
Definition Functional.hh:98
const value_type & const_reference
Definition Functional.hh:95
bool operator==(const iterator &other)
Definition Functional.hh:103
Definition Functional.hh:90
Ex::sibling_iterator begin_
Definition Functional.hh:131
split_it(Ex::iterator it, const std::string &delim="")
Definition Functional.hh:114
iterator begin()
Definition Functional.hh:127
Ex::sibling_iterator end_
Definition Functional.hh:131
iterator end()
Definition Functional.hh:128