Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/multi_index/example/hashed.cpp @ 29

Last change on this file since 29 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 4.0 KB
Line 
1/* Boost.MultiIndex example of use of hashed indices.
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/hashed_index.hpp>
18#include <boost/multi_index/member.hpp>
19#include <boost/multi_index/ordered_index.hpp>
20#include <boost/tokenizer.hpp>
21#include <iomanip>
22#include <iostream>
23#include <string>
24
25using boost::multi_index_container;
26using namespace boost::multi_index;
27
28/* word_counter keeps the ocurrences of words inserted. A hashed
29 * index allows for fast checking of preexisting entries.
30 */
31
32struct word_counter_entry
33{
34  std::string  word;
35  unsigned int occurrences;
36
37  word_counter_entry(std::string word_):word(word_),occurrences(0){}
38};
39
40/* see Compiler specifics: Use of member_offset for info on
41 * BOOST_MULTI_INDEX_MEMBER
42 */
43
44typedef multi_index_container<
45  word_counter_entry,
46  indexed_by<
47    ordered_non_unique<
48      BOOST_MULTI_INDEX_MEMBER(word_counter_entry,unsigned int,occurrences),
49      std::greater<unsigned int> /* sorted beginning with most frequent */
50    >,
51    hashed_unique<
52      BOOST_MULTI_INDEX_MEMBER(word_counter_entry,std::string,word)
53    >
54  >
55> word_counter;
56
57/* utilities */
58
59template<typename T>
60struct increment
61{
62  void operator()(T& x)const{++x;}
63};
64
65typedef boost::tokenizer<boost::char_separator<char> > text_tokenizer;
66
67int main()
68{
69  std::string text=
70    "En un lugar de la Mancha, de cuyo nombre no quiero acordarme, no ha "
71    "mucho tiempo que vivía un hidalgo de los de lanza en astillero, adarga "
72    "antigua, rocín flaco y galgo corredor. Una olla de algo más vaca que "
73    "carnero, salpicón las más noches, duelos y quebrantos los sábados, "
74    "lantejas los viernes, algún palomino de añadidura los domingos, "
75    "consumían las tres partes de su hacienda. El resto della concluían sayo "
76    "de velarte, calzas de velludo para las fiestas, con sus pantuflos de lo "
77    "mesmo, y los días de entresemana se honraba con su vellorí de lo más "
78    "fino. Tenía en su casa una ama que pasaba de los cuarenta, y una "
79    "sobrina que no llegaba a los veinte, y un mozo de campo y plaza, que "
80    "así ensillaba el rocín como tomaba la podadera. Frisaba la edad de "
81    "nuestro hidalgo con los cincuenta años; era de complexión recia, seco "
82    "de carnes, enjuto de rostro, gran madrugador y amigo de la caza. "
83    "Quieren decir que tenía el sobrenombre de Quijada, o Quesada, que en "
84    "esto hay alguna diferencia en los autores que deste caso escriben; "
85    "aunque, por conjeturas verosímiles, se deja entender que se llamaba "
86    "Quejana. Pero esto importa poco a nuestro cuento; basta que en la "
87    "narración dél no se salga un punto de la verdad.";
88
89  /* feed the text into the container */
90
91  word_counter   wc;
92  text_tokenizer tok(text,boost::char_separator<char>(" \t\n.,;:!?'\"-"));
93  unsigned int   total_occurrences=0;
94  for(text_tokenizer::iterator it=tok.begin(),it_end=tok.end();
95      it!=it_end;++it){
96    /* Insert the word into the container. If duplicate, wit will point to
97     * the preexistent entry.
98     */
99
100    ++total_occurrences;
101    word_counter::iterator wit=wc.insert(*it).first;
102
103    /* Increment occurrences.
104     * In a lambda-capable compiler, this can be written as:
105     *   wc.modify_key(wit,++_1);
106     */
107
108    wc.modify_key(wit,increment<unsigned int>());
109  }
110
111  /* list words by frequency of appearance */
112
113  std::cout<<std::fixed<<std::setprecision(2);
114  for(word_counter::iterator wit=wc.begin(),wit_end=wc.end();
115      wit!=wit_end;++wit){
116    std::cout<<std::setw(11)<<wit->word<<": "
117             <<std::setw(5) <<100.0*wit->occurrences/total_occurrences<<"%"
118             <<std::endl;
119  }
120
121  return 0;
122}
Note: See TracBrowser for help on using the repository browser.