![]() |
Home | Libraries | People | FAQ | More |
| Name | Description | author | Purpose |
|---|---|---|---|
| Boost.Serialization | Serialization for persistence and marshalling | Robert Ramey | Serialization support for bimap containers and iterators |
| Boost.Assign | Filling containers with constant or generated data has never been easier | Thorsten Ottosen | Help to fill a bimap or views of it |
| Boost.Hash | A TR1 hash function object that can be extended to hash user defined types | Daniel James | Default hashing function |
| Boost.Lambda | Define small unnamed function objects at the actual call site, and more | from Jaakko Järvi, Gary Powell | Functors for modify, range, lower_bound and upper_bound |
| Boost.Range | A new infrastructure for generic algorithms that builds on top of the new iterator concepts | Thorsten Ottosen | Range based algorithms |
| Boost.PropertyMap | Concepts defining interfaces which map key objects to value objects | Jeremy Siek | Integration with BGL |
A bimap can be archived and retrieved by means of the Boost.Serialization Library. Both regular and XML archives are supported. The usage is straightforward and does not differ from that of any other serializable type. For instance:
#include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <fstream> ... void save(const bimap_type & b) { std::ofstream ofs("data"); boost::archive::text_oarchive oa(ofs); oa << b; } void load(bimap_type & b) { std::ifstream ifs("data"); boost::archive::text_iarchive ia(ifs); ia >> b; } ... bimap_type b; ... // fill it with data save(b); ... bimap_type restored_b; load(restored_b);
Serialization capabilities are automatically provided by just linking with the appropriate Boost.Serialization library module: it is not necessary to explicitly include any header from Boost.Serialization, apart from those declaring the type of archive used in the process. If not used, however, serialization support can be disabled by globally defining the macro BOOST_BIMAP_DISABLE_SERIALIZATION. Disabling serialization for Boost.MultiIndex can yield a small improvement in build times, and may be necessary in those defective compilers that fail to correctly process Boost.Serialization headers.
![]() |
Warning |
|---|---|
Boost.Bimap and Boost.MultiIndex share a lot of serialization code. The
macro |
Retrieving an archived bimap restores not only the elements, but also the order they were arranged in the views of the container. There is an exception to this rule, though: for unordered sets, no guarantee is made about the order in which elements will be iterated in the restored container; in general, it is unwise to rely on the ordering of elements of a hashed view, since it can change in arbitrary ways during insertion or rehashing --this is precisely the reason why hashed indices and TR1 unordered associative containers do not define an equality operator.
Iterators of a bimap can also be serialized. Serialization of an iterator must be done only after serializing its corresponding container.
The purpose of this library is to make it easy to fill containers with data by overloading operator,() and operator()(). These two operators make it possible to construct lists of values that are then copied into a container.
These lists are particularly useful in learning, testing, and prototyping
situations, but can also be handy otherwise. The library comes with predefined
operators for the containers of the standard library, but most functionality
will work with any standard compliant container. The library also makes
it possible to extend user defined types so for example a member function
can be called for a list of values instead of its normal arguments.
Boost.Assign can be used with bimap containers. For example, list_of can be used to initialize a bimap
in the following way:
typedef bimap<int,std::string> bm; bm b = list_of< bm::relation > (1,"one") (2,"two") (3,"three");
The views of a bimap are signature-compatible with their standard counterparts, so we can use other Boost.Assign utilities with them. For example:
bimap< multiset_of<int>, list_of<double> > b; // Since it is left_based the main view is a multiset, so we use insert insert( b ) (1,0.1) (2,0.2) (3,0.3); // The left map view is a multiset, again we use insert insert( b.left ) (4,0.4) (5,0.5) (6,0.6) (7,0.7); // The right map view is a list so we use push_back here // Note the order of the elements in the list! push_back( b.right ) (0.8,8) (0.9,9);
The hash function is the very core of the fast lookup capabilities of the unordered sets: a hasher is just a Unary Function returning an std::size_t value for any given key. In general, it is impossible that every key map to a different hash value, for the space of keys can be greater than the number of permissible hash codes: what makes for a good hasher is that the probability of a collision (two different keys with the same hash value) is as close to zero as possible.
This is a statistical property depending on the typical distribution of keys in a given application, so it is not feasible to have a general-purpose hash function with excellent results in every possible scenario; the default value for this parameter uses Boost.Hash, which often provides good enough results.
Boost.Hash can be extended for custom data types, enabling to use the default parameter of the unordered set types with any user types.
The Boost Lambda Library (BLL in the sequel) is a C++ template library, which implements form of lambda abstractions for C++. The term originates from functional programming and lambda calculus, where a lambda abstraction defines an unnamed function. Lambda expressions are very useful to construct the function objects required by some of the functions in a bimap view.
Boost.Bimap defines new placeholders to allow a sounder solution. For pairs,
two new placeholders are instantiated: _first
and _second, and for a
relation, two more complete the set: _left
and _right. A final placeholder
serves to refer to the keys uniformly in an associative container: _key. The following example shows how
to use them:
#include <boost/bimap/support/lambda.hpp> #include <boost/bimap/bimap.hpp> int main() { using namespace boost::bimap; typedef bimap<int,double> bm; bm b; b.insert( bm::relation(1,0.1) ); b.left .modify( b.left.begin() , _first = 2 ); b.left .modify( b.left.begin() , _second = 0.1 ); b.right.modify( b.right.begin(), ( _first = 0.1, _second = 1 ) ); BOOST_CHECK( b.left[1] == 0.1 ); BOOST_CHECK( b.size() == 1 ); b.modify( b.begin(), ( _left = 2, _right = 0.2 ) ); BOOST_CHECK( b.left.range( 1 <= _key , _key < 3 ).first == b.left.begin() ); }
Boost.Range is a collection of concepts and utilities that are particularly useful for specifying and implementing generic algorithms. Generic algorithms have so far been specified in terms of two or more iterators. Two iterators would together form a range of values that the algorithm could work on. This leads to a very general interface, but also to a somewhat clumsy use of the algorithms with redundant specification of container names. Therefore we would like to raise the abstraction level for algorithms so they specify their interface in terms of Ranges as much as possible.
As Boost.Bimap views are signature-compatible with their standard container
counterparts, they are compatible with the concept of a range. As an additional
feature, ordered bimap views offer a function named range
that allows a range of values to be obtained. Range-searching, i.e. the
look-up of all elements in a given interval, is a very frequently performed
operation for which it is possible to resort to standard lower_bound and upper_bound,
although this is cumbersome. Here is an example of what the code will like.
Boost.Lambda helps very much in the construction of the Lower and Upper
bound.
bimap<int,double> b; ... foo( b. left.range( 1 <= _key, _key < 3 ) ); foo( b.right.range( 0.3 <= _key, _key < 1.2 ) );
The Boost Property Map Library consists mainly of interface specifications in the form of concepts (similar to the iterator concepts in the STL). These interface specifications are intended for use by implementers of generic libraries in communicating requirements on template parameters to their users. In particular, the Boost Property Map concepts define a general purpose interface for mapping key objects to corresponding value objects, thereby hiding the details of how the mapping is implemented from algorithms.
The need for the property map interface came from the Boost Graph Library (BGL), which contains many examples of algorithms that use the property map concepts to specify their interface. For an example, note the ColorMap template parameter of the breadth_first_search. In addition, the BGL contains many examples of concrete types that implement the property map interface. The adjacency_list class implements property maps for accessing objects (properties) that are attached to vertices and edges of the graph.
The counterparts of two of the views of Boost.Bimap map, the set and unordered_set,
are read-write property maps. In order to use these, you need to include
one of the following headers:
#include <boost/bimap/property_map/set_support.hpp> #include <boost/bimap/property_map/unordered_set_support.hpp>
The following is adapted from the example in the Boost.PropertyMap documentation.
#include <iostream> #include <string> #include <boost/bimap/bimap.hpp> #include <boost/bimap/multiset_of.hpp> #include <boost/bimap/property_map/set_of.hpp> template <typename AddressMap> void foo(AddressMap & address) { typedef typename boost::property_traits<AddressMap>::value_type value_type; typedef typename boost::property_traits<AddressMap>::key_type key_type; value_type old_address, new_address; key_type fred = "Fred"; old_address = get(address, fred); new_address = "384 Fitzpatrick Street"; put(address, fred, new_address); } int main() { typedef boost::bimap::bimap<std::string, multiset_of<std::string> > Name2Address; typedef Name2Address::relation location; Name2Address name2address; name2address.insert(location("Fred", "710 West 13th Street")); name2address.insert(location( "Joe", "710 West 13th Street")); foo(name2address); return 0; }
| Copyright © 2006 Matias Capeletto |