Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/multi_index/example/bimap.cpp @ 25

Last change on this file since 25 was 12, checked in by landauf, 18 years ago

added boost

File size: 3.8 KB
Line 
1/* Boost.MultiIndex example of a bidirectional map.
2 *
3 * Copyright 2003-2005 Joaquín M López Muñoz.
4 * Distributed under the Boost Software License, Version 1.0.
5 * (See accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
7 *
8 * See http://www.boost.org/libs/multi_index for library home page.
9 */
10
11#if !defined(NDEBUG)
12#define BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING
13#define BOOST_MULTI_INDEX_ENABLE_SAFE_MODE
14#endif
15
16#include <boost/multi_index_container.hpp>
17#include <boost/multi_index/member.hpp>
18#include <boost/multi_index/ordered_index.hpp>
19#include <iostream>
20#include <string>
21#include <utility>
22
23using boost::multi_index_container;
24using namespace boost::multi_index;
25
26/* tags for accessing both sides of a bidirectional map */
27
28struct from{};
29struct to{};
30
31/* The class template bidirectional_map wraps the specification
32 * of a bidirectional map based on multi_index_container.
33 */
34
35template<typename FromType,typename ToType>
36struct bidirectional_map
37{
38  typedef std::pair<FromType,ToType> value_type;
39
40#if defined(BOOST_NO_POINTER_TO_MEMBER_TEMPLATE_PARAMETERS) ||\
41    defined(BOOST_MSVC)&&(BOOST_MSVC<1300) ||\
42    defined(BOOST_INTEL_CXX_VERSION)&&defined(_MSC_VER)&&\
43           (BOOST_INTEL_CXX_VERSION<=700)
44
45/* see Compiler specifics: Use of member_offset for info on member<> and
46 * member_offset<>
47 */
48
49  BOOST_STATIC_CONSTANT(unsigned,from_offset=offsetof(value_type,first));
50  BOOST_STATIC_CONSTANT(unsigned,to_offset  =offsetof(value_type,second));
51
52  typedef multi_index_container<
53    value_type,
54    indexed_by<
55      ordered_unique<
56        tag<from>,member_offset<value_type,FromType,from_offset> >,
57      ordered_unique<
58        tag<to>,  member_offset<value_type,ToType,to_offset> >
59    >
60  > type;
61
62#else
63
64  /* A bidirectional map can be simulated as a multi_index_container
65   * of pairs of (FromType,ToType) with two unique indices, one
66   * for each member of the pair.
67   */
68
69  typedef multi_index_container<
70    value_type,
71    indexed_by<
72      ordered_unique<
73        tag<from>,member<value_type,FromType,&value_type::first> >,
74      ordered_unique<
75        tag<to>,  member<value_type,ToType,&value_type::second> >
76    >
77  > type;
78
79#endif
80};
81
82/* a dictionary is a bidirectional map from strings to strings */
83
84typedef bidirectional_map<std::string,std::string>::type dictionary;
85
86int main()
87{
88  dictionary d;
89
90  /* Fill up our microdictionary. first members Spanish, second members
91   * English.
92   */
93
94  d.insert(dictionary::value_type("hola","hello"));
95  d.insert(dictionary::value_type("adios","goodbye"));
96  d.insert(dictionary::value_type("rosa","rose"));
97  d.insert(dictionary::value_type("mesa","table"));
98
99
100  std::cout<<"enter a word"<<std::endl;
101  std::string word;
102  std::getline(std::cin,word);
103
104#if defined(BOOST_NO_MEMBER_TEMPLATES) /* use global get<> and family instead */
105
106  dictionary::iterator it=get<from>(d).find(word);
107  if(it!=d.end()){
108    std::cout<<word<<" is said "<<it->second<<" in English"<<std::endl;
109  }
110  else{
111    nth_index_iterator<dictionary,1>::type it2=get<1>(d).find(word);
112    if(it2!=get<1>(d).end()){
113      std::cout<<word<<" is said "<<it2->first<<" in Spanish"<<std::endl;
114    }
115    else std::cout<<"No such word in the dictionary"<<std::endl;
116  }
117
118#else
119
120  /* search the queried word on the from index (Spanish) */
121
122  dictionary::iterator it=d.get<from>().find(word);
123  if(it!=d.end()){ /* found */
124
125    /* the second part of the element is the equivalent in English */
126
127    std::cout<<word<<" is said "<<it->second<<" in English"<<std::endl;
128  }
129  else{
130    /* word not found in Spanish, try our luck in English */
131
132    dictionary::index_iterator<to>::type it2=d.get<to>().find(word);
133    if(it2!=d.get<to>().end()){
134      std::cout<<word<<" is said "<<it2->first<<" in Spanish"<<std::endl;
135    }
136    else std::cout<<"No such word in the dictionary"<<std::endl;
137  }
138
139#endif
140
141  return 0;
142}
Note: See TracBrowser for help on using the repository browser.