3 // Copyright (C) 2005-2013 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 binary_heap_/resize_policy.hpp
38 * Contains an implementation class for a binary_heap.
41 #ifndef PB_DS_BINARY_HEAP_RESIZE_POLICY_HPP
42 #define PB_DS_BINARY_HEAP_RESIZE_POLICY_HPP
44 #include <debug/debug.h>
50 /// Resize policy for binary heap.
51 template<typename _Tp>
68 typedef _Tp size_type;
70 static const _Tp min_size = 16;
72 resize_policy() : m_shrink_size(0), m_grow_size(min_size)
73 { PB_DS_ASSERT_VALID((*this)) }
75 resize_policy(const resize_policy& other)
76 : m_shrink_size(other.m_shrink_size), m_grow_size(other.m_grow_size)
77 { PB_DS_ASSERT_VALID((*this)) }
80 swap(resize_policy<_Tp>&);
83 resize_needed_for_grow(size_type) const;
86 resize_needed_for_shrink(size_type) const;
89 grow_needed(size_type) const;
92 shrink_needed(size_type) const;
95 get_new_size_for_grow() const;
98 get_new_size_for_shrink() const;
101 get_new_size_for_arbitrary(size_type) const;
104 notify_grow_resize();
107 notify_shrink_resize();
110 notify_arbitrary(size_type);
112 #ifdef _GLIBCXX_DEBUG
114 assert_valid(const char*, int) const;
117 #ifdef PB_DS_BINARY_HEAP_TRACE_
123 template<typename _Tp>
124 const _Tp resize_policy<_Tp>::min_size;
126 template<typename _Tp>
129 swap(resize_policy<_Tp>& other)
131 std::swap(m_shrink_size, other.m_shrink_size);
132 std::swap(m_grow_size, other.m_grow_size);
135 template<typename _Tp>
138 resize_needed_for_grow(size_type size) const
140 _GLIBCXX_DEBUG_ASSERT(size <= m_grow_size);
141 return size == m_grow_size;
144 template<typename _Tp>
147 resize_needed_for_shrink(size_type size) const
149 _GLIBCXX_DEBUG_ASSERT(size <= m_grow_size);
150 return size == m_shrink_size;
153 template<typename _Tp>
154 inline typename resize_policy<_Tp>::size_type
156 get_new_size_for_grow() const
157 { return m_grow_size * factor; }
159 template<typename _Tp>
160 inline typename resize_policy<_Tp>::size_type
162 get_new_size_for_shrink() const
164 const size_type half_size = m_grow_size / factor;
165 return std::max(min_size, half_size);
168 template<typename _Tp>
169 inline typename resize_policy<_Tp>::size_type
171 get_new_size_for_arbitrary(size_type size) const
173 size_type ret = min_size;
179 template<typename _Tp>
184 PB_DS_ASSERT_VALID((*this))
185 _GLIBCXX_DEBUG_ASSERT(m_grow_size >= min_size);
186 m_grow_size *= factor;
187 m_shrink_size = m_grow_size / ratio;
188 PB_DS_ASSERT_VALID((*this))
191 template<typename _Tp>
194 notify_shrink_resize()
196 PB_DS_ASSERT_VALID((*this))
197 m_shrink_size /= factor;
198 if (m_shrink_size == 1)
200 m_grow_size = std::max(m_grow_size / factor, min_size);
201 PB_DS_ASSERT_VALID((*this))
204 template<typename _Tp>
207 notify_arbitrary(size_type actual_size)
209 m_grow_size = actual_size;
210 m_shrink_size = m_grow_size / ratio;
211 PB_DS_ASSERT_VALID((*this))
214 #ifdef _GLIBCXX_DEBUG
215 template<typename _Tp>
218 assert_valid(const char* __file, int __line) const
220 PB_DS_DEBUG_VERIFY(m_shrink_size == 0
221 || m_shrink_size * ratio == m_grow_size);
222 PB_DS_DEBUG_VERIFY(m_grow_size >= min_size);
226 #ifdef PB_DS_BINARY_HEAP_TRACE_
227 template<typename _Tp>
232 std::cerr << "shrink = " << m_shrink_size
233 << " grow = " << m_grow_size << std::endl;
237 } // namespace detail
238 } // namespace __gnu_pbds