expression.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 COPYING.txt, 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 ) :
  18. m_terms( 1, term ), m_constant( constant ) {}
  19. Expression( const std::vector<Term>& terms, double constant = 0.0 ) :
  20. m_terms( terms ), m_constant( constant ) {}
  21. ~Expression() {}
  22. const std::vector<Term>& terms() const
  23. {
  24. return m_terms;
  25. }
  26. double constant() const
  27. {
  28. return m_constant;
  29. }
  30. double value() const
  31. {
  32. typedef std::vector<Term>::const_iterator iter_t;
  33. double result = m_constant;
  34. iter_t end = m_terms.end();
  35. for( iter_t it = m_terms.begin(); it != end; ++it )
  36. result += it->value();
  37. return result;
  38. }
  39. private:
  40. std::vector<Term> m_terms;
  41. double m_constant;
  42. };
  43. } // namespace kiwi