cell_vector.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright (c) 2014-2021 Thomas Fussell
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE
  20. //
  21. // @license: http://www.opensource.org/licenses/mit-license.php
  22. // @author: see AUTHORS file
  23. #include <algorithm> // std::all_of
  24. #include <xlnt/cell/cell.hpp>
  25. #include <xlnt/worksheet/cell_iterator.hpp>
  26. #include <xlnt/worksheet/cell_vector.hpp>
  27. namespace xlnt {
  28. cell_vector::cell_vector(worksheet ws, const cell_reference &cursor,
  29. const range_reference &bounds, major_order order, bool skip_null, bool wrap)
  30. : ws_(ws),
  31. cursor_(cursor),
  32. bounds_(bounds),
  33. order_(order),
  34. skip_null_(skip_null),
  35. wrap_(wrap)
  36. {
  37. }
  38. cell_vector::iterator cell_vector::begin()
  39. {
  40. return iterator(ws_, cursor_, bounds_, order_, skip_null_, wrap_);
  41. }
  42. cell_vector::iterator cell_vector::end()
  43. {
  44. auto past_end = cursor_;
  45. if (order_ == major_order::row)
  46. {
  47. past_end.column_index(bounds_.bottom_right().column_index() + 1);
  48. }
  49. else
  50. {
  51. past_end.row(bounds_.bottom_right().row() + 1);
  52. }
  53. return iterator(ws_, past_end, bounds_, order_, skip_null_, wrap_);
  54. }
  55. cell_vector::const_iterator cell_vector::cbegin() const
  56. {
  57. return const_iterator(ws_, cursor_, bounds_, order_, skip_null_, wrap_);
  58. }
  59. cell_vector::const_iterator cell_vector::cend() const
  60. {
  61. auto past_end = cursor_;
  62. if (order_ == major_order::row)
  63. {
  64. past_end.column_index(bounds_.bottom_right().column_index() + 1);
  65. }
  66. else
  67. {
  68. past_end.row(bounds_.bottom_right().row() + 1);
  69. }
  70. return const_iterator(ws_, past_end, bounds_, order_, skip_null_, wrap_);
  71. }
  72. bool cell_vector::empty() const
  73. {
  74. return begin() == end();
  75. }
  76. cell cell_vector::front()
  77. {
  78. return *begin();
  79. }
  80. const cell cell_vector::front() const
  81. {
  82. return *cbegin();
  83. }
  84. cell cell_vector::back()
  85. {
  86. return *(--end());
  87. }
  88. const cell cell_vector::back() const
  89. {
  90. return *(--cend());
  91. }
  92. std::size_t cell_vector::length() const
  93. {
  94. return order_ == major_order::row ? bounds_.width() : bounds_.height();
  95. }
  96. cell_vector::const_iterator cell_vector::begin() const
  97. {
  98. return cbegin();
  99. }
  100. cell_vector::const_iterator cell_vector::end() const
  101. {
  102. return cend();
  103. }
  104. cell_vector::reverse_iterator cell_vector::rbegin()
  105. {
  106. return reverse_iterator(end());
  107. }
  108. cell_vector::reverse_iterator cell_vector::rend()
  109. {
  110. return reverse_iterator(begin());
  111. }
  112. cell_vector::const_reverse_iterator cell_vector::crbegin() const
  113. {
  114. return const_reverse_iterator(cend());
  115. }
  116. cell_vector::const_reverse_iterator cell_vector::rbegin() const
  117. {
  118. return crbegin();
  119. }
  120. cell_vector::const_reverse_iterator cell_vector::crend() const
  121. {
  122. return const_reverse_iterator(cbegin());
  123. }
  124. cell_vector::const_reverse_iterator cell_vector::rend() const
  125. {
  126. return crend();
  127. }
  128. cell cell_vector::operator[](std::size_t cell_index)
  129. {
  130. if (order_ == major_order::row)
  131. {
  132. return ws_.cell(cursor_.make_offset(static_cast<int>(cell_index), 0));
  133. }
  134. return ws_.cell(cursor_.make_offset(0, static_cast<int>(cell_index)));
  135. }
  136. const cell cell_vector::operator[](std::size_t cell_index) const
  137. {
  138. if (order_ == major_order::row)
  139. {
  140. return ws_.cell(cursor_.make_offset(static_cast<int>(cell_index), 0));
  141. }
  142. return ws_.cell(cursor_.make_offset(0, static_cast<int>(cell_index)));
  143. }
  144. } // namespace xlnt