libstdc++
tree_policy/order_statistics_imp.hpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2005, 2006, 2009 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the terms
00007 // of the GNU General Public License as published by the Free Software
00008 // Foundation; either version 3, or (at your option) any later
00009 // version.
00010 
00011 // This library is distributed in the hope that it will be useful, but
00012 // WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
00026 
00027 // Permission to use, copy, modify, sell, and distribute this software
00028 // is hereby granted without fee, provided that the above copyright
00029 // notice appears in all copies, and that both that copyright notice
00030 // and this permission notice appear in supporting documentation. None
00031 // of the above authors, nor IBM Haifa Research Laboratories, make any
00032 // representation about the suitability of this software for any
00033 // purpose. It is provided "as is" without express or implied
00034 // warranty.
00035 
00036 /**
00037  * @file tree_policy/order_statistics_imp.hpp
00038  * Contains forward declarations for order_statistics_key
00039  */
00040 
00041 PB_DS_CLASS_T_DEC
00042 inline typename PB_DS_CLASS_C_DEC::iterator
00043 PB_DS_CLASS_C_DEC::
00044 find_by_order(size_type order)
00045 {
00046   node_iterator it = node_begin();
00047   node_iterator end_it = node_end();
00048 
00049   while (it != end_it)
00050     {
00051       node_iterator l_it = it.get_l_child();
00052       const size_type o = (l_it == end_it)? 0 : l_it.get_metadata();
00053 
00054       if (order == o)
00055     return *it;
00056       else if (order < o)
00057     it = l_it;
00058       else
00059         {
00060       order -= o + 1;
00061       it = it.get_r_child();
00062         }
00063     }
00064 
00065   return base_type::end_iterator();
00066 }
00067 
00068 PB_DS_CLASS_T_DEC
00069 inline typename PB_DS_CLASS_C_DEC::const_iterator
00070 PB_DS_CLASS_C_DEC::
00071 find_by_order(size_type order) const
00072 { return const_cast<PB_DS_CLASS_C_DEC*>(this)->find_by_order(order); }
00073 
00074 PB_DS_CLASS_T_DEC
00075 inline typename PB_DS_CLASS_C_DEC::size_type
00076 PB_DS_CLASS_C_DEC::
00077 order_of_key(key_const_reference r_key) const
00078 {
00079   node_const_iterator it = node_begin();
00080   node_const_iterator end_it = node_end();
00081 
00082   const cmp_fn& r_cmp_fn = const_cast<PB_DS_CLASS_C_DEC*>(this)->get_cmp_fn();
00083   size_type ord = 0;
00084   while (it != end_it)
00085     {
00086       node_const_iterator l_it = it.get_l_child();
00087 
00088       if (r_cmp_fn(r_key, this->extract_key(*(*it))))
00089     it = l_it;
00090       else if (r_cmp_fn(this->extract_key(*(*it)), r_key))
00091         {
00092       ord += (l_it == end_it)? 1 : 1 + l_it.get_metadata();
00093       it = it.get_r_child();
00094         }
00095       else
00096         {
00097       ord += (l_it == end_it)? 0 : l_it.get_metadata();
00098       it = end_it;
00099         }
00100     }
00101   return ord;
00102 }
00103 
00104 PB_DS_CLASS_T_DEC
00105 inline void
00106 PB_DS_CLASS_C_DEC::
00107 operator()(node_iterator node_it, node_const_iterator end_nd_it) const
00108 {
00109   node_iterator l_it = node_it.get_l_child();
00110   const size_type l_rank = (l_it == end_nd_it) ? 0 : l_it.get_metadata();
00111 
00112   node_iterator r_it = node_it.get_r_child();
00113   const size_type r_rank = (r_it == end_nd_it) ? 0 : r_it.get_metadata();
00114 
00115   const_cast<metadata_reference>(node_it.get_metadata())= 1 + l_rank + r_rank;
00116 }
00117 
00118 PB_DS_CLASS_T_DEC
00119 PB_DS_CLASS_C_DEC::
00120 ~tree_order_statistics_node_update()
00121 { }