blob: ac76b7d9556e350473856440afe9e98681821db7 [file] [log] [blame]
Garret Rieger5c4e0ff2020-11-04 16:08:01 -08001/*
2 * Copyright © 2020 Google, Inc.
3 *
4 * This is part of HarfBuzz, a text shaping library.
5 *
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
11 *
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16 * DAMAGE.
17 *
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23 *
24 * Google Author(s): Garret Rieger
25 */
26
27#ifndef HB_PRIORITY_QUEUE_HH
28#define HB_PRIORITY_QUEUE_HH
29
30#include "hb.hh"
31#include "hb-vector.hh"
32
33/*
34 * hb_priority_queue_t
35 *
36 * Priority queue implemented as a binary heap. Supports extract minimum
37 * and insert operations.
38 */
39struct hb_priority_queue_t
40{
Garret Rieger5c4e0ff2020-11-04 16:08:01 -080041 private:
Garret Riegerf561fa62021-03-18 11:13:47 -070042 typedef hb_pair_t<int64_t, unsigned> item_t;
Garret Rieger5c4e0ff2020-11-04 16:08:01 -080043 hb_vector_t<item_t> heap;
44
45 public:
Garret Rieger5c4e0ff2020-11-04 16:08:01 -080046
47 void reset () { heap.resize (0); }
48
49 bool in_error () const { return heap.in_error (); }
50
Garret Riegerf561fa62021-03-18 11:13:47 -070051 void insert (int64_t priority, unsigned value)
Garret Rieger5c4e0ff2020-11-04 16:08:01 -080052 {
Garret Riegerf561fa62021-03-18 11:13:47 -070053 heap.push (item_t (priority, value));
Garret Rieger73b83602022-05-19 22:59:51 +000054 if (unlikely (heap.in_error ())) return;
Garret Rieger5c4e0ff2020-11-04 16:08:01 -080055 bubble_up (heap.length - 1);
56 }
57
Garret Riegerf561fa62021-03-18 11:13:47 -070058 item_t pop_minimum ()
Garret Rieger5c4e0ff2020-11-04 16:08:01 -080059 {
Behdad Esfahbod39a424c2022-05-18 16:17:16 -060060 assert (!is_empty ());
Garret Rieger5c4e0ff2020-11-04 16:08:01 -080061
Behdad Esfahbod39a424c2022-05-18 16:17:16 -060062 item_t result = heap.arrayZ[0];
63
64 heap.arrayZ[0] = heap.arrayZ[heap.length - 1];
Garret Rieger5c4e0ff2020-11-04 16:08:01 -080065 heap.shrink (heap.length - 1);
Seigo Nonakac62d6f42023-03-01 19:52:57 +090066
67 if (!is_empty ())
68 bubble_down (0);
Garret Rieger5c4e0ff2020-11-04 16:08:01 -080069
70 return result;
71 }
72
73 const item_t& minimum ()
74 {
75 return heap[0];
76 }
77
78 bool is_empty () const { return heap.length == 0; }
Garret Riegerf561fa62021-03-18 11:13:47 -070079 explicit operator bool () const { return !is_empty (); }
80 unsigned int get_population () const { return heap.length; }
Garret Rieger5c4e0ff2020-11-04 16:08:01 -080081
82 /* Sink interface. */
83 hb_priority_queue_t& operator << (item_t item)
84 { insert (item.first, item.second); return *this; }
85
86 private:
87
Garret Riegerf561fa62021-03-18 11:13:47 -070088 static constexpr unsigned parent (unsigned index)
Garret Rieger5c4e0ff2020-11-04 16:08:01 -080089 {
90 return (index - 1) / 2;
91 }
92
Garret Riegerf561fa62021-03-18 11:13:47 -070093 static constexpr unsigned left_child (unsigned index)
Garret Rieger5c4e0ff2020-11-04 16:08:01 -080094 {
95 return 2 * index + 1;
96 }
97
Garret Riegerf561fa62021-03-18 11:13:47 -070098 static constexpr unsigned right_child (unsigned index)
Garret Rieger5c4e0ff2020-11-04 16:08:01 -080099 {
100 return 2 * index + 2;
101 }
102
103 void bubble_down (unsigned index)
104 {
Seigo Nonakac62d6f42023-03-01 19:52:57 +0900105 assert (index < heap.length);
Behdad Esfahbod39a424c2022-05-18 16:17:16 -0600106
Garret Rieger5c4e0ff2020-11-04 16:08:01 -0800107 unsigned left = left_child (index);
108 unsigned right = right_child (index);
109
110 bool has_left = left < heap.length;
111 if (!has_left)
112 // If there's no left, then there's also no right.
113 return;
114
115 bool has_right = right < heap.length;
Behdad Esfahbod39a424c2022-05-18 16:17:16 -0600116 if (heap.arrayZ[index].first <= heap.arrayZ[left].first
Seigo Nonakac62d6f42023-03-01 19:52:57 +0900117 && (!has_right || heap.arrayZ[index].first <= heap.arrayZ[right].first))
Garret Rieger5c4e0ff2020-11-04 16:08:01 -0800118 return;
119
Behdad Esfahbod39a424c2022-05-18 16:17:16 -0600120 if (!has_right || heap.arrayZ[left].first < heap.arrayZ[right].first)
Garret Rieger5c4e0ff2020-11-04 16:08:01 -0800121 {
122 swap (index, left);
123 bubble_down (left);
124 return;
125 }
126
127 swap (index, right);
128 bubble_down (right);
129 }
130
131 void bubble_up (unsigned index)
132 {
Seigo Nonakac62d6f42023-03-01 19:52:57 +0900133 assert (index < heap.length);
Behdad Esfahbod39a424c2022-05-18 16:17:16 -0600134
Garret Rieger5c4e0ff2020-11-04 16:08:01 -0800135 if (index == 0) return;
136
137 unsigned parent_index = parent (index);
Behdad Esfahbod39a424c2022-05-18 16:17:16 -0600138 if (heap.arrayZ[parent_index].first <= heap.arrayZ[index].first)
Garret Rieger5c4e0ff2020-11-04 16:08:01 -0800139 return;
140
141 swap (index, parent_index);
142 bubble_up (parent_index);
143 }
144
145 void swap (unsigned a, unsigned b)
146 {
Seigo Nonakac62d6f42023-03-01 19:52:57 +0900147 assert (a < heap.length);
148 assert (b < heap.length);
Behdad Esfahbod93086592022-05-18 16:14:25 -0600149 hb_swap (heap.arrayZ[a], heap.arrayZ[b]);
Garret Rieger5c4e0ff2020-11-04 16:08:01 -0800150 }
151};
152
153#endif /* HB_PRIORITY_QUEUE_HH */