• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Boost.Bimap
2 //
3 // Copyright (c) 2006-2007 Matias Capeletto
4 //
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 
9 //  VC++ 8.0 warns on usage of certain Standard Library and API functions that
10 //  can be cause buffer overruns or other possible security issues if misused.
11 //  See https://web.archive.org/web/20071014014301/http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx
12 //  But the wording of the warning is misleading and unsettling, there are no
13 //  portable alternative functions, and VC++ 8.0's own libraries use the
14 //  functions in question. So turn off the warnings.
15 #define _CRT_SECURE_NO_DEPRECATE
16 #define _SCL_SECURE_NO_DEPRECATE
17 
18 #include <boost/config.hpp>
19 
20 #include <boost/core/ignore_unused.hpp>
21 #include <boost/core/lightweight_test.hpp>
22 
23 #include <boost/config.hpp>
24 
25 #include <algorithm>
26 
27 #include <boost/range/functions.hpp>
28 #include <boost/range/const_iterator.hpp>
29 
30 #include <boost/bimap/bimap.hpp>
31 #include <boost/bimap/multiset_of.hpp>
32 #include <boost/bimap/support/lambda.hpp>
33 
34 
35 template< class ForwardReadableRange, class UnaryFunctor >
for_each(const ForwardReadableRange & r,UnaryFunctor func)36 UnaryFunctor for_each(const ForwardReadableRange & r, UnaryFunctor func)
37 {
38     typedef typename
39     boost::range_const_iterator<ForwardReadableRange>::type const_iterator;
40 
41     for(const_iterator i= boost::begin(r), iend= boost::end(r); i!=iend; ++i )
42     {
43         func(*i);
44     }
45 
46     return func;
47 }
48 
49 struct do_something_with_a_pair
50 {
51     template< class Pair >
operator ()do_something_with_a_pair52     void operator()(const Pair & p)
53     {
54         BOOST_TEST( p.first && p.second );
55     }
56 };
57 
test_bimap_range()58 int test_bimap_range()
59 {
60     using namespace boost::bimaps;
61 
62     typedef bimap< double, multiset_of<int> > bm_type;
63 
64 
65     bm_type bm;
66     bm.insert( bm_type::value_type(1.1 , 1) );
67     bm.insert( bm_type::value_type(2.2 , 2) );
68     bm.insert( bm_type::value_type(3.3 , 3) );
69     bm.insert( bm_type::value_type(4.4 , 4) );
70 
71 
72     for_each( bm.left.range( 1.0 < _key, _key < 5.0 ),
73               do_something_with_a_pair() );
74 
75     for_each( bm.right.range( unbounded, _key <= 2 ),
76               do_something_with_a_pair() );
77 
78 
79     // left range
80     {
81 
82     bm_type::left_range_type r = bm.left.range( 2.0 < _key, _key < 4.0 );
83     BOOST_TEST( ! boost::empty(r) );
84     BOOST_TEST( boost::begin(r) == bm.left.upper_bound(2.0) );
85     BOOST_TEST( boost::end(r)   == bm.left.lower_bound(4.0) );
86 
87     }
88 
89     // right range
90     {
91 
92     bm_type::right_range_type r = bm.right.range( 2 <= _key, _key <= 3 );
93     BOOST_TEST( ! boost::empty(r) );
94     BOOST_TEST( boost::begin(r) == bm.right.lower_bound(2) );
95     BOOST_TEST( boost::end(r)   == bm.right.upper_bound(3) );
96 
97     }
98 
99     // const range from range
100     {
101 
102     bm_type:: left_const_range_type lr = bm. left.range( unbounded, _key < 4.0 );
103     bm_type::right_const_range_type rr = bm.right.range( 2 < _key ,  unbounded );
104     boost::ignore_unused(lr);
105     boost::ignore_unused(rr);
106     }
107 
108     const bm_type & cbm = bm;
109 
110     // left const range
111     {
112     bm_type:: left_const_range_type r = cbm.left.range( unbounded, unbounded );
113     BOOST_TEST( ! boost::empty(r) );
114     BOOST_TEST( boost::begin(r) == cbm.left.begin() );
115 
116     }
117 
118     // right const range
119     {
120 
121     bm_type::right_const_range_type r = cbm.right.range( 1 < _key, _key < 1 );
122     BOOST_TEST( boost::empty(r) );
123 
124     }
125 
126     return 0;
127 }
128 //]
129 
main()130 int main()
131 {
132     test_bimap_range();
133     return boost::report_errors();
134 }
135 
136