resize_policy.hpp revision 169691
1169691Skan// -*- C++ -*- 2169691Skan 3169691Skan// Copyright (C) 2005, 2006 Free Software Foundation, Inc. 4169691Skan// 5169691Skan// This file is part of the GNU ISO C++ Library. This library is free 6169691Skan// software; you can redistribute it and/or modify it under the terms 7169691Skan// of the GNU General Public License as published by the Free Software 8169691Skan// Foundation; either version 2, or (at your option) any later 9169691Skan// version. 10169691Skan 11169691Skan// This library is distributed in the hope that it will be useful, but 12169691Skan// WITHOUT ANY WARRANTY; without even the implied warranty of 13169691Skan// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14169691Skan// General Public License for more details. 15169691Skan 16169691Skan// You should have received a copy of the GNU General Public License 17169691Skan// along with this library; see the file COPYING. If not, write to 18169691Skan// the Free Software Foundation, 59 Temple Place - Suite 330, Boston, 19169691Skan// MA 02111-1307, USA. 20169691Skan 21169691Skan// As a special exception, you may use this file as part of a free 22169691Skan// software library without restriction. Specifically, if other files 23169691Skan// instantiate templates or use macros or inline functions from this 24169691Skan// file, or you compile this file and link it with other files to 25169691Skan// produce an executable, this file does not by itself cause the 26169691Skan// resulting executable to be covered by the GNU General Public 27169691Skan// License. This exception does not however invalidate any other 28169691Skan// reasons why the executable file might be covered by the GNU General 29169691Skan// Public License. 30169691Skan 31169691Skan// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL. 32169691Skan 33169691Skan// Permission to use, copy, modify, sell, and distribute this software 34169691Skan// is hereby granted without fee, provided that the above copyright 35169691Skan// notice appears in all copies, and that both that copyright notice 36169691Skan// and this permission notice appear in supporting documentation. None 37169691Skan// of the above authors, nor IBM Haifa Research Laboratories, make any 38169691Skan// representation about the suitability of this software for any 39169691Skan// purpose. It is provided "as is" without express or implied 40169691Skan// warranty. 41169691Skan 42169691Skan/** 43169691Skan * @file resize_policy.hpp 44169691Skan * Contains an implementation class for a binary_heap. 45169691Skan */ 46169691Skan 47169691Skan#ifndef PB_DS_BINARY_HEAP_RESIZE_POLICY_HPP 48169691Skan#define PB_DS_BINARY_HEAP_RESIZE_POLICY_HPP 49169691Skan 50169691Skan#include <debug/debug.h> 51169691Skan 52169691Skannamespace pb_ds 53169691Skan{ 54169691Skan namespace detail 55169691Skan { 56169691Skan 57169691Skan#define PB_DS_CLASS_T_DEC template<typename Size_Type> 58169691Skan 59169691Skan#define PB_DS_CLASS_C_DEC resize_policy<Size_Type> 60169691Skan 61169691Skan template<typename Size_Type> 62169691Skan class resize_policy 63169691Skan { 64169691Skan public: 65169691Skan typedef Size_Type size_type; 66169691Skan 67169691Skan enum 68169691Skan { 69169691Skan min_size = 16 70169691Skan }; 71169691Skan 72169691Skan public: 73169691Skan inline 74169691Skan resize_policy(); 75169691Skan 76169691Skan inline void 77169691Skan swap(PB_DS_CLASS_C_DEC& other); 78169691Skan 79169691Skan inline bool 80169691Skan resize_needed_for_grow(size_type size) const; 81169691Skan 82169691Skan inline bool 83169691Skan resize_needed_for_shrink(size_type size) const; 84169691Skan 85169691Skan inline bool 86169691Skan grow_needed(size_type size) const; 87169691Skan 88169691Skan inline bool 89169691Skan shrink_needed(size_type size) const; 90169691Skan 91169691Skan inline size_type 92169691Skan get_new_size_for_grow() const; 93169691Skan 94169691Skan inline size_type 95169691Skan get_new_size_for_shrink() const; 96169691Skan 97169691Skan size_type 98169691Skan get_new_size_for_arbitrary(size_type size) const; 99169691Skan 100169691Skan inline void 101169691Skan notify_grow_resize(); 102169691Skan 103169691Skan inline void 104169691Skan notify_shrink_resize(); 105169691Skan 106169691Skan void 107169691Skan notify_arbitrary(size_type actual_size); 108169691Skan 109169691Skan#ifdef _GLIBCXX_DEBUG 110169691Skan void 111169691Skan assert_valid() const; 112169691Skan#endif 113169691Skan 114169691Skan#ifdef PB_DS_BINARY_HEAP_TRACE_ 115169691Skan void 116169691Skan trace() const; 117169691Skan#endif 118169691Skan 119169691Skan private: 120169691Skan enum 121169691Skan { 122169691Skan ratio = 8, 123169691Skan factor = 2 124169691Skan }; 125169691Skan 126169691Skan private: 127169691Skan size_type m_next_shrink_size; 128169691Skan size_type m_next_grow_size; 129169691Skan }; 130169691Skan 131169691Skan PB_DS_CLASS_T_DEC 132169691Skan inline 133169691Skan PB_DS_CLASS_C_DEC:: 134169691Skan resize_policy() : 135169691Skan m_next_shrink_size(0), 136169691Skan m_next_grow_size(min_size) 137169691Skan { _GLIBCXX_DEBUG_ONLY(assert_valid();) } 138169691Skan 139169691Skan PB_DS_CLASS_T_DEC 140169691Skan inline void 141169691Skan PB_DS_CLASS_C_DEC:: 142169691Skan swap(PB_DS_CLASS_C_DEC& other) 143169691Skan { 144169691Skan std::swap(m_next_shrink_size, other.m_next_shrink_size); 145169691Skan std::swap(m_next_grow_size, other.m_next_grow_size); 146169691Skan } 147169691Skan 148169691Skan PB_DS_CLASS_T_DEC 149169691Skan inline bool 150169691Skan PB_DS_CLASS_C_DEC:: 151169691Skan resize_needed_for_grow(size_type size) const 152169691Skan { 153169691Skan _GLIBCXX_DEBUG_ASSERT(size <= m_next_grow_size); 154169691Skan return size == m_next_grow_size; 155169691Skan } 156169691Skan 157169691Skan PB_DS_CLASS_T_DEC 158169691Skan inline bool 159169691Skan PB_DS_CLASS_C_DEC:: 160169691Skan resize_needed_for_shrink(size_type size) const 161169691Skan { 162169691Skan _GLIBCXX_DEBUG_ASSERT(size <= m_next_grow_size); 163169691Skan return size == m_next_shrink_size; 164169691Skan } 165169691Skan 166169691Skan PB_DS_CLASS_T_DEC 167169691Skan inline typename PB_DS_CLASS_C_DEC::size_type 168169691Skan PB_DS_CLASS_C_DEC:: 169169691Skan get_new_size_for_grow() const 170169691Skan { return m_next_grow_size* factor; } 171169691Skan 172169691Skan PB_DS_CLASS_T_DEC 173169691Skan inline typename PB_DS_CLASS_C_DEC::size_type 174169691Skan PB_DS_CLASS_C_DEC:: 175169691Skan get_new_size_for_shrink() const 176169691Skan { 177169691Skan const size_type half_size = m_next_grow_size / factor; 178169691Skan return std::max(static_cast<size_type>(min_size), half_size); 179169691Skan } 180169691Skan 181169691Skan PB_DS_CLASS_T_DEC 182169691Skan inline typename PB_DS_CLASS_C_DEC::size_type 183169691Skan PB_DS_CLASS_C_DEC:: 184169691Skan get_new_size_for_arbitrary(size_type size) const 185169691Skan { 186169691Skan size_type ret = min_size; 187169691Skan while (ret < size) 188169691Skan ret *= factor; 189169691Skan return ret; 190169691Skan } 191169691Skan 192169691Skan PB_DS_CLASS_T_DEC 193169691Skan inline void 194169691Skan PB_DS_CLASS_C_DEC:: 195169691Skan notify_grow_resize() 196169691Skan { 197169691Skan _GLIBCXX_DEBUG_ONLY(assert_valid();) 198169691Skan _GLIBCXX_DEBUG_ASSERT(m_next_grow_size >= min_size); 199169691Skan m_next_grow_size *= factor; 200169691Skan m_next_shrink_size = m_next_grow_size / ratio; 201169691Skan _GLIBCXX_DEBUG_ONLY(assert_valid();) 202169691Skan } 203169691Skan 204169691Skan PB_DS_CLASS_T_DEC 205169691Skan inline void 206169691Skan PB_DS_CLASS_C_DEC:: 207169691Skan notify_shrink_resize() 208169691Skan { 209169691Skan _GLIBCXX_DEBUG_ONLY(assert_valid();) 210169691Skan m_next_shrink_size /= factor; 211169691Skan if (m_next_shrink_size == 1) 212169691Skan m_next_shrink_size = 0; 213169691Skan 214169691Skan m_next_grow_size = 215169691Skan std::max(m_next_grow_size / factor, static_cast<size_type>(min_size)); 216169691Skan _GLIBCXX_DEBUG_ONLY(assert_valid();) 217169691Skan } 218169691Skan 219169691Skan PB_DS_CLASS_T_DEC 220169691Skan inline void 221169691Skan PB_DS_CLASS_C_DEC:: 222169691Skan notify_arbitrary(size_type actual_size) 223169691Skan { 224169691Skan m_next_grow_size = actual_size; 225169691Skan m_next_shrink_size = m_next_grow_size / ratio; 226169691Skan _GLIBCXX_DEBUG_ONLY(assert_valid();) 227169691Skan } 228169691Skan 229169691Skan#ifdef _GLIBCXX_DEBUG 230169691Skan PB_DS_CLASS_T_DEC 231169691Skan void 232169691Skan PB_DS_CLASS_C_DEC:: 233169691Skan assert_valid() const 234169691Skan { 235169691Skan _GLIBCXX_DEBUG_ASSERT(m_next_shrink_size == 0 || 236169691Skan m_next_shrink_size* ratio == m_next_grow_size); 237169691Skan 238169691Skan _GLIBCXX_DEBUG_ASSERT(m_next_grow_size >= min_size); 239169691Skan } 240169691Skan#endif 241169691Skan 242169691Skan#ifdef PB_DS_BINARY_HEAP_TRACE_ 243169691Skan PB_DS_CLASS_T_DEC 244169691Skan void 245169691Skan PB_DS_CLASS_C_DEC:: 246169691Skan trace() const 247169691Skan { 248169691Skan std::cerr << "shrink = " << m_next_shrink_size << 249169691Skan " grow = " << m_next_grow_size << std::endl; 250169691Skan } 251169691Skan#endif 252169691Skan 253169691Skan#undef PB_DS_CLASS_T_DEC 254169691Skan#undef PB_DS_CLASS_C_DEC 255169691Skan 256169691Skan} // namespace detail 257169691Skan} // namespace pb_ds 258169691Skan 259169691Skan#endif 260