BloomFilter.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. /**
  46. * Construct an empty filter
  47. */
  48. BloomFilter()
  49. throw()
  50. {
  51. memset(_field,0,sizeof(_field));
  52. }
  53. /**
  54. * Construct from a raw filter
  55. *
  56. * @param b Raw filter bits, must be exactly bytes() in length, or NULL to construct empty
  57. */
  58. BloomFilter(const void *b)
  59. throw()
  60. {
  61. if (b)
  62. memcpy(_field,b,sizeof(_field));
  63. else memset(_field,0,sizeof(_field));
  64. }
  65. /**
  66. * @return Size of filter in bits
  67. */
  68. static inline unsigned int bits() throw() { return B; }
  69. /**
  70. * @return Size of filter in bytes
  71. */
  72. static inline unsigned int bytes() throw() { return (B / 8); }
  73. /**
  74. * @return Pointer to portable array of bytes of bytes() length representing filter
  75. */
  76. inline const unsigned char *data() const throw() { return _field; }
  77. /**
  78. * Clear all bits in filter
  79. */
  80. inline void clear()
  81. throw()
  82. {
  83. memset(_field,0,sizeof(_field));
  84. }
  85. /**
  86. * @param n Value to set
  87. */
  88. inline void set(unsigned int n)
  89. throw()
  90. {
  91. n %= B;
  92. _field[n / 8] |= (1 << (n % 8));
  93. }
  94. /**
  95. * @param n Value to test
  96. * @return True if value is present is set
  97. */
  98. inline bool contains(unsigned int n)
  99. throw()
  100. {
  101. n %= B;
  102. return (_field[n / 8] & (1 << (n % 8)));
  103. }
  104. /**
  105. * Clear a random bit in this bloom filter
  106. */
  107. inline void decay()
  108. throw()
  109. {
  110. const unsigned int rn = Utils::randomInt<unsigned int>();
  111. _field[(rn >> 3) % (B / 8)] &= ~((unsigned char)(1 << (rn & 7)));
  112. }
  113. private:
  114. unsigned char _field[B / 8];
  115. };
  116. } // namespace ZeroTier
  117. #endif