Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 280 lines (226 sloc) 7.051 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
/* This file is part of libsnippets
*
* Copyright (C) 2010 Sebastian Dröge <slomo@circular-chaos.org>
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
* If not, see <http://www.gnu.org/licenses/>.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <snippets/bloomfilter.h>
#include <snippets/fnv.h>

#include <math.h>

#ifndef M_LN2
#define M_LN2 0.69314718055994530942
#endif

#include <assert.h>

/* This implements a bloom filter, see
*
* http://en.wikipedia.org/wiki/Bloom_filter
* or
* "Space/Time Trade-offs in Hash Coding with Allowable Errors"
* by Burton H. Bloom in Communications of the ACM Volume 13,
* Number 7 (1970).
*
* The hashing implemented here is the enhanced double hashing
* as proposed in "Bloom Filters in Probabilistic Verification"
* by Peter C. Dillinger and Panagiotis Manolios.
*
* The formula for the optimal number of hash functions and
* the false positive probability comes from their paper too.
*/

struct _SnippetsBloomFilter
{
  uint8_t *filter;
  uint32_t size;
  uint64_t n_elements;
  unsigned int n_hash_functions;
  unsigned int hash_size;

  void (*hash) (const uint8_t * data, size_t size, uint8_t * hash);
};

SnippetsBloomFilter *
snippets_bloom_filter_new (uint32_t size, unsigned int n_hash_functions,
    unsigned int hash_size)
{
  SnippetsBloomFilter *filter;

  assert (size > 0);
  assert (n_hash_functions > 0);
  assert (hash_size > 0 && hash_size <= 1024);

  /* Round up to next byte multiple */
  size = (((size) + 7) & ~7);

  filter = calloc (sizeof (SnippetsBloomFilter) + size / 8, 1);

  if (hash_size <= 64) {
    filter->hash_size = 64;
    filter->hash = snippets_fnv1a_64;
  } else if (hash_size <= 128) {
    filter->hash_size = 128;
    filter->hash = snippets_fnv1a_128;
  } else if (hash_size <= 256) {
    filter->hash_size = 256;
    filter->hash = snippets_fnv1a_256;
  } else if (hash_size <= 512) {
    filter->hash_size = 512;
    filter->hash = snippets_fnv1a_512;
  } else if (hash_size <= 1024) {
    filter->hash_size = 1024;
    filter->hash = snippets_fnv1a_1024;
  }

  filter->filter = ((uint8_t *) filter) + sizeof (SnippetsBloomFilter);
  filter->size = size;
  filter->n_hash_functions = n_hash_functions;

  return filter;
}

#define SET_BIT(filter, bit) (filter[bit / 8] |= (1 << (bit % 8)))
#define GET_BIT(filter, bit) (filter[bit / 8] & (1 << (bit % 8)))

static int
snippets_bloom_filter_hash (SnippetsBloomFilter * filter, const uint8_t * data,
    size_t length, int set)
{
  uint32_t hash[32];
  unsigned int n_hash_functions, first_n_hash_functions;
  unsigned int hash_index;
  unsigned int hash_values;
  unsigned int functions_per_value;
  unsigned int last_functions;
  uint32_t x, y;
  uint32_t size;
  int i, j;

  size = filter->size;

  n_hash_functions = filter->n_hash_functions;
  hash_values = filter->hash_size / 32;

  if (n_hash_functions <= hash_values / 2) {
    first_n_hash_functions = n_hash_functions;
    functions_per_value = 1;
    last_functions = 0;
  } else {
    functions_per_value = n_hash_functions / (hash_values / 2);
    last_functions = functions_per_value + n_hash_functions % (hash_values / 2);

    if (last_functions == functions_per_value)
      last_functions = 0;
    first_n_hash_functions = n_hash_functions - last_functions;
  }

  filter->hash (data, length, (uint8_t *) hash);

  hash_index = 0;

  /* If there are less hash functions than hash value pairs
* or the number of hash functions is an integer multiple
* of the number of hsah value pairs: Handle all hash
* functions in this loop.
*
* Otherwise handle all hash functions in this loop
* except the ones for the last hash value pair */
  for (i = 0; i < first_n_hash_functions; i += functions_per_value) {
    x = hash[hash_index] % size;
    y = hash[hash_index + hash_values / 2] % size;

    if (functions_per_value == 1)
      x = (x + y) % size;

    if (set)
      SET_BIT (filter->filter, x);
    else if (!GET_BIT (filter->filter, x))
      return FALSE;

    for (j = 0; j < functions_per_value - 1; j++) {
      x = (x + y) % size;
      y = (y + i) % size;

      if (set)
        SET_BIT (filter->filter, x);
      else if (!GET_BIT (filter->filter, x))
        return FALSE;
    }
    hash_index++;
  }

  /* If there are hash functions left handle
* them here
*/
  if (last_functions) {
    x = hash[hash_index] % size;
    y = hash[hash_index + hash_values / 2] % size;

    if (set)
      SET_BIT (filter->filter, x);
    else if (!GET_BIT (filter->filter, x))
      return FALSE;

    for (j = 0; j < last_functions - 1; j++) {
      x = (x + y) % size;
      y = (y + i) % size;

      if (set)
        SET_BIT (filter->filter, x);
      else if (!GET_BIT (filter->filter, x))
        return FALSE;
    }
  }

  return TRUE;
}

void
snippets_bloom_filter_insert (SnippetsBloomFilter * filter,
    const uint8_t * data, size_t length)
{
  assert (filter != NULL);
  assert (data != NULL);

  snippets_bloom_filter_hash (filter, data, length, TRUE);
  filter->n_elements++;
}

int
snippets_bloom_filter_contains (SnippetsBloomFilter * filter,
    const uint8_t * data, size_t length)
{
  assert (filter != NULL);
  assert (data != NULL);

  return snippets_bloom_filter_hash (filter, data, length, FALSE);
}

void
snippets_bloom_filter_free (SnippetsBloomFilter * filter)
{
  assert (filter != NULL);

  free (filter);
}

unsigned int
snippets_bloom_filter_optimal_n_hash_functions (uint32_t filter_size,
    uint64_t n_elements)
{
  double m, n;

  assert (n_elements != 0);

  m = filter_size;
  n = n_elements;

  return (unsigned int) (pow (3.8, 1.0 / (m / n + 4.2)) * (m / n) * M_LN2);
}

unsigned int
snippets_bloom_filter_n_hash_functions (SnippetsBloomFilter * filter)
{
  assert (filter != NULL);

  return filter->n_hash_functions;
}

uint32_t
snippets_bloom_filter_size (SnippetsBloomFilter * filter)
{
  assert (filter != NULL);

  return filter->size;
}

uint64_t
snippets_bloom_filter_n_elements (SnippetsBloomFilter * filter)
{
  assert (filter != NULL);

  return filter->n_elements;
}

double
snippets_bloom_filter_false_positive_rate (SnippetsBloomFilter * filter)
{
  double n_hash_functions;
  double n_elements;
  double filter_size;

  assert (filter != NULL);

  n_hash_functions = filter->n_hash_functions;
  n_elements = filter->n_elements;
  filter_size = filter->size;

  return pow (1.0 - pow (M_E, (-n_hash_functions * n_elements) / filter_size),
      n_hash_functions);
}
Something went wrong with that request. Please try again.