docc 0.8.3 (0d44999b)
C++ API Reference
Loading...
Searching...
No Matches
assumptions.h
Go to the documentation of this file.
1
40
41#pragma once
42
43#include <cstddef>
44#include <initializer_list>
45#include <iterator>
46#include <memory>
47#include <stdexcept>
48#include <unordered_map>
49#include <utility>
50
52#include "sdfg/types/type.h"
53
54namespace sdfg {
55namespace symbolic {
56
62private:
63 // Copy-on-write payload: scope propagation copies whole Assumptions maps, so copies share it.
64 struct Data {
70 bool constant = false;
72 };
73
74 Symbol symbol_;
75 std::shared_ptr<Data> data_;
76
77 Data& mut();
78
79public:
84
90
96
103
108 const Symbol symbol() const;
109
118
127
134
141
150
159
166
173
182
190 void tight_lower_bound(const Expression tight_lb);
191
200
208 void tight_upper_bound(const Expression tight_ub);
209
223
234
239
245
253 bool constant() const;
254
259 void constant(bool constant);
260
269 const Expression map() const;
270
278 void map(const Expression map);
279
295 static Assumption create(const Symbol symbol, const types::IType& type);
296
297 enum class ReplaceResult { IdSame = 0, IdChanged };
298
300};
301
314public:
315 using Map = std::unordered_map<Symbol, Assumption, SymEngine::RCPBasicHash, SymEngine::RCPBasicKeyEq>;
318 using value_type = Map::value_type;
319 using size_type = std::size_t;
320 using iterator = Map::iterator;
321
323 class const_iterator {
324 friend class Assumptions;
325 const Assumptions* owner_ = nullptr;
326 Map::const_iterator own_;
327 Map::const_iterator base_;
328 bool in_base_ = false;
329
330 const_iterator(const Assumptions* owner, Map::const_iterator own, Map::const_iterator base, bool in_base)
331 : owner_(owner), own_(own), base_(base), in_base_(in_base) {
332 }
333
334 void skip_shadowed() {
335 while (base_ != owner_->base_->end() && owner_->own_.find(base_->first) != owner_->own_.end()) {
336 ++base_;
337 }
338 }
339
340 public:
341 using iterator_category = std::forward_iterator_tag;
343 using difference_type = std::ptrdiff_t;
344 using pointer = const value_type*;
345 using reference = const value_type&;
346
347 const_iterator() = default;
348
350 return in_base_ ? *base_ : *own_;
351 }
353 return in_base_ ? &*base_ : &*own_;
354 }
355
356 const_iterator& operator++() {
357 if (in_base_) {
358 ++base_;
359 skip_shadowed();
360 return *this;
361 }
362 ++own_;
363 if (own_ == owner_->own_.end() && owner_->base_) {
364 in_base_ = true;
365 base_ = owner_->base_->begin();
366 skip_shadowed();
367 }
368 return *this;
369 }
370
371 const_iterator operator++(int) {
372 auto copy = *this;
373 ++*this;
374 return copy;
375 }
376
377 bool operator==(const const_iterator& other) const {
378 if (in_base_ != other.in_base_) {
379 return false;
380 }
381 return in_base_ ? base_ == other.base_ : own_ == other.own_;
382 }
383 bool operator!=(const const_iterator& other) const {
384 return !(*this == other);
385 }
386 };
387
388 Assumptions() = default;
389 Assumptions(std::initializer_list<value_type> entries) : own_(entries) {
390 }
391 Assumptions(const Map& entries) : own_(entries) {
392 }
393 Assumptions(Map&& entries) : own_(std::move(entries)) {
394 }
395
397 static Assumptions over(std::shared_ptr<const Map> base) {
398 Assumptions result;
399 result.base_ = std::move(base);
400 if (result.base_ && result.base_->empty()) {
401 result.base_.reset();
402 }
403 return result;
404 }
405
406 size_type size() const {
407 return own_.size() + (base_ ? base_->size() : 0) - shadowed_;
408 }
409 bool empty() const {
410 return size() == 0;
411 }
412
413 void clear() {
414 own_.clear();
415 base_.reset();
416 shadowed_ = 0;
417 }
418
420 if (own_.empty() && base_) {
421 const_iterator it(this, own_.end(), base_->begin(), true);
422 it.skip_shadowed();
423 return it;
424 }
425 return const_iterator(this, own_.begin(), {}, false);
426 }
428 return base_ ? const_iterator(this, own_.end(), base_->end(), true)
429 : const_iterator(this, own_.end(), {}, false);
430 }
432 return begin();
433 }
435 return end();
436 }
437
440 densify();
441 return own_.begin();
442 }
444 return own_.end();
445 }
446
448 auto own = own_.find(symbol);
449 if (own != own_.end()) {
450 return const_iterator(this, own, {}, false);
451 }
452 if (base_) {
453 auto base = base_->find(symbol);
454 if (base != base_->end()) {
455 return const_iterator(this, own_.end(), base, true);
456 }
457 }
458 return end();
459 }
460
463 auto own = own_.find(symbol);
464 if (own != own_.end() || !base_) {
465 return own;
466 }
467 auto base = base_->find(symbol);
468 if (base == base_->end()) {
469 return own_.end();
470 }
471 return promote(*base);
472 }
473
474 bool contains(const Symbol& symbol) const {
475 return own_.contains(symbol) || (base_ && base_->contains(symbol));
476 }
477 size_type count(const Symbol& symbol) const {
478 return contains(symbol) ? 1 : 0;
479 }
480
481 const Assumption& at(const Symbol& symbol) const {
482 auto own = own_.find(symbol);
483 if (own != own_.end()) {
484 return own->second;
485 }
486 if (base_) {
487 auto base = base_->find(symbol);
488 if (base != base_->end()) {
489 return base->second;
490 }
491 }
492 throw std::out_of_range("Assumptions::at: no assumption for " + symbol->get_name());
493 }
494
496 auto it = find(symbol);
497 if (it == own_.end()) {
498 throw std::out_of_range("Assumptions::at: no assumption for " + symbol->get_name());
499 }
500 return it->second;
501 }
502
504 auto it = find(symbol);
505 if (it != own_.end()) {
506 return it->second;
507 }
508 return own_[symbol];
509 }
510
513 std::pair<iterator, bool> insert(const value_type& entry) {
514 auto present = find(entry.first);
515 if (present != own_.end()) {
516 return {present, false};
517 }
518 return own_.insert(entry);
519 }
520 std::pair<iterator, bool> insert(value_type&& entry) {
521 auto present = find(entry.first);
522 if (present != own_.end()) {
523 return {present, false};
524 }
525 return own_.insert(std::move(entry));
526 }
527 template<typename InputIt>
528 void insert(InputIt first, InputIt last) {
529 for (; first != last; ++first) {
530 insert(value_type(*first));
531 }
532 }
533 template<typename... Args>
534 std::pair<iterator, bool> emplace(Args&&... args) {
535 return insert(value_type(std::forward<Args>(args)...));
536 }
537 template<typename... Args>
538 std::pair<iterator, bool> try_emplace(const Symbol& symbol, Args&&... args) {
539 auto present = find(symbol);
540 if (present != own_.end()) {
541 return {present, false};
542 }
543 return own_.try_emplace(symbol, std::forward<Args>(args)...);
544 }
545 template<typename M>
546 std::pair<iterator, bool> insert_or_assign(const Symbol& symbol, M&& value) {
547 auto present = find(symbol);
548 if (present != own_.end()) {
549 present->second = std::forward<M>(value);
550 return {present, false};
551 }
552 return own_.insert_or_assign(symbol, std::forward<M>(value));
553 }
554
556 Map::node_type extract(const Symbol& symbol) {
557 densify();
558 return own_.extract(symbol);
559 }
560 Map::insert_return_type insert(Map::node_type&& node) {
561 densify();
562 return own_.insert(std::move(node));
563 }
564
566 densify();
567 return own_.erase(symbol);
568 }
570 densify();
571 return own_.erase(position);
572 }
573
574private:
575 Map own_;
576 std::shared_ptr<const Map> base_;
577 // The own entries that shadow one of the base's.
578 size_type shadowed_ = 0;
579
580 iterator promote(const value_type& base_entry) {
581 ++shadowed_;
582 return own_.insert(base_entry).first;
583 }
584
585 void densify() {
586 if (!base_) {
587 return;
588 }
589 for (auto& entry : *base_) {
590 own_.insert(entry);
591 }
592 base_.reset();
593 shadowed_ = 0;
594 }
595};
596
597bool substitute(Assumptions& assumptions, const symbolic::ExpressionMapping& replacements);
598;
599
600} // namespace symbolic
601} // namespace sdfg
Represents assumptions about a symbol's properties and behavior.
Definition assumptions.h:61
Assumption & operator=(const Assumption &a)
Assignment operator.
Assumption(const Assumption &a)
Copy constructor.
const ExpressionSet & upper_bounds() const
Gets all upper bound constraints.
const Expression tight_upper_bound() const
Gets the tight upper bound.
ReplaceResult
Definition assumptions.h:297
@ IdSame
Definition assumptions.h:297
@ IdChanged
Definition assumptions.h:297
bool contains_constraint(const Expression c)
Checks if a specific constraint is present.
bool contains_lower_bound(const Expression lb)
Checks if a specific lower bound exists.
void tight_lower_bound(const Expression tight_lb)
Sets the tight lower bound.
bool contains_upper_bound(const Expression ub)
Checks if a specific upper bound exists.
const Expression map() const
Gets the evolution map for this symbol.
void constant(bool constant)
Sets whether the symbol is constant.
void tight_upper_bound(const Expression tight_ub)
Sets the tight upper bound.
void add_upper_bound(const Expression ub)
Adds an upper bound constraint.
const Symbol symbol() const
Gets the symbol this assumption is for.
static Assumption create(const Symbol symbol, const types::IType &type)
Creates an assumption with bounds derived from a type.
void map(const Expression map)
Sets the evolution map for this symbol.
ReplaceResult replace(const symbolic::ExpressionMapping &replacements)
bool remove_upper_bound(const Expression ub)
Removes an upper bound constraint.
const Expression tight_lower_bound() const
Gets the tight lower bound.
bool remove_lower_bound(const Expression lb)
Removes a lower bound constraint.
const ExpressionSet & lower_bounds() const
Gets all lower bound constraints.
void add_lower_bound(const Expression lb)
Adds a lower bound constraint.
Assumption()
Default constructor creating an empty assumption.
const ExpressionSet & constraints() const
Gets the constraints registered on this symbol.
bool remove_constraint(const Expression c)
Removes a constraint.
bool constant() const
Checks if the symbol is constant.
void add_constraint(const Expression c)
Adds a constraint involving this symbol.
Assumption(const Symbol symbol)
Constructs an assumption for a specific symbol.
Visits the own entries, then the base's entries the own ones do not shadow.
Definition assumptions.h:323
std::ptrdiff_t difference_type
Definition assumptions.h:343
const_iterator operator++(int)
Definition assumptions.h:371
pointer operator->() const
Definition assumptions.h:352
bool operator==(const const_iterator &other) const
Definition assumptions.h:377
bool operator!=(const const_iterator &other) const
Definition assumptions.h:383
const value_type * pointer
Definition assumptions.h:344
std::forward_iterator_tag iterator_category
Definition assumptions.h:341
const value_type & reference
Definition assumptions.h:345
const_iterator & operator++()
Definition assumptions.h:356
reference operator*() const
Definition assumptions.h:349
friend class Assumptions
Definition assumptions.h:324
Assumptions::value_type value_type
Definition assumptions.h:342
Map from symbols to their assumptions.
Definition assumptions.h:313
iterator end()
Definition assumptions.h:443
void clear()
Definition assumptions.h:413
Map::value_type value_type
Definition assumptions.h:318
const Assumption & at(const Symbol &symbol) const
Definition assumptions.h:481
static Assumptions over(std::shared_ptr< const Map > base)
An instance holding no entries of its own over base, which it shares.
Definition assumptions.h:397
size_type size() const
Definition assumptions.h:406
const_iterator end() const
Definition assumptions.h:427
void insert(InputIt first, InputIt last)
Definition assumptions.h:528
std::pair< iterator, bool > try_emplace(const Symbol &symbol, Args &&... args)
Definition assumptions.h:538
Assumption mapped_type
Definition assumptions.h:317
Map::node_type extract(const Symbol &symbol)
Node handles, as for renaming an entry's symbol, work on own entries: the base is copied in first.
Definition assumptions.h:556
const_iterator cend() const
Definition assumptions.h:434
size_type count(const Symbol &symbol) const
Definition assumptions.h:477
Symbol key_type
Definition assumptions.h:316
Assumption & at(const Symbol &symbol)
Definition assumptions.h:495
const_iterator begin() const
Definition assumptions.h:419
std::pair< iterator, bool > insert_or_assign(const Symbol &symbol, M &&value)
Definition assumptions.h:546
Map::insert_return_type insert(Map::node_type &&node)
Definition assumptions.h:560
std::pair< iterator, bool > insert(value_type &&entry)
Definition assumptions.h:520
iterator find(const Symbol &symbol)
A mutable entry, copied from the base first if only the base holds it.
Definition assumptions.h:462
Assumption & operator[](const Symbol &symbol)
Definition assumptions.h:503
Assumptions(Map &&entries)
Definition assumptions.h:393
std::unordered_map< Symbol, Assumption, SymEngine::RCPBasicHash, SymEngine::RCPBasicKeyEq > Map
Definition assumptions.h:315
bool empty() const
Definition assumptions.h:409
std::pair< iterator, bool > emplace(Args &&... args)
Definition assumptions.h:534
const_iterator find(const Symbol &symbol) const
Definition assumptions.h:447
iterator erase(iterator position)
Definition assumptions.h:569
std::pair< iterator, bool > insert(const value_type &entry)
Inserts entry unless its symbol has an entry, here or in the base; true if it did.
Definition assumptions.h:513
const_iterator cbegin() const
Definition assumptions.h:431
size_type erase(const Symbol &symbol)
Definition assumptions.h:565
bool contains(const Symbol &symbol) const
Definition assumptions.h:474
Map::iterator iterator
Definition assumptions.h:320
iterator begin()
Mutable iteration: the base is copied in first. The end is the own entries', which mutable finds retu...
Definition assumptions.h:439
Assumptions(std::initializer_list< value_type > entries)
Definition assumptions.h:389
std::size_t size_type
Definition assumptions.h:319
Assumptions(const Map &entries)
Definition assumptions.h:391
Abstract base interface for all types in the sdfglib type system.
Definition type.h:625
Symbol symbol(const std::string &name)
Creates a symbolic variable with the given name.
SymEngine::RCP< const SymEngine::Basic > Expression
Reference-counted pointer to a symbolic expression (SymEngine Basic).
Definition symbolic.h:83
SymEngine::RCP< const SymEngine::Symbol > Symbol
Reference-counted pointer to a SymEngine Symbol.
Definition symbolic.h:71
std::set< Expression, SymEngine::RCPBasicKeyLess > ExpressionSet
Ordered set of expressions.
Definition symbolic.h:104
SymEngine::map_basic_basic ExpressionMapping
Definition symbolic.h:109
Definition assumptions.h:55
bool substitute(Assumptions &assumptions, const symbolic::ExpressionMapping &replacements)
Definition analysis.h:7
Symbolic system for integer-based symbol analysis.
Core type system definitions for sdfglib.