]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/ext/pb_ds/example/trie_split.cc
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_split_example.cpp
34 * A basic example showing how to split trie-based container objects.
38 * This example shows how to split trie based containers, i.e., the opposite
39 * of a join operation.
44 #include <ext/pb_ds/assoc_container.hpp>
47 using namespace __gnu_pbds
;
51 // A PATRICIA trie table mapping strings to chars.
52 typedef trie
<string
, char> map_type
;
57 // Inserts some entries into r.
58 for (int i
= 0; i
< 100; ++ i
)
59 r
.insert(make_pair(string(i
, 'a'), 'b'));
61 // Now split r into a different map_type object.
63 // larger_r will hold the larger values following the split.
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
);
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(""));
75 // Now perform a split which actually changes the content of r.
77 // Split all elements with key larger than "aaa" into larger_r.
78 r
.split(string("aaa"), larger_r
);
80 assert(r
.size() == 4);
81 assert(larger_r
.begin()->first
== string("aaaa"));