libBf 0.1

bloom_filter_bitwise.h

00001 #ifndef BLOOM_FILTER_BITWISE_H
00002 #define BLOOM_FILTER_BITWISE_H
00003 
00004 #include "bloom_filter.h"
00005 #include "core.h"
00006 #include "detail/bitwise.h"
00007 
00008 namespace bf {
00009 
00011 template <typename Core = core<>>
00012 class bitwise : public bloom_filter<bitwise<Core>>
00013 {
00014 public:
00015     typedef Core core_type;
00016     typedef std::function<unsigned(unsigned, unsigned)> growth_func_type;
00017 private:
00018     typedef std::vector<core_type> core_vector;
00019 
00020 public:
00025     bitwise(core_type&& core, unsigned min_size = 128)
00026       : levels_(1, core)
00027       , min_size_(min_size)
00028     {
00029         assert(min_size > 0);
00030     }
00031 
00032     template <typename T>
00033     void add(const T& x)
00034     {
00035         detail::bitwise::add(x, levels_, min_size_);
00036     }
00037 
00038     template <typename T>
00039     void remove(const T& x)
00040     {
00041         detail::bitwise::remove(x, levels_);
00042     }
00043 
00044     template <typename T>
00045     unsigned count(const T& x) const
00046     {
00047         return detail::bitwise::count(x, levels_);
00048     }
00049 
00050     unsigned levels() const
00051     {
00052         return levels_.size();
00053     }
00054 
00055     void clear()
00056     {
00057         for (auto& core : levels_)
00058             core.store.reset();
00059     }
00060 
00061     std::string to_string() const
00062     {
00063         std::string str;
00064         auto i = levels_.size();
00065         while (i)
00066         {
00067             str += levels_[--i].store.to_string();
00068             if (i)
00069                 str += "\n";
00070         }
00071 
00072         return str;
00073     }
00074 
00075 private:
00076     core_vector levels_;
00077     unsigned min_size_;
00078 };
00079 
00080 template <
00081     typename Elem,
00082     typename Traits,
00083     typename Core
00084 >
00085 std::basic_ostream<Elem, Traits>& operator<<(
00086         std::basic_ostream<Elem, Traits>& os, const bf::bitwise<Core>& b)
00087 {
00088     os << b.to_string();
00089     return os;
00090 }
00091 
00092 } // namespace bf
00093 
00094 #endif
 All Classes Functions Variables