QuickRank  v2.0
QuickRank: A C++ suite of Learning to Rank algorithms
maxheap.h
Go to the documentation of this file.
1 /*
2  * QuickRank - A C++ suite of Learning to Rank algorithms
3  * Webpage: http://quickrank.isti.cnr.it/
4  * Contact: quickrank@isti.cnr.it
5  *
6  * Unless explicitly acquired and licensed from Licensor under another
7  * license, the contents of this file are subject to the Reciprocal Public
8  * License ("RPL") Version 1.5, or subsequent versions as allowed by the RPL,
9  * and You may not copy or use this file in either source code or executable
10  * form, except in compliance with the terms and conditions of the RPL.
11  *
12  * All software distributed under the RPL is provided strictly on an "AS
13  * IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, AND
14  * LICENSOR HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
15  * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
16  * PURPOSE, QUIET ENJOYMENT, OR NON-INFRINGEMENT. See the RPL for specific
17  * language governing rights and limitations under the RPL.
18  *
19  * Contributor:
20  * HPC. Laboratory - ISTI - CNR - http://hpc.isti.cnr.it/
21  */
22 #pragma once
23 
24 #include <cstdlib>
25 #include <cfloat>
26 
27 /*! \class mahheap
28  * \brief max-heap implementation with key of type float
29  */
30 template<typename val_t>
31 class MaxHeap {
32  public:
33  /** \brief default constructor
34  * @param initsize set the initial size of the data structure if available
35  */
36  MaxHeap(size_t initsize = 0)
37  : maxsize(initsize + 2) {
38  arr = (item *) malloc(sizeof(item) * maxsize);
39  arrsize = 0, arr[0] = item(DBL_MAX);
40  }
42  free(arr);
43  }
44  /** \brief return true if heap is empty
45  */
46  bool is_notempty() const {
47  return arrsize != 0;
48  }
49  /** \brief return numebr of items stored in the heap
50  */
51  size_t get_size() const {
52  return arrsize;
53  }
54  /** \brief push a new element in the heap and resize the data structure if it is full
55  * @param key ordering key of the new element
56  * @param val value of the new element
57  */
58  void push(const double &key, const val_t &val) {
59  if (++arrsize == maxsize) {
60  maxsize = 2 * maxsize + 1;
61  arr = (item *) realloc(arr, sizeof(item) * maxsize);
62  }
63  size_t p = arrsize;
64  while (key > arr[p >> 1].key) {
65  arr[p] = arr[p >> 1];
66  p >>= 1;
67  }
68  arr[p] = item(key, val);
69  }
70  /** \brief remove the element on the top of the heap, i.e. the element with max key value
71  */
72  void pop() {
73  const item &last = arr[arrsize--];
74  size_t child, p = 1;
75  while (p << 1 <= arrsize) {
76  child = p << 1;
77  if (child < arrsize && arr[child + 1].key > arr[child].key)
78  ++child;
79  if (last.key < arr[child].key)
80  arr[p] = arr[child];
81  else
82  break;
83  p = child;
84  }
85  arr[p] = last;
86  }
87  /** \brief ref to the top element
88  */
89  val_t &top() const {
90  return arr[1].val;
91  }
92  protected:
93  struct item {
94  item(double key)
95  : key(key) {
96  }
97  item(double key, val_t val)
98  : key(key),
99  val(val) {
100  }
101  double key;
102  val_t val;
103  };
105  size_t arrsize, maxsize;
106 };
val_t & top() const
ref to the top element
Definition: maxheap.h:89
size_t arrsize
Definition: maxheap.h:105
val_t val
Definition: maxheap.h:102
MaxHeap(size_t initsize=0)
default constructor
Definition: maxheap.h:36
item * arr
Definition: maxheap.h:104
item(double key)
Definition: maxheap.h:94
Definition: maxheap.h:31
~MaxHeap()
Definition: maxheap.h:41
size_t maxsize
Definition: maxheap.h:105
Definition: maxheap.h:93
void pop()
remove the element on the top of the heap, i.e.
Definition: maxheap.h:72
item(double key, val_t val)
Definition: maxheap.h:97
size_t get_size() const
return numebr of items stored in the heap
Definition: maxheap.h:51
double key
Definition: maxheap.h:101
bool is_notempty() const
return true if heap is empty
Definition: maxheap.h:46
void push(const double &key, const val_t &val)
push a new element in the heap and resize the data structure if it is full
Definition: maxheap.h:58