carl  24.04
Computer ARithmetic Library
SignCondition.h
Go to the documentation of this file.
1 /*
2  * File: SignCondition.h
3  * Author: tobias
4  *
5  * Created on 16. August 2016, 10:54
6  */
7 
8 #pragma once
9 
10 #include <carl-arith/core/Sign.h>
11 #include <list>
12 
13 namespace carl {
14 
16 
17 class SignCondition : public std::list<Sign> {
18 
19  // to inherit constructors
20  using std::list<Sign>::list;
21 
22 public:
23  bool isPrefixOf(const SignCondition& other);
24 
25  bool isSuffixOf(const SignCondition& other) const {
26  if(this->size() > other.size()) return false;
27  auto it_this = this->crbegin();
28  auto it_other = other.crbegin();
29  while(it_this != this->crend()) {
30  if(*it_this != *it_other) return false;
31  it_this++;
32  it_other++;
33  }
34  return true;
35  }
36 
38  SignCondition result;
39  auto it = this->rbegin();
40  for(uint i = 0; i < count; i++) {
41  result.push_front(*it);
42  it++;
43  }
44  assert(result.size() == count);
45  return result;
46  }
47 
48  static ThomComparisonResult compare(const SignCondition& lhs, const SignCondition& rhs) {
49  assert(lhs.size() == rhs.size());
50  assert(lhs.back() == rhs.back());
51  if(lhs.size() == 1) return EQUAL;
52  assert(lhs.size() >= 2);
53  auto it_lhs = lhs.rbegin();
54  auto it_rhs = rhs.rbegin();
55  it_lhs++; it_rhs++; // let the iterators point to the second last element
56  while(it_lhs != lhs.rend() && it_rhs != rhs.rend()) {
57  if(*it_lhs != *it_rhs) {
58  it_lhs--; it_rhs--;
59  assert(*it_lhs == *it_rhs);
60  if(*it_lhs == Sign::POSITIVE) {
61  it_rhs++; it_lhs++;
62  return (*it_lhs < *it_rhs) ? ThomComparisonResult::LESS : ThomComparisonResult::GREATER;
63  }
64  else {
65  it_rhs++; it_lhs++;
66  return (*it_lhs < *it_rhs) ? ThomComparisonResult::GREATER : ThomComparisonResult::LESS;
67  }
68  }
69  it_rhs++; it_lhs++;
70  }
72  }
73 
74  friend std::ostream& operator<<(std::ostream& os, const SignCondition& rhs) {
75  os << "[" << rhs.size() << ": ";
76  for(const auto& sign : rhs) {
77  switch(sign) {
78  case Sign::ZERO:
79  os << "0";
80  break;
81  case Sign::POSITIVE:
82  os << "+";
83  break;
84  case Sign::NEGATIVE:
85  os << "-";
86  break;
87  }
88  os << ", ";
89  }
90  os << "]";
91  return os;
92  }
93 };
94 
95 
96 } // namespace carl
carl is the main namespace for the library.
std::uint64_t uint
Definition: numbers.h:16
@ NEGATIVE
Indicates that .
@ ZERO
Indicates that .
@ POSITIVE
Indicates that .
ThomComparisonResult
Definition: SignCondition.h:15
@ GREATER
Definition: SignCondition.h:15
bool isSuffixOf(const SignCondition &other) const
Definition: SignCondition.h:25
bool isPrefixOf(const SignCondition &other)
friend std::ostream & operator<<(std::ostream &os, const SignCondition &rhs)
Definition: SignCondition.h:74
static ThomComparisonResult compare(const SignCondition &lhs, const SignCondition &rhs)
Definition: SignCondition.h:48
SignCondition trailingPart(uint count) const
Definition: SignCondition.h:37