libstdc++
tree_trace_base.hpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2005, 2006, 2009, 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 detail/tree_trace_base.hpp
00038  * Contains tree-related policies.
00039  */
00040 
00041 #ifndef PB_DS_TREE_TRACE_BASE_HPP
00042 #define PB_DS_TREE_TRACE_BASE_HPP
00043 
00044 #ifdef PB_DS_TREE_TRACE
00045 
00046 #include <ext/pb_ds/detail/branch_policy/branch_policy.hpp>
00047 #include <ext/pb_ds/detail/branch_policy/null_node_metadata.hpp>
00048 
00049 namespace __gnu_pbds
00050 {
00051   namespace detail
00052   {
00053 #ifdef PB_DS_TREE_TRACE
00054 
00055 #define PB_DS_CLASS_T_DEC                       \
00056     template<typename Node_CItr, typename Node_Itr, \
00057          typename Cmp_Fn, bool Node_Based, typename _Alloc>
00058 
00059 #define PB_DS_CLASS_C_DEC                       \
00060     tree_trace_base<Node_CItr, Node_Itr, Cmp_Fn,         \
00061             Node_Based, _Alloc>
00062 
00063 #define PB_DS_TRACE_BASE \
00064     branch_policy<Node_CItr, Node_Itr, _Alloc>
00065 
00066     /// Tracing base class.
00067     template<typename Node_CItr, typename Node_Itr,
00068          typename Cmp_Fn, bool Node_Based, typename _Alloc>
00069     class tree_trace_base : private PB_DS_TRACE_BASE
00070     {
00071     public:
00072       void
00073       trace() const;
00074 
00075     private:
00076       typedef PB_DS_TRACE_BASE          base_type;
00077       typedef Node_CItr         node_const_iterator;
00078       typedef typename _Alloc::size_type    size_type;
00079 
00080       void
00081       trace_node(node_const_iterator, size_type) const;
00082 
00083       virtual bool
00084       empty() const = 0;
00085 
00086       virtual node_const_iterator
00087       node_begin() const = 0;
00088 
00089       virtual node_const_iterator
00090       node_end() const = 0;
00091 
00092       static void
00093       print_node_pointer(Node_CItr, integral_constant<int,true>);
00094 
00095       static void
00096       print_node_pointer(Node_CItr, integral_constant<int,false>);
00097 
00098       template<typename Metadata_>
00099       static void
00100       trace_it_metadata(Node_CItr, type_to_type<Metadata_>);
00101 
00102       static void
00103       trace_it_metadata(Node_CItr, type_to_type<null_type>);
00104     };
00105 
00106     PB_DS_CLASS_T_DEC
00107     void
00108     PB_DS_CLASS_C_DEC::
00109     trace() const
00110     {
00111       if (empty())
00112     return;
00113       trace_node(node_begin(), 0);
00114     }
00115 
00116     PB_DS_CLASS_T_DEC
00117     void
00118     PB_DS_CLASS_C_DEC::
00119     trace_node(node_const_iterator nd_it, size_type level) const
00120     {
00121       if (nd_it.get_r_child() != node_end())
00122     trace_node(nd_it.get_r_child(), level + 1);
00123 
00124       for (size_type i = 0; i < level; ++i)
00125     std::cerr << ' ';
00126 
00127       print_node_pointer(nd_it, integral_constant<int,Node_Based>());
00128       std::cerr << base_type::extract_key(*(*nd_it));
00129 
00130       typedef type_to_type<typename node_const_iterator::metadata_type>
00131     m_type_ind_t;
00132 
00133       trace_it_metadata(nd_it, m_type_ind_t());
00134 
00135       std::cerr << std::endl;
00136 
00137       if (nd_it.get_l_child() != node_end())
00138     trace_node(nd_it.get_l_child(), level + 1);
00139     }
00140 
00141     PB_DS_CLASS_T_DEC
00142     template<typename Metadata_>
00143     void
00144     PB_DS_CLASS_C_DEC::
00145     trace_it_metadata(Node_CItr nd_it, type_to_type<Metadata_>)
00146     {
00147       const unsigned long ul = static_cast<unsigned long>(nd_it.get_metadata());
00148       std::cerr << " (" << ul << ") ";
00149     }
00150 
00151     PB_DS_CLASS_T_DEC
00152     void
00153     PB_DS_CLASS_C_DEC::
00154     trace_it_metadata(Node_CItr, type_to_type<null_type>)
00155     { }
00156 
00157     PB_DS_CLASS_T_DEC
00158     void
00159     PB_DS_CLASS_C_DEC::
00160     print_node_pointer(Node_CItr nd_it, integral_constant<int,true>)
00161     { std::cerr << nd_it.m_p_nd << " "; }
00162 
00163     PB_DS_CLASS_T_DEC
00164     void
00165     PB_DS_CLASS_C_DEC::
00166     print_node_pointer(Node_CItr nd_it, integral_constant<int,false>)
00167     { std::cerr << *nd_it << " "; }
00168 
00169 #undef PB_DS_CLASS_T_DEC
00170 #undef PB_DS_CLASS_C_DEC
00171 #undef PB_DS_TRACE_BASE
00172 #endif // #ifdef    PB_DS_TREE_TRACE
00173 
00174   } // namespace detail
00175 } // namespace __gnu_pbds
00176 
00177 #endif // #ifdef PB_DS_TREE_TRACE
00178 
00179 #endif // #ifndef PB_DS_TREE_TRACE_BASE_HPP