Move Dict constants to detail namespace

This commit is contained in:
Tim Wojtulewicz 2020-08-11 10:17:16 -07:00
parent c795f2014f
commit 47f3eb69fa
2 changed files with 12 additions and 20 deletions

View file

@ -253,16 +253,16 @@ int Dictionary::ThresholdEntries() const
// always resize when the current insertion causes it to be full. This ensures that the // always resize when the current insertion causes it to be full. This ensures that the
// current insertion should always be successful. // current insertion should always be successful.
int capacity = Capacity(); int capacity = Capacity();
if ( log2_buckets <= DICT_THRESHOLD_BITS ) if ( log2_buckets <= detail::DICT_THRESHOLD_BITS )
return capacity; //20 or less elements, 1.0, only size up when necessary. return capacity; //20 or less elements, 1.0, only size up when necessary.
return capacity - ( capacity>>DICT_LOAD_FACTOR_BITS ); return capacity - ( capacity >> detail::DICT_LOAD_FACTOR_BITS );
} }
zeek::detail::hash_t Dictionary::FibHash(zeek::detail::hash_t h) const zeek::detail::hash_t Dictionary::FibHash(zeek::detail::hash_t h) const
{ {
//GoldenRatio phi = (sqrt(5)+1)/2 = 1.6180339887... //GoldenRatio phi = (sqrt(5)+1)/2 = 1.6180339887...
//1/phi = phi - 1 //1/phi = phi - 1
h &= HASH_MASK; h &= detail::HASH_MASK;
h *= 11400714819323198485llu; //2^64/phi h *= 11400714819323198485llu; //2^64/phi
return h; return h;
} }
@ -967,7 +967,7 @@ void Dictionary::Remap()
if ( num_iterators ) if ( num_iterators )
return; return;
int left = DICT_REMAP_ENTRIES; int left = detail::DICT_REMAP_ENTRIES;
while ( remap_end >= 0 && left > 0 ) while ( remap_end >= 0 && left > 0 )
{ {
if ( ! table[remap_end].Empty() && Remap(remap_end) ) if ( ! table[remap_end].Empty() && Remap(remap_end) )

View file

@ -15,6 +15,13 @@ typedef void (*dict_delete_func)(void*);
namespace zeek { namespace zeek {
enum DictOrder { ORDERED, UNORDERED };
// A dict_delete_func that just calls delete.
extern void generic_delete_func(void*);
namespace detail {
// Default number of hash buckets in dictionary. The dictionary will increase the size // Default number of hash buckets in dictionary. The dictionary will increase the size
// of the hash table as needed. // of the hash table as needed.
constexpr uint32_t HASH_MASK = 0xFFFFFFFF; //only lower 32 bits. constexpr uint32_t HASH_MASK = 0xFFFFFFFF; //only lower 32 bits.
@ -24,38 +31,23 @@ constexpr uint32_t HASH_MASK = 0xFFFFFFFF; //only lower 32 bits.
// When incrementally resizing and remapping, it remaps DICT_REMAP_ENTRIES each step. Use // When incrementally resizing and remapping, it remaps DICT_REMAP_ENTRIES each step. Use
// 2 for debug. 16 is best for a release build. // 2 for debug. 16 is best for a release build.
#ifndef DICT_REMAP_ENTRIES
constexpr uint8_t DICT_REMAP_ENTRIES = 16; constexpr uint8_t DICT_REMAP_ENTRIES = 16;
#endif
// Load factor = 1 - 0.5 ^ LOAD_FACTOR_BITS. 0.75 is the optimal value for release builds. // Load factor = 1 - 0.5 ^ LOAD_FACTOR_BITS. 0.75 is the optimal value for release builds.
#ifndef DICT_LOAD_FACTOR_BITS
constexpr uint8_t DICT_LOAD_FACTOR_BITS = 2; constexpr uint8_t DICT_LOAD_FACTOR_BITS = 2;
#endif
// Default number of hash buckets in dictionary. The dictionary will // Default number of hash buckets in dictionary. The dictionary will
// increase the size of the hash table as needed. // increase the size of the hash table as needed.
#ifndef DEFAULT_DICT_SIZE
constexpr uint8_t DEFAULT_DICT_SIZE = 0; constexpr uint8_t DEFAULT_DICT_SIZE = 0;
#endif
// When log2_buckets > DICT_THRESHOLD_BITS, DICT_LOAD_FACTOR_BITS becomes effective. // When log2_buckets > DICT_THRESHOLD_BITS, DICT_LOAD_FACTOR_BITS becomes effective.
// Basically if dict size < 2^DICT_THRESHOLD_BITS + n, we size up only if necessary. // Basically if dict size < 2^DICT_THRESHOLD_BITS + n, we size up only if necessary.
#ifndef DICT_THRESHOLD_BITS
constexpr uint8_t DICT_THRESHOLD_BITS = 3; constexpr uint8_t DICT_THRESHOLD_BITS = 3;
#endif
// The value of an iteration cookie is the bucket and offset within the // The value of an iteration cookie is the bucket and offset within the
// bucket at which to start looking for the next value to return. // bucket at which to start looking for the next value to return.
constexpr uint16_t TOO_FAR_TO_REACH = 0xFFFF; constexpr uint16_t TOO_FAR_TO_REACH = 0xFFFF;
enum DictOrder { ORDERED, UNORDERED };
// A dict_delete_func that just calls delete.
extern void generic_delete_func(void*);
namespace detail {
/** /**
* An entry stored in the dictionary. * An entry stored in the dictionary.
*/ */
@ -160,7 +152,7 @@ public:
*/ */
class Dictionary { class Dictionary {
public: public:
explicit Dictionary(DictOrder ordering = UNORDERED, int initial_size = DEFAULT_DICT_SIZE); explicit Dictionary(DictOrder ordering = UNORDERED, int initial_size = detail::DEFAULT_DICT_SIZE);
~Dictionary(); ~Dictionary();
// Member functions for looking up a key, inserting/changing its // Member functions for looking up a key, inserting/changing its