alglib::heap::binary_heap< elt_t, key_t > Class Template Reference

#include <binary_heap.h>

Inheritance diagram for alglib::heap::binary_heap< elt_t, key_t >:
alglib::heap::min_heap< elt_t, key_t >

Public Types

typedef elt_t elt_type
 
typedef key_t key_type
 

Public Member Functions

 binary_heap ()
 
template<typename InputIter >
 binary_heap (InputIter elt_first, InputIter elt_last, InputIter key_first)
 
void insert (const elt_t &elt, const key_t &key)
 
const elt_t & get_min_elt () const
 
const key_t & get_min_key () const
 
void delete_min ()
 
void replace (const elt_t &elt, const key_t &key)
 
int size () const override
 
bool empty () const override
 
void update_key (const elt_t &elt, const key_t &key)
 

Member Typedef Documentation

template<typename elt_t, typename key_t>
typedef elt_t alglib::heap::binary_heap< elt_t, key_t >::elt_type
template<typename elt_t, typename key_t>
typedef key_t alglib::heap::binary_heap< elt_t, key_t >::key_type

Constructor & Destructor Documentation

template<typename elt_t , typename key_t >
alglib::heap::binary_heap< elt_t, key_t >::binary_heap ( )
102  {
103  __sz = 0;
104 }
template<typename elt_t , typename key_t >
template<typename InputIter >
alglib::heap::binary_heap< elt_t, key_t >::binary_heap ( InputIter  elt_first,
InputIter  elt_last,
InputIter  key_first 
)
explicit
110  {
111  __sz = 0;
112  while (elt_first != elt_last) {
113  auto loc = make_entry(*elt_first);
114  heaparr[__sz++] = node_t {*key_first, loc};
115  ++elt_first;
116  }
117 
118  // Heapify the __stash
119  for (int i = __sz / 2; i >= 0; i--)
120  __sift_down(i);
121 }

Member Function Documentation

template<typename elt_t , typename key_t >
void alglib::heap::binary_heap< elt_t, key_t >::delete_min ( )
virtual

Implements alglib::heap::min_heap< elt_t, key_t >.

145  {
146  nodeof.erase(heaparr[0].elt_it);
147  std::swap(heaparr[0], heaparr[__sz - 1]);
148  --__sz;
149  __sift_down(0);
150 }
template<typename elt_t, typename key_t>
bool alglib::heap::binary_heap< elt_t, key_t >::empty ( ) const
inlineoverridevirtual

Implements alglib::heap::min_heap< elt_t, key_t >.

59 { return __sz == 0; }
template<typename elt_t , typename key_t >
const elt_t & alglib::heap::binary_heap< elt_t, key_t >::get_min_elt ( ) const
virtual

Implements alglib::heap::min_heap< elt_t, key_t >.

134  {
135  return (heaparr[0].elt_it)->first;
136 }
template<typename elt_t , typename key_t >
const key_t & alglib::heap::binary_heap< elt_t, key_t >::get_min_key ( ) const
virtual

Implements alglib::heap::min_heap< elt_t, key_t >.

140  {
141  return heaparr[0].key;
142 }
template<typename elt_t, typename key_t>
void alglib::heap::binary_heap< elt_t, key_t >::insert ( const elt_t &  elt,
const key_t &  key 
)
virtual

Implements alglib::heap::min_heap< elt_t, key_t >.

125  {
126  auto loc = make_entry(elt);
127  heaparr.push_back(node_t {key, loc});
128  __sift_up(__sz);
129  ++__sz;
130 }
template<typename elt_t, typename key_t>
void alglib::heap::binary_heap< elt_t, key_t >::replace ( const elt_t &  elt,
const key_t &  key 
)
virtual

Implements alglib::heap::min_heap< elt_t, key_t >.

154  {
155  if (__sz == 0)
156  throw std::underflow_error("Empty heap");
157  auto loc = make_entry(elt);
158  nodeof.erase(heaparr[0].elt_it); // Clean up the elt from nodeof map
159  heaparr[0] = node_t {key, loc};
160  __sift_down(0);
161 }
template<typename elt_t, typename key_t>
int alglib::heap::binary_heap< elt_t, key_t >::size ( ) const
inlineoverridevirtual

Implements alglib::heap::min_heap< elt_t, key_t >.

58 { return __sz; }
template<typename elt_t, typename key_t>
void alglib::heap::binary_heap< elt_t, key_t >::update_key ( const elt_t &  elt,
const key_t &  key 
)
virtual

Implements alglib::heap::min_heap< elt_t, key_t >.

165  {
166  if (nodeof.find(elt) == nodeof.end())
167  throw std::domain_error("The element doesn't exist");
168  int index_in_heap = nodeof[elt];
169  heaparr[index_in_heap] = node_t {key, heaparr[index_in_heap].elt_it};
170  __sift_up(index_in_heap);
171  __sift_down(index_in_heap);
172 }

The documentation for this class was generated from the following file: