symbol.cpp
1 #include <symengine/constants.h>
2 #include <symengine/symengine_casts.h>
3 
4 namespace SymEngine
5 {
6 
7 Symbol::Symbol(const std::string &name)
8  : name_{name} {SYMENGINE_ASSIGN_TYPEID()}
9 
10  hash_t Symbol::__hash__() const
11 {
12  hash_t seed = 0;
13  hash_combine(seed, name_);
14  return seed;
15 }
16 
17 bool Symbol::__eq__(const Basic &o) const
18 {
19  if (is_a<Symbol>(o))
20  return name_ == down_cast<const Symbol &>(o).name_;
21  return false;
22 }
23 
24 int Symbol::compare(const Basic &o) const
25 {
26  SYMENGINE_ASSERT(is_a<Symbol>(o))
27  const Symbol &s = down_cast<const Symbol &>(o);
28  if (name_ == s.name_)
29  return 0;
30  return name_ < s.name_ ? -1 : 1;
31 }
32 
33 RCP<const Symbol> Symbol::as_dummy() const
34 {
35  return dummy(name_);
36 }
37 
38 #ifdef WITH_SYMENGINE_THREAD_SAFE
39 std::atomic<size_t> Dummy::count_{0};
40 #else
41 size_t Dummy::count_ = 0;
42 #endif
43 
44 Dummy::Dummy() : Dummy("_Dummy") {}
45 
46 Dummy::Dummy(const std::string &name)
47  : Dummy(name,
48 #ifdef WITH_SYMENGINE_THREAD_SAFE
49  Dummy::count_.fetch_add(1, std::memory_order_relaxed)
50 #else
51  Dummy::count_++
52 #endif
53  )
54 {
55 }
56 
57 Dummy::Dummy(const std::string &name, size_t dummy_index)
58  : Symbol(name), dummy_index(dummy_index){SYMENGINE_ASSIGN_TYPEID()}
59 
60  hash_t Dummy::__hash__() const
61 {
62  hash_t seed = 0;
63  hash_combine(seed, get_name());
65  return seed;
66 }
67 
68 bool Dummy::__eq__(const Basic &o) const
69 {
70  if (is_a<Dummy>(o))
71  return ((get_name() == down_cast<const Dummy &>(o).get_name())
72  and (dummy_index == down_cast<const Dummy &>(o).get_index()));
73  return false;
74 }
75 
76 int Dummy::compare(const Basic &o) const
77 {
78  SYMENGINE_ASSERT(is_a<Dummy>(o))
79  const Dummy &s = down_cast<const Dummy &>(o);
80  if (get_name() == s.get_name()) {
81  if (dummy_index == s.get_index())
82  return 0;
83  return dummy_index < s.get_index() ? -1 : 1;
84  }
85  return get_name() < s.get_name() ? -1 : 1;
86 }
87 
88 } // namespace SymEngine
The lowest unit of symbolic representation.
Definition: basic.h:97
Dummy()
Dummy Constructors.
Definition: symbol.cpp:44
int compare(const Basic &o) const override
Definition: symbol.cpp:76
hash_t __hash__() const override
Definition: symbol.cpp:60
size_t dummy_index
Dummy index.
Definition: symbol.h:60
static size_t count_
Dummy count.
Definition: symbol.h:57
bool __eq__(const Basic &o) const override
Definition: symbol.cpp:68
const std::string & get_name() const
Definition: symbol.h:38
Symbol(const std::string &name)
Symbol Constructor.
Definition: symbol.cpp:7
bool __eq__(const Basic &o) const override
Definition: symbol.cpp:17
std::string name_
name of Symbol
Definition: symbol.h:18
int compare(const Basic &o) const override
Definition: symbol.cpp:24
hash_t __hash__() const override
Definition: symbol.cpp:10
Main namespace for SymEngine package.
Definition: add.cpp:19
RCP< const Dummy > dummy()
inline version to return Dummy
Definition: symbol.h:93
void hash_combine(hash_t &seed, const T &v)
Definition: basic-inl.h:97