18 #ifndef TOP_K_QUEUE_CC
19 #define TOP_K_QUEUE_CC
25 template <
class Score,
class ValueType>
29 scoreTypeInfo.setup<Score>();
30 valueTypeInfo.setup<ValueType>();
34 valueOffset = ((
char*)&tempValue) - ((
char*)
this);
40 template <
class Score,
class ValueType>
44 scoreTypeInfo.setup<Score>();
45 valueTypeInfo.setup<ValueType>();
49 valueOffset = ((
char*)&tempValue) - ((
char*)
this);
52 template <
class Score,
class ValueType>
56 scoreTypeInfo.setup<Score>();
57 valueTypeInfo.setup<ValueType>();
61 valueOffset = ((
char*)&tempValue) - ((
char*)
this);
67 tempScore = initScore;
68 tempValue = initValue;
74 template <
class Score,
class ValueType>
122 template <
class Score,
class ValueType>
133 template <
class Score,
class ValueType>
139 template <
class Score,
class ValueType>
142 Score* scores = allScores->c_ptr();
143 ValueType* values = allValues->c_ptr();
144 tempScore = scores[i];
145 tempValue = values[i];
146 scores[i] = scores[j];
147 values[i] = values[j];
148 scores[j] = tempScore;
149 values[j] = tempValue;
152 template <
class Score,
class ValueType>
161 }
else if (allScores ==
nullptr) {
162 allScores = makeObject<Vector<Score>>();
163 allValues = makeObject<Vector<ValueType>>();
164 allScores->push_back(tempScore);
165 allValues->push_back(tempValue);
169 if (score < (*allScores)[0] && allScores->size() >= k)
174 allScores->push_back(score);
175 allValues->push_back(value);
178 Score* scores = allScores->c_ptr();
179 ValueType* values = allValues->c_ptr();
181 int current = allScores->size() - 1;
182 while (current > 0 && scores[current] < scores[(current - 1) / 2]) {
186 swap(current, (current - 1) / 2);
187 current = (current - 1) / 2;
191 if (allScores->size() <= k)
200 scores[0] = scores[allScores->size() - 1];
201 values[0] = values[allScores->size() - 1];
202 allScores->pop_back();
203 allValues->pop_back();
207 int limit = allScores->size();
210 if (current * 2 + 2 < limit) {
214 if (scores[current * 2 + 1] < scores[current * 2 + 2]) {
215 smallPos = current * 2 + 1;
217 smallPos = current * 2 + 2;
221 if (scores[smallPos] < scores[current]) {
222 swap(smallPos, current);
228 }
else if (current * 2 + 1 < limit) {
229 int smallPos = current * 2 + 1;
232 if (scores[smallPos] < scores[current]) {
233 swap(smallPos, current);
244 template <
class Score,
class ValueType>
249 template <
class Score,
class ValueType>
254 template <
class Score,
class ValueType>
271 Score* scores = addMeIn.
allScores->c_ptr();
272 ValueType* values = addMeIn.
allValues->c_ptr();
273 for (
int i = 0; i < len; i++) {
275 insert(scores[i], values[i]);
277 std::cout <<
"Not enough space when trying to insert the " << i <<
"-th score"
286 template <
class Score,
class ValueType>
288 return allValues->size();
291 template <
class Score,
class ValueType>
294 std::cout <<
"You tried to extract a value from an empty queue!\n";
298 if (allValues ==
nullptr && i == 0) {
303 if (allValues ==
nullptr) {
304 std::cout <<
"You tried to extract a value from past the end of the queue!\n";
308 if (i < allValues->size()) {
313 std::cout <<
"You tried to extract a value from past the end of the queue!\n";
size_t getSizeOfConstituentObject(void *forMe) const
void deleteConstituentObject(void *deleteMe) const
ScoreValuePair< Score, ValueType > operator[](unsigned i)
void setUpAndCopyFrom(void *target, void *source) const
void setUpAndCopyFromConstituentObject(void *target, void *source) const
TopKQueue< Score, ValueType > & operator+(TopKQueue< Score, ValueType > &addMeIn)
void deleteObject(void *deleteMe)
void insert(Score &score, ValueType &value)
Handle< Vector< ValueType > > allValues
PDBTemplateBase scoreTypeInfo
Handle< Vector< Score > > allScores
size_t getSize(void *forMe)
PDBTemplateBase valueTypeInfo
bool descendsFromObject() const
TopKQueue< Score, ValueType > & getValue()