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 left_child_next_sibling_heap_.hpp
44169691Skan * Contains an implementation class for a basic heap.
45169691Skan */
46169691Skan
47169691Skan#ifndef PB_DS_LEFT_CHILD_NEXT_SIBLING_HEAP_HPP
48169691Skan#define PB_DS_LEFT_CHILD_NEXT_SIBLING_HEAP_HPP
49169691Skan
50169691Skan/*
51169691Skan * Based on CLRS.
52169691Skan */
53169691Skan
54169691Skan#include <iterator>
55169691Skan#include <ext/pb_ds/detail/cond_dealtor.hpp>
56169691Skan#include <ext/pb_ds/detail/type_utils.hpp>
57169691Skan#include <ext/pb_ds/detail/left_child_next_sibling_heap_/node.hpp>
58169691Skan#include <ext/pb_ds/detail/left_child_next_sibling_heap_/const_point_iterator.hpp>
59169691Skan#include <ext/pb_ds/detail/left_child_next_sibling_heap_/const_iterator.hpp>
60169691Skan#ifdef PB_DS_LC_NS_HEAP_TRACE_
61169691Skan#include <iostream>
62169691Skan#endif
63169691Skan#include <debug/debug.h>
64169691Skan
65169691Skannamespace pb_ds
66169691Skan{
67169691Skan  namespace detail
68169691Skan  {
69169691Skan
70169691Skan#ifdef _GLIBCXX_DEBUG
71169691Skan#define PB_DS_CLASS_T_DEC						\
72169691Skan    template<								\
73169691Skan						typename Value_Type,	\
74169691Skan						class Cmp_Fn,		\
75169691Skan						typename Node_Metadata,	\
76169691Skan						class Allocator,	\
77169691Skan						bool Single_Link_Roots>
78169691Skan#else
79169691Skan#define PB_DS_CLASS_T_DEC						\
80169691Skan    template<								\
81169691Skan						typename Value_Type,	\
82169691Skan						class Cmp_Fn,		\
83169691Skan						typename Node_Metadata,	\
84169691Skan						class Allocator>
85169691Skan#endif
86169691Skan
87169691Skan#ifdef _GLIBCXX_DEBUG
88169691Skan#define PB_DS_CLASS_C_DEC						\
89169691Skan    left_child_next_sibling_heap_<					\
90169691Skan							Value_Type,	\
91169691Skan							Cmp_Fn,		\
92169691Skan							Node_Metadata,	\
93169691Skan							Allocator,	\
94169691Skan							Single_Link_Roots>
95169691Skan#else
96169691Skan#define PB_DS_CLASS_C_DEC						\
97169691Skan    left_child_next_sibling_heap_<					\
98169691Skan							Value_Type,	\
99169691Skan							Cmp_Fn,		\
100169691Skan							Node_Metadata,	\
101169691Skan							Allocator>
102169691Skan#endif
103169691Skan
104169691Skan    /**
105169691Skan     * class description = "Base class for some types of h3ap$">
106169691Skan     **/
107169691Skan#ifdef _GLIBCXX_DEBUG
108169691Skan    template<typename Value_Type,
109169691Skan	     class Cmp_Fn,
110169691Skan	     typename Node_Metadata,
111169691Skan	     class Allocator,
112169691Skan	     bool Single_Link_Roots>
113169691Skan#else
114169691Skan    template<typename Value_Type,
115169691Skan	     class Cmp_Fn,
116169691Skan	     typename Node_Metadata,
117169691Skan	     class Allocator>
118169691Skan#endif
119169691Skan    class left_child_next_sibling_heap_ : public Cmp_Fn
120169691Skan    {
121169691Skan
122169691Skan    protected:
123169691Skan      typedef
124169691Skan      typename Allocator::template rebind<
125169691Skan      left_child_next_sibling_heap_node_<
126169691Skan      Value_Type,
127169691Skan      Node_Metadata,
128169691Skan      Allocator> >::other
129169691Skan      node_allocator;
130169691Skan
131169691Skan      typedef typename node_allocator::value_type node;
132169691Skan
133169691Skan      typedef typename node_allocator::pointer node_pointer;
134169691Skan
135169691Skan      typedef typename node_allocator::const_pointer const_node_pointer;
136169691Skan
137169691Skan      typedef Node_Metadata node_metadata;
138169691Skan
139169691Skan      typedef std::pair< node_pointer, node_pointer> node_pointer_pair;
140169691Skan
141169691Skan    private:
142169691Skan      typedef cond_dealtor< node, Allocator> cond_dealtor_t;
143169691Skan
144169691Skan      enum
145169691Skan	{
146169691Skan	  simple_value = is_simple<Value_Type>::value
147169691Skan	};
148169691Skan
149169691Skan      typedef integral_constant<int, simple_value> no_throw_copies_t;
150169691Skan
151169691Skan    public:
152169691Skan
153169691Skan      typedef typename Allocator::size_type size_type;
154169691Skan
155169691Skan      typedef typename Allocator::difference_type difference_type;
156169691Skan
157169691Skan      typedef Value_Type value_type;
158169691Skan
159169691Skan      typedef
160169691Skan      typename Allocator::template rebind<
161169691Skan	value_type>::other::pointer
162169691Skan      pointer;
163169691Skan
164169691Skan      typedef
165169691Skan      typename Allocator::template rebind<
166169691Skan	value_type>::other::const_pointer
167169691Skan      const_pointer;
168169691Skan
169169691Skan      typedef
170169691Skan      typename Allocator::template rebind<
171169691Skan	value_type>::other::reference
172169691Skan      reference;
173169691Skan
174169691Skan      typedef
175169691Skan      typename Allocator::template rebind<
176169691Skan	value_type>::other::const_reference
177169691Skan      const_reference;
178169691Skan
179169691Skan      typedef
180169691Skan      left_child_next_sibling_heap_node_const_point_iterator_<
181169691Skan	node,
182169691Skan	Allocator>
183169691Skan      const_point_iterator;
184169691Skan
185169691Skan      typedef const_point_iterator point_iterator;
186169691Skan
187169691Skan      typedef
188169691Skan      left_child_next_sibling_heap_const_iterator_<
189169691Skan	node,
190169691Skan	Allocator>
191169691Skan      const_iterator;
192169691Skan
193169691Skan      typedef const_iterator iterator;
194169691Skan
195169691Skan      typedef Cmp_Fn cmp_fn;
196169691Skan
197169691Skan      typedef Allocator allocator;
198169691Skan
199169691Skan    public:
200169691Skan
201169691Skan      left_child_next_sibling_heap_();
202169691Skan
203169691Skan      left_child_next_sibling_heap_(const Cmp_Fn& r_cmp_fn);
204169691Skan
205169691Skan      left_child_next_sibling_heap_(const PB_DS_CLASS_C_DEC& other);
206169691Skan
207169691Skan      void
208169691Skan      swap(PB_DS_CLASS_C_DEC& other);
209169691Skan
210169691Skan      ~left_child_next_sibling_heap_();
211169691Skan
212169691Skan      inline bool
213169691Skan      empty() const;
214169691Skan
215169691Skan      inline size_type
216169691Skan      size() const;
217169691Skan
218169691Skan      inline size_type
219169691Skan      max_size() const;
220169691Skan
221169691Skan      Cmp_Fn&
222169691Skan      get_cmp_fn();
223169691Skan
224169691Skan      const Cmp_Fn&
225169691Skan      get_cmp_fn() const;
226169691Skan
227169691Skan      inline iterator
228169691Skan      begin();
229169691Skan
230169691Skan      inline const_iterator
231169691Skan      begin() const;
232169691Skan
233169691Skan      inline iterator
234169691Skan      end();
235169691Skan
236169691Skan      inline const_iterator
237169691Skan      end() const;
238169691Skan
239169691Skan      void
240169691Skan      clear();
241169691Skan
242169691Skan#ifdef PB_DS_LC_NS_HEAP_TRACE_
243169691Skan      void
244169691Skan      trace() const;
245169691Skan#endif
246169691Skan
247169691Skan    protected:
248169691Skan
249169691Skan      inline node_pointer
250169691Skan      get_new_node_for_insert(const_reference r_val);
251169691Skan
252169691Skan      inline static void
253169691Skan      make_child_of(node_pointer p_nd, node_pointer p_new_parent);
254169691Skan
255169691Skan      void
256169691Skan      value_swap(PB_DS_CLASS_C_DEC& other);
257169691Skan
258169691Skan      inline static node_pointer
259169691Skan      parent(node_pointer p_nd);
260169691Skan
261169691Skan      inline void
262169691Skan      swap_with_parent(node_pointer p_nd, node_pointer p_parent);
263169691Skan
264169691Skan      void
265169691Skan      bubble_to_top(node_pointer p_nd);
266169691Skan
267169691Skan      inline void
268169691Skan      actual_erase_node(node_pointer p_nd);
269169691Skan
270169691Skan      void
271169691Skan      clear_imp(node_pointer p_nd);
272169691Skan
273169691Skan      void
274169691Skan      to_linked_list();
275169691Skan
276169691Skan      template<typename Pred>
277169691Skan      node_pointer
278169691Skan      prune(Pred pred);
279169691Skan
280169691Skan#ifdef _GLIBCXX_DEBUG
281169691Skan      void
282169691Skan      assert_valid() const;
283169691Skan
284169691Skan      void
285169691Skan      assert_node_consistent(const_node_pointer p_nd, bool single_link) const;
286169691Skan
287169691Skan      static size_type
288169691Skan      size_under_node(const_node_pointer p_nd);
289169691Skan
290169691Skan      static size_type
291169691Skan      degree(const_node_pointer p_nd);
292169691Skan#endif
293169691Skan
294169691Skan#ifdef PB_DS_LC_NS_HEAP_TRACE_
295169691Skan      static void
296169691Skan      trace_node(const_node_pointer, size_type level);
297169691Skan#endif
298169691Skan
299169691Skan    protected:
300169691Skan      node_pointer m_p_root;
301169691Skan
302169691Skan      size_type m_size;
303169691Skan
304169691Skan    private:
305169691Skan#ifdef _GLIBCXX_DEBUG
306169691Skan      void
307169691Skan      assert_iterators() const;
308169691Skan
309169691Skan      void
310169691Skan      assert_size() const;
311169691Skan
312169691Skan      static size_type
313169691Skan      size_from_node(const_node_pointer p_nd);
314169691Skan#endif
315169691Skan
316169691Skan      node_pointer
317169691Skan      recursive_copy_node(const_node_pointer p_nd);
318169691Skan
319169691Skan      inline node_pointer
320169691Skan      get_new_node_for_insert(const_reference r_val, false_type);
321169691Skan
322169691Skan      inline node_pointer
323169691Skan      get_new_node_for_insert(const_reference r_val, true_type);
324169691Skan
325169691Skan#ifdef PB_DS_LC_NS_HEAP_TRACE_
326169691Skan      template<typename Metadata_>
327169691Skan      static void
328169691Skan      trace_node_metadata(const_node_pointer p_nd, type_to_type<Metadata_>);
329169691Skan
330169691Skan      static void
331169691Skan      trace_node_metadata(const_node_pointer, type_to_type<null_left_child_next_sibling_heap_node_metadata>);
332169691Skan#endif
333169691Skan
334169691Skan    private:
335169691Skan      static node_allocator s_node_allocator;
336169691Skan
337169691Skan      static no_throw_copies_t s_no_throw_copies_ind;
338169691Skan    };
339169691Skan
340169691Skan#include <ext/pb_ds/detail/left_child_next_sibling_heap_/constructors_destructor_fn_imps.hpp>
341169691Skan#include <ext/pb_ds/detail/left_child_next_sibling_heap_/iterators_fn_imps.hpp>
342169691Skan#include <ext/pb_ds/detail/left_child_next_sibling_heap_/debug_fn_imps.hpp>
343169691Skan#include <ext/pb_ds/detail/left_child_next_sibling_heap_/trace_fn_imps.hpp>
344169691Skan#include <ext/pb_ds/detail/left_child_next_sibling_heap_/insert_fn_imps.hpp>
345169691Skan#include <ext/pb_ds/detail/left_child_next_sibling_heap_/erase_fn_imps.hpp>
346169691Skan#include <ext/pb_ds/detail/left_child_next_sibling_heap_/info_fn_imps.hpp>
347169691Skan#include <ext/pb_ds/detail/left_child_next_sibling_heap_/policy_access_fn_imps.hpp>
348169691Skan
349169691Skan#undef PB_DS_CLASS_C_DEC
350169691Skan#undef PB_DS_CLASS_T_DEC
351169691Skan
352169691Skan  } // namespace detail
353169691Skan} // namespace pb_ds
354169691Skan
355169691Skan#endif
356