Bug 1862332 [wpt PR 42877] - WebKit export of https://bugs.webkit.org/show_bug.cgi...
[gecko.git] / gfx / harfbuzz / src / hb-priority-queue.hh
blob9b962a29d9bbb1968fda8587fd9d4256278d2172
1 /*
2 * Copyright © 2020 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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.
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.
24 * Google Author(s): Garret Rieger
27 #ifndef HB_PRIORITY_QUEUE_HH
28 #define HB_PRIORITY_QUEUE_HH
30 #include "hb.hh"
31 #include "hb-vector.hh"
34 * hb_priority_queue_t
36 * Priority queue implemented as a binary heap. Supports extract minimum
37 * and insert operations.
39 * The priority queue is implemented as a binary heap, which is a complete
40 * binary tree. The root of the tree is the minimum element. The heap
41 * property is that the priority of a node is less than or equal to the
42 * priority of its children. The heap is stored in an array, with the
43 * children of node i stored at indices 2i + 1 and 2i + 2.
45 template <typename K>
46 struct hb_priority_queue_t
48 private:
49 typedef hb_pair_t<K, unsigned> item_t;
50 hb_vector_t<item_t> heap;
52 public:
54 void reset () { heap.resize (0); }
56 bool in_error () const { return heap.in_error (); }
58 bool alloc (unsigned size)
59 { return heap.alloc (size); }
61 #ifndef HB_OPTIMIZE_SIZE
62 HB_ALWAYS_INLINE
63 #endif
64 void insert (K priority, unsigned value)
66 heap.push (item_t (priority, value));
67 if (unlikely (heap.in_error ())) return;
68 bubble_up (heap.length - 1);
71 #ifndef HB_OPTIMIZE_SIZE
72 HB_ALWAYS_INLINE
73 #endif
74 item_t pop_minimum ()
76 assert (!is_empty ());
78 item_t result = heap.arrayZ[0];
80 heap.arrayZ[0] = heap.arrayZ[heap.length - 1];
81 heap.resize (heap.length - 1);
83 if (!is_empty ())
84 bubble_down (0);
86 return result;
89 const item_t& minimum ()
91 return heap[0];
94 bool is_empty () const { return heap.length == 0; }
95 explicit operator bool () const { return !is_empty (); }
96 unsigned int get_population () const { return heap.length; }
98 /* Sink interface. */
99 hb_priority_queue_t& operator << (item_t item)
100 { insert (item.first, item.second); return *this; }
102 private:
104 static constexpr unsigned parent (unsigned index)
106 return (index - 1) / 2;
109 static constexpr unsigned left_child (unsigned index)
111 return 2 * index + 1;
114 static constexpr unsigned right_child (unsigned index)
116 return 2 * index + 2;
119 HB_ALWAYS_INLINE
120 void bubble_down (unsigned index)
122 repeat:
123 assert (index < heap.length);
125 unsigned left = left_child (index);
126 unsigned right = right_child (index);
128 bool has_left = left < heap.length;
129 if (!has_left)
130 // If there's no left, then there's also no right.
131 return;
133 bool has_right = right < heap.length;
134 if (heap.arrayZ[index].first <= heap.arrayZ[left].first
135 && (!has_right || heap.arrayZ[index].first <= heap.arrayZ[right].first))
136 return;
138 unsigned child;
139 if (!has_right || heap.arrayZ[left].first < heap.arrayZ[right].first)
140 child = left;
141 else
142 child = right;
144 swap (index, child);
145 index = child;
146 goto repeat;
149 HB_ALWAYS_INLINE
150 void bubble_up (unsigned index)
152 repeat:
153 assert (index < heap.length);
155 if (index == 0) return;
157 unsigned parent_index = parent (index);
158 if (heap.arrayZ[parent_index].first <= heap.arrayZ[index].first)
159 return;
161 swap (index, parent_index);
162 index = parent_index;
163 goto repeat;
166 void swap (unsigned a, unsigned b)
168 assert (a < heap.length);
169 assert (b < heap.length);
170 hb_swap (heap.arrayZ[a], heap.arrayZ[b]);
174 #endif /* HB_PRIORITY_QUEUE_HH */