]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/ext/pb_ds/example/tree_order_statistics.cc
re PR testsuite/39696 (gcc.dg/tree-ssa/ssa-ccp-25.c scan-tree-dump doesn't work on...
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / ext / pb_ds / example / tree_order_statistics.cc
CommitLineData
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 tree_order_statistics_example.cpp
44 * An example showing how to use functors for order-statistics
45 * in tree-based containers.
46 */
47
48/**
49 * In some cases tree structure can be used for various purposes asides
50 * from storing entries by key order.
51 * This example shows how a tree-based container can be used for
52 * order-statistics, i.e., for determining the order of each key
53 * in the (ordered) sequence of keys stored within the container object.
54 */
55
56#include <cassert>
57#include <ext/pb_ds/assoc_container.hpp>
58#include <ext/pb_ds/tree_policy.hpp>
59
60using namespace std;
5e11f978 61using namespace __gnu_pbds;
4569a895
AT
62
63// A red-black tree table storing ints and their order
64// statistics. Note that since the tree uses
65// tree_order_statistics_node_update as its update policy, then it
66// includes its methods by_order and order_of_key.
67typedef
68tree<
69 int,
70 null_mapped_type,
71 less<int>,
72 rb_tree_tag,
73 // This policy updates nodes' metadata for order statistics.
74 tree_order_statistics_node_update>
75set_t;
76
77int main()
78{
79 // Insert some entries into s.
80 set_t s;
81 s.insert(12);
82 s.insert(505);
83 s.insert(30);
84 s.insert(1000);
85 s.insert(10000);
86 s.insert(100);
87
88 // The order of the keys should be: 12, 30, 100, 505, 1000, 10000.
89 assert(*s.find_by_order(0) == 12);
90 assert(*s.find_by_order(1) == 30);
91 assert(*s.find_by_order(2) == 100);
92 assert(*s.find_by_order(3) == 505);
93 assert(*s.find_by_order(4) == 1000);
94 assert(*s.find_by_order(5) == 10000);
95 assert(s.find_by_order(6) == s.end());
96
97 // The order of the keys should be: 12, 30, 100, 505, 1000, 10000.
98 assert(s.order_of_key(10) == 0);
99 assert(s.order_of_key(12) == 0);
100 assert(s.order_of_key(15) == 1);
101 assert(s.order_of_key(30) == 1);
102 assert(s.order_of_key(99) == 2);
103 assert(s.order_of_key(100) == 2);
104 assert(s.order_of_key(505) == 3);
105 assert(s.order_of_key(1000) == 4);
106 assert(s.order_of_key(10000) == 5);
107 assert(s.order_of_key(9999999) == 6);
108
109 // Erase an entry.
110 s.erase(30);
111
112 // The order of the keys should be: 12, 100, 505, 1000, 10000.
113 assert(*s.find_by_order(0) == 12);
114 assert(*s.find_by_order(1) == 100);
115 assert(*s.find_by_order(2) == 505);
116 assert(*s.find_by_order(3) == 1000);
117 assert(*s.find_by_order(4) == 10000);
118 assert(s.find_by_order(5) == s.end());
119
120 // The order of the keys should be: 12, 100, 505, 1000, 10000.
121 assert(s.order_of_key(10) == 0);
122 assert(s.order_of_key(12) == 0);
123 assert(s.order_of_key(100) == 1);
124 assert(s.order_of_key(505) == 2);
125 assert(s.order_of_key(707) == 3);
126 assert(s.order_of_key(1000) == 3);
127 assert(s.order_of_key(1001) == 4);
128 assert(s.order_of_key(10000) == 4);
129 assert(s.order_of_key(100000) == 5);
130 assert(s.order_of_key(9999999) == 5);
131
132 return 0;
133}
134