1169691Skan// -*- C++ -*- 2169691Skan 3169691Skan// Copyright (C) 2005, 2006 Free Software Foundation, Inc. 4169691Skan// 5169691Skan// This file is part of the GNU ISO C++ Library. This library is free 6169691Skan// software; you can redistribute it and/or modify it under the terms 7169691Skan// of the GNU General Public License as published by the Free Software 8169691Skan// Foundation; either version 2, or (at your option) any later 9169691Skan// version. 10169691Skan 11169691Skan// This library is distributed in the hope that it will be useful, but 12169691Skan// WITHOUT ANY WARRANTY; without even the implied warranty of 13169691Skan// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14169691Skan// General Public License for more details. 15169691Skan 16169691Skan// You should have received a copy of the GNU General Public License 17169691Skan// along with this library; see the file COPYING. If not, write to 18169691Skan// the Free Software Foundation, 59 Temple Place - Suite 330, Boston, 19169691Skan// MA 02111-1307, USA. 20169691Skan 21169691Skan// As a special exception, you may use this file as part of a free 22169691Skan// software library without restriction. Specifically, if other files 23169691Skan// instantiate templates or use macros or inline functions from this 24169691Skan// file, or you compile this file and link it with other files to 25169691Skan// produce an executable, this file does not by itself cause the 26169691Skan// resulting executable to be covered by the GNU General Public 27169691Skan// License. This exception does not however invalidate any other 28169691Skan// reasons why the executable file might be covered by the GNU General 29169691Skan// Public License. 30169691Skan 31169691Skan// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL. 32169691Skan 33169691Skan// Permission to use, copy, modify, sell, and distribute this software 34169691Skan// is hereby granted without fee, provided that the above copyright 35169691Skan// notice appears in all copies, and that both that copyright notice 36169691Skan// and this permission notice appear in supporting documentation. None 37169691Skan// of the above authors, nor IBM Haifa Research Laboratories, make any 38169691Skan// representation about the suitability of this software for any 39169691Skan// purpose. It is provided "as is" without express or implied 40169691Skan// warranty. 41169691Skan 42169691Skan/** 43169691Skan * @file order_statistics_imp.hpp 44169691Skan * Contains forward declarations for order_statistics_key 45169691Skan */ 46169691Skan 47169691SkanPB_DS_CLASS_T_DEC 48169691Skaninline typename PB_DS_CLASS_C_DEC::iterator 49169691SkanPB_DS_CLASS_C_DEC:: 50169691Skanfind_by_order(size_type order) 51169691Skan{ 52169691Skan if (empty()) 53169691Skan return (end()); 54169691Skan 55169691Skan ++order; 56169691Skan 57169691Skan node_iterator nd_it = node_begin(); 58169691Skan 59169691Skan node_iterator end_nd_it = node_end(); 60169691Skan 61169691Skan while (true) 62169691Skan { 63169691Skan if (order > nd_it.get_metadata()) 64169691Skan return (++base_type::rightmost_it(nd_it)); 65169691Skan 66169691Skan const size_type num_children = nd_it.num_children(); 67169691Skan 68169691Skan if (num_children == 0) 69169691Skan return (*nd_it); 70169691Skan 71169691Skan for (size_type i = 0; i < num_children; ++i) 72169691Skan { 73169691Skan node_iterator child_nd_it = nd_it.get_child(i); 74169691Skan 75169691Skan if (order <= child_nd_it.get_metadata()) 76169691Skan { 77169691Skan i = num_children; 78169691Skan 79169691Skan nd_it = child_nd_it; 80169691Skan } 81169691Skan else 82169691Skan order -= child_nd_it.get_metadata(); 83169691Skan } 84169691Skan } 85169691Skan} 86169691Skan 87169691SkanPB_DS_CLASS_T_DEC 88169691Skaninline typename PB_DS_CLASS_C_DEC::const_iterator 89169691SkanPB_DS_CLASS_C_DEC:: 90169691Skanfind_by_order(size_type order) const 91169691Skan{ 92169691Skan return (const_cast<PB_DS_CLASS_C_DEC* >(this)->find_by_order(order)); 93169691Skan} 94169691Skan 95169691SkanPB_DS_CLASS_T_DEC 96169691Skaninline typename PB_DS_CLASS_C_DEC::size_type 97169691SkanPB_DS_CLASS_C_DEC:: 98169691Skanorder_of_key(const_key_reference r_key) const 99169691Skan{ 100169691Skan const E_Access_Traits& r_traits = 101169691Skan const_cast<PB_DS_CLASS_C_DEC* >(this)->get_e_access_traits(); 102169691Skan 103169691Skan return (order_of_prefix( 104169691Skan r_traits.begin(r_key), 105169691Skan r_traits.end(r_key))); 106169691Skan} 107169691Skan 108169691SkanPB_DS_CLASS_T_DEC 109169691Skaninline typename PB_DS_CLASS_C_DEC::size_type 110169691SkanPB_DS_CLASS_C_DEC:: 111169691Skanorder_of_prefix(typename e_access_traits::const_iterator b, typename e_access_traits::const_iterator e) const 112169691Skan{ 113169691Skan if (empty()) 114169691Skan return (0); 115169691Skan 116169691Skan const E_Access_Traits& r_traits = 117169691Skan const_cast<PB_DS_CLASS_C_DEC* >(this)->get_e_access_traits(); 118169691Skan 119169691Skan const_node_iterator nd_it = node_begin(); 120169691Skan 121169691Skan const_node_iterator end_nd_it = node_end(); 122169691Skan 123169691Skan size_type ord = 0; 124169691Skan 125169691Skan while (true) 126169691Skan { 127169691Skan const size_type num_children = nd_it.num_children(); 128169691Skan 129169691Skan if (num_children == 0) 130169691Skan { 131169691Skan const_key_reference r_key = 132169691Skan base_type::extract_key(*(*nd_it)); 133169691Skan 134169691Skan typename e_access_traits::const_iterator key_b = 135169691Skan r_traits.begin(r_key); 136169691Skan 137169691Skan typename e_access_traits::const_iterator key_e = 138169691Skan r_traits.end(r_key); 139169691Skan 140169691Skan return ((base_type::less( key_b, key_e, b, e, r_traits))? 141169691Skan ord + 1 : 142169691Skan ord); 143169691Skan } 144169691Skan 145169691Skan const_node_iterator next_nd_it = end_nd_it; 146169691Skan 147169691Skan size_type i = num_children - 1; 148169691Skan 149169691Skan do 150169691Skan { 151169691Skan const_node_iterator child_nd_it = nd_it.get_child(i); 152169691Skan 153169691Skan if (next_nd_it != end_nd_it) 154169691Skan ord += child_nd_it.get_metadata(); 155169691Skan else if (!base_type::less( 156169691Skan b, e, 157169691Skan child_nd_it.valid_prefix().first, 158169691Skan child_nd_it.valid_prefix().second, 159169691Skan r_traits)) 160169691Skan next_nd_it = child_nd_it; 161169691Skan } 162169691Skan while (i-- > 0); 163169691Skan 164169691Skan if (next_nd_it == end_nd_it) 165169691Skan return (ord); 166169691Skan 167169691Skan nd_it = next_nd_it; 168169691Skan } 169169691Skan} 170169691Skan 171169691SkanPB_DS_CLASS_T_DEC 172169691Skaninline void 173169691SkanPB_DS_CLASS_C_DEC:: 174169691Skanoperator()(node_iterator nd_it, const_node_iterator /*end_nd_it*/) const 175169691Skan{ 176169691Skan const size_type num_children = nd_it.num_children(); 177169691Skan 178169691Skan size_type children_rank = 0; 179169691Skan 180169691Skan for (size_type i = 0; i < num_children; ++i) 181169691Skan children_rank += nd_it.get_child(i).get_metadata(); 182169691Skan 183169691Skan const_cast<size_type& >(nd_it.get_metadata()) =(num_children == 0)? 1 : children_rank; 184169691Skan} 185169691Skan 186169691SkanPB_DS_CLASS_T_DEC 187169691SkanPB_DS_CLASS_C_DEC:: 188169691Skan~trie_order_statistics_node_update() 189169691Skan{ } 190