@@ -26,6 +26,17 @@ limitations under the License.
2626
2727namespace osp {
2828
29+ /* *
30+ * @class PairingHeap
31+ * @brief A pairing heap implementation.
32+ *
33+ * This class implements a pairing heap, which is a type of heap data structure with excellent amortized performance.
34+ * It supports standard heap operations such as insertion, finding the minimum element, and deleting the minimum element.
35+ *
36+ * @tparam Key The type of the keys stored in the heap. keys must be hashable and comparable.
37+ * @tparam Value The type of the values associated with the keys. Values are used for ordering the heap.
38+ * @tparam Compare The comparison function object type. Defaults to std::less<Value> for a min-heap.
39+ */
2940template <typename Key, typename Value, typename Compare>
3041class PairingHeap {
3142 private:
@@ -124,10 +135,14 @@ class PairingHeap {
124135 }
125136
126137 public:
127- PairingHeap () = default ;
128138
139+ PairingHeap () = default ;
129140 ~PairingHeap () { Clear (); }
130141
142+ /* *
143+ * @brief Copy constructor.
144+ * @param other The PairingHeap to copy from.
145+ */
131146 PairingHeap (const PairingHeap &other) : numElements_(other.numElements_), comp_(other.comp_) {
132147 root_ = nullptr ;
133148 if (!other.root_ ) {
@@ -179,6 +194,11 @@ class PairingHeap {
179194 }
180195 }
181196
197+ /* *
198+ * @brief Copy assignment operator.
199+ * @param other The PairingHeap to assign from.
200+ * @return Reference to this heap.
201+ */
182202 PairingHeap &operator =(const PairingHeap &other) {
183203 if (this != &other) {
184204 PairingHeap temp (other);
@@ -190,19 +210,41 @@ class PairingHeap {
190210 return *this ;
191211 }
192212
213+ /* *
214+ * @brief Move constructor.
215+ */
193216 PairingHeap (PairingHeap &&) = default ;
194- PairingHeap &operator =(PairingHeap &&) = default ;
195217
196- // Checks if the heap is empty.
197- bool IsEmpty () const { return root_ == nullptr ; }
198-
199- // Returns the number of elements in the heap.
200- size_t size () const { return numElements_; }
201-
202- // Checks if a key exists in the heap.
203- bool Contains (const Key &key) const { return nodeMap_.count (key); }
218+ /* *
219+ * @brief Move assignment operator.
220+ */
221+ PairingHeap &operator =(PairingHeap &&) = default ;
204222
205- // Inserts a new key-value pair into the heap.
223+ /* *
224+ * @brief Checks if the heap is empty.
225+ * @return True if the heap is empty, false otherwise.
226+ */
227+ [[nodiscard]] bool IsEmpty () const noexcept { return root_ == nullptr ; }
228+
229+ /* *
230+ * @brief Returns the number of elements in the heap.
231+ * @return The number of elements.
232+ */
233+ [[nodiscard]] size_t size () const noexcept { return numElements_; }
234+
235+ /* *
236+ * @brief Checks if a key exists in the heap.
237+ * @param key The key to check.
238+ * @return True if the key exists, false otherwise.
239+ */
240+ [[nodiscard]] bool Contains (const Key &key) const noexcept { return nodeMap_.count (key); }
241+
242+ /* *
243+ * @brief Inserts a new key-value pair into the heap.
244+ * @param key The key to insert.
245+ * @param value The value associated with the key.
246+ * @throws std::invalid_argument If the key already exists in the heap.
247+ */
206248 void Push (const Key &key, const Value &value) {
207249 Node *newNode = new Node{key, value};
208250 // emplace and check for success to avoid a separate lookup with contains()
@@ -216,15 +258,23 @@ class PairingHeap {
216258 numElements_++;
217259 }
218260
219- // Returns the key with the minimum value without removing it.
220- const Key &Top () const {
261+ /* *
262+ * @brief Returns the key with the minimum (or maximum) value depending on Compare.
263+ * @return The key at the top of the heap.
264+ * @throws std::out_of_range If the heap is empty.
265+ */
266+ [[nodiscard]] const Key &Top () const {
221267 if (IsEmpty ()) {
222268 throw std::out_of_range (" Heap is empty." );
223269 }
224270 return root_->key_ ;
225271 }
226272
227- // Removes and returns the key with the minimum value.
273+ /* *
274+ * @brief Removes and returns the key with the minimum (or maximum) value.
275+ * @return The key that was at the top of the heap.
276+ * @throws std::out_of_range If the heap is empty.
277+ */
228278 Key Pop () {
229279 if (IsEmpty ()) {
230280 throw std::out_of_range (" Heap is empty." );
@@ -242,7 +292,12 @@ class PairingHeap {
242292 return topKey;
243293 }
244294
245- // Updates the value of an existing key.
295+ /* *
296+ * @brief Updates the value of an existing key.
297+ * @param key The key whose value to update.
298+ * @param newValue The new value.
299+ * @throws std::invalid_argument If the key does not exist in the heap.
300+ */
246301 void Update (const Key &key, const Value &newValue) {
247302 auto it = nodeMap_.find (key);
248303 if (it == nodeMap_.end ()) {
@@ -281,7 +336,11 @@ class PairingHeap {
281336 // If values are equal, do nothing.
282337 }
283338
284- // Removes an arbitrary key from the heap.
339+ /* *
340+ * @brief Removes an arbitrary key from the heap.
341+ * @param key The key to remove.
342+ * @throws std::invalid_argument If the key does not exist in the heap.
343+ */
285344 void Erase (const Key &key) {
286345 auto it = nodeMap_.find (key);
287346 if (it == nodeMap_.end ()) {
@@ -307,16 +366,23 @@ class PairingHeap {
307366 numElements_--;
308367 }
309368
310- // Gets the value for a given key.
311- const Value &GetValue (const Key &key) const {
369+ /* *
370+ * @brief Gets the value for a given key.
371+ * @param key The key to look up.
372+ * @return The value associated with the key.
373+ * @throws std::out_of_range If the key does not exist in the heap.
374+ */
375+ [[nodiscard]] const Value &GetValue (const Key &key) const {
312376 auto it = nodeMap_.find (key);
313377 if (it == nodeMap_.end ()) {
314378 throw std::out_of_range (" Key does not exist in the heap." );
315379 }
316380 return it->second ->value_ ;
317381 }
318382
319- // Removes all elements from the heap.
383+ /* *
384+ * @brief Removes all elements from the heap.
385+ */
320386 void Clear () {
321387 if (!root_) {
322388 return ;
@@ -346,9 +412,12 @@ class PairingHeap {
346412 numElements_ = 0 ;
347413 }
348414
349- // Retrieves keys with the top value, up to a specified limit.
350- // If limit is 0, all keys with the top value are returned.
351- std::vector<Key> GetTopKeys (size_t limit = 0 ) const {
415+ /* *
416+ * @brief Retrieves keys with the top value, up to a specified limit.
417+ * @param limit The maximum number of keys to return. If 0, all keys with the top value are returned.
418+ * @return A vector of keys.
419+ */
420+ [[nodiscard]] std::vector<Key> GetTopKeys (size_t limit = 0 ) const {
352421 std::vector<Key> topKeys;
353422 if (IsEmpty ()) {
354423 return topKeys;
@@ -385,9 +454,15 @@ class PairingHeap {
385454 }
386455};
387456
457+ /* *
458+ * @brief Alias for a max-pairing heap.
459+ */
388460template <typename Key, typename Value>
389461using MaxPairingHeap = PairingHeap<Key, Value, std::greater<Value>>;
390462
463+ /* *
464+ * @brief Alias for a min-pairing heap.
465+ */
391466template <typename Key, typename Value>
392467using MinPairingHeap = PairingHeap<Key, Value, std::less<Value>>;
393468
0 commit comments