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
23
24 int list_size(const Ex& tr, Ex::iterator it);
25
29
30 Ex::iterator find_in_list(const Ex& tr, Ex::iterator it, std::function<Ex::iterator(Ex::iterator)> f);
31
35
36 Ex::iterator find_in_subtree(const Ex& tr, Ex::iterator it, std::function<bool(Ex::iterator)> f, bool including_head=true);
37
41
42 Ex make_list(Ex el);
43
44
45
51
52 template<typename T>
53 typename T::iterator do_subtree(const T& tr, typename T::iterator it, std::function<typename T::iterator(typename T::iterator)> f)
54 {
55 if(it==tr.end()) return it;
56
57 class T::post_order_iterator walk=it, last=it;
58 ++last;
59 walk.descend_all();
60
61 do {
62 auto nxt=walk;
63 ++nxt;
64
65 bool cpy=false;
66 if(walk==it) cpy=true;
67 walk = f(walk);
68 if(cpy) it=walk;
69
70 walk=nxt;
71 }
72 while(walk!=last);
73
74 return it;
75 }
76
77 // Iterate over the children of 'it' if the node is named 'delim', otherwise only
78 // iterate over the single node. Note: this yields iterators, not str_nodes.
79 struct split_it
80 {
81 struct iterator {
82 using value_type = Ex::sibling_iterator;
87 using const_pointer = const value_type*;
88 using iterator_category = std::input_iterator_tag;
89
91 iterator(Ex::sibling_iterator it) : it(it) {}
92
93 bool operator == (const iterator& other) { return it == other.it; }
94 bool operator != (const iterator& other) { return !(*this == other); }
95 reference operator* () { return it; }
96 pointer operator-> () { return &it; }
97 reference operator ++ () { return ++it; }
98 value_type operator ++ (int) { return it++; }
99
100
101 Ex::sibling_iterator it;
102 };
103
104 split_it(Ex::iterator it, const std::string& delim = "")
105 {
106 if (delim == "" || *it->name == delim) {
107 begin_ = it.begin();
108 end_ = it.end();
109 }
110 else {
111 begin_ = it;
112 end_ = it;
113 ++end_;
114 }
115 }
116
118 iterator end() { return iterator(end_); }
119
120 private:
121 Ex::sibling_iterator begin_, end_;
122 };
123
124 };
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:53
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:25
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:59
void do_list(const Ex &tr, Ex::iterator it, std::function< bool(Ex::iterator)> f)
Apply a function on every element of a list, or if the iterator 'it' does not point to a list,...
Definition Functional.cc:6
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:33
Ex make_list(Ex el)
Ensure that the tree is a list, even if it contains only a single element.
Definition Functional.cc:76
Functions to handle the exchange properties of two or more symbols in a product.
Definition Adjform.cc:83
void set(rset_t::iterator &num, multiplier_t fac)
Definition Storage.cc:1063
Definition Functional.hh:81
pointer operator->()
Definition Functional.hh:96
bool operator!=(const iterator &other)
Definition Functional.hh:94
iterator()
Definition Functional.hh:90
value_type * pointer
Definition Functional.hh:86
ptrdiff_t difference_type
Definition Functional.hh:83
value_type & reference
Definition Functional.hh:84
Ex::sibling_iterator it
Definition Functional.hh:101
reference operator*()
Definition Functional.hh:95
Ex::sibling_iterator value_type
Definition Functional.hh:82
iterator(Ex::sibling_iterator it)
Definition Functional.hh:91
reference operator++()
Definition Functional.hh:97
const value_type * const_pointer
Definition Functional.hh:87
std::input_iterator_tag iterator_category
Definition Functional.hh:88
const value_type & const_reference
Definition Functional.hh:85
bool operator==(const iterator &other)
Definition Functional.hh:93
Definition Functional.hh:80
Ex::sibling_iterator begin_
Definition Functional.hh:121
split_it(Ex::iterator it, const std::string &delim="")
Definition Functional.hh:104
iterator begin()
Definition Functional.hh:117
Ex::sibling_iterator end_
Definition Functional.hh:121
iterator end()
Definition Functional.hh:118