3 // Copyright (C) 2005-2013 Free Software Foundation, Inc.
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 3, or (at your option) any later
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.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
21 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
23 // Permission to use, copy, modify, sell, and distribute this software
24 // is hereby granted without fee, provided that the above copyright
25 // notice appears in all copies, and that both that copyright notice
26 // and this permission notice appear in supporting documentation. None
27 // of the above authors, nor IBM Haifa Research Laboratories, make any
28 // representation about the suitability of this software for any
29 // purpose. It is provided "as is" without express or implied
33 * @file trie_prefix_search_example.cpp
34 * An example showing how to use a trie for searching
35 * for words with a given prefix.
39 * This example shows how to use a PATRICIA trie for searching
40 * for words with a given prefix.
45 #include <ext/pb_ds/assoc_container.hpp>
46 #include <ext/pb_ds/trie_policy.hpp>
47 #include <ext/pb_ds/tag_and_trait.hpp>
50 using namespace __gnu_pbds
;
52 // A PATRICIA trie with a prefix-search node-updator type. Note that
53 // since the node updator is trie_prefix_search_node_update, then the
54 // container includes its method prefix_range.
55 typedef null_type mapped_type
;
56 typedef trie_string_access_traits
<> cmp_fn
;
57 typedef pat_trie_tag tag_type
;
59 typedef trie
<string
, mapped_type
, cmp_fn
, tag_type
,
60 trie_prefix_search_node_update
> trie_type
;
62 // The following helper function takes a trie object and r_key, a
63 // const reference to a string, and prints all entries whose key
64 // includes r_key as a prefix.
66 print_prefix_match(const trie_type
& t
, const string
& key
)
68 typedef trie_type::const_iterator const_iterator
;
69 typedef pair
<const_iterator
, const_iterator
> pair_type
;
71 cout
<< "All keys whose prefix matches \"" << key
<< "\":" << endl
;
73 const pair_type match_range
= t
.prefix_range(key
);
74 for (const_iterator it
= match_range
.first
; it
!= match_range
.second
; ++it
)
83 // Insert some entries.
84 assert(t
.insert("I").second
== true);
85 assert(t
.insert("wish").second
== true);
86 assert(t
.insert("that").second
== true);
87 assert(t
.insert("I").second
== false);
88 assert(t
.insert("could").second
== true);
89 assert(t
.insert("ever").second
== true);
90 assert(t
.insert("see").second
== true);
91 assert(t
.insert("a").second
== true);
92 assert(t
.insert("poem").second
== true);
93 assert(t
.insert("lovely").second
== true);
94 assert(t
.insert("as").second
== true);
95 assert(t
.insert("a").second
== false);
96 assert(t
.insert("trie").second
== true);
98 // Now search for prefixes.
99 print_prefix_match(t
, "");
100 print_prefix_match(t
, "a");
101 print_prefix_match(t
, "as");
102 print_prefix_match(t
, "ad");
103 print_prefix_match(t
, "t");
104 print_prefix_match(t
, "tr");
105 print_prefix_match(t
, "trie");
106 print_prefix_match(t
, "zzz");