C++ Boost

template<typename T, typename IndexMap = identity_property_map>
class vector_property_map;

This property map is used to efficiently store properties for a variable number of elements. It's somewhere between associative_property_map and iterator_property_map. The latter is very fast, but requires that the number of stored elements is known when creating property map. The former does not have this requirement, but is slower, and requires stored elements to be comparable.

The vector_property_map uses mapping from key to indices, and allows to add new elements. It accomplishes this by storing values in a vector, which is resized on demand.

Note that vector_property_map does not provide reference/pointer stability for stored values.

Example

example3.cpp:

#include <boost/property_map/vector_property_map.hpp>
#include <string>
#include <iostream>

int main()
{
    boost::vector_property_map<std::string> m;
    
    // Assign string to '4'. 
    m[4] = "e";
    std::cout << "'" << m[4] << "'\n";
    
    // Grab string from '10'. Since none is associated,
    // "" will be returned.
    std::cout << "'" << m[10] << "'\n";
}

Where Defined

boost/property_map/vector_property_map.hpp

Model Of

Lvalue Property Map

Template Parameters

ParameterDescriptionDefault
T The type of property value. Must be both Assignable and DefaultConstructible.  
IndexMap Must be a model of Readable Property Map and the value type must be convertible to std::vector<T>::size_type. identity_property_map

Members

In addition to the methods and functions required by Lvalue Property Map, this class has the following members.


vector_property_map(const IndexMap& index = IndexMap())
Constructor which takes an index map.
vector_property_map(unsigned initial_size, const IndexMap& index = IndexMap())
This constructor version allows to specify maximum index of element that will be stored. Correct number will improve performance, but semantic is always the same.
vector_property_map(const vector_property_map&)
Copy constructor. The copy will share the same data and changes made to it will affect original property map.
vector_property_map& operator=(const vector_property_map&)
Assignment operator. The semantic is the same as for copy constructor.
reference operator[](const key_type& v) const
The operator bracket for property access.
std::vector<T>::iterator storage_begin()
std::vector<T>::iterator storage_end()
std::vector<T>::const_iterator storage_begin()
std::vector<T>::const_iterator storage_end()

This group of methods gives access to begin and end iterators of the underlying vector.

Rationale: The methods are handy, for example, when it's needed to specify a single value for all elements in a freshly created property map. The methods are not called simply "begin" and "end" since conceptually, vector_property_map is unbounded map, and has no end iterator. The direct access to the underlying method is not provided, since it would decrease encapsulation and make future performance tuning dangerous.

Acknolegements: Matthias Troyer suggested adding those functions.


void reserve(unsigned size)
Reserve the space for storing elements with maximum index of 'size'. Unless element with greater index is accesses, all accesses will be take O(1) time.

Non-Member functions


template
vector_property_map
make_vector_property_map(IndexMap index)
{
  return vector_property_map(index);
} 
A function for conveniently creating a vector property map.

Copyright © 2002 Jeremy Siek, Indiana University (jsiek@osl.iu.edu)
Lie-Quan Lee, Indiana University (llee1@osl.iu.edu)
Andrew Lumsdaine, Indiana University (lums@osl.iu.edu)
Copyright © 2003Vladimir Prus