1// -*- C++ -*-
2
3// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library.  This library is free
6// software; you can redistribute it and/or modify it under the terms
7// of the GNU General Public License as published by the Free Software
8// Foundation; either version 2, or (at your option) any later
9// version.
10
11// This library is distributed in the hope that it will be useful, but
12// WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14// General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this library; see the file COPYING.  If not, write to
18// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19// MA 02111-1307, USA.
20
21// As a special exception, you may use this file as part of a free
22// software library without restriction.  Specifically, if other files
23// instantiate templates or use macros or inline functions from this
24// file, or you compile this file and link it with other files to
25// produce an executable, this file does not by itself cause the
26// resulting executable to be covered by the GNU General Public
27// License.  This exception does not however invalidate any other
28// reasons why the executable file might be covered by the GNU General
29// Public License.
30
31// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32
33// Permission to use, copy, modify, sell, and distribute this software
34// is hereby granted without fee, provided that the above copyright
35// notice appears in all copies, and that both that copyright notice
36// and this permission notice appear in supporting documentation. None
37// of the above authors, nor IBM Haifa Research Laboratories, make any
38// representation about the suitability of this software for any
39// purpose. It is provided "as is" without express or implied
40// warranty.
41
42/**
43 * @file tree_trace_base.hpp
44 * Contains tree-related policies.
45 */
46
47#ifndef PB_DS_TREE_TRACE_BASE_HPP
48#define PB_DS_TREE_TRACE_BASE_HPP
49
50#ifdef PB_DS_TREE_TRACE
51
52#include <ext/pb_ds/detail/basic_tree_policy/basic_tree_policy_base.hpp>
53#include <ext/pb_ds/detail/basic_tree_policy/null_node_metadata.hpp>
54
55namespace pb_ds
56{
57
58  namespace detail
59  {
60
61#ifdef PB_DS_TREE_TRACE
62
63#define PB_DS_CLASS_T_DEC						\
64    template<								\
65						class Const_Node_Iterator, \
66						class Node_Iterator,	\
67						class Cmp_Fn,		\
68						bool Node_Based,	\
69						class Allocator>
70
71#define PB_DS_CLASS_C_DEC						\
72    tree_trace_base<							\
73						Const_Node_Iterator,	\
74						Node_Iterator,		\
75						Cmp_Fn,			\
76						Node_Based,		\
77						Allocator>
78
79#define PB_DS_BASE_C_DEC						\
80    basic_tree_policy_base<				\
81								Const_Node_Iterator, \
82								Node_Iterator, \
83								Allocator>
84
85    template<typename Const_Node_Iterator,
86	     class Node_Iterator,
87	     class Cmp_Fn,
88	     bool Node_Based,
89	     class Allocator>
90    class tree_trace_base : private PB_DS_BASE_C_DEC
91    {
92    public:
93      void
94      trace() const;
95
96    private:
97      typedef PB_DS_BASE_C_DEC base_type;
98
99      typedef Const_Node_Iterator const_node_iterator;
100
101      typedef typename Allocator::size_type size_type;
102
103    private:
104      void
105      trace_node(const_node_iterator nd_it, size_type level) const;
106
107      virtual bool
108      empty() const = 0;
109
110      virtual const_node_iterator
111      node_begin() const = 0;
112
113      virtual const_node_iterator
114      node_end() const = 0;
115
116      static void
117      print_node_pointer(Const_Node_Iterator nd_it, integral_constant<int,true>);
118
119      static void
120      print_node_pointer(Const_Node_Iterator nd_it, integral_constant<int,false>);
121
122      template<typename Metadata_>
123      static void
124      trace_it_metadata(Const_Node_Iterator nd_it, type_to_type<Metadata_>);
125
126      static void
127      trace_it_metadata(Const_Node_Iterator, type_to_type<null_node_metadata>);
128    };
129
130    PB_DS_CLASS_T_DEC
131    void
132    PB_DS_CLASS_C_DEC::
133    trace() const
134    {
135      if (empty())
136        return;
137
138      trace_node(node_begin(), 0);
139    }
140
141    PB_DS_CLASS_T_DEC
142    void
143    PB_DS_CLASS_C_DEC::
144    trace_node(const_node_iterator nd_it, size_type level) const
145    {
146      if (nd_it.get_r_child() != node_end())
147        trace_node(nd_it.get_r_child(), level + 1);
148
149      for (size_type i = 0; i < level; ++i)
150        std::cerr << ' ';
151
152      print_node_pointer(nd_it, integral_constant<int,Node_Based>());
153      std::cerr << base_type::extract_key(*(*nd_it));
154
155      typedef
156        type_to_type<
157	typename const_node_iterator::metadata_type>
158        m_type_ind_t;
159
160      trace_it_metadata(nd_it, m_type_ind_t());
161
162      std::cerr << std::endl;
163
164      if (nd_it.get_l_child() != node_end())
165        trace_node(nd_it.get_l_child(), level + 1);
166    }
167
168    PB_DS_CLASS_T_DEC
169    template<typename Metadata_>
170    void
171    PB_DS_CLASS_C_DEC::
172    trace_it_metadata(Const_Node_Iterator nd_it, type_to_type<Metadata_>)
173    {
174      std::cerr << " (" <<
175        static_cast<unsigned long>(nd_it.get_metadata()) << ") ";
176    }
177
178    PB_DS_CLASS_T_DEC
179    void
180    PB_DS_CLASS_C_DEC::
181    trace_it_metadata(Const_Node_Iterator, type_to_type<null_node_metadata>)
182    { }
183
184    PB_DS_CLASS_T_DEC
185    void
186    PB_DS_CLASS_C_DEC::
187    print_node_pointer(Const_Node_Iterator nd_it, integral_constant<int,true>)
188    {
189      std::cerr << nd_it.m_p_nd << " ";
190    }
191
192    PB_DS_CLASS_T_DEC
193    void
194    PB_DS_CLASS_C_DEC::
195    print_node_pointer(Const_Node_Iterator nd_it, integral_constant<int,false>)
196    {
197      std::cerr <<* nd_it << " ";
198    }
199
200#undef PB_DS_CLASS_T_DEC
201
202#undef PB_DS_CLASS_C_DEC
203
204#undef PB_DS_BASE_C_DEC
205
206#endif // #ifdef    PB_DS_TREE_TRACE
207
208  } // namespace detail
209
210} // namespace pb_ds
211
212#endif // #ifdef PB_DS_TREE_TRACE
213
214#endif // #ifndef PB_DS_TREE_TRACE_BASE_HPP
215
216