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 #if HAVE_SYMENGINE_RTTI
42 template <class T>
43 inline bool is_a_sub(const Basic &b)
44 {
45  return dynamic_cast<const T *>(&b) != nullptr;
46 }
47 #endif
48 
49 inline bool is_same_type(const Basic &a, const Basic &b)
50 {
51  return a.get_type_code() == b.get_type_code();
52 }
53 
56 {
57  out << p.__str__();
58  return out;
59 }
60 
62 template <typename T>
63 inline void hash_combine_impl(
64  hash_t &seed, const T &v,
65  typename std::enable_if<std::is_base_of<Basic, T>::value>::type * = nullptr)
66 {
67  hash_combine(seed, v.hash());
68 }
69 
70 template <typename T>
71 inline void hash_combine_impl(
72  hash_t &seed, const T &v,
73  typename std::enable_if<std::is_integral<T>::value>::type * = nullptr)
74 {
75  seed ^= hash_t(v) + hash_t(0x9e3779b9) + (seed << 6) + (seed >> 2);
76 }
77 
78 inline void hash_combine_impl(hash_t &seed, const std::string &s)
79 {
80  for (const char &c : s) {
81  hash_combine<hash_t>(seed, static_cast<hash_t>(c));
82  }
83 }
84 
85 inline void hash_combine_impl(hash_t &seed, const double &s)
86 {
87  union {
88  hash_t h;
89  double d;
90  } u;
91  u.h = 0u;
92  u.d = s;
93  hash_combine(seed, u.h);
94 }
95 
96 template <class T>
97 inline void hash_combine(hash_t &seed, const T &v)
98 {
99  hash_combine_impl(seed, v);
100 }
101 
102 template <typename T>
103 hash_t vec_hash<T>::operator()(const T &v) const
104 {
105  hash_t h = 0;
106  for (auto i : v)
107  hash_combine<typename T::value_type>(h, i);
108  return h;
109 }
110 
112 template <typename T>
113 std::string to_string(const T &value)
114 {
115 #ifdef HAVE_SYMENGINE_STD_TO_STRING
116  return std::to_string(value);
117 #else
119  ss << value;
120  return ss.str();
121 #endif
122 }
123 
124 static struct ConstantInitializer {
127 } constantInitializer; // static initializer for every translation unit
128 
129 } // namespace SymEngine
130 
131 // std namespace functions
132 namespace std
133 {
135 template <>
136 struct hash<SymEngine::Basic> {
137  std::size_t operator()(const SymEngine::Basic &b) const
138  {
139  return static_cast<std::size_t>(b.hash());
140  }
141 };
142 } // namespace std
143 
144 #endif
The lowest unit of symbolic representation.
Definition: basic.h:97
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:48
virtual bool __eq__(const Basic &o) const =0
Test equality.
virtual hash_t __hash__() const =0
hash_t hash_
Private variables.
Definition: basic.h:107
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)
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:97
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:49
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:63
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:113
std::ostream & operator<<(std::ostream &out, const SymEngine::Basic &p)
<< Operator
Definition: basic-inl.h:55
STL namespace.
T operator()(T... args)
T str(T... args)
T to_string(T... args)
Code