NetSurf
bloom.h
Go to the documentation of this file.
1/*
2 * Copyright 2013 Rob Kendrick <rjek@netsurf-browser.org>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf 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/** \file
20 * Trivial bloom filter */
21
22#ifndef _NETSURF_UTILS_BLOOM_H_
23#define _NETSURF_UTILS_BLOOM_H_
24
25#include <stdbool.h>
26#include <stddef.h>
27#include <stdint.h>
28
29struct bloom_filter;
30
31/**
32 * Create a new bloom filter.
33 *
34 * \param size Size of bloom filter in bytes
35 * \return Handle for newly-created bloom filter, or NULL
36 */
37struct bloom_filter *bloom_create(size_t size);
38
39/**
40 * Destroy a previously-created bloom filter
41 *
42 * \param b Bloom filter to destroy
43 */
44void bloom_destroy(struct bloom_filter *b);
45
46/**
47 * Insert a string of given length (may include NULs) into the filter,
48 * using an internal hash function.
49 *
50 * \param b Bloom filter to add to
51 * \param s Pointer to data
52 * \param z Length of data
53 */
54void bloom_insert_str(struct bloom_filter *b, const char *s, size_t z);
55
56/**
57 * Insert a given hash value into the filter, should you already have
58 * one to hand.
59 *
60 * \param b Bloom filter to add to
61 * \param hash Value to add
62 */
63void bloom_insert_hash(struct bloom_filter *b, uint32_t hash);
64
65/**
66 * Search the filter for the given string, assuming it was added by
67 * bloom_insert_str(). May return false-positives.
68 *
69 * \param b Bloom filter to search
70 * \param s Pointer to data to search for
71 * \param z Length of data
72 *
73 * \return False if never added, True if it might have been.
74 */
75bool bloom_search_str(struct bloom_filter *b, const char *s, size_t z);
76
77/**
78 * Search the filter for the given hash value, assuming it was added by
79 * bloom_insert_hash(). May return false-positives.
80 *
81 * \param b Bloom filter to search
82 * \param hash Hash value to search for
83 *
84 * \return False if never added, True if it might have been.
85 */
86bool bloom_search_hash(struct bloom_filter *b, uint32_t hash);
87
88/**
89 * Find out how many items have been added to this bloom filter. This
90 * is useful for deciding the size of a new bloom filter should you
91 * need to rehash it.
92 *
93 * \param b Bloom filter to examine
94 *
95 * \return Number of items that have been added
96 */
97uint32_t bloom_items(struct bloom_filter *b);
98
99#endif
uint32_t bloom_items(struct bloom_filter *b)
Find out how many items have been added to this bloom filter.
Definition: bloom.c:107
struct bloom_filter * bloom_create(size_t size)
Create a new bloom filter.
Definition: bloom.c:59
void bloom_destroy(struct bloom_filter *b)
Destroy a previously-created bloom filter.
Definition: bloom.c:71
bool bloom_search_str(struct bloom_filter *b, const char *s, size_t z)
Search the filter for the given string, assuming it was added by bloom_insert_str().
Definition: bloom.c:92
void bloom_insert_str(struct bloom_filter *b, const char *s, size_t z)
Insert a string of given length (may include NULs) into the filter, using an internal hash function.
Definition: bloom.c:76
bool bloom_search_hash(struct bloom_filter *b, uint32_t hash)
Search the filter for the given hash value, assuming it was added by bloom_insert_hash().
Definition: bloom.c:98
void bloom_insert_hash(struct bloom_filter *b, uint32_t hash)
Insert a given hash value into the filter, should you already have one to hand.
Definition: bloom.c:82
size_t size
Definition: bloom.c:54