Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
PriorityQueue.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Containers_PriorityQueue_h_
5#define _Stroika_Foundation_Containers_PriorityQueue_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include "Stroika/Foundation/Common/Common.h"
10#include "Stroika/Foundation/Containers/Common.h"
13
14/**
15 *
16 * STATUS: NOT EVEN DRAFT IMPL READY YET TO TEST.
17 *
18 * \note Code-Status: <a href="Code-Status.md#Draft">Draft</a> -- CODE NO WHERE NEAR COMPILING - just rough draft of API based on 1992 Stroika...
19 *
20 *
21 * TODO:
22 *
23 * @todo Use TRAITS mechanism - like with Bag<>
24 *
25 * @todo Implement first draft of code based on
26 * http://github.com/SophistSolutions/Stroika/blob/master/Archive/Stroika_FINAL_for_STERL_1992/Library/Foundation/Headers/PriorityQueue.hh
27 *
28 * @todo FIX so can remove top prioty itme to return btoih at a time.
29 *
30 * @todo Add Traits, and part of triats is priority type.
31 * min/max comes from numeric_limits<T>::min/max, and memmbers of the traits - not global
32 *
33 * @todo Rename PQEntry to PriorityQueueEntry
34 * (document why not nested type, and why not using pair<> - and then put docs into MultiSet code,
35 * and then redo Mapping to use KeyValuePair<> instead of pair<> - and have .fKey first elt!
36 *
37 * @todo redo operator== (const PQEntry<T>& lhs, const PQEntry<T>& rhs); as member function
38 */
39
41
42 using Traversal::Iterable;
43 using Traversal::Iterator;
44
45 using Priority = uint16_t;
46 const Priority kMinPriority = kMinUInt16;
47 const Priority kMaxPriority = kMaxUInt16;
48
49 // Someday this should be renamed ...
50 template <typename T>
51 class PQEntry {
52 public:
53 PQEntry (T item, Priority p);
54
55 T fItem;
56 Priority fPriority;
57 };
58 template <typename T>
59 Boolean operator== (const PQEntry<T>& lhs, const PQEntry<T>& rhs);
60
61 /*
62 * PriorityQueues are a like a Queue that allows retrieval based the priority assigned an item.
63 * This priority is given either at the time when the item is Enqueueed to the PriorityQueue, or
64 * by a function. The default function always assigns the lowest possible priority to an item.
65 * Priority start at zero and work upwards, so a zero priority item will be the last item
66 * removed from the PriorityQueue.
67 *
68 * PriorityQueues support two kinds of iteration: over type T, or over ProirityQueueEntrys of
69 * type T. A PriorityQueueEntry is a simple structure that couples together the item and its
70 * priority.
71 *
72 *
73 * PriorityQueues always iterate from highest to lowest priority.
74 *
75 * \note \em Thread-Safety <a href="Thread-Safety.md#C++-Standard-Thread-Safety">C++-Standard-Thread-Safety</a>
76 *
77 * \note See <a href="./ReadMe.md">ReadMe.md</a> for common features of all Stroika containers (especially
78 * constructors, iterators, etc)
79 *
80 *
81 */
82 template <typename T>
83 class [[nodiscard]] PriorityQueue : public Iterable<pair<T, Priority>> {
84 protected:
85 class _IRep;
86
87 public:
88 /**
89 * Use this typedef in templates to recover the basic functional container pattern of concrete types.
90 */
91 using ArchetypeContainerType = PriorityQueue;
92
93 public:
94 /**
95 */
96 PriorityQueue ();
97 PriorityQueue (const PriorityQueue& src) = default;
98
99 protected:
100 explicit PriorityQueue (const shared_ptr<_IRep>& rep);
101
102#if qStroika_Foundation_Debug_AssertionsChecked
103 public:
104 ~PriorityQueue ();
105#endif
106
107 public:
108 /**
109 */
110 nonvirtual PriorityQueue<T>& operator= (const PriorityQueue<T>& src);
111 nonvirtual PriorityQueue<T>& operator= (PriorityQueue<T>&& rhs) = default;
112
113 public:
114 nonvirtual void RemoveAll ();
115
116 public:
117 nonvirtual Iterable<T> Elements () const;
118
119 public:
120 nonvirtual void Enqueue (T item, Priority priority);
121
122 public:
123 nonvirtual T Dequeue ();
124
125 public:
126 nonvirtual T Head () const;
127
128 public:
129 nonvirtual Priority TopPriority () const;
130
131 public:
132 nonvirtual void RemoveHead ();
133
134 public:
135 nonvirtual PriorityQueue<T>& operator+= (T item);
136 nonvirtual PriorityQueue<T>& operator+= (IteratorRep<PQEntry<T>>* it);
137 nonvirtual PriorityQueue<T>& operator-- ();
138
139 protected:
140 nonvirtual const _IRep& _ConstGetRep () const;
141
142 protected:
143 nonvirtual const _IRep& _GetRep () const;
144 nonvirtual _IRep& _GetRep ();
145 };
146
147 /**
148 * \brief Implementation detail for PriorityQueue<T> implementors.
149 *
150 * Protected abstract interface to support concrete implementations of
151 * the PriorityQueue<T> container API.
152 */
153 template <typename T>
154 class PriorityQueue<T>::_IRep : public Iterable<pair<T, Priority>>::_IRep {
155 protected:
156 _IRep ();
157
158 public:
159 virtual ~_IRep ();
160
161 public:
162 virtual shared_ptr<_IRep> CloneEmpty () const = 0;
163 virtual void Enqueue (T item, Priority priority) = 0;
164 virtual T Dequeue () = 0;
165 virtual T Head () const = 0;
166 virtual Iterable<T> Elements () const = 0;
167 };
168}
169
170/*
171 ********************************************************************************
172 ******************************* Implementation Details *************************
173 ********************************************************************************
174 */
175
176#include "PriorityQueue.inl"
177
178#endif /*_Stroika_Foundation_Containers_PriorityQueue_h_ */
Implementation detail for PriorityQueue<T> implementors.
Iterable<T> is a base class for containers which easily produce an Iterator<T> to traverse them.
Definition Iterable.h:237