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 
54 {
55  out << p.__str__();
56  return out;
57 }
58 
60 template <typename T>
61 inline void hash_combine_impl(
62  hash_t &seed, const T &v,
63  typename std::enable_if<std::is_base_of<Basic, T>::value>::type * = nullptr)
64 {
65  hash_combine(seed, v.hash());
66 }
67 
68 template <typename T>
69 inline void hash_combine_impl(
70  hash_t &seed, const T &v,
71  typename std::enable_if<std::is_integral<T>::value>::type * = nullptr)
72 {
73  seed ^= hash_t(v) + hash_t(0x9e3779b9) + (seed << 6) + (seed >> 2);
74 }
75 
76 inline void hash_combine_impl(hash_t &seed, const std::string &s)
77 {
78  for (const char &c : s) {
79  hash_combine<hash_t>(seed, static_cast<hash_t>(c));
80  }
81 }
82 
83 inline void hash_combine_impl(hash_t &seed, const double &s)
84 {
85  union {
86  hash_t h;
87  double d;
88  } u;
89  u.h = 0u;
90  u.d = s;
91  hash_combine(seed, u.h);
92 }
93 
94 template <class T>
95 inline void hash_combine(hash_t &seed, const T &v)
96 {
97  hash_combine_impl(seed, v);
98 }
99 
100 template <typename T>
101 hash_t vec_hash<T>::operator()(const T &v) const
102 {
103  hash_t h = 0;
104  for (auto i : v)
105  hash_combine<typename T::value_type>(h, i);
106  return h;
107 }
108 
110 template <typename T>
111 std::string to_string(const T &value)
112 {
113 #ifdef HAVE_SYMENGINE_STD_TO_STRING
114  return std::to_string(value);
115 #else
117  ss << value;
118  return ss.str();
119 #endif
120 }
121 
122 static struct ConstantInitializer {
125 } constantInitializer; // static initializer for every translation unit
126 
127 } // namespace SymEngine
128 
129 // std namespace functions
130 namespace std
131 {
133 template <>
134 struct hash<SymEngine::Basic> {
135  std::size_t operator()(const SymEngine::Basic &b) const
136  {
137  return static_cast<std::size_t>(b.hash());
138  }
139 };
140 } // namespace std
141 
142 #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:46
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)
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:95
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:61
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:111
std::ostream & operator<<(std::ostream &out, const SymEngine::Basic &p)
<< Operator
Definition: basic-inl.h:53
STL namespace.
T operator()(T... args)
T str(T... args)
T to_string(T... args)