range_iterator.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 <xlnt/cell/cell.hpp>
  24. #include <xlnt/worksheet/range.hpp>
  25. #include <xlnt/worksheet/range_iterator.hpp>
  26. #include <xlnt/worksheet/range_reference.hpp>
  27. #include <xlnt/worksheet/worksheet.hpp>
  28. namespace xlnt {
  29. range_iterator::reference range_iterator::operator*()
  30. {
  31. return cell_vector(ws_, cursor_, bounds_, order_, skip_null_, false);
  32. }
  33. const range_iterator::reference range_iterator::operator*() const
  34. {
  35. return cell_vector(ws_, cursor_, bounds_, order_, skip_null_, false);
  36. }
  37. range_iterator::range_iterator(worksheet &ws, const cell_reference &cursor,
  38. const range_reference &bounds, major_order order, bool skip_null)
  39. : skip_null_(skip_null),
  40. order_(order),
  41. ws_(ws),
  42. cursor_(cursor),
  43. bounds_(bounds)
  44. {
  45. if (skip_null_ && (**this).empty())
  46. {
  47. ++(*this);
  48. }
  49. }
  50. bool range_iterator::operator==(const range_iterator &other) const
  51. {
  52. return ws_ == other.ws_
  53. && cursor_ == other.cursor_
  54. && order_ == other.order_
  55. && skip_null_ == other.skip_null_;
  56. }
  57. bool range_iterator::operator!=(const range_iterator &other) const
  58. {
  59. return !(*this == other);
  60. }
  61. range_iterator &range_iterator::operator--()
  62. {
  63. if (order_ == major_order::row)
  64. {
  65. if (cursor_.row() > bounds_.top_left().row())
  66. {
  67. cursor_.row(cursor_.row() - 1);
  68. }
  69. if (skip_null_)
  70. {
  71. while ((**this).empty() && cursor_.row() > bounds_.top_left().row())
  72. {
  73. cursor_.row(cursor_.row() - 1);
  74. }
  75. }
  76. }
  77. else
  78. {
  79. if (cursor_.column() > bounds_.top_left().column())
  80. {
  81. cursor_.column_index(cursor_.column_index() - 1);
  82. }
  83. if (skip_null_)
  84. {
  85. while ((**this).empty() && cursor_.column() > bounds_.top_left().column())
  86. {
  87. cursor_.column_index(cursor_.column_index() - 1);
  88. }
  89. }
  90. }
  91. return *this;
  92. }
  93. range_iterator range_iterator::operator--(int)
  94. {
  95. range_iterator old = *this;
  96. --*this;
  97. return old;
  98. }
  99. range_iterator &range_iterator::operator++()
  100. {
  101. if (order_ == major_order::row)
  102. {
  103. if (cursor_.row() <= bounds_.bottom_right().row())
  104. {
  105. cursor_.row(cursor_.row() + 1);
  106. }
  107. if (skip_null_)
  108. {
  109. while ((**this).empty() && cursor_.row() <= bounds_.bottom_right().row())
  110. {
  111. cursor_.row(cursor_.row() + 1);
  112. }
  113. }
  114. }
  115. else
  116. {
  117. if (cursor_.column() <= bounds_.bottom_right().column())
  118. {
  119. cursor_.column_index(cursor_.column_index() + 1);
  120. }
  121. if (skip_null_)
  122. {
  123. while ((**this).empty() && cursor_.column() <= bounds_.bottom_right().column())
  124. {
  125. cursor_.column_index(cursor_.column_index() + 1);
  126. }
  127. }
  128. }
  129. return *this;
  130. }
  131. range_iterator range_iterator::operator++(int)
  132. {
  133. range_iterator old = *this;
  134. ++*this;
  135. return old;
  136. }
  137. const_range_iterator::const_range_iterator(const worksheet &ws, const cell_reference &cursor,
  138. const range_reference &bounds, major_order order, bool skip_null)
  139. : skip_null_(skip_null),
  140. order_(order),
  141. ws_(ws.d_),
  142. cursor_(cursor),
  143. bounds_(bounds)
  144. {
  145. if (skip_null_ && (**this).empty())
  146. {
  147. ++(*this);
  148. }
  149. }
  150. bool const_range_iterator::operator==(const const_range_iterator &other) const
  151. {
  152. return ws_ == other.ws_
  153. && cursor_ == other.cursor_
  154. && order_ == other.order_
  155. && skip_null_ == other.skip_null_;
  156. }
  157. bool const_range_iterator::operator!=(const const_range_iterator &other) const
  158. {
  159. return !(*this == other);
  160. }
  161. const_range_iterator &const_range_iterator::operator--()
  162. {
  163. if (order_ == major_order::row)
  164. {
  165. if (cursor_.row() > bounds_.top_left().row())
  166. {
  167. cursor_.row(cursor_.row() - 1);
  168. }
  169. if (skip_null_)
  170. {
  171. while ((**this).empty() && cursor_.row() > bounds_.top_left().row())
  172. {
  173. cursor_.row(cursor_.row() - 1);
  174. }
  175. }
  176. }
  177. else
  178. {
  179. if (cursor_.column() > bounds_.top_left().column())
  180. {
  181. cursor_.column_index(cursor_.column_index() - 1);
  182. }
  183. if (skip_null_)
  184. {
  185. while ((**this).empty() && cursor_.row() > bounds_.top_left().column())
  186. {
  187. cursor_.column_index(cursor_.column_index() - 1);
  188. }
  189. }
  190. }
  191. return *this;
  192. }
  193. const_range_iterator const_range_iterator::operator--(int)
  194. {
  195. const_range_iterator old = *this;
  196. --*this;
  197. return old;
  198. }
  199. const_range_iterator &const_range_iterator::operator++()
  200. {
  201. if (order_ == major_order::row)
  202. {
  203. if (cursor_.row() <= bounds_.bottom_right().row())
  204. {
  205. cursor_.row(cursor_.row() + 1);
  206. }
  207. if (skip_null_)
  208. {
  209. while ((**this).empty() && cursor_.row() <= bounds_.bottom_right().row())
  210. {
  211. cursor_.row(cursor_.row() + 1);
  212. }
  213. }
  214. }
  215. else
  216. {
  217. if (cursor_.column() <= bounds_.bottom_right().column())
  218. {
  219. cursor_.column_index(cursor_.column_index() + 1);
  220. }
  221. if (skip_null_)
  222. {
  223. while ((**this).empty() && cursor_.column() <= bounds_.bottom_right().column())
  224. {
  225. cursor_.column_index(cursor_.column_index() + 1);
  226. }
  227. }
  228. }
  229. return *this;
  230. }
  231. const_range_iterator const_range_iterator::operator++(int)
  232. {
  233. const_range_iterator old = *this;
  234. ++*this;
  235. return old;
  236. }
  237. const const_range_iterator::reference const_range_iterator::operator*() const
  238. {
  239. return cell_vector(ws_, cursor_, bounds_, order_, skip_null_, false);
  240. }
  241. } // namespace xlnt