eXpress “1.5”

src/robertsfilter.cpp

00001 //
00002 //  robertsfilter.cpp
00003 //  express
00004 //
00005 //  Created by Adam Roberts on 5/1/12.
00006 //  Copyright (c) 2012 UC Berkeley. All rights reserved.
00007 //
00008 
00009 #include "robertsfilter.h"
00010 
00011 using namespace std;
00012 
00013 RobertsFilter::RobertsFilter(size_t local_size, size_t global_size)
00014     : _global_vector(global_size),
00015       _local_size(local_size),
00016       _global_size(global_size) {
00017 }
00018 
00019 bool RobertsFilter::test_and_push(const string& key) {
00020   if (_local_set.count(key) || _global_set.count(key)) {
00021     return true;
00022   }
00023 
00024   _local_set.insert(key);
00025   _local_queue.push(key);
00026   if (_local_set.size() > _local_size) {
00027     size_t r = _global_set.size();
00028     if (_global_set.size() == _global_size) {
00029       r = (size_t)(rand()/double(RAND_MAX)*(_global_size-1));
00030       _global_set.erase(_global_vector[r]);
00031     }
00032 
00033     _local_set.erase(_local_queue.front());
00034     _global_set.insert(_local_queue.front());
00035     _global_vector[r] = _local_queue.front();
00036     _local_queue.pop();
00037   }
00038   return false;
00039 }
 All Classes Functions Variables