A platform for high-performance distributed tool and library development written in C++. It can be deployed in two different cluster modes: standalone or distributed. API for v0.5.0, released on June 13, 2018.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TopKQueue.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * *
3  * Copyright 2018 Rice University *
4  * *
5  * Licensed under the Apache License, Version 2.0 (the "License"); *
6  * you may not use this file except in compliance with the License. *
7  * You may obtain a copy of the License at *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14  * See the License for the specific language governing permissions and *
15  * limitations under the License. *
16  * *
17  *****************************************************************************/
18 #ifndef TOP_K_QUEUE_H
19 #define TOP_K_QUEUE_H
20 
21 #include "PDBVector.h"
22 
23 // PRELOAD %TopKQueue <Nothing>%
24 
25 namespace pdb {
26 
27 // this class is used to access items from the TopKQueue class
28 template <class Score, class ValueType>
30 
31 // template class that is used by top-K aggregation... a top-k aggregation query
32 // returns one of these queues, containing the items with the top-K largest scores
33 
34 // note that the < operation must be defined on the Score class...
35 template <class Score, class ValueType = Nothing>
36 class TopKQueue : public Object {
37 
38 private:
39  // the key
40  int myKey = 1;
41 
42  // true if tempScore and tempValue are empty and should be ignored
43  bool empty = true;
44 
45  // stores type info for the score and the value
48 
49  // list of scores and values; implements a priority queue
52 
53  // the total size of this object
54  unsigned mySize;
55 
56  // the offset (in bytes) from the start of the object until tempValue
57  unsigned valueOffset;
58 
59  // the number of items we can store
60  unsigned k = 1;
61 
62  // in the case where there is just one item, these store them
63  Score tempScore;
64  ValueType tempValue;
65 
66 public:
67  // no arg constructor
68  TopKQueue();
69 
70  // create a queue and give a max size
71  TopKQueue(unsigned k);
72 
73  // create a queue that stores the best k items; initialize by storing initScore and initValue
74  TopKQueue(unsigned k, Score initScore, ValueType initValue);
75 
76  // normally these would be defined by the ENABLE_DEEP_COPY macro, but because
77  // this class is special, we need to manually override these methods
78  void setUpAndCopyFrom(void* target, void* source) const;
79  void deleteObject(void* deleteMe);
80  size_t getSize(void* forMe);
81 
82 private:
83  // swap the entries at positions i and j in the queue
84  void swap(int i, int j);
85 
86 public:
87  // insert an item into the queue
88  void insert(Score& score, ValueType& value);
89 
90  // always returns 1
91  int& getKey();
92 
93  // always returns this
95 
96  // merges the two queues, returning this one
98 
99  // access the i^th item in the queue
101 
102  // get the numer of items in the queue
103  unsigned size();
104 };
105 
106 template <class Score, class ValueType>
107 class ScoreValuePair {
108 
109  Score& myScore;
110  ValueType& myValue;
111 
112 public:
114 
115  ScoreValuePair(Score& myScore, ValueType& myValue) : myScore(myScore), myValue(myValue) {}
116 
117  Score& getScore() {
118  return myScore;
119  }
120 
121  ValueType& getValue() {
122  return myValue;
123  }
124 };
125 }
126 
127 #include "TopKQueue.cc"
128 
129 #endif
ValueType tempValue
Definition: TopKQueue.h:64
ValueType & getValue()
Definition: TopKQueue.h:121
Score & getScore()
Definition: TopKQueue.h:117
ScoreValuePair< Score, ValueType > operator[](unsigned i)
Definition: TopKQueue.cc:292
void setUpAndCopyFrom(void *target, void *source) const
Definition: TopKQueue.cc:75
Score tempScore
Definition: TopKQueue.h:63
TopKQueue< Score, ValueType > & operator+(TopKQueue< Score, ValueType > &addMeIn)
Definition: TopKQueue.cc:255
ValueType & myValue
Definition: TopKQueue.h:110
void deleteObject(void *deleteMe)
Definition: TopKQueue.cc:123
void insert(Score &score, ValueType &value)
Definition: TopKQueue.cc:153
Handle< Vector< ValueType > > allValues
Definition: TopKQueue.h:51
void swap(int i, int j)
Definition: TopKQueue.cc:140
unsigned valueOffset
Definition: TopKQueue.h:57
PDBTemplateBase scoreTypeInfo
Definition: TopKQueue.h:46
Handle< Vector< Score > > allScores
Definition: TopKQueue.h:50
size_t getSize(void *forMe)
Definition: TopKQueue.cc:134
int & getKey()
Definition: TopKQueue.cc:245
PDBTemplateBase valueTypeInfo
Definition: TopKQueue.h:47
ScoreValuePair(Score &myScore, ValueType &myValue)
Definition: TopKQueue.h:115
TopKQueue< Score, ValueType > & getValue()
Definition: TopKQueue.cc:250
unsigned k
Definition: TopKQueue.h:60
ScoreValuePair(ScoreValuePair &fromMe)
Definition: TopKQueue.h:113
unsigned mySize
Definition: TopKQueue.h:54
unsigned size()
Definition: TopKQueue.cc:287