order_statistics_imp.hpp revision 169691
1218885Sdim// -*- C++ -*-
2218885Sdim
3218885Sdim// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
4218885Sdim//
5218885Sdim// This file is part of the GNU ISO C++ Library.  This library is free
6218885Sdim// software; you can redistribute it and/or modify it under the terms
7218885Sdim// of the GNU General Public License as published by the Free Software
8218885Sdim// Foundation; either version 2, or (at your option) any later
9218885Sdim// version.
10218885Sdim
11218885Sdim// This library is distributed in the hope that it will be useful, but
12218885Sdim// WITHOUT ANY WARRANTY; without even the implied warranty of
13218885Sdim// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14218885Sdim// General Public License for more details.
15218885Sdim
16218885Sdim// You should have received a copy of the GNU General Public License
17218885Sdim// along with this library; see the file COPYING.  If not, write to
18218885Sdim// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19218885Sdim// MA 02111-1307, USA.
20218885Sdim
21218885Sdim// As a special exception, you may use this file as part of a free
22218885Sdim// software library without restriction.  Specifically, if other files
23218885Sdim// instantiate templates or use macros or inline functions from this
24218885Sdim// file, or you compile this file and link it with other files to
25218885Sdim// produce an executable, this file does not by itself cause the
26218885Sdim// resulting executable to be covered by the GNU General Public
27218885Sdim// License.  This exception does not however invalidate any other
28218885Sdim// reasons why the executable file might be covered by the GNU General
29218885Sdim// Public License.
30218885Sdim
31218885Sdim// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32218885Sdim
33218885Sdim// Permission to use, copy, modify, sell, and distribute this software
34218885Sdim// is hereby granted without fee, provided that the above copyright
35218885Sdim// notice appears in all copies, and that both that copyright notice
36218885Sdim// and this permission notice appear in supporting documentation. None
37218885Sdim// of the above authors, nor IBM Haifa Research Laboratories, make any
38218885Sdim// representation about the suitability of this software for any
39218885Sdim// purpose. It is provided "as is" without express or implied
40218885Sdim// warranty.
41218885Sdim
42218885Sdim/**
43218885Sdim * @file order_statistics_imp.hpp
44218885Sdim * Contains forward declarations for order_statistics_key
45218885Sdim */
46218885Sdim
47218885SdimPB_DS_CLASS_T_DEC
48218885Sdiminline typename PB_DS_CLASS_C_DEC::iterator
49218885SdimPB_DS_CLASS_C_DEC::
50218885Sdimfind_by_order(size_type order)
51218885Sdim{
52218885Sdim  node_iterator it = node_begin();
53218885Sdim
54218885Sdim  node_iterator end_it = node_end();
55218885Sdim
56218885Sdim  while (it != end_it)
57218885Sdim    {
58218885Sdim      node_iterator l_it = it.get_l_child();
59218885Sdim
60218885Sdim      const size_type o = (l_it == end_it)?
61218885Sdim	0 :
62218885Sdim	l_it.get_metadata();
63218885Sdim
64218885Sdim      if (order == o)
65218885Sdim	return (*it);
66218885Sdim      else if (order < o)
67218885Sdim	it = l_it;
68218885Sdim      else
69218885Sdim        {
70218885Sdim	  order -= o + 1;
71218885Sdim
72218885Sdim	  it = it.get_r_child();
73218885Sdim        }
74218885Sdim    }
75218885Sdim
76218885Sdim  return (PB_DS_BASE_C_DEC::end_iterator());
77218885Sdim}
78218885Sdim
79218885SdimPB_DS_CLASS_T_DEC
80218885Sdiminline typename PB_DS_CLASS_C_DEC::const_iterator
81218885SdimPB_DS_CLASS_C_DEC::
82218885Sdimfind_by_order(size_type order) const
83218885Sdim{
84218885Sdim  return (const_cast<PB_DS_CLASS_C_DEC* >(this)->find_by_order(order));
85218885Sdim}
86218885Sdim
87218885SdimPB_DS_CLASS_T_DEC
88218885Sdiminline typename PB_DS_CLASS_C_DEC::size_type
89218885SdimPB_DS_CLASS_C_DEC::
90218885Sdimorder_of_key(const_key_reference r_key) const
91218885Sdim{
92218885Sdim  const_node_iterator it = node_begin();
93218885Sdim
94218885Sdim  const_node_iterator end_it = node_end();
95218885Sdim
96218885Sdim  const cmp_fn& r_cmp_fn =
97218885Sdim    const_cast<PB_DS_CLASS_C_DEC* >(this)->get_cmp_fn();
98218885Sdim
99218885Sdim  size_type ord = 0;
100218885Sdim
101218885Sdim  while (it != end_it)
102218885Sdim    {
103218885Sdim      const_node_iterator l_it = it.get_l_child();
104218885Sdim
105218885Sdim      if (r_cmp_fn(r_key, extract_key(*(*it))))
106218885Sdim	it = l_it;
107218885Sdim      else if (r_cmp_fn(extract_key(*(*it)), r_key))
108218885Sdim        {
109218885Sdim
110218885Sdim	  ord += (l_it == end_it)?
111218885Sdim	    1 :
112218885Sdim	    1 + l_it.get_metadata();
113218885Sdim
114218885Sdim	  it = it.get_r_child();
115218885Sdim        }
116218885Sdim      else
117        {
118	  ord += (l_it == end_it)?
119	    0 :
120	    l_it.get_metadata();
121
122	  it = end_it;
123        }
124    }
125
126  return (ord);
127}
128
129PB_DS_CLASS_T_DEC
130inline void
131PB_DS_CLASS_C_DEC::
132operator()(node_iterator node_it, const_node_iterator end_nd_it) const
133{
134  node_iterator l_child_it = node_it.get_l_child();
135  const size_type l_rank =(l_child_it == end_nd_it)? 0 : l_child_it.get_metadata();
136
137  node_iterator r_child_it = node_it.get_r_child();
138  const size_type r_rank =(r_child_it == end_nd_it)? 0 : r_child_it.get_metadata();
139
140  const_cast<metadata_reference>(node_it.get_metadata())=
141    1 + l_rank + r_rank;
142}
143
144PB_DS_CLASS_T_DEC
145PB_DS_CLASS_C_DEC::
146~tree_order_statistics_node_update()
147{ }
148