1/* $NetBSD: wcscspn_bloom.h,v 1.3 2011/11/25 16:46:56 dholland Exp $ */ 2 3/*- 4 * Copyright (c) 2011 Joerg Sonnenberger, 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 15 * 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 26 * SUCH DAMAGE. 27 */ 28 29/* 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 36 * multiplication. 37 */ 38 39#include <limits.h> 40 41#define BLOOM_SIZE 64 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) 45 46static inline size_t 47wcscspn_bloom1(size_t x) 48{ 49 return x % BLOOM_BITS; 50} 51 52static inline size_t 53wcscspn_bloom2(size_t x) 54{ 55 return (size_t)((uint32_t)(x * 2654435761U) / 56 (0x100000000ULL / BLOOM_BITS)); 57} 58 59static inline void 60wcsspn_bloom_init(size_t *bloom, const wchar_t *charset) 61{ 62 size_t val; 63 64 memset(bloom, 0, BLOOM_SIZE); 65 do { 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); 70 } 71 while (*++charset); 72} 73 74static inline int 75wcsspn_in_bloom(const size_t *bloom, wchar_t ch) 76{ 77 size_t val; 78 79 val = wcscspn_bloom1((size_t)ch); 80 if (bloom[val / BLOOM_DIV] & ((size_t)1 << (val % BLOOM_DIV))) 81 return 1; 82 val = wcscspn_bloom2((size_t)ch); 83 if (bloom[val / BLOOM_DIV] & ((size_t)1 << (val % BLOOM_DIV))) 84 return 1; 85 return 0; 86} 87