Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:50:16

0001 // Copyright (C) Vladimir Prus 2003.
0002 // Distributed under the Boost Software License, Version 1.0. (See
0003 // accompanying file LICENSE_1_0.txt or copy at
0004 // http://www.boost.org/LICENSE_1_0.txt)
0005 //
0006 // See http://www.boost.org/libs/graph/vector_property_map.html for
0007 // documentation.
0008 //
0009 
0010 #ifndef BOOST_PROPERTY_MAP_VECTOR_PROPERTY_MAP_HPP
0011 #define BOOST_PROPERTY_MAP_VECTOR_PROPERTY_MAP_HPP
0012 
0013 #include <boost/property_map/property_map.hpp>
0014 #include <boost/smart_ptr/shared_ptr.hpp>
0015 #include <iterator>
0016 #include <vector>
0017 
0018 namespace boost {
0019     template<typename T, typename IndexMap = identity_property_map>
0020     class vector_property_map
0021         : public boost::put_get_helper< 
0022               typename std::iterator_traits< 
0023                   typename std::vector<T>::iterator >::reference,
0024               vector_property_map<T, IndexMap> >
0025     {
0026     public:
0027         typedef typename property_traits<IndexMap>::key_type  key_type;
0028         typedef T value_type;
0029         typedef typename std::iterator_traits< 
0030             typename std::vector<T>::iterator >::reference reference;
0031         typedef boost::lvalue_property_map_tag category;
0032 
0033         vector_property_map(const IndexMap& index = IndexMap())
0034         : store(new std::vector<T>()), index(index)
0035         {}
0036 
0037         vector_property_map(unsigned initial_size, 
0038                             const IndexMap& index = IndexMap())
0039         : store(new std::vector<T>(initial_size)), index(index)
0040         {}
0041 
0042         typename std::vector<T>::iterator storage_begin()
0043         {
0044             return store->begin();
0045         }
0046 
0047         typename std::vector<T>::iterator storage_end()
0048         {
0049             return store->end();
0050         }
0051 
0052         typename std::vector<T>::const_iterator storage_begin() const
0053         {
0054             return store->begin();
0055         }
0056 
0057         typename std::vector<T>::const_iterator storage_end() const
0058         {
0059             return store->end();
0060         }
0061 
0062         IndexMap&       get_index_map()       { return index; }
0063         const IndexMap& get_index_map() const { return index; }
0064 
0065     public:
0066         // Copy ctor absent, default semantics is OK.
0067         // Assignment operator absent, default semantics is OK.
0068         // CONSIDER: not sure that assignment to 'index' is correct.
0069 
0070         reference operator[](const key_type& v) const {
0071             typename property_traits<IndexMap>::value_type i = get(index, v);
0072             if (static_cast<unsigned>(i) >= store->size()) {
0073                 store->resize(i + 1, T());
0074             }
0075             return (*store)[i];
0076         }
0077     private:
0078         // Conceptually, we have a vector of infinite size. For practical 
0079         // purposes, we start with an empty vector and grow it as needed.
0080         // Note that we cannot store pointer to vector here -- we cannot
0081         // store pointer to data, because if copy of property map resizes
0082         // the vector, the pointer to data will be invalidated. 
0083         // I wonder if class 'pmap_ref' is simply needed.
0084         shared_ptr< std::vector<T> > store;
0085         IndexMap index;
0086     };
0087 
0088     template<typename T, typename IndexMap>
0089     vector_property_map<T, IndexMap>
0090     make_vector_property_map(IndexMap index)
0091     {
0092         return vector_property_map<T, IndexMap>(index);
0093     }
0094 }
0095 
0096 #endif