libBf 0.1
|
00001 #ifndef BLOOM_FILTER_BASIC_H 00002 #define BLOOM_FILTER_BASIC_H 00003 00004 #include <iosfwd> 00005 #include <vector> 00006 #include "bloom_filter.h" 00007 #include "core.h" 00008 #include "detail/basic.h" 00009 #include "detail/spectral.h" 00010 00011 namespace bf { 00012 00014 template <typename Core = core<>> 00015 class basic : public bloom_filter<basic<Core>> 00016 { 00017 public: 00018 typedef Core core_type; 00019 00023 static double k(double f) 00024 { 00025 return std::floor(- (std::log(f) / std::log(2))); 00026 } 00027 00035 static double capacity(unsigned k, unsigned m) 00036 { 00037 return std::floor(m / k * std::log(2)); 00038 } 00039 00042 basic(core_type&& core) 00043 : core_(core) 00044 { 00045 } 00046 00047 template <typename T> 00048 void add(const T& x) 00049 { 00050 detail::basic::add(x, core_); 00051 } 00052 00053 template <typename T> 00054 void remove(const T& x) 00055 { 00056 detail::basic::remove(x, core_); 00057 } 00058 00059 template <typename T> 00060 unsigned count(const T& x) const 00061 { 00062 auto pos = core_.positions(x); 00063 return detail::spectral::minimum(pos, core_.store); 00064 } 00065 00066 void clear() 00067 { 00068 core_.store.reset(); 00069 } 00070 00071 std::string to_string() const 00072 { 00073 return core_.store.to_string(); 00074 } 00075 00078 unsigned k() const 00079 { 00080 return core_.hash.k(); 00081 } 00082 00085 const core_type& core() const 00086 { 00087 return core_; 00088 } 00089 00090 protected: 00091 core_type core_; 00092 }; 00093 00094 template < 00095 typename Elem, 00096 typename Traits, 00097 typename Core 00098 > 00099 std::basic_ostream<Elem, Traits>& operator<<( 00100 std::basic_ostream<Elem, Traits>& os, const bf::basic<Core>& b) 00101 { 00102 os << b.to_string(); 00103 return os; 00104 } 00105 00106 } // namespace bf 00107 00108 #endif