]>
Commit | Line | Data |
---|---|---|
4569a895 AT |
1 | // -*- C++ -*- |
2 | ||
3 | // Copyright (C) 2005, 2006 Free Software Foundation, Inc. | |
4 | // | |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the terms | |
7 | // of the GNU General Public License as published by the Free Software | |
8 | // Foundation; either version 2, or (at your option) any later | |
9 | // version. | |
10 | ||
11 | // This library is distributed in the hope that it will be useful, but | |
12 | // WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | // General Public License for more details. | |
15 | ||
16 | // You should have received a copy of the GNU General Public License | |
17 | // along with this library; see the file COPYING. If not, write to | |
18 | // the Free Software Foundation, 59 Temple Place - Suite 330, Boston, | |
19 | // MA 02111-1307, USA. | |
20 | ||
21 | // As a special exception, you may use this file as part of a free | |
22 | // software library without restriction. Specifically, if other files | |
23 | // instantiate templates or use macros or inline functions from this | |
24 | // file, or you compile this file and link it with other files to | |
25 | // produce an executable, this file does not by itself cause the | |
26 | // resulting executable to be covered by the GNU General Public | |
27 | // License. This exception does not however invalidate any other | |
28 | // reasons why the executable file might be covered by the GNU General | |
29 | // Public License. | |
30 | ||
31 | // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL. | |
32 | ||
33 | // Permission to use, copy, modify, sell, and distribute this software | |
34 | // is hereby granted without fee, provided that the above copyright | |
35 | // notice appears in all copies, and that both that copyright notice | |
36 | // and this permission notice appear in supporting documentation. None | |
37 | // of the above authors, nor IBM Haifa Research Laboratories, make any | |
38 | // representation about the suitability of this software for any | |
39 | // purpose. It is provided "as is" without express or implied | |
40 | // warranty. | |
41 | ||
42 | /** | |
43 | * @file trie_prefix_search_example.cpp | |
44 | * An example showing how to use a trie for searching | |
45 | * for words with a given prefix. | |
46 | */ | |
47 | ||
48 | /** | |
49 | * This example shows how to use a PATRICIA trie for searching | |
50 | * for words with a given prefix. | |
51 | */ | |
52 | ||
53 | #include <cassert> | |
54 | #include <iostream> | |
55 | #include <ext/pb_ds/assoc_container.hpp> | |
56 | #include <ext/pb_ds/trie_policy.hpp> | |
57 | #include <ext/pb_ds/tag_and_trait.hpp> | |
58 | ||
59 | using namespace std; | |
60 | using namespace pb_ds; | |
61 | using namespace pb_ds; | |
62 | ||
63 | // A PATRICIA trie with a prefix-search node-updator type. Note that | |
64 | // since the node updator is trie_prefix_search_node_update, then the | |
65 | // container includes its method prefix_range. | |
66 | typedef null_mapped_type mapped_type; | |
67 | typedef string_trie_e_access_traits<> cmp_fn; | |
68 | typedef pat_trie_tag tag_type; | |
69 | ||
70 | typedef trie<string, mapped_type, cmp_fn, tag_type, | |
71 | trie_prefix_search_node_update> trie_type; | |
72 | ||
73 | // The following helper function takes a trie object and r_key, a | |
74 | // const reference to a string, and prints all entries whose key | |
75 | // includes r_key as a prefix. | |
76 | void | |
77 | print_prefix_match(const trie_type& t, const string& key) | |
78 | { | |
79 | typedef trie_type::const_iterator const_iterator; | |
80 | typedef pair<const_iterator, const_iterator> pair_type; | |
81 | ||
82 | cout << "All keys whose prefix matches \"" << key << "\":" << endl; | |
83 | ||
84 | const pair_type match_range = t.prefix_range(key); | |
85 | for (const_iterator it = match_range.first; it != match_range.second; ++it) | |
86 | cout << *it << ' '; | |
87 | cout << endl; | |
88 | } | |
89 | ||
90 | int main() | |
91 | { | |
92 | trie_type t; | |
93 | ||
94 | // Insert some entries. | |
95 | assert(t.insert("I").second == true); | |
96 | assert(t.insert("wish").second == true); | |
97 | assert(t.insert("that").second == true); | |
98 | assert(t.insert("I").second == false); | |
99 | assert(t.insert("could").second == true); | |
100 | assert(t.insert("ever").second == true); | |
101 | assert(t.insert("see").second == true); | |
102 | assert(t.insert("a").second == true); | |
103 | assert(t.insert("poem").second == true); | |
104 | assert(t.insert("lovely").second == true); | |
105 | assert(t.insert("as").second == true); | |
106 | assert(t.insert("a").second == false); | |
107 | assert(t.insert("trie").second == true); | |
108 | ||
109 | // Now search for prefixes. | |
110 | print_prefix_match(t, ""); | |
111 | print_prefix_match(t, "a"); | |
112 | print_prefix_match(t, "as"); | |
113 | print_prefix_match(t, "ad"); | |
114 | print_prefix_match(t, "t"); | |
115 | print_prefix_match(t, "tr"); | |
116 | print_prefix_match(t, "trie"); | |
117 | print_prefix_match(t, "zzz"); | |
118 | ||
119 | return 0; | |
120 | } | |
121 |