18b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen// -*- C++ -*-
28b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen
38b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen// Copyright (C) 2005-2014 Free Software Foundation, Inc.
48b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen//
58b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen// This file is part of the GNU ISO C++ Library.  This library is free
68b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen// software; you can redistribute it and/or modify it under the terms
78b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen// of the GNU General Public License as published by the Free Software
88b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen// Foundation; either version 3, or (at your option) any later
98b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen// version.
108b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen
118b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen// This library is distributed in the hope that it will be useful, but
128b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen// WITHOUT ANY WARRANTY; without even the implied warranty of
138b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
148b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen// General Public License for more details.
158b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen
168b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen// Under Section 7 of GPL version 3, you are granted additional
178b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen// permissions described in the GCC Runtime Library Exception, version
188b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen// 3.1, as published by the Free Software Foundation.
198b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen
208b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen// You should have received a copy of the GNU General Public License and
218b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen// a copy of the GCC Runtime Library Exception along with this program;
228b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
238b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen// <http://www.gnu.org/licenses/>.
248b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen
2557de61296cc8f29d8740fc7e6983af9553e7a410Jason Tholstrup// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
268b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen
278b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen// Permission to use, copy, modify, sell, and distribute this software
288b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen// is hereby granted without fee, provided that the above copyright
298b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen// notice appears in all copies, and that both that copyright notice
308b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen// and this permission notice appear in supporting documentation. None
318b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen// of the above authors, nor IBM Haifa Research Laboratories, make any
328b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen// representation about the suitability of this software for any
338b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen// purpose. It is provided "as is" without express or implied
348b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen// warranty.
358b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen
368b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen/**
378b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen * @file bin_search_tree_/node_iterators.hpp
388b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen * Contains an implementation class for bin_search_tree_.
398b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen */
408b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen
418b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen#ifndef PB_DS_BIN_SEARCH_TREE_NODE_ITERATORS_HPP
428b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen#define PB_DS_BIN_SEARCH_TREE_NODE_ITERATORS_HPP
438b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen
448b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen#include <ext/pb_ds/tag_and_trait.hpp>
458b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen
468b7af7188228ca88838e31e070f0fca0d1f90581Yao Chennamespace __gnu_pbds
478b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen{
488b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen  namespace detail
498b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen  {
508b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen#define PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC \
518b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen    bin_search_tree_const_node_it_<Node, Const_Iterator, Iterator, _Alloc>
528b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen
538b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen    /// Const node iterator.
548b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen    template<typename Node,
558b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen	     class Const_Iterator,
568b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen	     class Iterator,
578b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen	     typename _Alloc>
588b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen    class bin_search_tree_const_node_it_
598b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen    {
608b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen    private:
618b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen      typedef
62      typename _Alloc::template rebind<
63      Node>::other::pointer
64      node_pointer;
65
66    public:
67      /// Category.
68      typedef trivial_iterator_tag iterator_category;
69
70      /// Difference type.
71      typedef trivial_iterator_difference_type difference_type;
72
73      /// Iterator's value type.
74      typedef Const_Iterator value_type;
75
76      /// Iterator's reference type.
77      typedef Const_Iterator reference;
78
79      /// Iterator's __const reference type.
80      typedef Const_Iterator const_reference;
81
82      /// Metadata type.
83      typedef typename Node::metadata_type metadata_type;
84
85      /// Const metadata reference type.
86      typedef
87      typename _Alloc::template rebind<metadata_type>::other::const_reference
88      metadata_const_reference;
89
90
91      bin_search_tree_const_node_it_(const node_pointer p_nd = 0)
92      : m_p_nd(const_cast<node_pointer>(p_nd))
93      { }
94
95      /// Access.
96      const_reference
97      operator*() const
98      { return Const_Iterator(m_p_nd); }
99
100      /// Metadata access.
101      metadata_const_reference
102      get_metadata() const
103      { return m_p_nd->get_metadata(); }
104
105      /// Returns the __const node iterator associated with the left node.
106      PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC
107      get_l_child() const
108      { return PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC(m_p_nd->m_p_left); }
109
110      /// Returns the __const node iterator associated with the right node.
111      PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC
112      get_r_child() const
113      { return PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC(m_p_nd->m_p_right); }
114
115      /// Compares to a different iterator object.
116      bool
117      operator==(const PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC& other) const
118      { return m_p_nd == other.m_p_nd; }
119
120      /// Compares (negatively) to a different iterator object.
121      bool
122      operator!=(const PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC& other) const
123      { return m_p_nd != other.m_p_nd; }
124
125      node_pointer m_p_nd;
126    };
127
128#define PB_DS_TREE_NODE_ITERATOR_CLASS_C_DEC \
129    bin_search_tree_node_it_<Node, Const_Iterator, Iterator, _Alloc>
130
131    /// Node iterator.
132    template<typename Node,
133	     class Const_Iterator,
134	     class Iterator,
135	     typename _Alloc>
136    class bin_search_tree_node_it_
137    : public PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC
138    {
139    private:
140      typedef
141      typename _Alloc::template rebind<
142      Node>::other::pointer
143      node_pointer;
144
145    public:
146      /// Iterator's value type.
147      typedef Iterator value_type;
148
149      /// Iterator's reference type.
150      typedef Iterator reference;
151
152      /// Iterator's __const reference type.
153      typedef Iterator const_reference;
154
155      inline
156      bin_search_tree_node_it_(const node_pointer p_nd = 0)
157      : PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC(const_cast<node_pointer>(p_nd))
158      { }
159
160      /// Access.
161      Iterator
162      operator*() const
163      { return Iterator(PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC::m_p_nd); }
164
165      /// Returns the node iterator associated with the left node.
166      PB_DS_TREE_NODE_ITERATOR_CLASS_C_DEC
167      get_l_child() const
168      {
169	return PB_DS_TREE_NODE_ITERATOR_CLASS_C_DEC(
170						    PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC::m_p_nd->m_p_left);
171      }
172
173      /// Returns the node iterator associated with the right node.
174      PB_DS_TREE_NODE_ITERATOR_CLASS_C_DEC
175      get_r_child() const
176      {
177	return PB_DS_TREE_NODE_ITERATOR_CLASS_C_DEC(
178						    PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC::m_p_nd->m_p_right);
179      }
180
181    };
182
183#undef PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC
184#undef PB_DS_TREE_NODE_ITERATOR_CLASS_C_DEC
185
186  } // namespace detail
187} // namespace __gnu_pbds
188
189#endif // #ifndef PB_DS_BIN_SEARCH_TREE_NODE_ITERATORS_HPP
190