3 // Copyright (C) 2005-2024 Free Software Foundation, Inc.
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
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.
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.
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/>.
25 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
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
37 * @file rc_binomial_heap_/rc.hpp
38 * Contains a redundant (binary counter).
48 /// Redundant binary counter.
49 template<typename _Node, typename _Alloc>
53 typedef _Alloc allocator_type;
54 typedef typename allocator_type::size_type size_type;
57 typedef typename rebind_traits<_Alloc, node>::pointer node_pointer;
60 typedef typename rebind_traits<_Alloc, node_pointer>::pointer
62 typedef typename rebind_traits<_Alloc, node_pointer>::const_pointer
67 max_entries = sizeof(size_type) << 3
71 typedef node_pointer entry;
72 typedef entry_const_pointer const_iterator;
90 _GLIBCXX_NODISCARD inline bool
105 #ifdef _GLIBCXX_DEBUG
107 assert_valid(const char*, int) const;
110 #ifdef PB_DS_RC_BINOMIAL_HEAP_TRACE_
116 node_pointer m_a_entries[max_entries];
117 size_type m_over_top;
120 template<typename _Node, typename _Alloc>
123 { PB_DS_ASSERT_VALID((*this)) }
125 template<typename _Node, typename _Alloc>
127 rc(const rc<_Node, _Alloc>& other) : m_over_top(0)
128 { PB_DS_ASSERT_VALID((*this)) }
130 template<typename _Node, typename _Alloc>
133 swap(rc<_Node, _Alloc>& other)
135 PB_DS_ASSERT_VALID((*this))
136 PB_DS_ASSERT_VALID(other)
138 const size_type over_top = std::max(m_over_top, other.m_over_top);
140 for (size_type i = 0; i < over_top; ++i)
141 std::swap(m_a_entries[i], other.m_a_entries[i]);
143 std::swap(m_over_top, other.m_over_top);
144 PB_DS_ASSERT_VALID((*this))
145 PB_DS_ASSERT_VALID(other)
148 template<typename _Node, typename _Alloc>
153 PB_DS_ASSERT_VALID((*this))
154 _GLIBCXX_DEBUG_ASSERT(m_over_top < max_entries);
155 m_a_entries[m_over_top++] = p_nd;
156 PB_DS_ASSERT_VALID((*this))
159 template<typename _Node, typename _Alloc>
164 PB_DS_ASSERT_VALID((*this))
165 _GLIBCXX_DEBUG_ASSERT(!empty());
167 PB_DS_ASSERT_VALID((*this))
170 template<typename _Node, typename _Alloc>
171 inline typename rc<_Node, _Alloc>::node_pointer
175 PB_DS_ASSERT_VALID((*this))
176 _GLIBCXX_DEBUG_ASSERT(!empty());
177 return *(m_a_entries + m_over_top - 1);
180 template<typename _Node, typename _Alloc>
181 _GLIBCXX_NODISCARD inline bool
185 PB_DS_ASSERT_VALID((*this))
186 return m_over_top == 0;
189 template<typename _Node, typename _Alloc>
190 inline typename rc<_Node, _Alloc>::size_type
193 { return m_over_top; }
195 template<typename _Node, typename _Alloc>
200 PB_DS_ASSERT_VALID((*this))
202 PB_DS_ASSERT_VALID((*this))
205 template<typename _Node, typename _Alloc>
206 const typename rc<_Node, _Alloc>::const_iterator
209 { return& m_a_entries[0]; }
211 template<typename _Node, typename _Alloc>
212 const typename rc<_Node, _Alloc>::const_iterator
215 { return& m_a_entries[m_over_top]; }
217 #ifdef _GLIBCXX_DEBUG
218 template<typename _Node, typename _Alloc>
221 assert_valid(const char* __file, int __line) const
222 { PB_DS_DEBUG_VERIFY(m_over_top < max_entries); }
225 #ifdef PB_DS_RC_BINOMIAL_HEAP_TRACE_
226 template<typename _Node, typename _Alloc>
231 std::cout << "rc" << std::endl;
232 for (size_type i = 0; i < m_over_top; ++i)
233 std::cerr << m_a_entries[i] << std::endl;
234 std::cout << std::endl;
237 } // namespace detail
238 } // namespace __gnu_pbds