Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Logger{
- public :
- unordered_map <string, bool> msgs;
- queue< pair <int , string> > sliding_win;
- Logger (){
- }
- bool shouldPrintMessage ( int timestamp, string msg )
- {
- while (!sliding_win.empty())
- {
- //cout << timestamp << endl;
- auto it = sliding_win.front();
- string msg_txt = it.second;
- int msg_timestamp = it.first;
- if (timestamp - msg_timestamp >= 10 )
- {
- sliding_win.pop();
- msgs.erase (msg_txt);
- }
- else
- break;
- }
- if (msgs.find(msg) != msgs.end())
- {
- return false;
- }
- msgs[msg] = timestamp;
- sliding_win.push({timestamp,msg});
- return true;
- }
- };
- int main() {
- Logger logger = Logger();
- // logging string "foo" at timestamp 1
- cout <<logger.shouldPrintMessage(1, "foo") << endl; //returns true;
- // logging string "bar" at timestamp 2
- cout << logger.shouldPrintMessage(2,"bar") << endl; //returns true;
- // logging string "foo" at timestamp 3
- cout << logger.shouldPrintMessage(3,"foo") << endl; //returns false;
- // logging string "bar" at timestamp 8
- cout << logger.shouldPrintMessage(8,"bar") << endl; //returns false;
- // logging string "foo" at timestamp 10
- cout << logger.shouldPrintMessage(10,"foo") << endl; //returns false;
- // logging string "foo" at timestamp 11
- cout << logger.shouldPrintMessage(11,"foo") << endl; //returns true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement