00001 /******************************************************************************* 00002 Copyright (c) 2011, Yahoo! Inc. 00003 All rights reserved. 00004 00005 Redistribution and use of this software in source and binary forms, 00006 with or without modification, are permitted provided that the following 00007 conditions are met: 00008 00009 * Redistributions of source code must retain the above 00010 copyright notice, this list of conditions and the 00011 following disclaimer. 00012 00013 * Redistributions in binary form must reproduce the above 00014 copyright notice, this list of conditions and the 00015 following disclaimer in the documentation and/or other 00016 materials provided with the distribution. 00017 00018 * Neither the name of Yahoo! Inc. nor the names of its 00019 contributors may be used to endorse or promote products 00020 derived from this software without specific prior 00021 written permission of Yahoo! Inc. 00022 00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 00024 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 00025 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00026 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00027 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00028 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00029 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00030 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00031 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00032 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00033 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00034 00035 The Initial Developer of the Original Code is Shravan Narayanamurthy. 00036 ******************************************************************************/ 00037 /* 00038 * GenericTopKList.h 00039 * 00040 * 00041 * Created on: 22-Jul-2010 00042 * 00043 */ 00044 00045 #ifndef GENERICTOPKLIST_H_ 00046 #define GENERICTOPKLIST_H_ 00047 00048 #include <queue> 00049 #include <stack> 00050 #include <fstream> 00051 00052 using namespace std; 00053 00054 //!A list that maintains top K elements 00055 template<class T, class GreaterThan> 00056 class GenericTopKList { 00057 private: 00058 priority_queue<T, vector<T> , GreaterThan> pq; 00059 size_t K; 00060 00061 public: 00062 GenericTopKList(size_t K_ = 10) : 00063 K(K_) { 00064 } 00065 00066 virtual ~GenericTopKList() { 00067 } 00068 00069 void push(T elem) { 00070 if (pq.size() < K) { 00071 pq.push(elem); 00072 } else { 00073 T top_elem = pq.top(); 00074 GreaterThan gt; 00075 if (gt(elem, top_elem)) { 00076 pq.pop(); 00077 pq.push(elem); 00078 } 00079 } 00080 } 00081 00082 T top() { 00083 return pq.top(); 00084 } 00085 00086 void pop() { 00087 if (!pq.empty()) 00088 pq.pop(); 00089 } 00090 00091 bool empty() { 00092 return pq.empty(); 00093 } 00094 00095 void print(ostream& out) { 00096 stack<T> st; 00097 while (!pq.empty()) { 00098 st.push(pq.top()); 00099 pq.pop(); 00100 } 00101 while (!st.empty()) { 00102 out << st.top() << ","; 00103 st.pop(); 00104 } 00105 } 00106 00107 void clear() { 00108 while (!pq.empty()) 00109 pq.pop(); 00110 } 00111 }; 00112 00113 #endif /* GENERICTOPKLIST_H_ */