io.H 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // (c) Daniel Llorens - 2014-2016
  2. // This library is free software; you can redistribute it and/or modify it under
  3. // the terms of the GNU Lesser General Public License as published by the Free
  4. // Software Foundation; either version 3 of the License, or (at your option) any
  5. // later version.
  6. #pragma once
  7. #include "ra/type.H"
  8. #include "ra/traits.H"
  9. #include <iosfwd>
  10. #include <sstream>
  11. /// @file io.H
  12. /// @brief Write and read arrays, expressions.
  13. namespace ra {
  14. template <class A>
  15. struct FormatArray
  16. {
  17. A const & a;
  18. bool const shape;
  19. char const * sep0;
  20. char const * sep1;
  21. char const * sep2;
  22. };
  23. // written this way to avoid conversions too bool shape.
  24. template <class A, class B> inline
  25. FormatArray<A>
  26. format_array(A const & a, B const shape, char const * sep0=" ", char const * sep1="\n", char const * sep2="\n")
  27. {
  28. static_assert(std::is_same<std::decay_t<B>, bool>::value, "bad call to format_array");
  29. return FormatArray<A> { a, shape, sep0, sep1, sep2 };
  30. }
  31. template <class A> inline
  32. FormatArray<A> format_array(A const & a) { return format_array(a, true); }
  33. // After ply_index(). TODO merge with that.
  34. // TODO custom spacers at turn of dimension.
  35. // is_foreign_vector is included b/c std::vector or std::array may be used as shape_type.
  36. template <class A> inline
  37. std::ostream & operator<<(std::ostream & o, FormatArray<A> const & fa)
  38. {
  39. // work with ArrayIterator
  40. auto a = ra::start(fa.a);
  41. static_assert(decltype(a)::size_s()!=DIM_BAD, "cannot print type");
  42. rank_t const rank = a.rank();
  43. auto sha(a.shape());
  44. auto ind = ra_traits<decltype(sha)>::make(rank, 0);
  45. if (fa.shape && a.size_s()==DIM_ANY) {
  46. o << start(sha) << '\n';
  47. }
  48. for (rank_t k=0; k<rank; ++k) {
  49. if (sha[k]==0) {
  50. return o;
  51. }
  52. }
  53. // unlike in ply_index(), order here is row-major on purpose.
  54. for (;;) {
  55. next: ;
  56. o << a.at(ind);
  57. for (int k=0; k<rank; ++k) {
  58. if (++ind[rank-1-k]<sha[rank-1-k]) {
  59. switch (k) {
  60. case 0: o << fa.sep0; break;
  61. case 1: o << fa.sep1; break;
  62. default: std::fill_n(std::ostream_iterator<char const *>(o, ""), k-1, fa.sep2);
  63. }
  64. goto next;
  65. } else {
  66. ind[rank-1-k] = 0;
  67. }
  68. }
  69. return o;
  70. }
  71. }
  72. template <class A> inline
  73. std::enable_if_t<is_ra<A> || is_foreign_vector<A>, std::ostream &>
  74. operator<<(std::ostream & o, A && a)
  75. {
  76. return o << format_array(a);
  77. }
  78. // Static size.
  79. template <class C> inline
  80. std::enable_if_t<ra_traits<C>::size_s()!=DIM_ANY, std::istream &>
  81. operator>>(std::istream & i, C & c)
  82. {
  83. using T = typename ra_traits<C>::value_type;
  84. std::copy_n(std::istream_iterator<T>(i), c.size(), c.begin());
  85. return i;
  86. }
  87. // Special case for std::vector, to handle create-new / resize() difference.
  88. template <class T, class A> inline
  89. std::istream &
  90. operator>>(std::istream & i, std::vector<T, A> & c)
  91. {
  92. dim_t n;
  93. if (!((i >> n).fail())) {
  94. assert(n>=0);
  95. c.resize(n);
  96. std::copy_n(std::istream_iterator<T>(i), c.size(), c.begin());
  97. }
  98. return i;
  99. }
  100. // Expr size, so read shape and possibly allocate (TODO try to avoid).
  101. template <class C> inline
  102. std::enable_if_t<ra_traits<C>::size_s()==DIM_ANY, std::istream &>
  103. operator>>(std::istream & i, C & c)
  104. {
  105. typename ra_traits<C>::shape_type s;
  106. if (i >> s) {
  107. std::decay_t<C> cc(s, ra::unspecified);
  108. // avoid copying in case WithStorage's elements don't support it.
  109. swap(c, cc);
  110. // need row-major, serial iteration here. FIXME use ra:: traversal.
  111. for (auto & ci: c) {
  112. i >> ci;
  113. }
  114. }
  115. return i;
  116. }
  117. } // namespace ra