Documentation of CSL
index_new.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 #ifndef INDEX_NEW_H_INCLUDED
24 #define INDEX_NEW_H_INCLUDED
25 
26 #include "index.h"
27 #include "error.h"
28 #include "support.h"
29 
30 namespace csl {
31 
32 template<size_t N>
34 
36 
37  Index* first;
38  Index* last;
39 
40  public:
41 
42  template<size_t N>
44  :first(structure.begin()),
45  last(structure.end())
46  {
47 
48  }
49 
50  inline bool empty() const {
51  return first == last;
52  }
53 
54  inline size_t size() const {
55  return (last - first);
56  }
57 
58  inline const Index* begin() const {
59  return first;
60  }
61 
62  inline const Index* end() const {
63  return last;
64  }
65 
66  inline Index* begin() {
67  return first;
68  }
69 
70  inline Index* end() {
71  return last;
72  }
73 };
74 
75 
76 template<size_t N>
77 class IndexStructure_new {
78 
79  public:
80 
81  using iterator
82  = typename std::array<csl::Index, N>::iterator;
83  using const_iterator
84  = typename std::array<csl::Index, N>::const_iterator;
85  using reverse_iterator
86  = typename std::array<csl::Index, N>::reverse_iterator;
87  using const_reverse_iterator
88  = typename std::array<csl::Index, N>::const_reverse_iterator;
89 
90  IndexStructure_new() = default;
91  ~IndexStructure_new() = default;
92  IndexStructure_new(IndexStructure_new const&) = default;
94  IndexStructure_new<N>& operator=(IndexStructure_new<N> const&) = default;
95  IndexStructure_new<N>& operator=(IndexStructure_new<N>&&) = default;
96 
97  explicit
98  IndexStructure_new(csl::Index const& first) {
99  static_assert(N == 1);
100  indices[0] == first;
101  }
102 
104  :indices(other.begin(), other.end())
105  {
106  CSL_ASSERT_SPEC(other.size() == N,
107  CSLError::ValueError,
108  "Size mismatch for construction of IndexStructure, "
109  + toString(other.size()) + " given, " + toString(N)
110  + " given.");
111  }
112 
113  explicit
114  IndexStructure_new(std::array<csl::Index, N> const& t_indices)
115  :indices(t_indices)
116  {
117 
118  }
119 
120  explicit
121  IndexStructure_new(std::array<csl::Index, N>&& t_indices)
122  :indices(std::move(t_indices))
123  {
124 
125  }
126 
127  constexpr bool empty() const {
128  return N == 0;
129  }
130 
131  constexpr size_t size() const {
132  return N;
133  }
134 
135  inline iterator begin() {
136  return indices.begin();
137  }
138 
139  inline iterator end() {
140  return indices.end();
141  }
142 
143  inline const_iterator begin() const {
144  return indices.begin();
145  }
146 
147  inline const_iterator end() const {
148  return indices.end();
149  }
150 
151  inline reverse_iterator rbegin() {
152  return indices.rbegin();
153  }
154 
155  inline reverse_iterator rend() {
156  return indices.rend();
157  }
158 
159  inline const_reverse_iterator rbegin() const {
160  return indices.rbegin();
161  }
162 
163  inline const_reverse_iterator rend() const {
164  return indices.rend();
165  }
166 
167  csl::Index operator[](size_t pos) const {
168  return indices[pos];
169  }
170 
171  csl::Index& operator[](size_t pos) {
172  return indices[pos];
173  }
174 
175  template<size_t M>
177  {
178  IndexStructure_new<M+N> newStruct;
179  auto iter = newStruct.begin();
180  for (const auto& index : *this)
181  iter++ = index;
182  for (const auto& index : other)
183  iter++ = index;
184  }
185 
186  std::vector<csl::Index> getVectorIndex() const {
187  return std::vector<csl::Index>(indices.begin(), indices.end());
188  }
189 
190  void reset() {
191  for (auto& index : indices) {
192  index = index.rename();
193  index.setFree(true);
194  }
195  }
196 
207  bool exactMatch(const IndexStructure_new<N>& structure) const {
208  auto self = begin();
209  for (auto other = structure.begin(); other != structure.end();
210  ++other, ++self)
211  if (not self->exactMatch(*other))
212  return false;
213  return true;
214  }
215 
226  bool compareWithDummy(const IndexStructure_new<N>& structure) const {
227  std::map<csl::Index,csl::Index> constraints;
228  return compareWithDummy(structure, constraints);
229  }
230 
250  std::map<Index,Index>& constraints,
251  bool keepAllCosntraints = false) const {
252  auto self = begin();
253  for (auto other = structure.begin(); other != structure.end();
254  ++other, ++self) {
255  // If the index is free, they must be the same
256  if ((*self).getFree() or (*self).getType() == cslIndex::Fixed) {
257  if (not (*self).exactMatch((*other)))
258  return false;
259  }
260  // Else we check if the dummie are already constrained, if the
261  // constraint is respected, or add a new constraint
262  else {
263  // (*other) must also be dummy here and must correspond (to a
264  // renaming) to (*self)
265  if (not (*self).compareWithDummy((*other)))
266  return false;
267  if ((*self) == (*other)) {
268  for (const auto& cons : constraints)
269  if (cons.second == (*self) and cons.first != cons.second)
270  return false;
271  }
272  if (constraints.find((*self)) == constraints.end()) {
273  if (constraints.find((*other)) == constraints.end()) {
274  // Here the dummies are not constrained: comparison
275  // is ok, and we add the new correspondance
276  constraints[(*self)] = (*other);
277  }
278  else {
279  if (constraints[(*other)] != (*self))
280  return false;
281  constraints[(*self)] = (*other);
282  }
283  }
284  else {
285  if (constraints.find(*other) == constraints.end()) {
286  if (not (constraints[*self] == *other))
287  return false;
288  }
289  else if (constraints[*self] != *other
290  or constraints[*other] != *self)
291  return false;
292  if (not keepAllCosntraints)
293  constraints.erase(*self);
294  }
295  }
296  }
297 
298  return true;
299  }
300 
312  bool operator==(const IndexStructure_new<N>& structure) const {
313  int t_nIndices = structure.size();
314  std::vector<int> indicesLeft(0);
315  for (int i=0; i<t_nIndices; ++i)
316  // comparing only free indices
317  if (structure[i].getFree())
318  indicesLeft.push_back(i);
319 
320  for (const auto& index : indices) {
321  // Comparing only free indices
322  if (index.getFree()) {
323  bool match = false;
324  for (size_t j=0; j!=indicesLeft.size(); ++j) {
325  // The free structure needs exact match to be correct
326  if (index.exactMatch(structure[indicesLeft[j]])) {
327  match = true;
328  indicesLeft.erase(indicesLeft.begin()+j);
329  break;
330  }
331  }
332  if (not match)
333  return false;
334  }
335  }
336 
337  if (not indicesLeft.empty())
338  return false;
339 
340  return true;
341  }
342 
350  bool operator!=(const IndexStructure_new<N>& structure) const {
351  return not (*this == structure);
352  }
353 
362  bool operator<(const IndexStructure_new<N>& structure) const {
363  auto other = structure.begin();
364  for (const auto& index : *this) {
365  if (index < *other)
366  return true;
367  else if (*other < index)
368  return false;
369  ++other;
370  }
371 
372  return false;
373  }
374 
383  bool operator>(const IndexStructure_new<N>& structure) const {
384  return structure < *this;
385  }
386 
395  bool operator<=(const IndexStructure_new<N>& structure) const {
396  return (*this < structure or not (structure < *this));
397  }
398 
407  bool operator>=(const IndexStructure_new<N>& structure) const {
408  return structure <= *this;
409  }
410 
411  private:
412 
413  std::array<csl::Index, N> indices;
414 };
415 
416 // void f() {
417 // IndexStructure_new<5> index;
418 // for (auto& i : index)
419 // i.setName("FRE");
420 // }
421 
422 }
423 
424 #endif
Some useful functions.
bool operator==(const IndexStructure_new< N > &structure) const
operator==, compares the IndexStructure_new<N> to structure in terms of free structure, independently of the dummy indices or the order of free indices.
Definition: index_new.h:312
Namespace for csl library.
Definition: abreviation.h:34
Definition: index_new.h:33
bool compareWithDummy(const IndexStructure_new< N > &structure) const
Compares *this with structure index by index (in order) using the function Index::compareWithDummy()...
Definition: index_new.h:226
Index object that is used for indicial objects.
Definition: index.h:75
Expr operator+(const Expr &a, const Expr &b)
Shortcut function that allows to use arithmetic operator + with Expr (== shared_ptr<Abstract>).
Definition: abstract.cpp:1298
bool operator>(const IndexStructure_new< N > &structure) const
operator>, simplicity comparator using comparators between indices, starting by comparing the first o...
Definition: index_new.h:383
bool operator!=(const IndexStructure_new< N > &structure) const
operator!=, returns the opposite of IndexStructure_new<N>::operator==().
Definition: index_new.h:350
File containing functions that are called by the program when something wrong happened: determines th...
bool compareWithDummy(const IndexStructure_new< N > &structure, std::map< Index, Index > &constraints, bool keepAllCosntraints=false) const
Compares *this with structure index by index (in order) using the function Index::compareWithDummy()...
Definition: index_new.h:249
Definition: index_new.h:35
Definition: index.h:52
bool exactMatch(const IndexStructure_new< N > &structure) const
Compares the IndexStructure_new<N> with structure. Each index must match exactly (see Index::exactMat...
Definition: index_new.h:207
bool operator>=(const IndexStructure_new< N > &structure) const
operator>=, simplicity comparator using comparators between indices, starting by comparing the first ...
Definition: index_new.h:407