BloomFilter.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef _ZT_BLOOMFILTER_HPP
  28. #define _ZT_BLOOMFILTER_HPP
  29. #include <string.h>
  30. #include "Utils.hpp"
  31. namespace ZeroTier {
  32. /**
  33. * A simple bit field bloom filter
  34. *
  35. * This actually isn't a total filter, in that it does not contain a hashing
  36. * algorithm. It's up to the caller to hash/sum the items being remembered
  37. * so that the distribution of 'n' is random.
  38. *
  39. * @tparam B Size in BITS, must be a multiple of 8
  40. */
  41. template<unsigned int B>
  42. class BloomFilter
  43. {
  44. public:
  45. BloomFilter()
  46. throw()
  47. {
  48. memset(_field,0,sizeof(_field));
  49. }
  50. /**
  51. * @return Size of filter in bits
  52. */
  53. inline unsigned int bits() const throw() { return B; }
  54. /**
  55. * @return Size of filter in bytes
  56. */
  57. inline unsigned int bytes() const throw() { return (B / 8); }
  58. /**
  59. * @return Pointer to portable array of bytes of bytes() length representing filter
  60. */
  61. inline const unsigned char *data() const throw() { return _field; }
  62. /**
  63. * Clear all bits in filter
  64. */
  65. inline void clear()
  66. throw()
  67. {
  68. memset(_field,0,sizeof(_field));
  69. }
  70. /**
  71. * @param n Value to set
  72. */
  73. inline void add(unsigned int n)
  74. throw()
  75. {
  76. n %= B;
  77. _field[n / 8] |= (1 << (n % 8));
  78. }
  79. /**
  80. * @param n Value to test
  81. * @return True if value is present is set
  82. */
  83. inline bool contains(unsigned int n)
  84. throw()
  85. {
  86. n %= B;
  87. return (_field[n / 8] & (1 << (n % 8)));
  88. }
  89. /**
  90. * Clear one or more random bits
  91. *
  92. * This is used to apply probabilistic decay and hence "fuzziness" to
  93. * a bloom filter over time.
  94. *
  95. * @param bits Number of bits to clear
  96. */
  97. inline void decay(unsigned int bits)
  98. throw()
  99. {
  100. for(unsigned int i=0;i<bits;++i) {
  101. unsigned int rn = Utils::randomInt<unsigned int>();
  102. _field[(rn >> 7) % (B / 8)] &= ~((unsigned char)(1 << (rn & 7)));
  103. }
  104. }
  105. private:
  106. unsigned char _field[B / 8];
  107. };
  108. } // namespace ZeroTier
  109. #endif