|
| 1 | +/* |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#pragma once |
| 18 | + |
| 19 | +#include <folly/container/F14Map.h> |
| 20 | +#include <folly/container/F14Set.h> |
| 21 | +#include <set> |
| 22 | + |
| 23 | +#include "velox/common/base/Exceptions.h" |
| 24 | + |
| 25 | +namespace facebook::velox { |
| 26 | +/// A priority queue with values of type 'T'. Each value has assigned priority |
| 27 | +/// on insertion which determines the value's position in the quue. It supports |
| 28 | +/// to update the priority of the existing values, which adjusts the value's |
| 29 | +/// position in the queue accordingly. Ties are broken by insertion order. If |
| 30 | +/// 'MaxQueue' is true, it is a max priority queue, otherwise a min priority |
| 31 | +/// queue. |
| 32 | +/// |
| 33 | +/// NOTE: this class is not thread-safe. |
| 34 | +template <typename T, bool MaxQueue> |
| 35 | +class IndexedPriorityQueue { |
| 36 | + public: |
| 37 | + IndexedPriorityQueue() = default; |
| 38 | + |
| 39 | + ~IndexedPriorityQueue() { |
| 40 | + VELOX_CHECK_EQ(queue_.size(), index_.size()); |
| 41 | + } |
| 42 | + |
| 43 | + size_t size() const { |
| 44 | + return queue_.size(); |
| 45 | + } |
| 46 | + |
| 47 | + bool empty() const { |
| 48 | + return queue_.empty(); |
| 49 | + } |
| 50 | + |
| 51 | + /// Inserts 'value' into the queue with specified 'priority'. If 'value' |
| 52 | + /// already exists, then update its priority and the corresponding position in |
| 53 | + /// the queue. |
| 54 | + bool addOrUpdate(T value, uint64_t priority) { |
| 55 | + auto it = index_.find(value); |
| 56 | + if (it != index_.end()) { |
| 57 | + if (it->second->priority() == priority) { |
| 58 | + return false; |
| 59 | + } |
| 60 | + queue_.erase(it->second.get()); |
| 61 | + |
| 62 | + it->second->updatePriority(priority); |
| 63 | + queue_.insert(it->second.get()); |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + auto newEntry = std::make_unique<Entry>(value, priority, generation_++); |
| 68 | + queue_.insert(newEntry.get()); |
| 69 | + index_.emplace(value, std::move(newEntry)); |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + std::optional<T> pop() { |
| 74 | + VELOX_CHECK_EQ(queue_.size(), index_.size()); |
| 75 | + if (queue_.empty()) { |
| 76 | + return std::nullopt; |
| 77 | + } |
| 78 | + |
| 79 | + auto it = queue_.begin(); |
| 80 | + Entry* removedEntry = *it; |
| 81 | + const auto value = removedEntry->value(); |
| 82 | + queue_.erase(it); |
| 83 | + VELOX_CHECK(index_.contains(removedEntry->value())); |
| 84 | + index_.erase(removedEntry->value()); |
| 85 | + return value; |
| 86 | + } |
| 87 | + |
| 88 | + /// Describes the state of an inserted 'value' in the queue. |
| 89 | + class Entry { |
| 90 | + public: |
| 91 | + Entry(T value, uint64_t priority, uint64_t generation) |
| 92 | + : value_(std::move(value)), |
| 93 | + priority_(priority), |
| 94 | + generation_(generation) {} |
| 95 | + |
| 96 | + const T& value() const { |
| 97 | + return value_; |
| 98 | + } |
| 99 | + |
| 100 | + uint64_t priority() const { |
| 101 | + return priority_; |
| 102 | + } |
| 103 | + |
| 104 | + void updatePriority(uint64_t priority) { |
| 105 | + priority_ = priority; |
| 106 | + } |
| 107 | + |
| 108 | + uint64_t generation() const { |
| 109 | + return generation_; |
| 110 | + } |
| 111 | + |
| 112 | + private: |
| 113 | + const T value_; |
| 114 | + uint64_t priority_; |
| 115 | + const uint64_t generation_; |
| 116 | + }; |
| 117 | + |
| 118 | + /// Used to iterate through the queue in priority order. |
| 119 | + class Iterator { |
| 120 | + public: |
| 121 | + Iterator( |
| 122 | + typename std::set<Entry*>::const_iterator cur, |
| 123 | + typename std::set<Entry*>::const_iterator end) |
| 124 | + : end_{end}, cur_{cur}, val_{0} { |
| 125 | + if (cur_ != end_) { |
| 126 | + val_ = (*cur_)->value(); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + bool operator==(const Iterator& other) const { |
| 131 | + return std::tie(cur_, end_) == std::tie(other.cur_, other.end_); |
| 132 | + } |
| 133 | + |
| 134 | + bool operator!=(const Iterator& other) const { |
| 135 | + return !operator==(other); |
| 136 | + } |
| 137 | + |
| 138 | + Iterator& operator++() { |
| 139 | + VELOX_DCHECK(cur_ != end_); |
| 140 | + ++cur_; |
| 141 | + if (cur_ != end_) { |
| 142 | + val_ = (*cur_)->value(); |
| 143 | + } |
| 144 | + return *this; |
| 145 | + } |
| 146 | + |
| 147 | + const T& operator*() const { |
| 148 | + VELOX_DCHECK(cur_ != end_); |
| 149 | + return val_; |
| 150 | + } |
| 151 | + |
| 152 | + private: |
| 153 | + const typename std::set<Entry*>::const_iterator end_; |
| 154 | + typename std::set<Entry*>::const_iterator cur_; |
| 155 | + T val_; |
| 156 | + }; |
| 157 | + |
| 158 | + Iterator begin() const { |
| 159 | + return Iterator{queue_.cbegin(), queue_.cend()}; |
| 160 | + } |
| 161 | + |
| 162 | + Iterator end() const { |
| 163 | + return Iterator{queue_.cend(), queue_.cend()}; |
| 164 | + } |
| 165 | + |
| 166 | + private: |
| 167 | + struct EntrySetComparator { |
| 168 | + bool operator()(Entry* lhs, Entry* rhs) const { |
| 169 | + if (MaxQueue) { |
| 170 | + if (lhs->priority() > rhs->priority()) { |
| 171 | + return true; |
| 172 | + } |
| 173 | + if (lhs->priority() < rhs->priority()) { |
| 174 | + return false; |
| 175 | + } |
| 176 | + } else { |
| 177 | + if (lhs->priority() > rhs->priority()) { |
| 178 | + return false; |
| 179 | + } |
| 180 | + if (lhs->priority() < rhs->priority()) { |
| 181 | + return true; |
| 182 | + } |
| 183 | + } |
| 184 | + return lhs->generation() < rhs->generation(); |
| 185 | + } |
| 186 | + }; |
| 187 | + |
| 188 | + uint64_t generation_{0}; |
| 189 | + folly::F14FastMap<T, std::unique_ptr<Entry>> index_; |
| 190 | + std::set<Entry*, EntrySetComparator> queue_; |
| 191 | +}; |
| 192 | + |
| 193 | +} // namespace facebook::velox |
0 commit comments