1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2015 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#ifndef SkTDPQueue_DEFINED
9cb93a386Sopenharmony_ci#define SkTDPQueue_DEFINED
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ci#include "include/private/SkTDArray.h"
12cb93a386Sopenharmony_ci#include "src/core/SkTSort.h"
13cb93a386Sopenharmony_ci
14cb93a386Sopenharmony_ci#include <utility>
15cb93a386Sopenharmony_ci
16cb93a386Sopenharmony_ci/**
17cb93a386Sopenharmony_ci * This class implements a priority queue. T is the type of the elements in the queue. LESS is a
18cb93a386Sopenharmony_ci * function that compares two Ts and returns true if the first is higher priority than the second.
19cb93a386Sopenharmony_ci *
20cb93a386Sopenharmony_ci * Optionally objects may know their index into the priority queue. The queue will update the index
21cb93a386Sopenharmony_ci * as the objects move through the queue. This is enabled by using a non-nullptr function for INDEX.
22cb93a386Sopenharmony_ci * When an INDEX function is provided random deletes from the queue are allowed using remove().
23cb93a386Sopenharmony_ci * Additionally, the * priority is allowed to change as long as priorityDidChange() is called
24cb93a386Sopenharmony_ci * afterwards. In debug builds the index will be set to -1 before an element is removed from the
25cb93a386Sopenharmony_ci * queue.
26cb93a386Sopenharmony_ci */
27cb93a386Sopenharmony_citemplate <typename T,
28cb93a386Sopenharmony_ci          bool (*LESS)(const T&, const T&),
29cb93a386Sopenharmony_ci          int* (*INDEX)(const T&) = (int* (*)(const T&))nullptr>
30cb93a386Sopenharmony_ciclass SkTDPQueue {
31cb93a386Sopenharmony_cipublic:
32cb93a386Sopenharmony_ci    SkTDPQueue() {}
33cb93a386Sopenharmony_ci    SkTDPQueue(int reserve) { fArray.setReserve(reserve); }
34cb93a386Sopenharmony_ci
35cb93a386Sopenharmony_ci    SkTDPQueue(SkTDPQueue&&) = default;
36cb93a386Sopenharmony_ci    SkTDPQueue& operator =(SkTDPQueue&&) = default;
37cb93a386Sopenharmony_ci
38cb93a386Sopenharmony_ci    SkTDPQueue(const SkTDPQueue&) = delete;
39cb93a386Sopenharmony_ci    SkTDPQueue& operator=(const SkTDPQueue&) = delete;
40cb93a386Sopenharmony_ci
41cb93a386Sopenharmony_ci    /** Number of items in the queue. */
42cb93a386Sopenharmony_ci    int count() const { return fArray.count(); }
43cb93a386Sopenharmony_ci
44cb93a386Sopenharmony_ci    /** Gets the next item in the queue without popping it. */
45cb93a386Sopenharmony_ci    const T& peek() const { return fArray[0]; }
46cb93a386Sopenharmony_ci    T& peek() { return fArray[0]; }
47cb93a386Sopenharmony_ci
48cb93a386Sopenharmony_ci    /** Removes the next item. */
49cb93a386Sopenharmony_ci    void pop() {
50cb93a386Sopenharmony_ci        this->validate();
51cb93a386Sopenharmony_ci        SkDEBUGCODE(if (SkToBool(INDEX)) { *INDEX(fArray[0]) = -1; })
52cb93a386Sopenharmony_ci        if (1 == fArray.count()) {
53cb93a386Sopenharmony_ci            fArray.pop();
54cb93a386Sopenharmony_ci            return;
55cb93a386Sopenharmony_ci        }
56cb93a386Sopenharmony_ci
57cb93a386Sopenharmony_ci        fArray[0] = fArray[fArray.count() - 1];
58cb93a386Sopenharmony_ci        this->setIndex(0);
59cb93a386Sopenharmony_ci        fArray.pop();
60cb93a386Sopenharmony_ci        this->percolateDownIfNecessary(0);
61cb93a386Sopenharmony_ci
62cb93a386Sopenharmony_ci        this->validate();
63cb93a386Sopenharmony_ci    }
64cb93a386Sopenharmony_ci
65cb93a386Sopenharmony_ci    /** Inserts a new item in the queue based on its priority. */
66cb93a386Sopenharmony_ci    void insert(T entry) {
67cb93a386Sopenharmony_ci        this->validate();
68cb93a386Sopenharmony_ci        int index = fArray.count();
69cb93a386Sopenharmony_ci        *fArray.append() = entry;
70cb93a386Sopenharmony_ci        this->setIndex(fArray.count() - 1);
71cb93a386Sopenharmony_ci        this->percolateUpIfNecessary(index);
72cb93a386Sopenharmony_ci        this->validate();
73cb93a386Sopenharmony_ci    }
74cb93a386Sopenharmony_ci
75cb93a386Sopenharmony_ci    /** Random access removal. This requires that the INDEX function is non-nullptr. */
76cb93a386Sopenharmony_ci    void remove(T entry) {
77cb93a386Sopenharmony_ci        SkASSERT(nullptr != INDEX);
78cb93a386Sopenharmony_ci        int index = *INDEX(entry);
79cb93a386Sopenharmony_ci        SkASSERT(index >= 0 && index < fArray.count());
80cb93a386Sopenharmony_ci        this->validate();
81cb93a386Sopenharmony_ci        SkDEBUGCODE(*INDEX(fArray[index]) = -1;)
82cb93a386Sopenharmony_ci        if (index == fArray.count() - 1) {
83cb93a386Sopenharmony_ci            fArray.pop();
84cb93a386Sopenharmony_ci            return;
85cb93a386Sopenharmony_ci        }
86cb93a386Sopenharmony_ci        fArray[index] = fArray[fArray.count() - 1];
87cb93a386Sopenharmony_ci        fArray.pop();
88cb93a386Sopenharmony_ci        this->setIndex(index);
89cb93a386Sopenharmony_ci        this->percolateUpOrDown(index);
90cb93a386Sopenharmony_ci        this->validate();
91cb93a386Sopenharmony_ci    }
92cb93a386Sopenharmony_ci
93cb93a386Sopenharmony_ci    /** Notification that the priority of an entry has changed. This must be called after an
94cb93a386Sopenharmony_ci        item's priority is changed to maintain correct ordering. Changing the priority is only
95cb93a386Sopenharmony_ci        allowed if an INDEX function is provided. */
96cb93a386Sopenharmony_ci    void priorityDidChange(T entry) {
97cb93a386Sopenharmony_ci        SkASSERT(nullptr != INDEX);
98cb93a386Sopenharmony_ci        int index = *INDEX(entry);
99cb93a386Sopenharmony_ci        SkASSERT(index >= 0 && index < fArray.count());
100cb93a386Sopenharmony_ci        this->validate(index);
101cb93a386Sopenharmony_ci        this->percolateUpOrDown(index);
102cb93a386Sopenharmony_ci        this->validate();
103cb93a386Sopenharmony_ci    }
104cb93a386Sopenharmony_ci
105cb93a386Sopenharmony_ci    /** Gets the item at index i in the priority queue (for i < this->count()). at(0) is equivalent
106cb93a386Sopenharmony_ci        to peek(). Otherwise, there is no guarantee about ordering of elements in the queue. */
107cb93a386Sopenharmony_ci    T at(int i) const { return fArray[i]; }
108cb93a386Sopenharmony_ci
109cb93a386Sopenharmony_ci    /** Sorts the queue into priority order.  The queue is only guarenteed to remain in sorted order
110cb93a386Sopenharmony_ci     *  until any other operation, other than at(), is performed.
111cb93a386Sopenharmony_ci     */
112cb93a386Sopenharmony_ci    void sort() {
113cb93a386Sopenharmony_ci        if (fArray.count() > 1) {
114cb93a386Sopenharmony_ci            SkTQSort<T>(fArray.begin(), fArray.end(), LESS);
115cb93a386Sopenharmony_ci            for (int i = 0; i < fArray.count(); i++) {
116cb93a386Sopenharmony_ci                this->setIndex(i);
117cb93a386Sopenharmony_ci            }
118cb93a386Sopenharmony_ci            this->validate();
119cb93a386Sopenharmony_ci        }
120cb93a386Sopenharmony_ci    }
121cb93a386Sopenharmony_ci
122cb93a386Sopenharmony_ciprivate:
123cb93a386Sopenharmony_ci    static int LeftOf(int x) { SkASSERT(x >= 0); return 2 * x + 1; }
124cb93a386Sopenharmony_ci    static int ParentOf(int x) { SkASSERT(x > 0); return (x - 1) >> 1; }
125cb93a386Sopenharmony_ci
126cb93a386Sopenharmony_ci    void percolateUpOrDown(int index) {
127cb93a386Sopenharmony_ci        SkASSERT(index >= 0);
128cb93a386Sopenharmony_ci        if (!percolateUpIfNecessary(index)) {
129cb93a386Sopenharmony_ci            this->validate(index);
130cb93a386Sopenharmony_ci            this->percolateDownIfNecessary(index);
131cb93a386Sopenharmony_ci        }
132cb93a386Sopenharmony_ci    }
133cb93a386Sopenharmony_ci
134cb93a386Sopenharmony_ci    bool percolateUpIfNecessary(int index) {
135cb93a386Sopenharmony_ci        SkASSERT(index >= 0);
136cb93a386Sopenharmony_ci        bool percolated = false;
137cb93a386Sopenharmony_ci        do {
138cb93a386Sopenharmony_ci            if (0 == index) {
139cb93a386Sopenharmony_ci                this->setIndex(index);
140cb93a386Sopenharmony_ci                return percolated;
141cb93a386Sopenharmony_ci            }
142cb93a386Sopenharmony_ci            int p = ParentOf(index);
143cb93a386Sopenharmony_ci            if (LESS(fArray[index], fArray[p])) {
144cb93a386Sopenharmony_ci                using std::swap;
145cb93a386Sopenharmony_ci                swap(fArray[index], fArray[p]);
146cb93a386Sopenharmony_ci                this->setIndex(index);
147cb93a386Sopenharmony_ci                index = p;
148cb93a386Sopenharmony_ci                percolated = true;
149cb93a386Sopenharmony_ci            } else {
150cb93a386Sopenharmony_ci                this->setIndex(index);
151cb93a386Sopenharmony_ci                return percolated;
152cb93a386Sopenharmony_ci            }
153cb93a386Sopenharmony_ci            this->validate(index);
154cb93a386Sopenharmony_ci        } while (true);
155cb93a386Sopenharmony_ci    }
156cb93a386Sopenharmony_ci
157cb93a386Sopenharmony_ci    void percolateDownIfNecessary(int index) {
158cb93a386Sopenharmony_ci        SkASSERT(index >= 0);
159cb93a386Sopenharmony_ci        do {
160cb93a386Sopenharmony_ci            int child = LeftOf(index);
161cb93a386Sopenharmony_ci
162cb93a386Sopenharmony_ci            if (child >= fArray.count()) {
163cb93a386Sopenharmony_ci                // We're a leaf.
164cb93a386Sopenharmony_ci                this->setIndex(index);
165cb93a386Sopenharmony_ci                return;
166cb93a386Sopenharmony_ci            }
167cb93a386Sopenharmony_ci
168cb93a386Sopenharmony_ci            if (child + 1 >= fArray.count()) {
169cb93a386Sopenharmony_ci                // We only have a left child.
170cb93a386Sopenharmony_ci                if (LESS(fArray[child], fArray[index])) {
171cb93a386Sopenharmony_ci                    using std::swap;
172cb93a386Sopenharmony_ci                    swap(fArray[child], fArray[index]);
173cb93a386Sopenharmony_ci                    this->setIndex(child);
174cb93a386Sopenharmony_ci                    this->setIndex(index);
175cb93a386Sopenharmony_ci                    return;
176cb93a386Sopenharmony_ci                }
177cb93a386Sopenharmony_ci            } else if (LESS(fArray[child + 1], fArray[child])) {
178cb93a386Sopenharmony_ci                // The right child is the one we should swap with, if we swap.
179cb93a386Sopenharmony_ci                child++;
180cb93a386Sopenharmony_ci            }
181cb93a386Sopenharmony_ci
182cb93a386Sopenharmony_ci            // Check if we need to swap.
183cb93a386Sopenharmony_ci            if (LESS(fArray[child], fArray[index])) {
184cb93a386Sopenharmony_ci                using std::swap;
185cb93a386Sopenharmony_ci                swap(fArray[child], fArray[index]);
186cb93a386Sopenharmony_ci                this->setIndex(index);
187cb93a386Sopenharmony_ci                index = child;
188cb93a386Sopenharmony_ci            } else {
189cb93a386Sopenharmony_ci                // We're less than both our children.
190cb93a386Sopenharmony_ci                this->setIndex(index);
191cb93a386Sopenharmony_ci                return;
192cb93a386Sopenharmony_ci            }
193cb93a386Sopenharmony_ci            this->validate(index);
194cb93a386Sopenharmony_ci        } while (true);
195cb93a386Sopenharmony_ci    }
196cb93a386Sopenharmony_ci
197cb93a386Sopenharmony_ci    void setIndex(int index) {
198cb93a386Sopenharmony_ci        SkASSERT(index < fArray.count());
199cb93a386Sopenharmony_ci        if (SkToBool(INDEX)) {
200cb93a386Sopenharmony_ci            *INDEX(fArray[index]) = index;
201cb93a386Sopenharmony_ci        }
202cb93a386Sopenharmony_ci    }
203cb93a386Sopenharmony_ci
204cb93a386Sopenharmony_ci    void validate(int excludedIndex = -1) const {
205cb93a386Sopenharmony_ci#ifdef SK_DEBUG
206cb93a386Sopenharmony_ci        for (int i = 1; i < fArray.count(); ++i) {
207cb93a386Sopenharmony_ci            int p = ParentOf(i);
208cb93a386Sopenharmony_ci            if (excludedIndex != p && excludedIndex != i) {
209cb93a386Sopenharmony_ci                SkASSERT(!(LESS(fArray[i], fArray[p])));
210cb93a386Sopenharmony_ci                SkASSERT(!SkToBool(INDEX) || *INDEX(fArray[i]) == i);
211cb93a386Sopenharmony_ci            }
212cb93a386Sopenharmony_ci        }
213cb93a386Sopenharmony_ci#endif
214cb93a386Sopenharmony_ci    }
215cb93a386Sopenharmony_ci
216cb93a386Sopenharmony_ci    SkTDArray<T> fArray;
217cb93a386Sopenharmony_ci};
218cb93a386Sopenharmony_ci
219cb93a386Sopenharmony_ci#endif
220