property_map.hpp 20 KB

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