lazy_vector_alloc_test.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright (C) 2016-2018 T. Zachary Laine
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/yap/expression.hpp>
  7. #include <algorithm>
  8. #include <cassert>
  9. #include <iostream>
  10. #include <vector>
  11. #include <boost/test/minimal.hpp>
  12. int allocations = 0;
  13. void * operator new(std::size_t size)
  14. {
  15. ++allocations;
  16. void * retval = malloc(size);
  17. if (!retval)
  18. throw std::bad_alloc();
  19. return retval;
  20. }
  21. void operator delete(void * ptr) noexcept { free(ptr); }
  22. template<boost::yap::expr_kind Kind, typename Tuple>
  23. struct lazy_vector_expr;
  24. struct take_nth
  25. {
  26. boost::yap::terminal<lazy_vector_expr, double> operator()(
  27. boost::yap::terminal<lazy_vector_expr, std::vector<double>> const &
  28. expr);
  29. std::size_t n;
  30. };
  31. template<boost::yap::expr_kind Kind, typename Tuple>
  32. struct lazy_vector_expr
  33. {
  34. static const boost::yap::expr_kind kind = Kind;
  35. Tuple elements;
  36. auto operator[](std::size_t n) const
  37. {
  38. return boost::yap::evaluate(boost::yap::transform(*this, take_nth{n}));
  39. }
  40. };
  41. BOOST_YAP_USER_BINARY_OPERATOR(plus, lazy_vector_expr, lazy_vector_expr)
  42. BOOST_YAP_USER_BINARY_OPERATOR(minus, lazy_vector_expr, lazy_vector_expr)
  43. boost::yap::terminal<lazy_vector_expr, double> take_nth::operator()(
  44. boost::yap::terminal<lazy_vector_expr, std::vector<double>> const & expr)
  45. {
  46. double x = boost::yap::value(expr)[n];
  47. return boost::yap::make_terminal<lazy_vector_expr, double>(std::move(x));
  48. }
  49. struct lazy_vector : lazy_vector_expr<
  50. boost::yap::expr_kind::terminal,
  51. boost::hana::tuple<std::vector<double>>>
  52. {
  53. lazy_vector() {}
  54. explicit lazy_vector(std::vector<double> && vec)
  55. {
  56. elements = boost::hana::tuple<std::vector<double>>(std::move(vec));
  57. }
  58. template<boost::yap::expr_kind Kind, typename Tuple>
  59. lazy_vector & operator+=(lazy_vector_expr<Kind, Tuple> const & rhs)
  60. {
  61. std::vector<double> & this_vec = boost::yap::value(*this);
  62. for (int i = 0, size = (int)this_vec.size(); i < size; ++i) {
  63. this_vec[i] += rhs[i];
  64. }
  65. return *this;
  66. }
  67. };
  68. int test_main(int, char * [])
  69. {
  70. lazy_vector v1{std::vector<double>(4, 1.0)};
  71. lazy_vector v2{std::vector<double>(4, 2.0)};
  72. lazy_vector v3{std::vector<double>(4, 3.0)};
  73. // Reset allocation count. There should be none from this point on.
  74. allocations = 0;
  75. double d1 = (v2 + v3)[2];
  76. std::cout << d1 << "\n";
  77. v1 += v2 - v3;
  78. std::cout << '{' << v1[0] << ',' << v1[1] << ',' << v1[2] << ',' << v1[3]
  79. << '}' << "\n";
  80. BOOST_CHECK(allocations == 0);
  81. return 0;
  82. }