result.hpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2017-2023 Adam Wulkiewicz, Lodz, Poland.
  4. // This file was modified by Oracle on 2013-2022.
  5. // Modifications copyright (c) 2013-2022 Oracle and/or its affiliates.
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_RELATE_RESULT_HPP
  11. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_RELATE_RESULT_HPP
  12. #include <algorithm>
  13. #include <cstddef>
  14. #include <cstring>
  15. #include <string>
  16. #include <tuple>
  17. #include <type_traits>
  18. #include <boost/throw_exception.hpp>
  19. #include <boost/geometry/core/assert.hpp>
  20. #include <boost/geometry/core/coordinate_dimension.hpp>
  21. #include <boost/geometry/core/exception.hpp>
  22. #include <boost/geometry/core/static_assert.hpp>
  23. #include <boost/geometry/util/constexpr.hpp>
  24. #include <boost/geometry/util/sequence.hpp>
  25. namespace boost { namespace geometry {
  26. #ifndef DOXYGEN_NO_DETAIL
  27. namespace detail { namespace relate {
  28. enum field { interior = 0, boundary = 1, exterior = 2 };
  29. // TODO: IF THE RESULT IS UPDATED WITH THE MAX POSSIBLE VALUE FOR SOME PAIR OF GEOEMTRIES
  30. // THE VALUE ALREADY STORED MUSN'T BE CHECKED
  31. // update() calls chould be replaced with set() in those cases
  32. // but for safety reasons (STATIC_ASSERT) we should check if parameter D is valid and set() doesn't do that
  33. // so some additional function could be added, e.g. set_dim()
  34. template <typename MatrixOrMask, field F1, field F2>
  35. using fields_in_bounds = util::bool_constant
  36. <
  37. (F1 < MatrixOrMask::static_height && F2 < MatrixOrMask::static_width)
  38. >;
  39. // --------------- MATRIX ----------------
  40. // matrix
  41. template <std::size_t Height, std::size_t Width = Height>
  42. class matrix
  43. {
  44. public:
  45. typedef char value_type;
  46. typedef std::size_t size_type;
  47. typedef const char * const_iterator;
  48. typedef const_iterator iterator;
  49. static const std::size_t static_width = Width;
  50. static const std::size_t static_height = Height;
  51. static const std::size_t static_size = Width * Height;
  52. inline matrix()
  53. {
  54. std::fill_n(m_array, static_size, 'F');
  55. }
  56. template
  57. <
  58. field F1, field F2,
  59. std::enable_if_t<fields_in_bounds<matrix, F1, F2>::value, int> = 0
  60. >
  61. inline char get() const
  62. {
  63. static const std::size_t index = F1 * Width + F2;
  64. BOOST_STATIC_ASSERT(index < static_size);
  65. return m_array[index];
  66. }
  67. template
  68. <
  69. field F1, field F2, char V,
  70. std::enable_if_t<fields_in_bounds<matrix, F1, F2>::value, int> = 0
  71. >
  72. inline void set()
  73. {
  74. static const std::size_t index = F1 * Width + F2;
  75. BOOST_STATIC_ASSERT(index < static_size);
  76. m_array[index] = V;
  77. }
  78. inline char operator[](std::size_t index) const
  79. {
  80. BOOST_GEOMETRY_ASSERT(index < static_size);
  81. return m_array[index];
  82. }
  83. inline const_iterator begin() const
  84. {
  85. return m_array;
  86. }
  87. inline const_iterator end() const
  88. {
  89. return m_array + static_size;
  90. }
  91. inline static std::size_t size()
  92. {
  93. return static_size;
  94. }
  95. inline const char * data() const
  96. {
  97. return m_array;
  98. }
  99. inline std::string str() const
  100. {
  101. return std::string(m_array, static_size);
  102. }
  103. private:
  104. char m_array[static_size];
  105. };
  106. // matrix_handler
  107. template <typename Matrix>
  108. class matrix_handler
  109. {
  110. public:
  111. typedef Matrix result_type;
  112. static const bool interrupt = false;
  113. matrix_handler()
  114. {}
  115. result_type const& result() const
  116. {
  117. return m_matrix;
  118. }
  119. result_type const& matrix() const
  120. {
  121. return m_matrix;
  122. }
  123. result_type & matrix()
  124. {
  125. return m_matrix;
  126. }
  127. template <field F1, field F2, char D>
  128. inline bool may_update() const
  129. {
  130. BOOST_STATIC_ASSERT('0' <= D && D <= '9');
  131. char const c = m_matrix.template get<F1, F2>();
  132. return D > c || c > '9';
  133. }
  134. template <field F1, field F2, char V>
  135. inline void update()
  136. {
  137. BOOST_STATIC_ASSERT(('0' <= V && V <= '9') || V == 'T');
  138. char const c = m_matrix.template get<F1, F2>();
  139. // If c == T and V == T it will be set anyway but that's fine
  140. if (V > c || c > '9')
  141. {
  142. m_matrix.template set<F1, F2, V>();
  143. }
  144. }
  145. template <field F1, field F2, char V>
  146. inline void set()
  147. {
  148. BOOST_STATIC_ASSERT(('0' <= V && V <= '9') || V == 'T');
  149. m_matrix.template set<F1, F2, V>();
  150. }
  151. template <field F1, field F2>
  152. inline char get() const
  153. {
  154. return m_matrix.template get<F1, F2>();
  155. }
  156. private:
  157. Matrix m_matrix;
  158. };
  159. // --------------- RUN-TIME MASK ----------------
  160. // run-time mask
  161. template <std::size_t Height, std::size_t Width = Height>
  162. class mask
  163. {
  164. public:
  165. static const std::size_t static_width = Width;
  166. static const std::size_t static_height = Height;
  167. static const std::size_t static_size = Width * Height;
  168. inline mask(const char * s)
  169. {
  170. char * it = m_array;
  171. char * const last = m_array + static_size;
  172. for ( ; it != last && *s != '\0' ; ++it, ++s )
  173. {
  174. char c = *s;
  175. check_char(c);
  176. *it = c;
  177. }
  178. if ( it != last )
  179. {
  180. std::fill(it, last, '*');
  181. }
  182. }
  183. inline mask(const char * s, std::size_t count)
  184. {
  185. if ( count > static_size )
  186. {
  187. count = static_size;
  188. }
  189. if ( count > 0 )
  190. {
  191. std::for_each(s, s + count, check_char);
  192. std::copy_n(s, count, m_array);
  193. }
  194. if ( count < static_size )
  195. {
  196. std::fill_n(m_array + count, static_size - count, '*');
  197. }
  198. }
  199. template
  200. <
  201. field F1, field F2,
  202. std::enable_if_t<fields_in_bounds<mask, F1, F2>::value, int> = 0
  203. >
  204. inline char get() const
  205. {
  206. static const std::size_t index = F1 * Width + F2;
  207. BOOST_STATIC_ASSERT(index < static_size);
  208. return m_array[index];
  209. }
  210. private:
  211. static inline void check_char(char c)
  212. {
  213. bool const is_valid = c == '*' || c == 'T' || c == 'F'
  214. || ( c >= '0' && c <= '9' );
  215. if ( !is_valid )
  216. {
  217. BOOST_THROW_EXCEPTION(geometry::invalid_input_exception());
  218. }
  219. }
  220. char m_array[static_size];
  221. };
  222. // interrupt()
  223. template <typename Mask, bool InterruptEnabled>
  224. struct interrupt_dispatch
  225. {
  226. template <field F1, field F2, char V>
  227. static inline bool apply(Mask const&)
  228. {
  229. return false;
  230. }
  231. };
  232. template <typename Mask>
  233. struct interrupt_dispatch<Mask, true>
  234. {
  235. template <field F1, field F2, char V>
  236. static inline bool apply(Mask const& mask)
  237. {
  238. char m = mask.template get<F1, F2>();
  239. return check_element<V>(m);
  240. }
  241. template <char V>
  242. static inline bool check_element(char m)
  243. {
  244. if BOOST_GEOMETRY_CONSTEXPR (V >= '0' && V <= '9')
  245. {
  246. return m == 'F' || ( m < V && m >= '0' && m <= '9' );
  247. }
  248. else if BOOST_GEOMETRY_CONSTEXPR (V == 'T')
  249. {
  250. return m == 'F';
  251. }
  252. else
  253. {
  254. return false;
  255. }
  256. }
  257. };
  258. template <typename Masks, int I = 0, int N = std::tuple_size<Masks>::value>
  259. struct interrupt_dispatch_tuple
  260. {
  261. template <field F1, field F2, char V>
  262. static inline bool apply(Masks const& masks)
  263. {
  264. typedef typename std::tuple_element<I, Masks>::type mask_type;
  265. mask_type const& mask = std::get<I>(masks);
  266. return interrupt_dispatch<mask_type, true>::template apply<F1, F2, V>(mask)
  267. && interrupt_dispatch_tuple<Masks, I+1>::template apply<F1, F2, V>(masks);
  268. }
  269. };
  270. template <typename Masks, int N>
  271. struct interrupt_dispatch_tuple<Masks, N, N>
  272. {
  273. template <field F1, field F2, char V>
  274. static inline bool apply(Masks const& )
  275. {
  276. return true;
  277. }
  278. };
  279. template <typename ...Masks>
  280. struct interrupt_dispatch<std::tuple<Masks...>, true>
  281. {
  282. typedef std::tuple<Masks...> mask_type;
  283. template <field F1, field F2, char V>
  284. static inline bool apply(mask_type const& mask)
  285. {
  286. return interrupt_dispatch_tuple<mask_type>::template apply<F1, F2, V>(mask);
  287. }
  288. };
  289. template <field F1, field F2, char V, bool InterruptEnabled, typename Mask>
  290. inline bool interrupt(Mask const& mask)
  291. {
  292. return interrupt_dispatch<Mask, InterruptEnabled>
  293. ::template apply<F1, F2, V>(mask);
  294. }
  295. // may_update()
  296. template <typename Mask>
  297. struct may_update_dispatch
  298. {
  299. template <field F1, field F2, char D, typename Matrix>
  300. static inline bool apply(Mask const& mask, Matrix const& matrix)
  301. {
  302. BOOST_STATIC_ASSERT('0' <= D && D <= '9');
  303. char const m = mask.template get<F1, F2>();
  304. if ( m == 'F' )
  305. {
  306. return true;
  307. }
  308. else if ( m == 'T' )
  309. {
  310. char const c = matrix.template get<F1, F2>();
  311. return c == 'F'; // if it's T or between 0 and 9, the result will be the same
  312. }
  313. else if ( m >= '0' && m <= '9' )
  314. {
  315. char const c = matrix.template get<F1, F2>();
  316. return D > c || c > '9';
  317. }
  318. return false;
  319. }
  320. };
  321. template <typename Masks, int I = 0, int N = std::tuple_size<Masks>::value>
  322. struct may_update_dispatch_tuple
  323. {
  324. template <field F1, field F2, char D, typename Matrix>
  325. static inline bool apply(Masks const& masks, Matrix const& matrix)
  326. {
  327. typedef typename std::tuple_element<I, Masks>::type mask_type;
  328. mask_type const& mask = std::get<I>(masks);
  329. return may_update_dispatch<mask_type>::template apply<F1, F2, D>(mask, matrix)
  330. || may_update_dispatch_tuple<Masks, I+1>::template apply<F1, F2, D>(masks, matrix);
  331. }
  332. };
  333. template <typename Masks, int N>
  334. struct may_update_dispatch_tuple<Masks, N, N>
  335. {
  336. template <field F1, field F2, char D, typename Matrix>
  337. static inline bool apply(Masks const& , Matrix const& )
  338. {
  339. return false;
  340. }
  341. };
  342. template <typename ...Masks>
  343. struct may_update_dispatch<std::tuple<Masks...>>
  344. {
  345. typedef std::tuple<Masks...> mask_type;
  346. template <field F1, field F2, char D, typename Matrix>
  347. static inline bool apply(mask_type const& mask, Matrix const& matrix)
  348. {
  349. return may_update_dispatch_tuple<mask_type>::template apply<F1, F2, D>(mask, matrix);
  350. }
  351. };
  352. template <field F1, field F2, char D, typename Mask, typename Matrix>
  353. inline bool may_update(Mask const& mask, Matrix const& matrix)
  354. {
  355. return may_update_dispatch<Mask>
  356. ::template apply<F1, F2, D>(mask, matrix);
  357. }
  358. // check_matrix()
  359. template <typename Mask>
  360. struct check_dispatch
  361. {
  362. template <typename Matrix>
  363. static inline bool apply(Mask const& mask, Matrix const& matrix)
  364. {
  365. return per_one<interior, interior>(mask, matrix)
  366. && per_one<interior, boundary>(mask, matrix)
  367. && per_one<interior, exterior>(mask, matrix)
  368. && per_one<boundary, interior>(mask, matrix)
  369. && per_one<boundary, boundary>(mask, matrix)
  370. && per_one<boundary, exterior>(mask, matrix)
  371. && per_one<exterior, interior>(mask, matrix)
  372. && per_one<exterior, boundary>(mask, matrix)
  373. && per_one<exterior, exterior>(mask, matrix);
  374. }
  375. template <field F1, field F2, typename Matrix>
  376. static inline bool per_one(Mask const& mask, Matrix const& matrix)
  377. {
  378. const char mask_el = mask.template get<F1, F2>();
  379. const char el = matrix.template get<F1, F2>();
  380. if ( mask_el == 'F' )
  381. {
  382. return el == 'F';
  383. }
  384. else if ( mask_el == 'T' )
  385. {
  386. return el == 'T' || ( el >= '0' && el <= '9' );
  387. }
  388. else if ( mask_el >= '0' && mask_el <= '9' )
  389. {
  390. return el == mask_el;
  391. }
  392. return true;
  393. }
  394. };
  395. template <typename Masks, int I = 0, int N = std::tuple_size<Masks>::value>
  396. struct check_dispatch_tuple
  397. {
  398. template <typename Matrix>
  399. static inline bool apply(Masks const& masks, Matrix const& matrix)
  400. {
  401. typedef typename std::tuple_element<I, Masks>::type mask_type;
  402. mask_type const& mask = std::get<I>(masks);
  403. return check_dispatch<mask_type>::apply(mask, matrix)
  404. || check_dispatch_tuple<Masks, I+1>::apply(masks, matrix);
  405. }
  406. };
  407. template <typename Masks, int N>
  408. struct check_dispatch_tuple<Masks, N, N>
  409. {
  410. template <typename Matrix>
  411. static inline bool apply(Masks const&, Matrix const&)
  412. {
  413. return false;
  414. }
  415. };
  416. template <typename ...Masks>
  417. struct check_dispatch<std::tuple<Masks...>>
  418. {
  419. typedef std::tuple<Masks...> mask_type;
  420. template <typename Matrix>
  421. static inline bool apply(mask_type const& mask, Matrix const& matrix)
  422. {
  423. return check_dispatch_tuple<mask_type>::apply(mask, matrix);
  424. }
  425. };
  426. template <typename Mask, typename Matrix>
  427. inline bool check_matrix(Mask const& mask, Matrix const& matrix)
  428. {
  429. return check_dispatch<Mask>::apply(mask, matrix);
  430. }
  431. // matrix_width
  432. template <typename MatrixOrMask>
  433. struct matrix_width
  434. {
  435. static const std::size_t value = MatrixOrMask::static_width;
  436. };
  437. template <typename Tuple,
  438. int I = 0,
  439. int N = std::tuple_size<Tuple>::value>
  440. struct matrix_width_tuple
  441. {
  442. static const std::size_t
  443. current = matrix_width<typename std::tuple_element<I, Tuple>::type>::value;
  444. static const std::size_t
  445. next = matrix_width_tuple<Tuple, I+1>::value;
  446. static const std::size_t
  447. value = current > next ? current : next;
  448. };
  449. template <typename Tuple, int N>
  450. struct matrix_width_tuple<Tuple, N, N>
  451. {
  452. static const std::size_t value = 0;
  453. };
  454. template <typename ...Masks>
  455. struct matrix_width<std::tuple<Masks...>>
  456. {
  457. static const std::size_t
  458. value = matrix_width_tuple<std::tuple<Masks...>>::value;
  459. };
  460. // mask_handler
  461. template <typename Mask, bool Interrupt>
  462. class mask_handler
  463. : private matrix_handler
  464. <
  465. relate::matrix<matrix_width<Mask>::value>
  466. >
  467. {
  468. typedef matrix_handler
  469. <
  470. relate::matrix<matrix_width<Mask>::value>
  471. > base_t;
  472. public:
  473. typedef bool result_type;
  474. bool interrupt;
  475. inline explicit mask_handler(Mask const& m)
  476. : interrupt(false)
  477. , m_mask(m)
  478. {}
  479. result_type result() const
  480. {
  481. return !interrupt
  482. && check_matrix(m_mask, base_t::matrix());
  483. }
  484. template <field F1, field F2, char D>
  485. inline bool may_update() const
  486. {
  487. return detail::relate::may_update<F1, F2, D>(m_mask, base_t::matrix());
  488. }
  489. template <field F1, field F2, char V>
  490. inline void update()
  491. {
  492. if (relate::interrupt<F1, F2, V, Interrupt>(m_mask))
  493. {
  494. interrupt = true;
  495. }
  496. else
  497. {
  498. base_t::template update<F1, F2, V>();
  499. }
  500. }
  501. template <field F1, field F2, char V>
  502. inline void set()
  503. {
  504. if (relate::interrupt<F1, F2, V, Interrupt>(m_mask))
  505. {
  506. interrupt = true;
  507. }
  508. else
  509. {
  510. base_t::template set<F1, F2, V>();
  511. }
  512. }
  513. template <field F1, field F2>
  514. inline char get() const
  515. {
  516. return base_t::template get<F1, F2>();
  517. }
  518. private:
  519. Mask const& m_mask;
  520. };
  521. // --------------- FALSE MASK ----------------
  522. struct false_mask {};
  523. // --------------- COMPILE-TIME MASK ----------------
  524. // static_check_characters
  525. template <typename Seq>
  526. struct static_check_characters {};
  527. template <char C, char ...Cs>
  528. struct static_check_characters<std::integer_sequence<char, C, Cs...>>
  529. : static_check_characters<std::integer_sequence<char, Cs...>>
  530. {
  531. typedef std::integer_sequence<char, C, Cs...> type;
  532. static const bool is_valid = (C >= '0' && C <= '9')
  533. || C == 'T' || C == 'F' || C == '*';
  534. BOOST_GEOMETRY_STATIC_ASSERT((is_valid),
  535. "Invalid static mask character",
  536. type);
  537. };
  538. template <char ...Cs>
  539. struct static_check_characters<std::integral_constant<char, Cs...>>
  540. {};
  541. // static_mask
  542. template <typename Seq, std::size_t Height, std::size_t Width = Height>
  543. struct static_mask
  544. {
  545. static const std::size_t static_width = Width;
  546. static const std::size_t static_height = Height;
  547. static const std::size_t static_size = Width * Height;
  548. BOOST_STATIC_ASSERT(
  549. std::size_t(util::sequence_size<Seq>::value) == static_size);
  550. template <detail::relate::field F1, detail::relate::field F2>
  551. struct static_get
  552. {
  553. BOOST_STATIC_ASSERT(std::size_t(F1) < static_height);
  554. BOOST_STATIC_ASSERT(std::size_t(F2) < static_width);
  555. static const char value
  556. = util::sequence_element<F1 * static_width + F2, Seq>::value;
  557. };
  558. private:
  559. // check static_mask characters
  560. enum { mask_check = sizeof(static_check_characters<Seq>) };
  561. };
  562. // static_should_handle_element
  563. template
  564. <
  565. typename StaticMask, field F1, field F2,
  566. bool IsSequence = util::is_sequence<StaticMask>::value
  567. >
  568. struct static_should_handle_element_dispatch
  569. {
  570. static const char mask_el = StaticMask::template static_get<F1, F2>::value;
  571. static const bool value = mask_el == 'F'
  572. || mask_el == 'T'
  573. || ( mask_el >= '0' && mask_el <= '9' );
  574. };
  575. template
  576. <
  577. typename Seq, field F1, field F2,
  578. std::size_t I = 0,
  579. std::size_t N = util::sequence_size<Seq>::value
  580. >
  581. struct static_should_handle_element_sequence
  582. {
  583. typedef typename util::sequence_element<I, Seq>::type StaticMask;
  584. static const bool value
  585. = static_should_handle_element_dispatch
  586. <
  587. StaticMask, F1, F2
  588. >::value
  589. || static_should_handle_element_sequence
  590. <
  591. Seq, F1, F2, I + 1
  592. >::value;
  593. };
  594. template <typename Seq, field F1, field F2, std::size_t N>
  595. struct static_should_handle_element_sequence<Seq, F1, F2, N, N>
  596. {
  597. static const bool value = false;
  598. };
  599. template <typename StaticMask, field F1, field F2>
  600. struct static_should_handle_element_dispatch<StaticMask, F1, F2, true>
  601. {
  602. static const bool value
  603. = static_should_handle_element_sequence
  604. <
  605. StaticMask, F1, F2
  606. >::value;
  607. };
  608. template <typename StaticMask, field F1, field F2>
  609. struct static_should_handle_element
  610. {
  611. static const bool value
  612. = static_should_handle_element_dispatch
  613. <
  614. StaticMask, F1, F2
  615. >::value;
  616. };
  617. // static_interrupt
  618. template
  619. <
  620. typename StaticMask, char V, field F1, field F2,
  621. bool InterruptEnabled,
  622. bool IsSequence = util::is_sequence<StaticMask>::value
  623. >
  624. struct static_interrupt_dispatch
  625. {
  626. static const bool value = false;
  627. };
  628. template <typename StaticMask, char V, field F1, field F2, bool IsSequence>
  629. struct static_interrupt_dispatch<StaticMask, V, F1, F2, true, IsSequence>
  630. {
  631. static const char mask_el = StaticMask::template static_get<F1, F2>::value;
  632. static const bool value
  633. = ( V >= '0' && V <= '9' ) ?
  634. ( mask_el == 'F' || ( mask_el < V && mask_el >= '0' && mask_el <= '9' ) ) :
  635. ( ( V == 'T' ) ? mask_el == 'F' : false );
  636. };
  637. template
  638. <
  639. typename Seq, char V, field F1, field F2,
  640. std::size_t I = 0,
  641. std::size_t N = util::sequence_size<Seq>::value
  642. >
  643. struct static_interrupt_sequence
  644. {
  645. typedef typename util::sequence_element<I, Seq>::type StaticMask;
  646. static const bool value
  647. = static_interrupt_dispatch
  648. <
  649. StaticMask, V, F1, F2, true
  650. >::value
  651. && static_interrupt_sequence
  652. <
  653. Seq, V, F1, F2, I + 1
  654. >::value;
  655. };
  656. template <typename Seq, char V, field F1, field F2, std::size_t N>
  657. struct static_interrupt_sequence<Seq, V, F1, F2, N, N>
  658. {
  659. static const bool value = true;
  660. };
  661. template <typename StaticMask, char V, field F1, field F2>
  662. struct static_interrupt_dispatch<StaticMask, V, F1, F2, true, true>
  663. {
  664. static const bool value
  665. = static_interrupt_sequence
  666. <
  667. StaticMask, V, F1, F2
  668. >::value;
  669. };
  670. template <typename StaticMask, char V, field F1, field F2, bool EnableInterrupt>
  671. struct static_interrupt
  672. {
  673. static const bool value
  674. = static_interrupt_dispatch
  675. <
  676. StaticMask, V, F1, F2, EnableInterrupt
  677. >::value;
  678. };
  679. // static_may_update
  680. template
  681. <
  682. typename StaticMask, char D, field F1, field F2,
  683. bool IsSequence = util::is_sequence<StaticMask>::value
  684. >
  685. struct static_may_update_dispatch
  686. {
  687. static const char mask_el = StaticMask::template static_get<F1, F2>::value;
  688. static const int version
  689. = mask_el == 'F' ? 0
  690. : mask_el == 'T' ? 1
  691. : mask_el >= '0' && mask_el <= '9' ? 2
  692. : 3;
  693. // TODO: use std::enable_if_t instead of std::integral_constant
  694. template <typename Matrix>
  695. static inline bool apply(Matrix const& matrix)
  696. {
  697. return apply_dispatch(matrix, std::integral_constant<int, version>());
  698. }
  699. // mask_el == 'F'
  700. template <typename Matrix>
  701. static inline bool apply_dispatch(Matrix const& , std::integral_constant<int, 0>)
  702. {
  703. return true;
  704. }
  705. // mask_el == 'T'
  706. template <typename Matrix>
  707. static inline bool apply_dispatch(Matrix const& matrix, std::integral_constant<int, 1>)
  708. {
  709. char const c = matrix.template get<F1, F2>();
  710. return c == 'F'; // if it's T or between 0 and 9, the result will be the same
  711. }
  712. // mask_el >= '0' && mask_el <= '9'
  713. template <typename Matrix>
  714. static inline bool apply_dispatch(Matrix const& matrix, std::integral_constant<int, 2>)
  715. {
  716. char const c = matrix.template get<F1, F2>();
  717. return D > c || c > '9';
  718. }
  719. // else
  720. template <typename Matrix>
  721. static inline bool apply_dispatch(Matrix const&, std::integral_constant<int, 3>)
  722. {
  723. return false;
  724. }
  725. };
  726. template
  727. <
  728. typename Seq, char D, field F1, field F2,
  729. std::size_t I = 0,
  730. std::size_t N = util::sequence_size<Seq>::value
  731. >
  732. struct static_may_update_sequence
  733. {
  734. typedef typename util::sequence_element<I, Seq>::type StaticMask;
  735. template <typename Matrix>
  736. static inline bool apply(Matrix const& matrix)
  737. {
  738. return static_may_update_dispatch
  739. <
  740. StaticMask, D, F1, F2
  741. >::apply(matrix)
  742. || static_may_update_sequence
  743. <
  744. Seq, D, F1, F2, I + 1
  745. >::apply(matrix);
  746. }
  747. };
  748. template <typename Seq, char D, field F1, field F2, std::size_t N>
  749. struct static_may_update_sequence<Seq, D, F1, F2, N, N>
  750. {
  751. template <typename Matrix>
  752. static inline bool apply(Matrix const& /*matrix*/)
  753. {
  754. return false;
  755. }
  756. };
  757. template <typename StaticMask, char D, field F1, field F2>
  758. struct static_may_update_dispatch<StaticMask, D, F1, F2, true>
  759. {
  760. template <typename Matrix>
  761. static inline bool apply(Matrix const& matrix)
  762. {
  763. return static_may_update_sequence
  764. <
  765. StaticMask, D, F1, F2
  766. >::apply(matrix);
  767. }
  768. };
  769. template <typename StaticMask, char D, field F1, field F2>
  770. struct static_may_update
  771. {
  772. template <typename Matrix>
  773. static inline bool apply(Matrix const& matrix)
  774. {
  775. return static_may_update_dispatch
  776. <
  777. StaticMask, D, F1, F2
  778. >::apply(matrix);
  779. }
  780. };
  781. // static_check_matrix
  782. template
  783. <
  784. typename StaticMask,
  785. bool IsSequence = util::is_sequence<StaticMask>::value
  786. >
  787. struct static_check_dispatch
  788. {
  789. template <typename Matrix>
  790. static inline bool apply(Matrix const& matrix)
  791. {
  792. return per_one<interior, interior>::apply(matrix)
  793. && per_one<interior, boundary>::apply(matrix)
  794. && per_one<interior, exterior>::apply(matrix)
  795. && per_one<boundary, interior>::apply(matrix)
  796. && per_one<boundary, boundary>::apply(matrix)
  797. && per_one<boundary, exterior>::apply(matrix)
  798. && per_one<exterior, interior>::apply(matrix)
  799. && per_one<exterior, boundary>::apply(matrix)
  800. && per_one<exterior, exterior>::apply(matrix);
  801. }
  802. template <field F1, field F2>
  803. struct per_one
  804. {
  805. static const char mask_el = StaticMask::template static_get<F1, F2>::value;
  806. static const int version
  807. = mask_el == 'F' ? 0
  808. : mask_el == 'T' ? 1
  809. : mask_el >= '0' && mask_el <= '9' ? 2
  810. : 3;
  811. // TODO: use std::enable_if_t instead of std::integral_constant
  812. template <typename Matrix>
  813. static inline bool apply(Matrix const& matrix)
  814. {
  815. const char el = matrix.template get<F1, F2>();
  816. return apply_dispatch(el, std::integral_constant<int, version>());
  817. }
  818. // mask_el == 'F'
  819. static inline bool apply_dispatch(char el, std::integral_constant<int, 0>)
  820. {
  821. return el == 'F';
  822. }
  823. // mask_el == 'T'
  824. static inline bool apply_dispatch(char el, std::integral_constant<int, 1>)
  825. {
  826. return el == 'T' || ( el >= '0' && el <= '9' );
  827. }
  828. // mask_el >= '0' && mask_el <= '9'
  829. static inline bool apply_dispatch(char el, std::integral_constant<int, 2>)
  830. {
  831. return el == mask_el;
  832. }
  833. // else
  834. static inline bool apply_dispatch(char /*el*/, std::integral_constant<int, 3>)
  835. {
  836. return true;
  837. }
  838. };
  839. };
  840. template
  841. <
  842. typename Seq,
  843. std::size_t I = 0,
  844. std::size_t N = util::sequence_size<Seq>::value
  845. >
  846. struct static_check_sequence
  847. {
  848. typedef typename util::sequence_element<I, Seq>::type StaticMask;
  849. template <typename Matrix>
  850. static inline bool apply(Matrix const& matrix)
  851. {
  852. return static_check_dispatch
  853. <
  854. StaticMask
  855. >::apply(matrix)
  856. || static_check_sequence
  857. <
  858. Seq, I + 1
  859. >::apply(matrix);
  860. }
  861. };
  862. template <typename Seq, std::size_t N>
  863. struct static_check_sequence<Seq, N, N>
  864. {
  865. template <typename Matrix>
  866. static inline bool apply(Matrix const& /*matrix*/)
  867. {
  868. return false;
  869. }
  870. };
  871. template <typename StaticMask>
  872. struct static_check_dispatch<StaticMask, true>
  873. {
  874. template <typename Matrix>
  875. static inline bool apply(Matrix const& matrix)
  876. {
  877. return static_check_sequence
  878. <
  879. StaticMask
  880. >::apply(matrix);
  881. }
  882. };
  883. template <typename StaticMask>
  884. struct static_check_matrix
  885. {
  886. template <typename Matrix>
  887. static inline bool apply(Matrix const& matrix)
  888. {
  889. return static_check_dispatch
  890. <
  891. StaticMask
  892. >::apply(matrix);
  893. }
  894. };
  895. // static_mask_handler
  896. template <typename StaticMask, bool Interrupt>
  897. class static_mask_handler
  898. : private matrix_handler< matrix<3> >
  899. {
  900. typedef matrix_handler< relate::matrix<3> > base_type;
  901. public:
  902. typedef bool result_type;
  903. bool interrupt;
  904. inline static_mask_handler()
  905. : interrupt(false)
  906. {}
  907. inline explicit static_mask_handler(StaticMask const& /*dummy*/)
  908. : interrupt(false)
  909. {}
  910. result_type result() const
  911. {
  912. return (!Interrupt || !interrupt)
  913. && static_check_matrix<StaticMask>::apply(base_type::matrix());
  914. }
  915. template <field F1, field F2, char D>
  916. inline bool may_update() const
  917. {
  918. return static_may_update<StaticMask, D, F1, F2>::
  919. apply(base_type::matrix());
  920. }
  921. template <field F1, field F2, char V>
  922. inline void update()
  923. {
  924. static const bool interrupt_c = static_interrupt<StaticMask, V, F1, F2, Interrupt>::value;
  925. static const bool should_handle = static_should_handle_element<StaticMask, F1, F2>::value;
  926. static const int version = interrupt_c ? 0
  927. : should_handle ? 1
  928. : 2;
  929. update_dispatch<F1, F2, V>(integral_constant<int, version>());
  930. }
  931. template
  932. <
  933. field F1, field F2, char V,
  934. std::enable_if_t<static_interrupt<StaticMask, V, F1, F2, Interrupt>::value, int> = 0
  935. >
  936. inline void set()
  937. {
  938. interrupt = true;
  939. }
  940. template
  941. <
  942. field F1, field F2, char V,
  943. std::enable_if_t<! static_interrupt<StaticMask, V, F1, F2, Interrupt>::value, int> = 0
  944. >
  945. inline void set()
  946. {
  947. base_type::template set<F1, F2, V>();
  948. }
  949. template <field F1, field F2>
  950. inline char get() const
  951. {
  952. return base_type::template get<F1, F2>();
  953. }
  954. private:
  955. // Interrupt && interrupt
  956. template <field F1, field F2, char V>
  957. inline void update_dispatch(integral_constant<int, 0>)
  958. {
  959. interrupt = true;
  960. }
  961. // else should_handle
  962. template <field F1, field F2, char V>
  963. inline void update_dispatch(integral_constant<int, 1>)
  964. {
  965. base_type::template update<F1, F2, V>();
  966. }
  967. // else
  968. template <field F1, field F2, char V>
  969. inline void update_dispatch(integral_constant<int, 2>)
  970. {}
  971. };
  972. // --------------- UTIL FUNCTIONS ----------------
  973. // update
  974. template <field F1, field F2, char D, typename Result>
  975. inline void update(Result & res)
  976. {
  977. res.template update<F1, F2, D>();
  978. }
  979. template
  980. <
  981. field F1, field F2, char D, bool Transpose, typename Result,
  982. std::enable_if_t<! Transpose, int> = 0
  983. >
  984. inline void update(Result & res)
  985. {
  986. res.template update<F1, F2, D>();
  987. }
  988. template
  989. <
  990. field F1, field F2, char D, bool Transpose, typename Result,
  991. std::enable_if_t<Transpose, int> = 0
  992. >
  993. inline void update(Result & res)
  994. {
  995. res.template update<F2, F1, D>();
  996. }
  997. // may_update
  998. template <field F1, field F2, char D, typename Result>
  999. inline bool may_update(Result const& res)
  1000. {
  1001. return res.template may_update<F1, F2, D>();
  1002. }
  1003. template
  1004. <
  1005. field F1, field F2, char D, bool Transpose, typename Result,
  1006. std::enable_if_t<! Transpose, int> = 0
  1007. >
  1008. inline bool may_update(Result const& res)
  1009. {
  1010. return res.template may_update<F1, F2, D>();
  1011. }
  1012. template
  1013. <
  1014. field F1, field F2, char D, bool Transpose, typename Result,
  1015. std::enable_if_t<Transpose, int> = 0
  1016. >
  1017. inline bool may_update(Result const& res)
  1018. {
  1019. return res.template may_update<F2, F1, D>();
  1020. }
  1021. // result_dimension
  1022. template <typename Geometry>
  1023. struct result_dimension
  1024. {
  1025. static const std::size_t dim = geometry::dimension<Geometry>::value;
  1026. BOOST_STATIC_ASSERT(dim >= 0);
  1027. static const char value = (dim <= 9) ? ('0' + dim) : 'T';
  1028. };
  1029. }} // namespace detail::relate
  1030. #endif // DOXYGEN_NO_DETAIL
  1031. }} // namespace boost::geometry
  1032. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_RELATE_RESULT_HPP