libstdc++
bin_search_tree_/debug_fn_imps.hpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2005, 2006, 2009, 2010, 2011 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 bin_search_tree_/debug_fn_imps.hpp
00038  * Contains an implementation class for bin_search_tree_.
00039  */
00040 
00041 #ifdef _GLIBCXX_DEBUG
00042 
00043 PB_DS_CLASS_T_DEC
00044 void
00045 PB_DS_CLASS_C_DEC::
00046 assert_valid(const char* __file, int __line) const
00047 {
00048   structure_only_assert_valid(__file, __line);
00049   assert_consistent_with_debug_base(__file, __line);
00050   assert_size(__file, __line);
00051   assert_iterators(__file, __line);
00052   if (m_p_head->m_p_parent == 0)
00053     {
00054       PB_DS_DEBUG_VERIFY(m_size == 0);
00055     }
00056   else
00057     {
00058       PB_DS_DEBUG_VERIFY(m_size > 0);
00059     }
00060 }
00061 
00062 PB_DS_CLASS_T_DEC
00063 void
00064 PB_DS_CLASS_C_DEC::
00065 structure_only_assert_valid(const char* __file, int __line) const
00066 {
00067   PB_DS_DEBUG_VERIFY(m_p_head != 0);
00068   if (m_p_head->m_p_parent == 0)
00069     {
00070       PB_DS_DEBUG_VERIFY(m_p_head->m_p_left == m_p_head);
00071       PB_DS_DEBUG_VERIFY(m_p_head->m_p_right == m_p_head);
00072     }
00073   else
00074     {
00075       PB_DS_DEBUG_VERIFY(m_p_head->m_p_parent->m_p_parent == m_p_head);
00076       PB_DS_DEBUG_VERIFY(m_p_head->m_p_left != m_p_head);
00077       PB_DS_DEBUG_VERIFY(m_p_head->m_p_right != m_p_head);
00078     }
00079 
00080   if (m_p_head->m_p_parent != 0)
00081     assert_node_consistent(m_p_head->m_p_parent, __file, __line);
00082   assert_min(__file, __line);
00083   assert_max(__file, __line);
00084 }
00085 
00086 PB_DS_CLASS_T_DEC
00087 void
00088 PB_DS_CLASS_C_DEC::
00089 assert_node_consistent(const node_pointer p_nd,
00090                const char* __file, int __line) const
00091 {
00092   assert_node_consistent_(p_nd, __file, __line);
00093 }
00094 
00095 PB_DS_CLASS_T_DEC
00096 typename PB_DS_CLASS_C_DEC::node_consistent_t
00097 PB_DS_CLASS_C_DEC::
00098 assert_node_consistent_(const node_pointer p_nd,
00099             const char* __file, int __line) const
00100 {
00101   if (p_nd == 0)
00102     return (std::make_pair((const_pointer)0,(const_pointer)0));
00103 
00104   assert_node_consistent_with_left(p_nd, __file, __line);
00105   assert_node_consistent_with_right(p_nd, __file, __line);
00106 
00107   const std::pair<const_pointer, const_pointer>
00108     l_range = assert_node_consistent_(p_nd->m_p_left, __file, __line);
00109 
00110   if (l_range.second != 0)
00111     PB_DS_DEBUG_VERIFY(Cmp_Fn::operator()(PB_DS_V2F(*l_range.second),
00112                       PB_DS_V2F(p_nd->m_value)));
00113 
00114   const std::pair<const_pointer, const_pointer>
00115     r_range = assert_node_consistent_(p_nd->m_p_right, __file, __line);
00116 
00117   if (r_range.first != 0)
00118     PB_DS_DEBUG_VERIFY(Cmp_Fn::operator()(PB_DS_V2F(p_nd->m_value),
00119                          PB_DS_V2F(*r_range.first)));
00120 
00121   return std::make_pair((l_range.first != 0) ? l_range.first : &p_nd->m_value,
00122             (r_range.second != 0)? r_range.second : &p_nd->m_value);
00123 }
00124 
00125 PB_DS_CLASS_T_DEC
00126 void
00127 PB_DS_CLASS_C_DEC::
00128 assert_node_consistent_with_left(const node_pointer p_nd,
00129                  const char* __file, int __line) const
00130 {
00131   if (p_nd->m_p_left == 0)
00132     return;
00133   PB_DS_DEBUG_VERIFY(p_nd->m_p_left->m_p_parent == p_nd);
00134   PB_DS_DEBUG_VERIFY(!Cmp_Fn::operator()(PB_DS_V2F(p_nd->m_value),
00135                      PB_DS_V2F(p_nd->m_p_left->m_value)));
00136 }
00137 
00138 PB_DS_CLASS_T_DEC
00139 void
00140 PB_DS_CLASS_C_DEC::
00141 assert_node_consistent_with_right(const node_pointer p_nd,
00142                   const char* __file, int __line) const
00143 {
00144   if (p_nd->m_p_right == 0)
00145     return;
00146   PB_DS_DEBUG_VERIFY(p_nd->m_p_right->m_p_parent == p_nd);
00147   PB_DS_DEBUG_VERIFY(!Cmp_Fn::operator()(PB_DS_V2F(p_nd->m_p_right->m_value),
00148                      PB_DS_V2F(p_nd->m_value)));
00149 }
00150 
00151 PB_DS_CLASS_T_DEC
00152 void
00153 PB_DS_CLASS_C_DEC::
00154 assert_min(const char* __file, int __line) const
00155 {
00156   assert_min_imp(m_p_head->m_p_parent, __file, __line);
00157 }
00158 
00159 PB_DS_CLASS_T_DEC
00160 void
00161 PB_DS_CLASS_C_DEC::
00162 assert_min_imp(const node_pointer p_nd, const char* __file, int __line) const
00163 {
00164   if (p_nd == 0)
00165     {
00166       PB_DS_DEBUG_VERIFY(m_p_head->m_p_left == m_p_head);
00167       return;
00168     }
00169 
00170   if (p_nd->m_p_left == 0)
00171     {
00172       PB_DS_DEBUG_VERIFY(p_nd == m_p_head->m_p_left);
00173       return;
00174     }
00175   assert_min_imp(p_nd->m_p_left, __file, __line);
00176 }
00177 
00178 PB_DS_CLASS_T_DEC
00179 void
00180 PB_DS_CLASS_C_DEC::
00181 assert_max(const char* __file, int __line) const
00182 {
00183   assert_max_imp(m_p_head->m_p_parent, __file, __line);
00184 }
00185 
00186 PB_DS_CLASS_T_DEC
00187 void
00188 PB_DS_CLASS_C_DEC::
00189 assert_max_imp(const node_pointer p_nd,
00190            const char* __file, int __line) const
00191 {
00192   if (p_nd == 0)
00193     {
00194       PB_DS_DEBUG_VERIFY(m_p_head->m_p_right == m_p_head);
00195       return;
00196     }
00197 
00198   if (p_nd->m_p_right == 0)
00199     {
00200       PB_DS_DEBUG_VERIFY(p_nd == m_p_head->m_p_right);
00201       return;
00202     }
00203 
00204   assert_max_imp(p_nd->m_p_right, __file, __line);
00205 }
00206 
00207 PB_DS_CLASS_T_DEC
00208 void
00209 PB_DS_CLASS_C_DEC::
00210 assert_iterators(const char* __file, int __line) const
00211 {
00212   size_type iterated_num = 0;
00213   const_iterator prev_it = end();
00214   for (const_iterator it = begin(); it != end(); ++it)
00215     {
00216       ++iterated_num;
00217       PB_DS_DEBUG_VERIFY(lower_bound(PB_DS_V2F(*it)).m_p_nd == it.m_p_nd);
00218       const_iterator upper_bound_it = upper_bound(PB_DS_V2F(*it));
00219       --upper_bound_it;
00220       PB_DS_DEBUG_VERIFY(upper_bound_it.m_p_nd == it.m_p_nd);
00221 
00222       if (prev_it != end())
00223     PB_DS_DEBUG_VERIFY(Cmp_Fn::operator()(PB_DS_V2F(*prev_it),
00224                           PB_DS_V2F(*it)));
00225       prev_it = it;
00226     }
00227 
00228   PB_DS_DEBUG_VERIFY(iterated_num == m_size);
00229   size_type reverse_iterated_num = 0;
00230   const_reverse_iterator reverse_prev_it = rend();
00231   for (const_reverse_iterator reverse_it = rbegin(); reverse_it != rend();
00232        ++reverse_it)
00233     {
00234       ++reverse_iterated_num;
00235       PB_DS_DEBUG_VERIFY(lower_bound(
00236                    PB_DS_V2F(*reverse_it)).m_p_nd == reverse_it.m_p_nd);
00237 
00238       const_iterator upper_bound_it = upper_bound(PB_DS_V2F(*reverse_it));
00239       --upper_bound_it;
00240       PB_DS_DEBUG_VERIFY(upper_bound_it.m_p_nd == reverse_it.m_p_nd);
00241       if (reverse_prev_it != rend())
00242     PB_DS_DEBUG_VERIFY(!Cmp_Fn::operator()(PB_DS_V2F(*reverse_prev_it),
00243                            PB_DS_V2F(*reverse_it)));
00244       reverse_prev_it = reverse_it;
00245     }
00246   PB_DS_DEBUG_VERIFY(reverse_iterated_num == m_size);
00247 }
00248 
00249 PB_DS_CLASS_T_DEC
00250 void
00251 PB_DS_CLASS_C_DEC::
00252 assert_consistent_with_debug_base(const char* __file, int __line) const
00253 {
00254   debug_base::check_size(m_size, __file, __line);
00255   assert_consistent_with_debug_base(m_p_head->m_p_parent, __file, __line);
00256 }
00257 
00258 PB_DS_CLASS_T_DEC
00259 void
00260 PB_DS_CLASS_C_DEC::
00261 assert_consistent_with_debug_base(const node_pointer p_nd,
00262                   const char* __file, int __line) const
00263 {
00264   if (p_nd == 0)
00265     return;
00266   debug_base::check_key_exists(PB_DS_V2F(p_nd->m_value), __file, __line);
00267   assert_consistent_with_debug_base(p_nd->m_p_left, __file, __line);
00268   assert_consistent_with_debug_base(p_nd->m_p_right, __file, __line);
00269 }
00270 
00271 PB_DS_CLASS_T_DEC
00272 void
00273 PB_DS_CLASS_C_DEC::
00274 assert_size(const char* __file, int __line) const
00275 { PB_DS_DEBUG_VERIFY(recursive_count(m_p_head->m_p_parent) == m_size); }
00276 
00277 #endif