1// -*- C++ -*- 2 3// Copyright (C) 2005 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 7// terms of the GNU General Public License as published by the 8// Free Software Foundation; either version 2, or (at your option) 9// any later version. 10 11// This library is distributed in the hope that it will be useful, 12// but WITHOUT ANY WARRANTY; without even the implied warranty of 13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14// GNU General Public License for more details. 15 16// You should have received a copy of the GNU General Public License along 17// with this library; see the file COPYING. If not, write to the Free 18// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 19// USA. 20 21// As a special exception, you may use this file as part of a free software 22// library without restriction. Specifically, if other files instantiate 23// templates or use macros or inline functions from this file, or you compile 24// this file and link it with other files to produce an executable, this 25// file does not by itself cause the resulting executable to be covered by 26// the GNU General Public License. This exception does not however 27// invalidate any other reasons why the executable file might be covered by 28// the GNU General Public License. 29 30// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL. 31 32// Permission to use, copy, modify, sell, and distribute this software 33// is hereby granted without fee, provided that the above copyright 34// notice appears in all copies, and that both that copyright notice and 35// this permission notice appear in supporting documentation. None of 36// the above authors, nor IBM Haifa Research Laboratories, make any 37// representation about the suitability of this software for any 38// purpose. It is provided "as is" without express or implied warranty. 39 40/* 41 * @file tree_order_statistics_example.cpp 42 * An example showing how to use functors for order-statistics 43 * in tree-based containers. 44 */ 45 46// For tree_assoc_cntnr 47#include <ext/pb_assoc/assoc_cntnr.hpp> 48// For order-statistics related policies 49#include <ext/pb_assoc/tree_policy.hpp> 50// For assert 51#include <cassert> 52 53/* 54 * A red-black tree table storing ints and their order statistics. 55 */ 56typedef 57pb_assoc::tree_assoc_cntnr< 58 pb_assoc::order_statistics_key<int>, 59 pb_assoc::null_data_type, 60 pb_assoc::order_statistics_key_cmp<std::less<int> >, 61 pb_assoc::rb_tree_ds_tag, 62 pb_assoc::order_statistics_node_updator<int> > 63set_t; 64 65int 66main() 67{ 68 set_t s; 69 70 typedef pb_assoc::order_statistics_key< int> int_order_statistics_key; 71 72 // Insert some entries into s. 73 74 s.insert(int_order_statistics_key(12)); 75 s.insert(int_order_statistics_key(505)); 76 s.insert(int_order_statistics_key(30)); 77 s.insert(int_order_statistics_key(1000)); 78 s.insert(int_order_statistics_key(10000)); 79 s.insert(int_order_statistics_key(100)); 80 81 // The order of the keys should be: 12, 30, 100, 505, 1000, 10000. 82 83 pb_assoc::find_by_order<set_t> by_order; 84 85 assert(*by_order(s, 0) == 12); 86 assert(*by_order(s, 1) == 30); 87 assert(*by_order(s, 2) == 100); 88 assert(*by_order(s, 3) == 505); 89 assert(*by_order(s, 4) == 1000); 90 assert(*by_order(s, 5) == 10000); 91 assert(by_order(s, 6) == s.end()); 92 93 // The order of the keys should be: 12, 30, 100, 505, 1000, 10000. 94 95 pb_assoc::order_by_key<set_t> by_key; 96 97 assert(by_key(s, 10) == 0); 98 assert(by_key(s, 12) == 0); 99 assert(by_key(s, 15) == 1); 100 assert(by_key(s, 30) == 1); 101 assert(by_key(s, 99) == 2); 102 assert(by_key(s, 100) == 2); 103 assert(by_key(s, 505) == 3); 104 assert(by_key(s, 1000) == 4); 105 assert(by_key(s, 10000) == 5); 106 assert(by_key(s, 9999999) == 6); 107 108 // Erase an entry. 109 s.erase(int_order_statistics_key(30)); 110 111 // The order of the keys should be: 12, 100, 505, 1000, 10000. 112 113 assert(*by_order(s, 0) == 12); 114 assert(*by_order(s, 1) == 100); 115 assert(*by_order(s, 2) == 505); 116 assert(*by_order(s, 3) == 1000); 117 assert(*by_order(s, 4) == 10000); 118 assert(by_order(s, 5) == s.end()); 119 120 // The order of the keys should be: 12, 100, 505, 1000, 10000. 121 122 assert(by_key(s, 10) == 0); 123 assert(by_key(s, 12) == 0); 124 assert(by_key(s, 100) == 1); 125 assert(by_key(s, 505) == 2); 126 assert(by_key(s, 707) == 3); 127 assert(by_key(s, 1000) == 3); 128 assert(by_key(s, 1001) == 4); 129 assert(by_key(s, 10000) == 4); 130 assert(by_key(s, 100000) == 5); 131 assert(by_key(s, 9999999) == 5); 132} 133 134