io.hh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file io.hh
  3. /// @brief Write and read arrays, expressions.
  4. // (c) Daniel Llorens - 2014-2018
  5. // This library is free software; you can redistribute it and/or modify it under
  6. // the terms of the GNU Lesser General Public License as published by the Free
  7. // Software Foundation; either version 3 of the License, or (at your option) any
  8. // later version.
  9. #pragma once
  10. #include "ra/atom.hh"
  11. #include "ra/format.hh"
  12. #include "ra/concrete.hh"
  13. #include <iosfwd>
  14. #include <sstream>
  15. namespace ra {
  16. // TODO merge with ply_ravel @ ply.hh. But should control order.
  17. template <class A>
  18. inline std::ostream &
  19. operator<<(std::ostream & o, FormatArray<A> const & fa)
  20. {
  21. // FIXME note that this copies / resets the RaIterator if fa.a already is one; see [ra35].
  22. auto a = ra::start(fa.a);
  23. static_assert(size_s(a)!=DIM_BAD, "cannot print type");
  24. rank_t const rank = a.rank();
  25. auto sha = concrete(shape(a));
  26. auto ind = with_same_shape(sha, 0);
  27. if (withshape==fa.shape || (defaultshape==fa.shape && size_s(a)==DIM_ANY)) {
  28. o << start(sha) << '\n';
  29. }
  30. for (rank_t k=0; k<rank; ++k) {
  31. if (sha[k]==0) {
  32. return o;
  33. }
  34. }
  35. // order here is row-major on purpose.
  36. for (;;) {
  37. o << *(a.flat());
  38. for (int k=0; ; ++k) {
  39. if (k>=rank) {
  40. return o;
  41. } else if (ind[rank-1-k]<sha[rank-1-k]-1) {
  42. ++ind[rank-1-k];
  43. a.adv(rank-1-k, 1);
  44. switch (k) {
  45. case 0: o << fa.sep0; break;
  46. case 1: o << fa.sep1; break;
  47. default: std::fill_n(std::ostream_iterator<char const *>(o, ""), k, fa.sep2);
  48. }
  49. break;
  50. } else {
  51. ind[rank-1-k] = 0;
  52. a.adv(rank-1-k, 1-sha[rank-1-k]);
  53. }
  54. }
  55. }
  56. }
  57. // is_foreign_vector is included b/c std::vector or std::array may be used as the type of shape().
  58. template <class A> requires (is_ra<A> || is_foreign_vector<A>)
  59. inline std::ostream &
  60. operator<<(std::ostream & o, A && a)
  61. {
  62. return o << format_array(a);
  63. }
  64. // initializer_list cannot match A && above.
  65. template <class T>
  66. inline std::ostream &
  67. operator<<(std::ostream & o, std::initializer_list<T> const & a)
  68. {
  69. return o << format_array(a);
  70. }
  71. // Static size.
  72. template <class C> requires (!is_scalar<C> && size_s<C>()!=DIM_ANY)
  73. inline std::istream &
  74. operator>>(std::istream & i, C & c)
  75. {
  76. for (auto & ci: c) { i >> ci; }
  77. return i;
  78. }
  79. // Special case for std::vector, to handle create-new / resize() difference.
  80. template <class T, class A>
  81. inline std::istream &
  82. operator>>(std::istream & i, std::vector<T, A> & c)
  83. {
  84. if (dim_t n; !((i >> n).fail())) {
  85. RA_CHECK(n>=0 && "negative sizes in input");
  86. c.resize(n);
  87. for (auto & ci: c) { i >> ci; }
  88. }
  89. return i;
  90. }
  91. // Expr size, so read shape and possibly allocate (TODO try to avoid).
  92. template <class C> requires (size_s<C>()==DIM_ANY)
  93. inline std::istream &
  94. operator>>(std::istream & i, C & c)
  95. {
  96. if (decltype(shape(c)) s; i >> s) {
  97. std::decay_t<C> cc(s, ra::none);
  98. RA_CHECK(every(start(s)>=0) && "negative sizes in input");
  99. // avoid copying in case Container's elements don't support it.
  100. swap(c, cc);
  101. // need row-major, serial iteration here. FIXME use ra:: traversal.
  102. for (auto & ci: c) { i >> ci; }
  103. }
  104. return i;
  105. }
  106. } // namespace ra