1 // -*- C++ -*-
2 
3 // Copyright (C) 2005-2019 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 3, 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 COPYING3.  If not see
18 // <http://www.gnu.org/licenses/>.
19 
20 
21 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
22 
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
30 // warranty.
31 
32 /**
33  * @file trie_split_example.cpp
34  * A basic example showing how to split trie-based container objects.
35  */
36 
37 /**
38  * This example shows how to split trie based containers, i.e., the opposite
39  * of a join operation.
40  */
41 
42 #include <string>
43 #include <cassert>
44 #include <ext/pb_ds/assoc_container.hpp>
45 
46 using namespace std;
47 using namespace __gnu_pbds;
48 
main()49 int main()
50 {
51   // A PATRICIA trie table mapping strings to chars.
52   typedef trie<string, char> map_type;
53 
54   // A map_type object.
55   map_type r;
56 
57   // Inserts some entries into r.
58   for (int i = 0; i < 100; ++ i)
59     r.insert(make_pair(string(i, 'a'), 'b'));
60 
61   // Now split r into a different map_type object.
62 
63   // larger_r will hold the larger values following the split.
64   map_type larger_r;
65 
66   // Split all elements with key larger than 'a'^1000 into larger_r.
67   // This is exception free.
68   r.split(string(1000, 'a'), larger_r);
69 
70   // Since there were no elements with key larger than 'a'^1000, r
71   // should be unchanged.
72   assert(r.size() == 100);
73   assert(r.begin()->first == string(""));
74 
75   // Now perform a split which actually changes the content of r.
76 
77   // Split all elements with key larger than "aaa" into larger_r.
78   r.split(string("aaa"), larger_r);
79 
80   assert(r.size() == 4);
81   assert(larger_r.begin()->first == string("aaaa"));
82 
83   return 0;
84 }
85 
86