property_map.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. // (C) Copyright Jeremy Siek 1999-2001.
  2. // Copyright (C) 2006 Trustees of Indiana University
  3. // Authors: Douglas Gregor and Jeremy Siek
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. // See http://www.boost.org/libs/property_map for documentation.
  8. #ifndef BOOST_PROPERTY_MAP_HPP
  9. #define BOOST_PROPERTY_MAP_HPP
  10. #include <boost/assert.hpp>
  11. #include <boost/config.hpp>
  12. #include <boost/static_assert.hpp>
  13. #include <cstddef>
  14. #include <boost/detail/iterator.hpp>
  15. #include <boost/concept_check.hpp>
  16. #include <boost/concept_archetype.hpp>
  17. #include <boost/mpl/assert.hpp>
  18. #include <boost/mpl/or.hpp>
  19. #include <boost/mpl/and.hpp>
  20. #include <boost/mpl/has_xxx.hpp>
  21. #include <boost/type_traits/is_same.hpp>
  22. namespace boost {
  23. //=========================================================================
  24. // property_traits class
  25. BOOST_MPL_HAS_XXX_TRAIT_DEF(key_type)
  26. BOOST_MPL_HAS_XXX_TRAIT_DEF(value_type)
  27. BOOST_MPL_HAS_XXX_TRAIT_DEF(reference)
  28. BOOST_MPL_HAS_XXX_TRAIT_DEF(category)
  29. template<class PA>
  30. struct is_property_map :
  31. boost::mpl::and_<
  32. has_key_type<PA>,
  33. has_value_type<PA>,
  34. has_reference<PA>,
  35. has_category<PA>
  36. >
  37. {};
  38. template <typename PA>
  39. struct default_property_traits {
  40. typedef typename PA::key_type key_type;
  41. typedef typename PA::value_type value_type;
  42. typedef typename PA::reference reference;
  43. typedef typename PA::category category;
  44. };
  45. struct null_property_traits {};
  46. template <typename PA>
  47. struct property_traits :
  48. boost::mpl::if_<is_property_map<PA>,
  49. default_property_traits<PA>,
  50. null_property_traits>::type
  51. {};
  52. #if 0
  53. template <typename PA>
  54. struct property_traits {
  55. typedef typename PA::key_type key_type;
  56. typedef typename PA::value_type value_type;
  57. typedef typename PA::reference reference;
  58. typedef typename PA::category category;
  59. };
  60. #endif
  61. //=========================================================================
  62. // property_traits category tags
  63. namespace detail {
  64. enum ePropertyMapID { READABLE_PA, WRITABLE_PA,
  65. READ_WRITE_PA, LVALUE_PA, OP_BRACKET_PA,
  66. RAND_ACCESS_ITER_PA, LAST_PA };
  67. }
  68. struct readable_property_map_tag { enum { id = detail::READABLE_PA }; };
  69. struct writable_property_map_tag { enum { id = detail::WRITABLE_PA }; };
  70. struct read_write_property_map_tag :
  71. public readable_property_map_tag,
  72. public writable_property_map_tag
  73. { enum { id = detail::READ_WRITE_PA }; };
  74. struct lvalue_property_map_tag : public read_write_property_map_tag
  75. { enum { id = detail::LVALUE_PA }; };
  76. //=========================================================================
  77. // property_traits specialization for pointers
  78. template <class T>
  79. struct property_traits<T*> {
  80. // BOOST_STATIC_ASSERT(boost::is_same<T, T*>::value && !"Using pointers as property maps is deprecated");
  81. typedef T value_type;
  82. typedef value_type& reference;
  83. typedef std::ptrdiff_t key_type;
  84. typedef lvalue_property_map_tag category;
  85. };
  86. template <class T>
  87. struct property_traits<const T*> {
  88. // BOOST_STATIC_ASSERT(boost::is_same<T, T*>::value && !"Using pointers as property maps is deprecated");
  89. typedef T value_type;
  90. typedef const value_type& reference;
  91. typedef std::ptrdiff_t key_type;
  92. typedef lvalue_property_map_tag category;
  93. };
  94. #if !defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
  95. // MSVC doesn't have Koenig lookup, so the user has to
  96. // do boost::get() anyways, and the using clause
  97. // doesn't really work for MSVC.
  98. } // namespace boost
  99. #endif
  100. // These need to go in global namespace because Koenig
  101. // lookup does not apply to T*.
  102. // V must be convertible to T
  103. template <class T, class V>
  104. inline void put(T* pa, std::ptrdiff_t k, const V& val) { pa[k] = val; }
  105. template <class T>
  106. inline const T& get(const T* pa, std::ptrdiff_t k) { return pa[k]; }
  107. #if !defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
  108. namespace boost {
  109. using ::put;
  110. using ::get;
  111. #endif
  112. //=========================================================================
  113. // concept checks for property maps
  114. template <class PMap, class Key>
  115. struct ReadablePropertyMapConcept
  116. {
  117. typedef typename property_traits<PMap>::key_type key_type;
  118. typedef typename property_traits<PMap>::reference reference;
  119. typedef typename property_traits<PMap>::category Category;
  120. typedef boost::readable_property_map_tag ReadableTag;
  121. void constraints() {
  122. function_requires< ConvertibleConcept<Category, ReadableTag> >();
  123. val = get(pmap, k);
  124. }
  125. PMap pmap;
  126. Key k;
  127. typename property_traits<PMap>::value_type val;
  128. };
  129. template <typename KeyArchetype, typename ValueArchetype>
  130. struct readable_property_map_archetype {
  131. typedef KeyArchetype key_type;
  132. typedef ValueArchetype value_type;
  133. typedef convertible_to_archetype<ValueArchetype> reference;
  134. typedef readable_property_map_tag category;
  135. };
  136. template <typename K, typename V>
  137. const typename readable_property_map_archetype<K,V>::reference&
  138. get(const readable_property_map_archetype<K,V>&,
  139. const typename readable_property_map_archetype<K,V>::key_type&)
  140. {
  141. typedef typename readable_property_map_archetype<K,V>::reference R;
  142. return static_object<R>::get();
  143. }
  144. template <class PMap, class Key>
  145. struct WritablePropertyMapConcept
  146. {
  147. typedef typename property_traits<PMap>::key_type key_type;
  148. typedef typename property_traits<PMap>::category Category;
  149. typedef boost::writable_property_map_tag WritableTag;
  150. void constraints() {
  151. function_requires< ConvertibleConcept<Category, WritableTag> >();
  152. put(pmap, k, val);
  153. }
  154. PMap pmap;
  155. Key k;
  156. typename property_traits<PMap>::value_type val;
  157. };
  158. template <typename KeyArchetype, typename ValueArchetype>
  159. struct writable_property_map_archetype {
  160. typedef KeyArchetype key_type;
  161. typedef ValueArchetype value_type;
  162. typedef void reference;
  163. typedef writable_property_map_tag category;
  164. };
  165. template <typename K, typename V>
  166. void put(const writable_property_map_archetype<K,V>&,
  167. const typename writable_property_map_archetype<K,V>::key_type&,
  168. const typename writable_property_map_archetype<K,V>::value_type&) { }
  169. template <class PMap, class Key>
  170. struct ReadWritePropertyMapConcept
  171. {
  172. typedef typename property_traits<PMap>::category Category;
  173. typedef boost::read_write_property_map_tag ReadWriteTag;
  174. void constraints() {
  175. function_requires< ReadablePropertyMapConcept<PMap, Key> >();
  176. function_requires< WritablePropertyMapConcept<PMap, Key> >();
  177. function_requires< ConvertibleConcept<Category, ReadWriteTag> >();
  178. }
  179. };
  180. template <typename KeyArchetype, typename ValueArchetype>
  181. struct read_write_property_map_archetype
  182. : public readable_property_map_archetype<KeyArchetype, ValueArchetype>,
  183. public writable_property_map_archetype<KeyArchetype, ValueArchetype>
  184. {
  185. typedef KeyArchetype key_type;
  186. typedef ValueArchetype value_type;
  187. typedef convertible_to_archetype<ValueArchetype> reference;
  188. typedef read_write_property_map_tag category;
  189. };
  190. template <class PMap, class Key>
  191. struct LvaluePropertyMapConcept
  192. {
  193. typedef typename property_traits<PMap>::category Category;
  194. typedef boost::lvalue_property_map_tag LvalueTag;
  195. typedef typename property_traits<PMap>::reference reference;
  196. void constraints() {
  197. function_requires< ReadablePropertyMapConcept<PMap, Key> >();
  198. function_requires< ConvertibleConcept<Category, LvalueTag> >();
  199. typedef typename property_traits<PMap>::value_type value_type;
  200. BOOST_MPL_ASSERT((boost::mpl::or_<
  201. boost::is_same<const value_type&, reference>,
  202. boost::is_same<value_type&, reference> >));
  203. reference ref = pmap[k];
  204. ignore_unused_variable_warning(ref);
  205. }
  206. PMap pmap;
  207. Key k;
  208. };
  209. template <typename KeyArchetype, typename ValueArchetype>
  210. struct lvalue_property_map_archetype
  211. : public readable_property_map_archetype<KeyArchetype, ValueArchetype>
  212. {
  213. typedef KeyArchetype key_type;
  214. typedef ValueArchetype value_type;
  215. typedef const ValueArchetype& reference;
  216. typedef lvalue_property_map_tag category;
  217. const value_type& operator[](const key_type&) const {
  218. return static_object<value_type>::get();
  219. }
  220. };
  221. template <class PMap, class Key>
  222. struct Mutable_LvaluePropertyMapConcept
  223. {
  224. typedef typename property_traits<PMap>::category Category;
  225. typedef boost::lvalue_property_map_tag LvalueTag;
  226. typedef typename property_traits<PMap>::reference reference;
  227. void constraints() {
  228. boost::function_requires< ReadWritePropertyMapConcept<PMap, Key> >();
  229. boost::function_requires<ConvertibleConcept<Category, LvalueTag> >();
  230. typedef typename property_traits<PMap>::value_type value_type;
  231. BOOST_MPL_ASSERT((boost::is_same<value_type&, reference>));
  232. reference ref = pmap[k];
  233. ignore_unused_variable_warning(ref);
  234. }
  235. PMap pmap;
  236. Key k;
  237. };
  238. template <typename KeyArchetype, typename ValueArchetype>
  239. struct mutable_lvalue_property_map_archetype
  240. : public readable_property_map_archetype<KeyArchetype, ValueArchetype>,
  241. public writable_property_map_archetype<KeyArchetype, ValueArchetype>
  242. {
  243. typedef KeyArchetype key_type;
  244. typedef ValueArchetype value_type;
  245. typedef ValueArchetype& reference;
  246. typedef lvalue_property_map_tag category;
  247. value_type& operator[](const key_type&) const {
  248. return static_object<value_type>::get();
  249. }
  250. };
  251. template <typename T>
  252. struct typed_identity_property_map;
  253. // A helper class for constructing a property map
  254. // from a class that implements operator[]
  255. template <class Reference, class LvaluePropertyMap>
  256. struct put_get_helper { };
  257. template <class PropertyMap, class Reference, class K>
  258. inline Reference
  259. get(const put_get_helper<Reference, PropertyMap>& pa, const K& k)
  260. {
  261. Reference v = static_cast<const PropertyMap&>(pa)[k];
  262. return v;
  263. }
  264. template <class PropertyMap, class Reference, class K, class V>
  265. inline void
  266. put(const put_get_helper<Reference, PropertyMap>& pa, K k, const V& v)
  267. {
  268. static_cast<const PropertyMap&>(pa)[k] = v;
  269. }
  270. //=========================================================================
  271. // Adapter to turn a RandomAccessIterator into a property map
  272. template <class RandomAccessIterator,
  273. class IndexMap
  274. #ifdef BOOST_NO_STD_ITERATOR_TRAITS
  275. , class T, class R
  276. #else
  277. , class T = typename std::iterator_traits<RandomAccessIterator>::value_type
  278. , class R = typename std::iterator_traits<RandomAccessIterator>::reference
  279. #endif
  280. >
  281. class iterator_property_map
  282. : public boost::put_get_helper< R,
  283. iterator_property_map<RandomAccessIterator, IndexMap,
  284. T, R> >
  285. {
  286. public:
  287. typedef typename property_traits<IndexMap>::key_type key_type;
  288. typedef T value_type;
  289. typedef R reference;
  290. typedef boost::lvalue_property_map_tag category;
  291. inline iterator_property_map(
  292. RandomAccessIterator cc = RandomAccessIterator(),
  293. const IndexMap& _id = IndexMap() )
  294. : iter(cc), index(_id) { }
  295. inline R operator[](key_type v) const { return *(iter + get(index, v)) ; }
  296. protected:
  297. RandomAccessIterator iter;
  298. IndexMap index;
  299. };
  300. #if !defined BOOST_NO_STD_ITERATOR_TRAITS
  301. template <class RAIter, class ID>
  302. inline iterator_property_map<
  303. RAIter, ID,
  304. typename std::iterator_traits<RAIter>::value_type,
  305. typename std::iterator_traits<RAIter>::reference>
  306. make_iterator_property_map(RAIter iter, ID id) {
  307. function_requires< RandomAccessIteratorConcept<RAIter> >();
  308. typedef iterator_property_map<
  309. RAIter, ID,
  310. typename std::iterator_traits<RAIter>::value_type,
  311. typename std::iterator_traits<RAIter>::reference> PA;
  312. return PA(iter, id);
  313. }
  314. #endif
  315. template <class RAIter, class Value, class ID>
  316. inline iterator_property_map<RAIter, ID, Value, Value&>
  317. make_iterator_property_map(RAIter iter, ID id, Value) {
  318. function_requires< RandomAccessIteratorConcept<RAIter> >();
  319. typedef iterator_property_map<RAIter, ID, Value, Value&> PMap;
  320. return PMap(iter, id);
  321. }
  322. template <class RandomAccessIterator,
  323. class IndexMap
  324. #ifdef BOOST_NO_STD_ITERATOR_TRAITS
  325. , class T, class R
  326. #else
  327. , class T = typename std::iterator_traits<RandomAccessIterator>::value_type
  328. , class R = typename std::iterator_traits<RandomAccessIterator>::reference
  329. #endif
  330. >
  331. class safe_iterator_property_map
  332. : public boost::put_get_helper< R,
  333. safe_iterator_property_map<RandomAccessIterator, IndexMap,
  334. T, R> >
  335. {
  336. public:
  337. typedef typename property_traits<IndexMap>::key_type key_type;
  338. typedef T value_type;
  339. typedef R reference;
  340. typedef boost::lvalue_property_map_tag category;
  341. inline safe_iterator_property_map(
  342. RandomAccessIterator first,
  343. std::size_t n_ = 0,
  344. const IndexMap& _id = IndexMap() )
  345. : iter(first), n(n_), index(_id) { }
  346. inline safe_iterator_property_map() { }
  347. inline R operator[](key_type v) const {
  348. BOOST_ASSERT(get(index, v) < n);
  349. return *(iter + get(index, v)) ;
  350. }
  351. typename property_traits<IndexMap>::value_type size() const { return n; }
  352. protected:
  353. RandomAccessIterator iter;
  354. typename property_traits<IndexMap>::value_type n;
  355. IndexMap index;
  356. };
  357. template <class RAIter, class ID>
  358. inline safe_iterator_property_map<
  359. RAIter, ID,
  360. typename boost::detail::iterator_traits<RAIter>::value_type,
  361. typename boost::detail::iterator_traits<RAIter>::reference>
  362. make_safe_iterator_property_map(RAIter iter, std::size_t n, ID id) {
  363. function_requires< RandomAccessIteratorConcept<RAIter> >();
  364. typedef safe_iterator_property_map<
  365. RAIter, ID,
  366. typename boost::detail::iterator_traits<RAIter>::value_type,
  367. typename boost::detail::iterator_traits<RAIter>::reference> PA;
  368. return PA(iter, n, id);
  369. }
  370. template <class RAIter, class Value, class ID>
  371. inline safe_iterator_property_map<RAIter, ID, Value, Value&>
  372. make_safe_iterator_property_map(RAIter iter, std::size_t n, ID id, Value) {
  373. function_requires< RandomAccessIteratorConcept<RAIter> >();
  374. typedef safe_iterator_property_map<RAIter, ID, Value, Value&> PMap;
  375. return PMap(iter, n, id);
  376. }
  377. //=========================================================================
  378. // An adaptor to turn a Unique Pair Associative Container like std::map or
  379. // std::hash_map into an Lvalue Property Map.
  380. template <typename UniquePairAssociativeContainer>
  381. class associative_property_map
  382. : public boost::put_get_helper<
  383. typename UniquePairAssociativeContainer::value_type::second_type&,
  384. associative_property_map<UniquePairAssociativeContainer> >
  385. {
  386. typedef UniquePairAssociativeContainer C;
  387. public:
  388. typedef typename C::key_type key_type;
  389. typedef typename C::value_type::second_type value_type;
  390. typedef value_type& reference;
  391. typedef lvalue_property_map_tag category;
  392. associative_property_map() : m_c(0) { }
  393. associative_property_map(C& c) : m_c(&c) { }
  394. reference operator[](const key_type& k) const {
  395. return (*m_c)[k];
  396. }
  397. private:
  398. C* m_c;
  399. };
  400. template <class UniquePairAssociativeContainer>
  401. associative_property_map<UniquePairAssociativeContainer>
  402. make_assoc_property_map(UniquePairAssociativeContainer& c)
  403. {
  404. return associative_property_map<UniquePairAssociativeContainer>(c);
  405. }
  406. template <typename UniquePairAssociativeContainer>
  407. class const_associative_property_map
  408. : public boost::put_get_helper<
  409. const typename UniquePairAssociativeContainer::value_type::second_type&,
  410. const_associative_property_map<UniquePairAssociativeContainer> >
  411. {
  412. typedef UniquePairAssociativeContainer C;
  413. public:
  414. typedef typename C::key_type key_type;
  415. typedef typename C::value_type::second_type value_type;
  416. typedef const value_type& reference;
  417. typedef lvalue_property_map_tag category;
  418. const_associative_property_map() : m_c(0) { }
  419. const_associative_property_map(const C& c) : m_c(&c) { }
  420. reference operator[](const key_type& k) const {
  421. return m_c->find(k)->second;
  422. }
  423. private:
  424. C const* m_c;
  425. };
  426. template <class UniquePairAssociativeContainer>
  427. const_associative_property_map<UniquePairAssociativeContainer>
  428. make_assoc_property_map(const UniquePairAssociativeContainer& c)
  429. {
  430. return const_associative_property_map<UniquePairAssociativeContainer>(c);
  431. }
  432. //=========================================================================
  433. // A property map that always returns the same object by value.
  434. //
  435. template <typename ValueType, typename KeyType = void>
  436. class static_property_map :
  437. public
  438. boost::put_get_helper<ValueType,static_property_map<ValueType> >
  439. {
  440. ValueType value;
  441. public:
  442. typedef KeyType key_type;
  443. typedef ValueType value_type;
  444. typedef ValueType reference;
  445. typedef readable_property_map_tag category;
  446. static_property_map(ValueType v) : value(v) {}
  447. template<typename T>
  448. inline reference operator[](T) const { return value; }
  449. };
  450. template <typename KeyType, typename ValueType>
  451. static_property_map<ValueType, KeyType>
  452. make_static_property_map(const ValueType& v) {
  453. return static_property_map<ValueType, KeyType>(v);
  454. }
  455. //=========================================================================
  456. // A property map that always returns a reference to the same object.
  457. //
  458. template <typename KeyType, typename ValueType>
  459. class ref_property_map :
  460. public
  461. boost::put_get_helper<ValueType&,ref_property_map<KeyType,ValueType> >
  462. {
  463. ValueType* value;
  464. public:
  465. typedef KeyType key_type;
  466. typedef ValueType value_type;
  467. typedef ValueType& reference;
  468. typedef lvalue_property_map_tag category;
  469. ref_property_map(ValueType& v) : value(&v) {}
  470. ValueType& operator[](key_type const&) const { return *value; }
  471. };
  472. //=========================================================================
  473. // A generalized identity property map
  474. template <typename T>
  475. struct typed_identity_property_map
  476. : public boost::put_get_helper<T, typed_identity_property_map<T> >
  477. {
  478. typedef T key_type;
  479. typedef T value_type;
  480. typedef T reference;
  481. typedef boost::readable_property_map_tag category;
  482. inline value_type operator[](const key_type& v) const { return v; }
  483. };
  484. //=========================================================================
  485. // A property map that applies the identity function to integers
  486. typedef typed_identity_property_map<std::size_t> identity_property_map;
  487. //=========================================================================
  488. // A property map that does not do anything, for
  489. // when you have to supply a property map, but don't need it.
  490. namespace detail {
  491. struct dummy_pmap_reference {
  492. template <class T>
  493. dummy_pmap_reference& operator=(const T&) { return *this; }
  494. operator int() { return 0; }
  495. };
  496. }
  497. class dummy_property_map
  498. : public boost::put_get_helper<detail::dummy_pmap_reference,
  499. dummy_property_map >
  500. {
  501. public:
  502. typedef void key_type;
  503. typedef int value_type;
  504. typedef detail::dummy_pmap_reference reference;
  505. typedef boost::read_write_property_map_tag category;
  506. inline dummy_property_map() : c(0) { }
  507. inline dummy_property_map(value_type cc) : c(cc) { }
  508. inline dummy_property_map(const dummy_property_map& x)
  509. : c(x.c) { }
  510. template <class Vertex>
  511. inline reference operator[](Vertex) const { return reference(); }
  512. protected:
  513. value_type c;
  514. };
  515. // Convert a Readable property map into a function object
  516. template <typename PropMap>
  517. class property_map_function {
  518. PropMap pm;
  519. typedef typename property_traits<PropMap>::key_type param_type;
  520. public:
  521. explicit property_map_function(const PropMap& pm): pm(pm) {}
  522. typedef typename property_traits<PropMap>::value_type result_type;
  523. result_type operator()(const param_type& k) const {return get(pm, k);}
  524. };
  525. template <typename PropMap>
  526. property_map_function<PropMap>
  527. make_property_map_function(const PropMap& pm) {
  528. return property_map_function<PropMap>(pm);
  529. }
  530. } // namespace boost
  531. #ifdef BOOST_GRAPH_USE_MPI
  532. #include <boost/property_map/parallel/parallel_property_maps.hpp>
  533. #endif
  534. #include <boost/property_map/vector_property_map.hpp>
  535. #endif /* BOOST_PROPERTY_MAP_HPP */