libstdc++
bin_search_tree_/node_iterators.hpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2005, 2006, 2009, 2010 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_/node_iterators.hpp
00038  * Contains an implementation class for bin_search_tree_.
00039  */
00040 
00041 #ifndef PB_DS_BIN_SEARCH_TREE_NODE_ITERATORS_HPP
00042 #define PB_DS_BIN_SEARCH_TREE_NODE_ITERATORS_HPP
00043 
00044 #include <ext/pb_ds/tag_and_trait.hpp>
00045 
00046 namespace __gnu_pbds
00047 {
00048   namespace detail
00049   {
00050 #define PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC \
00051     bin_search_tree_const_node_it_<Node, Const_Iterator, Iterator, _Alloc>
00052 
00053     /// Const node iterator.
00054     template<typename Node,
00055          class Const_Iterator,
00056          class Iterator,
00057          typename _Alloc>
00058     class bin_search_tree_const_node_it_
00059     {
00060     private:
00061       typedef
00062       typename _Alloc::template rebind<
00063       Node>::other::pointer
00064       node_pointer;
00065 
00066     public:
00067       /// Category.
00068       typedef trivial_iterator_tag iterator_category;
00069 
00070       /// Difference type.
00071       typedef trivial_iterator_difference_type difference_type;
00072 
00073       /// Iterator's value type.
00074       typedef Const_Iterator value_type;
00075 
00076       /// Iterator's reference type.
00077       typedef Const_Iterator reference;
00078 
00079       /// Iterator's __const reference type.
00080       typedef Const_Iterator const_reference;
00081 
00082       /// Metadata type.
00083       typedef typename Node::metadata_type metadata_type;
00084 
00085       /// Const metadata reference type.
00086       typedef
00087       typename _Alloc::template rebind<metadata_type>::other::const_reference
00088       metadata_const_reference;
00089 
00090 
00091       bin_search_tree_const_node_it_(const node_pointer p_nd = 0)
00092       : m_p_nd(const_cast<node_pointer>(p_nd))
00093       { }
00094 
00095       /// Access.
00096       const_reference
00097       operator*() const
00098       { return Const_Iterator(m_p_nd); }
00099 
00100       /// Metadata access.
00101       metadata_const_reference
00102       get_metadata() const
00103       { return m_p_nd->get_metadata(); }
00104 
00105       /// Returns the __const node iterator associated with the left node.
00106       PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC
00107       get_l_child() const
00108       { return PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC(m_p_nd->m_p_left); }
00109 
00110       /// Returns the __const node iterator associated with the right node.
00111       PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC
00112       get_r_child() const
00113       { return PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC(m_p_nd->m_p_right); }
00114 
00115       /// Compares to a different iterator object.
00116       bool
00117       operator==(const PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC& other) const
00118       { return m_p_nd == other.m_p_nd; }
00119 
00120       /// Compares (negatively) to a different iterator object.
00121       bool
00122       operator!=(const PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC& other) const
00123       { return m_p_nd != other.m_p_nd; }
00124 
00125       node_pointer m_p_nd;
00126     };
00127 
00128 #define PB_DS_TREE_NODE_ITERATOR_CLASS_C_DEC \
00129     bin_search_tree_node_it_<Node, Const_Iterator, Iterator, _Alloc>
00130 
00131     /// Node iterator.
00132     template<typename Node,
00133          class Const_Iterator,
00134          class Iterator,
00135          typename _Alloc>
00136     class bin_search_tree_node_it_
00137     : public PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC
00138     {
00139     private:
00140       typedef
00141       typename _Alloc::template rebind<
00142       Node>::other::pointer
00143       node_pointer;
00144 
00145     public:
00146       /// Iterator's value type.
00147       typedef Iterator value_type;
00148 
00149       /// Iterator's reference type.
00150       typedef Iterator reference;
00151 
00152       /// Iterator's __const reference type.
00153       typedef Iterator const_reference;
00154 
00155       inline
00156       bin_search_tree_node_it_(const node_pointer p_nd = 0)
00157       : PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC(const_cast<node_pointer>(p_nd))
00158       { }
00159 
00160       /// Access.
00161       Iterator
00162       operator*() const
00163       { return Iterator(PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC::m_p_nd); }
00164 
00165       /// Returns the node iterator associated with the left node.
00166       PB_DS_TREE_NODE_ITERATOR_CLASS_C_DEC
00167       get_l_child() const
00168       {
00169     return PB_DS_TREE_NODE_ITERATOR_CLASS_C_DEC(
00170                             PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC::m_p_nd->m_p_left);
00171       }
00172 
00173       /// Returns the node iterator associated with the right node.
00174       PB_DS_TREE_NODE_ITERATOR_CLASS_C_DEC
00175       get_r_child() const
00176       {
00177     return PB_DS_TREE_NODE_ITERATOR_CLASS_C_DEC(
00178                             PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC::m_p_nd->m_p_right);
00179       }
00180 
00181     };
00182 
00183 #undef PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC
00184 #undef PB_DS_TREE_NODE_ITERATOR_CLASS_C_DEC
00185 
00186   } // namespace detail
00187 } // namespace __gnu_pbds
00188 
00189 #endif // #ifndef PB_DS_BIN_SEARCH_TREE_NODE_ITERATORS_HPP