# Template Class `d_heap<T, Comp, d>`

# Synopsis

    #include "boost/d_heap.hpp"

    template <typename T>
    class boost::d_heap_base
    {
    public:
      class const_iterator
      {
      public:
        const_iterator();

        T const&        operator* () const;
        T const*        operator-> () const;
        const_iterator& operator++ ();
        const_iterator  operator++ (int);

        bool operator== (const_iterator const& it) const;
        bool operator!= (const_iterator const& it) const;
      };

      d_heap_base();
      const_reference top() const;
      bool            empty() const;
      size_type       size() const;
      const_iterator  begin() const;
      const_iterator  end() const;

    protected:
      ~d_heap_base();
    };

    template <typename T, typename Comp = std::less<T>, int d = 2>
    class boost::d_heap: public boost::d_heap_base<T>
    {
    public:
      typedef T                                   value_type;
      typedef T&                                  reference;
      typedef T const&                            const_reference;
      typedef typename d_heap<T>::const_iterator  const_iterator;
      typedef typename std::list<node>::size_type size_type;
      typedef Comp                                compare_type;

      explicit d_heap(Comp const& comp = Comp());

      pointer push(const_reference val);
      void    pop();
      void    remove(pointer ptr);

      template <typename K> void change_top(K const& val);
      template <typename K> void change(pointer ptr, K const& val
      template <typename K> void decrease(pointer ptr, K const& val);
      template <typename K> void increase(pointer ptr, K const& val);
    };

# Description

The class `d_heap<T, Comp, d>` represents the heap as a balanced d-ary tree. Normally `d == 2` is used in which case a binary tree is used to represent the heap. Internally, the tree is represented as an array and the navigation in the tree is made using simple index calculations. The type of objects stored in the heap is `T` which are compared using the comparator type `Comp`. During the heap manipulations the invariant is temporarily violated and then restored by swapping the violating node appropriately with its parent or its largest child depending on the whether the node has to travel up or down, respectively. Note, that the swapped objects are just pointers to the actual elements. Thus, even if copying and/or assigning objects of type `T` is a relatively expensive operation, the swaps performed to maintain the invariant are not.

The class `d_heap_base<T>` is used to factor out all portions of the code which are independent from the compare type and the number of child nodes. This class is intended to be used only by `d_heap<T, Comp, d>` and is not intended to be used as generally useful base class. Of course, you can use all public members defined for `d_heap_base<T>` on `d_heap<T, Comp, d>` objects.

For a description of the methods of `d_heap<T, Comp, d>` please refer to the general description on the page describing heaps and the page describing the common methods.

# See Also

heap(3) heap-common(3)

Copyright © 1999 Dietmar Kühl (dietmar.kuehl@claas-solutions.de)
Claas Solutions GmbH