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 binary_heap_.hpp
44169691Skan * Contains an implementation class for a binary heap.
45169691Skan */
46169691Skan
47169691Skan#ifndef PB_DS_BINARY_HEAP_HPP
48169691Skan#define PB_DS_BINARY_HEAP_HPP
49169691Skan
50169691Skan/*
51169691Skan * Based on CLRS.
52169691Skan */
53169691Skan
54169691Skan#include <queue>
55169691Skan#include <algorithm>
56169691Skan#include <ext/pb_ds/detail/cond_dealtor.hpp>
57169691Skan#include <ext/pb_ds/detail/cond_dealtor.hpp>
58169691Skan#include <ext/pb_ds/detail/type_utils.hpp>
59169691Skan#include <ext/pb_ds/detail/binary_heap_/entry_cmp.hpp>
60169691Skan#include <ext/pb_ds/detail/binary_heap_/entry_pred.hpp>
61169691Skan#include <ext/pb_ds/detail/binary_heap_/resize_policy.hpp>
62169691Skan#include <ext/pb_ds/detail/binary_heap_/const_point_iterator.hpp>
63169691Skan#include <ext/pb_ds/detail/binary_heap_/const_iterator.hpp>
64169691Skan#ifdef PB_DS_BINARY_HEAP_TRACE_
65169691Skan#include <iostream>
66169691Skan#endif
67169691Skan#include <ext/pb_ds/detail/type_utils.hpp>
68169691Skan#include <debug/debug.h>
69169691Skan
70169691Skannamespace pb_ds
71169691Skan{
72169691Skan  namespace detail
73169691Skan  {
74169691Skan#define PB_DS_CLASS_T_DEC \
75169691Skan    template<typename Value_Type, class Cmp_Fn, class Allocator>
76169691Skan
77169691Skan#define PB_DS_CLASS_C_DEC \
78169691Skan    binary_heap_<Value_Type, Cmp_Fn, Allocator>
79169691Skan
80169691Skan#define PB_DS_ENTRY_CMP_DEC \
81169691Skan    entry_cmp<Value_Type, Cmp_Fn, is_simple<Value_Type>::value, Allocator>::type
82169691Skan
83169691Skan#define PB_DS_RESIZE_POLICY_DEC	\
84169691Skan    resize_policy<typename Allocator::size_type>
85169691Skan
86169691Skan    /**
87169691Skan     * class description = "Base class for some types of h3ap$">
88169691Skan     **/
89169691Skan    template<typename Value_Type, class Cmp_Fn, class Allocator>
90169691Skan    class binary_heap_ : public PB_DS_ENTRY_CMP_DEC,
91169691Skan			 public PB_DS_RESIZE_POLICY_DEC
92169691Skan    {
93169691Skan
94169691Skan    private:
95169691Skan      enum
96169691Skan	{
97169691Skan	  simple_value = is_simple<Value_Type>::value
98169691Skan	};
99169691Skan
100169691Skan      typedef integral_constant<int, simple_value> no_throw_copies_t;
101169691Skan
102169691Skan      typedef
103169691Skan      typename Allocator::template rebind<
104169691Skan	Value_Type>::other
105169691Skan      value_allocator;
106169691Skan
107169691Skan      typedef
108169691Skan      typename __conditional_type<
109169691Skan	simple_value,
110169691Skan	Value_Type,
111169691Skan	typename value_allocator::pointer>::__type
112169691Skan      entry;
113169691Skan
114169691Skan      typedef
115169691Skan      typename Allocator::template rebind<
116169691Skan	entry>::other
117169691Skan      entry_allocator;
118169691Skan
119169691Skan      typedef typename entry_allocator::pointer entry_pointer;
120169691Skan
121169691Skan      typedef typename PB_DS_ENTRY_CMP_DEC entry_cmp;
122169691Skan
123169691Skan      typedef PB_DS_RESIZE_POLICY_DEC resize_policy;
124169691Skan
125169691Skan      typedef
126169691Skan      cond_dealtor<
127169691Skan	Value_Type,
128169691Skan	Allocator>
129169691Skan      cond_dealtor_t;
130169691Skan
131169691Skan    public:
132169691Skan
133169691Skan      typedef typename Allocator::size_type size_type;
134169691Skan
135169691Skan      typedef typename Allocator::difference_type difference_type;
136169691Skan
137169691Skan      typedef Value_Type value_type;
138169691Skan
139169691Skan      typedef
140169691Skan      typename Allocator::template rebind<
141169691Skan	value_type>::other::pointer
142169691Skan      pointer;
143169691Skan
144169691Skan      typedef
145169691Skan      typename Allocator::template rebind<
146169691Skan	value_type>::other::const_pointer
147169691Skan      const_pointer;
148169691Skan
149169691Skan      typedef
150169691Skan      typename Allocator::template rebind<
151169691Skan	value_type>::other::reference
152169691Skan      reference;
153169691Skan
154169691Skan      typedef
155169691Skan      typename Allocator::template rebind<
156169691Skan	value_type>::other::const_reference
157169691Skan      const_reference;
158169691Skan
159169691Skan      typedef
160169691Skan      binary_heap_const_point_iterator_<
161169691Skan	value_type,
162169691Skan	entry,
163169691Skan	simple_value,
164169691Skan	Allocator>
165169691Skan      const_point_iterator;
166169691Skan
167169691Skan      typedef const_point_iterator point_iterator;
168169691Skan
169169691Skan      typedef
170169691Skan      binary_heap_const_iterator_<
171169691Skan	value_type,
172169691Skan	entry,
173169691Skan	simple_value,
174169691Skan	Allocator>
175169691Skan      const_iterator;
176169691Skan
177169691Skan      typedef const_iterator iterator;
178169691Skan
179169691Skan      typedef Cmp_Fn cmp_fn;
180169691Skan
181169691Skan      typedef Allocator allocator;
182169691Skan
183169691Skan    public:
184169691Skan
185169691Skan      binary_heap_();
186169691Skan
187169691Skan      binary_heap_(const Cmp_Fn& r_cmp_fn);
188169691Skan
189169691Skan      binary_heap_(const PB_DS_CLASS_C_DEC& other);
190169691Skan
191169691Skan      void
192169691Skan      swap(PB_DS_CLASS_C_DEC& other);
193169691Skan
194169691Skan      ~binary_heap_();
195169691Skan
196169691Skan      inline bool
197169691Skan      empty() const;
198169691Skan
199169691Skan      inline size_type
200169691Skan      size() const;
201169691Skan
202169691Skan      inline size_type
203169691Skan      max_size() const;
204169691Skan
205169691Skan      Cmp_Fn&
206169691Skan      get_cmp_fn();
207169691Skan
208169691Skan      const Cmp_Fn&
209169691Skan      get_cmp_fn() const;
210169691Skan
211169691Skan      inline point_iterator
212169691Skan      push(const_reference r_val);
213169691Skan
214169691Skan      void
215169691Skan      modify(point_iterator it, const_reference r_new_val);
216169691Skan
217169691Skan      inline const_reference
218169691Skan      top() const;
219169691Skan
220169691Skan      inline void
221169691Skan      pop();
222169691Skan
223169691Skan      inline void
224169691Skan      erase(point_iterator it);
225169691Skan
226169691Skan      template<typename Pred>
227169691Skan      typename PB_DS_CLASS_C_DEC::size_type
228169691Skan      erase_if(Pred pred);
229169691Skan
230169691Skan      inline static void
231169691Skan      erase_at(entry_pointer a_entries, size_type size, false_type);
232169691Skan
233169691Skan      inline static void
234169691Skan      erase_at(entry_pointer a_entries, size_type size, true_type);
235169691Skan
236169691Skan      inline iterator
237169691Skan      begin();
238169691Skan
239169691Skan      inline const_iterator
240169691Skan      begin() const;
241169691Skan
242169691Skan      inline iterator
243169691Skan      end();
244169691Skan
245169691Skan      inline const_iterator
246169691Skan      end() const;
247169691Skan
248169691Skan      void
249169691Skan      clear();
250169691Skan
251169691Skan      template<typename Pred>
252169691Skan      void
253169691Skan      split(Pred pred, PB_DS_CLASS_C_DEC& other);
254169691Skan
255169691Skan      void
256169691Skan      join(PB_DS_CLASS_C_DEC& other);
257169691Skan
258169691Skan#ifdef PB_DS_BINARY_HEAP_TRACE_
259169691Skan      void
260169691Skan      trace() const;
261169691Skan#endif
262169691Skan
263169691Skan    protected:
264169691Skan
265169691Skan      template<typename It>
266169691Skan      void
267169691Skan      copy_from_range(It first_it, It last_it);
268169691Skan
269169691Skan    private:
270169691Skan
271169691Skan      void
272169691Skan      value_swap(PB_DS_CLASS_C_DEC& other);
273169691Skan
274169691Skan      inline void
275169691Skan      insert_value(const_reference r_val, false_type);
276169691Skan
277169691Skan      inline void
278169691Skan      insert_value(value_type val, true_type);
279169691Skan
280169691Skan      inline void
281169691Skan      insert_entry(entry e);
282169691Skan
283169691Skan      inline void
284169691Skan      resize_for_insert_if_needed();
285169691Skan
286169691Skan      inline void
287169691Skan      swap_value_imp(entry_pointer p_e, value_type new_val, true_type);
288169691Skan
289169691Skan      inline void
290169691Skan      swap_value_imp(entry_pointer p_e, const_reference r_new_val, false_type);
291169691Skan
292169691Skan      void
293169691Skan      fix(entry_pointer p_e);
294169691Skan
295169691Skan      inline const_reference
296169691Skan      top_imp(true_type) const;
297169691Skan
298169691Skan      inline const_reference
299169691Skan      top_imp(false_type) const;
300169691Skan
301169691Skan      inline static size_type
302169691Skan      left_child(size_type i);
303169691Skan
304169691Skan      inline static size_type
305169691Skan      right_child(size_type i);
306169691Skan
307169691Skan      inline static size_type
308169691Skan      parent(size_type i);
309169691Skan
310169691Skan      inline void
311169691Skan      resize_for_erase_if_needed();
312169691Skan
313169691Skan      template<typename Pred>
314169691Skan      size_type
315169691Skan      partition(Pred pred);
316169691Skan
317169691Skan#ifdef _GLIBCXX_DEBUG
318169691Skan      void
319169691Skan      assert_valid() const;
320169691Skan#endif
321169691Skan
322169691Skan#ifdef PB_DS_BINARY_HEAP_TRACE_
323169691Skan      void
324169691Skan      trace_entry(const entry& r_e, false_type) const;
325169691Skan
326169691Skan      void
327169691Skan      trace_entry(const entry& r_e, true_type) const;
328169691Skan#endif
329169691Skan
330169691Skan    private:
331169691Skan      static entry_allocator s_entry_allocator;
332169691Skan
333169691Skan      static value_allocator s_value_allocator;
334169691Skan
335169691Skan      static no_throw_copies_t s_no_throw_copies_ind;
336169691Skan
337169691Skan      size_type m_size;
338169691Skan
339169691Skan      size_type m_actual_size;
340169691Skan
341169691Skan      entry_pointer m_a_entries;
342169691Skan    };
343169691Skan
344169691Skan#include <ext/pb_ds/detail/binary_heap_/insert_fn_imps.hpp>
345169691Skan#include <ext/pb_ds/detail/binary_heap_/constructors_destructor_fn_imps.hpp>
346169691Skan#include <ext/pb_ds/detail/binary_heap_/iterators_fn_imps.hpp>
347169691Skan#include <ext/pb_ds/detail/binary_heap_/debug_fn_imps.hpp>
348169691Skan#include <ext/pb_ds/detail/binary_heap_/trace_fn_imps.hpp>
349169691Skan#include <ext/pb_ds/detail/binary_heap_/erase_fn_imps.hpp>
350169691Skan#include <ext/pb_ds/detail/binary_heap_/info_fn_imps.hpp>
351169691Skan#include <ext/pb_ds/detail/binary_heap_/find_fn_imps.hpp>
352169691Skan#include <ext/pb_ds/detail/binary_heap_/split_join_fn_imps.hpp>
353169691Skan#include <ext/pb_ds/detail/binary_heap_/policy_access_fn_imps.hpp>
354169691Skan
355169691Skan#undef PB_DS_CLASS_C_DEC
356169691Skan#undef PB_DS_CLASS_T_DEC
357169691Skan#undef PB_DS_ENTRY_CMP_DEC
358169691Skan#undef PB_DS_RESIZE_POLICY_DEC
359169691Skan
360169691Skan  } // namespace detail
361169691Skan} // namespace pb_ds
362169691Skan
363169691Skan#endif
364