1 /* $NetBSD: wcscspn_bloom.h,v 1.4 2011/11/25 17:48:22 joerg Exp $ */
4 * Copyright (c) 2011 Joerg Sonnenberger,
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Bloom filter for fast test if a given character is part of the reject set.
31 * The test may have false positives, but doesn't have false negatives.
32 * The first hash function is designed to be very fast to evaluate.
33 * It is collision free if the input is part of the same European language
34 * and shouldn't be too bad even other input. The second hash function
35 * tries to provide a much better mixing, but involves the slower
42 #define BLOOM_ARRAY_SIZE (BLOOM_SIZE / sizeof(size_t))
43 #define BLOOM_BITS (BLOOM_SIZE * CHAR_BIT)
44 #define BLOOM_DIV (sizeof(size_t) * CHAR_BIT)
47 wcscspn_bloom1(size_t x
)
49 return x
% BLOOM_BITS
;
53 wcscspn_bloom2(size_t x
)
55 return (size_t)((uint32_t)(x
* 2654435761U) /
56 (0x100000000ULL
/ BLOOM_BITS
));
60 wcsspn_bloom_init(size_t *bloom
, const wchar_t *charset
)
64 memset(bloom
, 0, BLOOM_SIZE
);
66 val
= wcscspn_bloom1((size_t)*charset
);
67 bloom
[val
/ BLOOM_DIV
] |= (size_t)1 << (val
% BLOOM_DIV
);
68 val
= wcscspn_bloom2((size_t)*charset
);
69 bloom
[val
/ BLOOM_DIV
] |= (size_t)1 << (val
% BLOOM_DIV
);
75 wcsspn_in_bloom(const size_t *bloom
, wchar_t ch
)
79 val
= wcscspn_bloom1((size_t)ch
);
80 if (bloom
[val
/ BLOOM_DIV
] & ((size_t)1 << (val
% BLOOM_DIV
)))
82 val
= wcscspn_bloom2((size_t)ch
);
83 if (bloom
[val
/ BLOOM_DIV
] & ((size_t)1 << (val
% BLOOM_DIV
)))