expression.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*-----------------------------------------------------------------------------
  2. | Copyright (c) 2013-2017, Nucleic Development Team.
  3. |
  4. | Distributed under the terms of the Modified BSD License.
  5. |
  6. | The full license is in the file LICENSE, distributed with this software.
  7. |----------------------------------------------------------------------------*/
  8. #pragma once
  9. #include <vector>
  10. #include "term.h"
  11. namespace kiwi
  12. {
  13. class Expression
  14. {
  15. public:
  16. Expression(double constant = 0.0) : m_constant(constant) {}
  17. Expression(const Term &term, double constant = 0.0) : m_terms(1, term), m_constant(constant) {}
  18. Expression(std::vector<Term> terms, double constant = 0.0) : m_terms(std::move(terms)), m_constant(constant) {}
  19. Expression(const Expression&) = default;
  20. // Could be marked noexcept but for a bug in the GCC of the manylinux1 image
  21. Expression(Expression&&) = default;
  22. ~Expression() = default;
  23. const std::vector<Term> &terms() const
  24. {
  25. return m_terms;
  26. }
  27. double constant() const
  28. {
  29. return m_constant;
  30. }
  31. double value() const
  32. {
  33. double result = m_constant;
  34. for (const Term &term : m_terms)
  35. result += term.value();
  36. return result;
  37. }
  38. Expression& operator=(const Expression&) = default;
  39. // Could be marked noexcept but for a bug in the GCC of the manylinux1 image
  40. Expression& operator=(Expression&&) = default;
  41. private:
  42. std::vector<Term> m_terms;
  43. double m_constant;
  44. };
  45. } // namespace kiwi