mirror of
https://github.com/zeek/zeek.git
synced 2025-10-07 00:58:19 +00:00
Update H3 documentation (and minor style nits.)
This commit is contained in:
parent
529d120376
commit
a6d7b7856e
1 changed files with 31 additions and 29 deletions
60
src/H3.h
60
src/H3.h
|
@ -49,9 +49,9 @@
|
||||||
// hash a substring of the data. Hashes of substrings can be bitwise-XOR'ed
|
// hash a substring of the data. Hashes of substrings can be bitwise-XOR'ed
|
||||||
// together to get the same result as hashing the full string.
|
// together to get the same result as hashing the full string.
|
||||||
// Any number of hash functions can be created by creating new instances of H3,
|
// Any number of hash functions can be created by creating new instances of H3,
|
||||||
// with the same or different template parameters. The hash function is
|
// with the same or different template parameters. The hash function
|
||||||
// randomly generated using bro_random(); you must call init_random_seed()
|
// constructor takes a seed as argument which defaults to a call to
|
||||||
// before the H3 constructor if you wish to seed it.
|
// bro_random().
|
||||||
|
|
||||||
|
|
||||||
#ifndef H3_H
|
#ifndef H3_H
|
||||||
|
@ -62,34 +62,34 @@
|
||||||
// The number of values representable by a byte.
|
// The number of values representable by a byte.
|
||||||
#define H3_BYTE_RANGE (UCHAR_MAX+1)
|
#define H3_BYTE_RANGE (UCHAR_MAX+1)
|
||||||
|
|
||||||
template<class T, int N> class H3 {
|
template <typename T, int N>
|
||||||
T byte_lookup[N][H3_BYTE_RANGE];
|
class H3 {
|
||||||
public:
|
public:
|
||||||
H3(T seed = bro_random())
|
H3(T seed = bro_random())
|
||||||
|
{
|
||||||
|
T bit_lookup[N * CHAR_BIT];
|
||||||
|
|
||||||
|
for ( size_t bit = 0; bit < N * CHAR_BIT; bit++ )
|
||||||
{
|
{
|
||||||
T bit_lookup[N * CHAR_BIT];
|
bit_lookup[bit] = 0;
|
||||||
|
for ( size_t i = 0; i < sizeof(T)/2; i++ )
|
||||||
for ( size_t bit = 0; bit < N * CHAR_BIT; bit++ )
|
// assume random() returns at least 16 random bits
|
||||||
{
|
bit_lookup[bit] = (bit_lookup[bit] << 16) | (seed & 0xFFFF);
|
||||||
bit_lookup[bit] = 0;
|
|
||||||
for ( size_t i = 0; i < sizeof(T)/2; i++ )
|
|
||||||
// assume random() returns at least 16 random bits
|
|
||||||
bit_lookup[bit] = (bit_lookup[bit] << 16) | (seed & 0xFFFF);
|
|
||||||
}
|
|
||||||
|
|
||||||
for ( size_t byte = 0; byte < N; byte++ )
|
|
||||||
{
|
|
||||||
for ( unsigned val = 0; val < H3_BYTE_RANGE; val++ )
|
|
||||||
{
|
|
||||||
byte_lookup[byte][val] = 0;
|
|
||||||
for ( size_t bit = 0; bit < CHAR_BIT; bit++ )
|
|
||||||
// Does this mean byte_lookup[*][0] == 0? -RP
|
|
||||||
if (val & (1 << bit))
|
|
||||||
byte_lookup[byte][val] ^= bit_lookup[byte*CHAR_BIT+bit];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for ( size_t byte = 0; byte < N; byte++ )
|
||||||
|
{
|
||||||
|
for ( unsigned val = 0; val < H3_BYTE_RANGE; val++ )
|
||||||
|
{
|
||||||
|
byte_lookup[byte][val] = 0;
|
||||||
|
for ( size_t bit = 0; bit < CHAR_BIT; bit++ )
|
||||||
|
// Does this mean byte_lookup[*][0] == 0? -RP
|
||||||
|
if (val & (1 << bit))
|
||||||
|
byte_lookup[byte][val] ^= bit_lookup[byte*CHAR_BIT+bit];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
T operator()(const void* data, size_t size, size_t offset = 0) const
|
T operator()(const void* data, size_t size, size_t offset = 0) const
|
||||||
{
|
{
|
||||||
const unsigned char *p = static_cast<const unsigned char*>(data);
|
const unsigned char *p = static_cast<const unsigned char*>(data);
|
||||||
|
@ -97,7 +97,7 @@ public:
|
||||||
|
|
||||||
// loop optmized with Duff's Device
|
// loop optmized with Duff's Device
|
||||||
register unsigned n = (size + 7) / 8;
|
register unsigned n = (size + 7) / 8;
|
||||||
switch (size % 8) {
|
switch ( size % 8 ) {
|
||||||
case 0: do { result ^= byte_lookup[offset++][*p++];
|
case 0: do { result ^= byte_lookup[offset++][*p++];
|
||||||
case 7: result ^= byte_lookup[offset++][*p++];
|
case 7: result ^= byte_lookup[offset++][*p++];
|
||||||
case 6: result ^= byte_lookup[offset++][*p++];
|
case 6: result ^= byte_lookup[offset++][*p++];
|
||||||
|
@ -106,11 +106,13 @@ public:
|
||||||
case 3: result ^= byte_lookup[offset++][*p++];
|
case 3: result ^= byte_lookup[offset++][*p++];
|
||||||
case 2: result ^= byte_lookup[offset++][*p++];
|
case 2: result ^= byte_lookup[offset++][*p++];
|
||||||
case 1: result ^= byte_lookup[offset++][*p++];
|
case 1: result ^= byte_lookup[offset++][*p++];
|
||||||
} while (--n > 0);
|
} while ( --n > 0 );
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
private:
|
||||||
|
T byte_lookup[N][H3_BYTE_RANGE];
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //H3_H
|
#endif //H3_H
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue