basic-inl.h
1 #ifndef SYMENGINE_BASIC_INL_H
2 #define SYMENGINE_BASIC_INL_H
3 
4 namespace SymEngine
5 {
6 
7 inline hash_t Basic::hash() const
8 {
9  if (hash_ == 0)
10  hash_ = __hash__();
11  return hash_;
12 }
13 
15 inline bool Basic::__neq__(const Basic &o) const
16 {
17  return not(eq(*this, o));
18 }
19 
21 inline bool eq(const Basic &a, const Basic &b)
22 {
23  if (&a == &b) {
24  return true;
25  }
26  return a.__eq__(b);
27 }
29 inline bool neq(const Basic &a, const Basic &b)
30 {
31  return not(a.__eq__(b));
32 }
33 
35 template <class T>
36 inline bool is_a(const Basic &b)
37 {
38  return T::type_code_id == b.get_type_code();
39 }
40 
41 template <class T>
42 inline bool is_a_sub(const Basic &b)
43 {
44  return dynamic_cast<const T *>(&b) != nullptr;
45 }
46 
47 inline bool is_same_type(const Basic &a, const Basic &b)
48 {
49  return a.get_type_code() == b.get_type_code();
50 }
51 
52 inline bool is_true(tribool x)
53 {
54  return x == tribool::tritrue;
55 }
56 
57 inline bool is_false(tribool x)
58 {
59  return x == tribool::trifalse;
60 }
61 
62 inline bool is_indeterminate(tribool x)
63 {
64  return x == tribool::indeterminate;
65 }
66 
67 inline tribool and_tribool(tribool a, tribool b)
68 {
69  if (!(a & b)) {
70  return tribool::trifalse;
71  } else {
72  return (tribool)(a | b);
73  }
74 }
75 
76 inline tribool not_tribool(tribool a)
77 {
78  if (is_indeterminate(a))
79  return a;
80  else
81  return (tribool)!a;
82 }
83 
84 // The weak kleene conjunction
85 // Indeterminate if any indeterminate otherwise like regular and
86 inline tribool andwk_tribool(tribool a, tribool b)
87 {
88  if (is_indeterminate(a) or is_indeterminate(b))
89  return tribool::indeterminate;
90  else
91  return (tribool)(a && b);
92 }
93 
96 {
97  out << p.__str__();
98  return out;
99 }
100 
102 template <typename T>
103 inline void hash_combine_impl(
104  hash_t &seed, const T &v,
105  typename std::enable_if<std::is_base_of<Basic, T>::value>::type * = nullptr)
106 {
107  hash_combine(seed, v.hash());
108 }
109 
110 template <typename T>
111 inline void hash_combine_impl(
112  hash_t &seed, const T &v,
113  typename std::enable_if<std::is_integral<T>::value>::type * = nullptr)
114 {
115  seed ^= hash_t(v) + hash_t(0x9e3779b9) + (seed << 6) + (seed >> 2);
116 }
117 
118 inline void hash_combine_impl(hash_t &seed, const std::string &s)
119 {
120  for (const char &c : s) {
121  hash_combine<hash_t>(seed, static_cast<hash_t>(c));
122  }
123 }
124 
125 inline void hash_combine_impl(hash_t &seed, const double &s)
126 {
127  union {
128  hash_t h;
129  double d;
130  } u;
131  u.h = 0u;
132  u.d = s;
133  hash_combine(seed, u.h);
134 }
135 
136 template <class T>
137 inline void hash_combine(hash_t &seed, const T &v)
138 {
139  hash_combine_impl(seed, v);
140 }
141 
142 template <typename T>
143 hash_t vec_hash<T>::operator()(const T &v) const
144 {
145  hash_t h = 0;
146  for (auto i : v)
147  hash_combine<typename T::value_type>(h, i);
148  return h;
149 }
150 
152 template <typename T>
153 std::string to_string(const T &value)
154 {
155 #ifdef HAVE_SYMENGINE_STD_TO_STRING
156  return std::to_string(value);
157 #else
159  ss << value;
160  return ss.str();
161 #endif
162 }
163 
164 } // namespace SymEngine
165 
166 // std namespace functions
167 namespace std
168 {
170 template <>
171 struct hash<SymEngine::Basic> {
172  std::size_t operator()(const SymEngine::Basic &b) const
173  {
174  return static_cast<std::size_t>(b.hash());
175  }
176 };
177 } // namespace std
178 
179 #endif
The lowest unit of symbolic representation.
Definition: basic.h:95
bool __neq__(const Basic &o) const
true if this is not equal to o.
Definition: basic-inl.h:15
std::string __str__() const
Definition: basic.cpp:22
virtual bool __eq__(const Basic &o) const =0
Test equality.
virtual hash_t __hash__() const =0
hash_t hash_
Private variables.
Definition: basic.h:105
hash_t hash() const
Definition: basic-inl.h:7
Main namespace for SymEngine package.
Definition: add.cpp:19
bool is_a_sub(const Basic &b)
Definition: basic-inl.h:42
bool eq(const Basic &a, const Basic &b)
Checks equality for a and b
Definition: basic-inl.h:21
void hash_combine(hash_t &seed, const T &v)
Definition: basic-inl.h:137
bool is_a(const Basic &b)
Templatised version to check is_a type.
Definition: basic-inl.h:36
bool is_same_type(const Basic &a, const Basic &b)
Returns true if a and b are exactly the same type T.
Definition: basic-inl.h:47
void hash_combine_impl(hash_t &seed, const T &v, typename std::enable_if< std::is_base_of< Basic, T >::value >::type *=nullptr)
Templatised version to combine hash.
Definition: basic-inl.h:103
bool neq(const Basic &a, const Basic &b)
Checks inequality for a and b
Definition: basic-inl.h:29
std::string to_string(const T &value)
workaround for MinGW bug
Definition: basic-inl.h:153
std::ostream & operator<<(std::ostream &out, const SymEngine::Basic &p)
<< Operator
Definition: basic-inl.h:95
STL namespace.
T operator()(T... args)
T str(T... args)
T to_string(T... args)