tribool.h
1 #ifndef SYMENGINE_TRIBOOL_H
2 #define SYMENGINE_TRIBOOL_H
3
4 namespace SymEngine
5 {
6
7 enum class tribool { indeterminate = -1, trifalse = 0, tritrue = 1 };
8
9 inline bool is_true(tribool x)
10 {
11  return x == tribool::tritrue;
12 }
13
14 inline bool is_false(tribool x)
15 {
16  return x == tribool::trifalse;
17 }
18
19 inline bool is_indeterminate(tribool x)
20 {
21  return x == tribool::indeterminate;
22 }
23
24 inline tribool tribool_from_bool(bool x)
25 {
26  return static_cast<tribool>(x);
27 }
28
29 inline tribool and_tribool(tribool a, tribool b)
30 {
31  if (!(static_cast<unsigned>(a) & static_cast<unsigned>(b))) {
32  return tribool::trifalse;
33  } else {
34  return static_cast<tribool>(static_cast<unsigned>(a)
35  | static_cast<unsigned>(b));
36  }
37 }
38
39 inline tribool or_tribool(tribool a, tribool b)
40 {
41  if (is_true(a) || is_true(b)) {
42  return tribool::tritrue;
43  } else if (is_indeterminate(a) || is_indeterminate(b)) {
44  return tribool::indeterminate;
45  } else {
46  return tribool::trifalse;
47  }
48 }
49
50 inline tribool not_tribool(tribool a)
51 {
52  if (is_indeterminate(a)) {
53  return a;
54  } else {
55  return static_cast<tribool>(!static_cast<unsigned>(a));
56  }
57 }
58
59 // The weak kleene conjunction
60 // Indeterminate if any indeterminate otherwise like regular and
61 inline tribool andwk_tribool(tribool a, tribool b)
62 {
63  if (is_indeterminate(a) || is_indeterminate(b)) {
64  return tribool::indeterminate;
65  } else {
66  return static_cast<tribool>(static_cast<unsigned>(a)
67  && static_cast<unsigned>(b));
68  }
69 }
70
71 // The weak kleene disjunction
72 // Indeterminate if any indeterminate otherwise like regular or
73 inline tribool orwk_tribool(tribool a, tribool b)
74 {
75  if (is_indeterminate(a) || is_indeterminate(b)) {
76  return tribool::indeterminate;
77  } else {
78  return static_cast<tribool>(static_cast<unsigned>(a)
79  || static_cast<unsigned>(b));
80  }
81 }
82
83 } // namespace SymEngine
84
85 #endif
Main namespace for SymEngine package.