libBf 0.1

bloom_filter.h

00001 #ifndef BLOOM_FILTER_H
00002 #define BLOOM_FILTER_H
00003 
00004 #include <string>
00005 
00006 namespace bf {
00007 
00010 template <typename Derived>
00011 class bloom_filter
00012 {
00013 public:
00017     template <typename T>
00018     void add(const T& x)
00019     {
00020         ++n_;
00021         derived().add(x);
00022     }
00023 
00027     template <typename T>
00028     void remove(const T& x)
00029     {
00030         --n_;
00031         derived().remove(x);
00032     }
00033 
00038     template <typename T>
00039     unsigned count(const T& x) const
00040     {
00041         return derived().count(x);
00042     }
00043 
00045     void clear()
00046     {
00047         n_ = 0;
00048         derived().clear();
00049     }
00050 
00053     unsigned long n() const
00054     {
00055         return n_;
00056     }
00057 
00060     std::string to_string() const
00061     {
00062         return derived().to_string();
00063     }
00064 
00065 protected:
00066     bloom_filter()
00067     {
00068     };
00069 
00070 private:
00071     //
00072     // CRTP interface
00073     //
00074     template <typename D>
00075     D& derived() const
00076     {
00077         return *static_cast<D*>(this);
00078     }
00079 
00080     template <typename D>
00081     const D& derived() const
00082     {
00083         return *static_cast<const D*>(this);
00084     }
00085 
00086 protected:
00088     unsigned long n_;
00089 };
00090 
00091 } // namespace bf
00092 
00093 #endif
 All Classes Functions Variables