piranha  0.10
term.hpp
1 /* Copyright 2009-2017 Francesco Biscani (bluescarni@gmail.com)
2 
3 This file is part of the Piranha library.
4 
5 The Piranha library is free software; you can redistribute it and/or modify
6 it under the terms of either:
7 
8  * the GNU Lesser General Public License as published by the Free
9  Software Foundation; either version 3 of the License, or (at your
10  option) any later version.
11 
12 or
13 
14  * the GNU General Public License as published by the Free Software
15  Foundation; either version 3 of the License, or (at your option) any
16  later version.
17 
18 or both in parallel, as here.
19 
20 The Piranha library is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 for more details.
24 
25 You should have received copies of the GNU General Public License and the
26 GNU Lesser General Public License along with the Piranha library. If not,
27 see https://www.gnu.org/licenses/. */
28 
29 #ifndef PIRANHA_TERM_HPP
30 #define PIRANHA_TERM_HPP
31 
32 #include <cstddef>
33 #include <functional>
34 #include <type_traits>
35 #include <utility>
36 
37 #include <piranha/is_cf.hpp>
38 #include <piranha/is_key.hpp>
39 #include <piranha/math.hpp>
40 #include <piranha/symbol_utils.hpp>
41 #include <piranha/type_traits.hpp>
42 
43 namespace piranha
44 {
45 
47 
65 template <typename Cf, typename Key>
66 class term
67 {
68  PIRANHA_TT_CHECK(is_cf, Cf);
69  PIRANHA_TT_CHECK(is_key, Key);
70  // Enabler for the generic binary ctor.
71  template <typename T, typename U>
72  using binary_ctor_enabler
73  = enable_if_t<conjunction<std::is_constructible<Cf, T &&>, std::is_constructible<Key, U &&>>::value, int>;
74 
75 public:
77  using cf_type = Cf;
79  using key_type = Key;
81 
86  term() : m_cf(), m_key() {}
88 
91  term(const term &) = default;
93  term(term &&) = default;
95 
106  template <typename T, typename U, binary_ctor_enabler<T, U> = 0>
107  explicit term(T &&cf, U &&key) : m_cf(std::forward<T>(cf)), m_key(std::forward<U>(key))
108  {
109  }
112  {
113  PIRANHA_TT_CHECK(is_container_element, term);
114  }
116 
125  term &operator=(const term &other) = default;
127 
132  term &operator=(term &&other) noexcept
133  {
134  m_cf = std::move(other.m_cf);
135  m_key = std::move(other.m_key);
136  return *this;
137  }
139 
148  bool operator==(const term &other) const
149  {
150  return m_key == other.m_key;
151  }
153 
160  std::size_t hash() const
161  {
162  return std::hash<key_type>{}(m_key);
163  }
165 
170  bool is_compatible(const symbol_fset &args) const noexcept
171  {
172  return m_key.is_compatible(args);
173  }
175 
184  bool is_zero(const symbol_fset &args) const noexcept
185  {
186  return math::is_zero(m_cf) || m_key.is_zero(args);
187  }
189  mutable Cf m_cf;
191  Key m_key;
192 };
193 
195 
199 template <typename Cf, typename Key>
200 struct enable_noexcept_checks<term<Cf, Key>> {
201 private:
202  static const bool implementation_defined
203  = conjunction<enable_noexcept_checks<Cf>, enable_noexcept_checks<Key>>::value;
204 
205 public:
207  static const bool value = implementation_defined;
208 };
209 
210 template <typename Cf, typename Key>
211 const bool enable_noexcept_checks<term<Cf, Key>>::value;
212 }
213 
214 namespace std
215 {
216 
218 template <typename Cf, typename Key>
219 struct hash<piranha::term<Cf, Key>> {
221  using result_type = size_t;
225 
231  {
232  return a.hash();
233  }
234 };
235 }
236 
237 #endif
size_t result_type
The result type.
Definition: term.hpp:221
Cf m_cf
Coefficient member.
Definition: term.hpp:189
Type trait to detect coefficient types.
Definition: is_cf.hpp:55
Key type concept check.
Definition: is_key.hpp:65
~term()
Destructor.
Definition: term.hpp:111
STL namespace.
std::size_t hash() const
Hash value.
Definition: term.hpp:160
Key m_key
Key member.
Definition: term.hpp:191
term(T &&cf, U &&key)
Constructor from generic coefficient and key.
Definition: term.hpp:107
Term class.
Definition: term.hpp:66
term()
Default constructor.
Definition: term.hpp:86
boost::container::flat_set< std::string > symbol_fset
Flat set of symbols.
Key key_type
Alias for the key type.
Definition: term.hpp:79
static const bool value
Default value of the type trait.
bool is_compatible(const symbol_fset &args) const noexcept
Compatibility test.
Definition: term.hpp:170
Cf cf_type
Alias for the coefficient type.
Definition: term.hpp:77
Enable noexcept checks.
result_type operator()(const argument_type &a) const
Hash operator.
Definition: term.hpp:230
bool is_zero(const T &x)
Zero test.
Definition: math.hpp:133
Root piranha namespace.
Definition: array_key.hpp:52
Type traits.
bool operator==(const term &other) const
Equality operator.
Definition: term.hpp:148
term & operator=(const term &other)=default
Copy assignment operator.
term & operator=(term &&other) noexcept
Move assignment operator.
Definition: term.hpp:132
Type trait for well-behaved container elements.
bool is_zero(const symbol_fset &args) const noexcept
Zero test.
Definition: term.hpp:184