Awali
Another Weighted Automata library
heap.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef VCRS_MISC_HEAP_HH
18 #define VCRS_MISC_HEAP_HH
19 
20 #include<vector>
21 #include<tuple>
22 
23 namespace awali { namespace utils {
24 
25  template < typename T, typename P>
26  struct min_heap {
27  public :
28  using value_type = T;
29  using priority_type = P;
30  using claim_ticket_type = size_t;
31  using store_type = std::tuple<T,P,size_t>;
32 
33  min_heap(size_t capacity_min) {
34  values_.reserve(capacity_min);
35  tickets_.reserve(capacity_min);
36  }
37 
38  size_t size() const {
39  return values_.size();
40  }
41 
42  size_t empty() const {
43  return values_.empty();
44  }
45 
47  size_t pos = values_.size();
48  claim_ticket_type ticket = tickets_.size();
49  values_.emplace_back(std::make_tuple(val, p, ticket));
50  tickets_.emplace_back(pos);
51  return ticket;
52  }
53 
54  value_type front() const {
55  return std::get<0>(values_.front());
56  }
57 
59  value_type r=front();
60  swap(0,values_.size()-1);
61  values_.pop_back();
62  top_down(0);
63  return r;
64  }
65 
67  claim_ticket_type ticket=emplace(val,p);
68  bottom_up(values_.size()-1);
69  return ticket;
70  }
71 
72  void heapify() {
73  if(values_.size()<2)
74  return;
75  for(size_t i=values_.size()/2-1, l=i+1; i<l; --i)
76  top_down(i);
77  }
78 
80  unsigned i = tickets_[t];
81  store_type& s=values_[i];
82  if(p < std::get<1>(s)) {
83  std::get<1>(s)=p;
84  bottom_up(i);
85  }
86  else {
87  std::get<1>(s)=p;
88  top_down(i);
89  }
90  }
91 
92  void print() {
93  for(size_t i=0;i<size();++i)
94  std::cout << std::get<0>(values_[i])
95  << " p: " << std::get<1>(values_[i])
96  << " t: " << std::get<2>(values_[i]) << std::endl;
97  }
98 
99  private :
100 
101  void bottom_up(size_t i) {
102  if(i==0)
103  return;
104  unsigned j=(i-1)/2;
105  if(std::get<1>(values_[i])<std::get<1>(values_[j])) {
106  swap(i,j);
107  bottom_up(j);
108  }
109  }
110 
111  void top_down(size_t i) {
112  if(2*i+2 > values_.size())
113  return;
114  unsigned j;
115  if(2*i+2 == values_.size())
116  j=2*i+1;
117  else
118  j=std::get<1>(values_[2*i+1])<std::get<1>(values_[2*i+2])?2*i+1:2*i+2;
119  if(std::get<1>(values_[j])<std::get<1>(values_[i])) {
120  swap(i,j);
121  top_down(j);
122  }
123  }
124 
125  void swap(size_t i, size_t j) {
126  store_type tmp=values_[i];
127  values_[i]=values_[j];values_[j]=tmp;
128  tickets_[std::get<2>(values_[i])]=i;
129  tickets_[std::get<2>(values_[j])]=j;
130  }
131 
132  std::vector<store_type> values_;
133  std::vector<size_t> tickets_;
134 
135  };
136 
137 
138 
139 
140 }}//end of ns awali::stc
141 
142 #endif
Main namespace of Awali.
Definition: ato.hh:22
Definition: heap.hh:26
value_type pop()
Definition: heap.hh:58
void update(claim_ticket_type t, const priority_type &p)
Definition: heap.hh:79
void print()
Definition: heap.hh:92
claim_ticket_type emplace(const value_type &val, const priority_type &p)
Definition: heap.hh:46
value_type front() const
Definition: heap.hh:54
size_t claim_ticket_type
Definition: heap.hh:30
min_heap(size_t capacity_min)
Definition: heap.hh:33
P priority_type
Definition: heap.hh:29
void heapify()
Definition: heap.hh:72
size_t size() const
Definition: heap.hh:38
std::tuple< T, P, size_t > store_type
Definition: heap.hh:31
T value_type
Definition: heap.hh:28
size_t empty() const
Definition: heap.hh:42
claim_ticket_type push(const value_type &val, const priority_type &p)
Definition: heap.hh:66