1 // -*- C++ -*- 2 3 // Copyright (C) 2005, 2006, 2009 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional 17 // permissions described in the GCC Runtime Library Exception, version 18 // 3.1, as published by the Free Software Foundation. 19 20 // You should have received a copy of the GNU General Public License and 21 // a copy of the GCC Runtime Library Exception along with this program; 22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23 // <http://www.gnu.org/licenses/>. 24 25 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL. 26 27 // Permission to use, copy, modify, sell, and distribute this software 28 // is hereby granted without fee, provided that the above copyright 29 // notice appears in all copies, and that both that copyright notice 30 // and this permission notice appear in supporting documentation. None 31 // of the above authors, nor IBM Haifa Research Laboratories, make any 32 // representation about the suitability of this software for any 33 // purpose. It is provided "as is" without express or implied 34 // warranty. 35 36 /** 37 * @file node_iterators.hpp 38 * Contains an implementation class for bin_search_tree_. 39 */ 40 41 #ifndef PB_DS_BIN_SEARCH_TREE_NODE_ITERATORS_HPP 42 #define PB_DS_BIN_SEARCH_TREE_NODE_ITERATORS_HPP 43 44 #include <ext/pb_ds/tag_and_trait.hpp> 45 46 namespace __gnu_pbds 47 { 48 namespace detail 49 { 50 51 #define PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC \ 52 bin_search_tree_const_node_it_< \ 53 Node, \ 54 Const_Iterator, \ 55 Iterator, \ 56 Allocator> 57 58 // Const node iterator. 59 template<typename Node, 60 class Const_Iterator, 61 class Iterator, 62 class Allocator> 63 class bin_search_tree_const_node_it_ 64 { 65 private: 66 67 private: 68 typedef 69 typename Allocator::template rebind< 70 Node>::other::pointer 71 node_pointer; 72 73 public: 74 75 // Category. 76 typedef trivial_iterator_tag iterator_category; 77 78 // Difference type. 79 typedef trivial_iterator_difference_type difference_type; 80 81 // __Iterator's value type. 82 typedef Const_Iterator value_type; 83 84 // __Iterator's reference type. 85 typedef Const_Iterator reference; 86 87 // __Iterator's __const reference type. 88 typedef Const_Iterator const_reference; 89 90 // Metadata type. 91 typedef typename Node::metadata_type metadata_type; 92 93 // Const metadata reference type. 94 typedef 95 typename Allocator::template rebind< 96 metadata_type>::other::const_reference 97 const_metadata_reference; 98 99 public: 100 101 // Default constructor. 102 /* 103 inline 104 bin_search_tree_const_node_it_() 105 */ 106 107 inline 108 bin_search_tree_const_node_it_(const node_pointer p_nd = NULL) : m_p_nd(const_cast<node_pointer>(p_nd)) 109 { } 110 111 // Access. 112 inline const_reference 113 operator*() const 114 { 115 return (Const_Iterator(m_p_nd)); 116 } 117 118 // Metadata access. 119 inline const_metadata_reference 120 get_metadata() const 121 { 122 return (m_p_nd->get_metadata()); 123 } 124 125 // Returns the __const node iterator associated with the left node. 126 inline PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC 127 get_l_child() const 128 { 129 return (PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC(m_p_nd->m_p_left)); 130 } 131 132 // Returns the __const node iterator associated with the right node. 133 inline PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC 134 get_r_child() const 135 { 136 return (PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC(m_p_nd->m_p_right)); 137 } 138 139 // Compares to a different iterator object. 140 inline bool 141 operator==(const PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC& other) const 142 { 143 return (m_p_nd == other.m_p_nd); 144 } 145 146 // Compares (negatively) to a different iterator object. 147 inline bool 148 operator!=(const PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC& other) const 149 { 150 return (m_p_nd != other.m_p_nd); 151 } 152 153 public: 154 node_pointer m_p_nd; 155 }; 156 157 #define PB_DS_TREE_NODE_ITERATOR_CLASS_C_DEC \ 158 bin_search_tree_node_it_< \ 159 Node, \ 160 Const_Iterator, \ 161 Iterator, \ 162 Allocator> 163 164 // Node iterator. 165 template<typename Node, 166 class Const_Iterator, 167 class Iterator, 168 class Allocator> 169 class bin_search_tree_node_it_ : 170 public PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC 171 172 { 173 174 private: 175 typedef 176 typename Allocator::template rebind< 177 Node>::other::pointer 178 node_pointer; 179 180 public: 181 182 // __Iterator's value type. 183 typedef Iterator value_type; 184 185 // __Iterator's reference type. 186 typedef Iterator reference; 187 188 // __Iterator's __const reference type. 189 typedef Iterator const_reference; 190 191 public: 192 193 // Default constructor. 194 /* 195 inline 196 bin_search_tree_node_it_(); 197 */ 198 199 inline 200 bin_search_tree_node_it_(const node_pointer p_nd = NULL) : PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC( 201 const_cast<node_pointer>(p_nd)) 202 { } 203 204 // Access. 205 inline Iterator 206 operator*() const 207 { 208 return (Iterator(PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC::m_p_nd)); 209 } 210 211 // Returns the node iterator associated with the left node. 212 inline PB_DS_TREE_NODE_ITERATOR_CLASS_C_DEC 213 get_l_child() const 214 { 215 return (PB_DS_TREE_NODE_ITERATOR_CLASS_C_DEC( 216 PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC::m_p_nd->m_p_left)); 217 } 218 219 // Returns the node iterator associated with the right node. 220 inline PB_DS_TREE_NODE_ITERATOR_CLASS_C_DEC 221 get_r_child() const 222 { 223 return (PB_DS_TREE_NODE_ITERATOR_CLASS_C_DEC( 224 PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC::m_p_nd->m_p_right)); 225 } 226 227 }; 228 229 #undef PB_DS_TREE_CONST_NODE_ITERATOR_CLASS_C_DEC 230 231 #undef PB_DS_TREE_NODE_ITERATOR_CLASS_C_DEC 232 233 } // namespace detail 234 } // namespace __gnu_pbds 235 236 #endif // #ifndef PB_DS_BIN_SEARCH_TREE_NODE_ITERATORS_HPP 237 238