result.hpp 35 KB

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