Adjust Dict whitespace/style

This commit is contained in:
Jon Siwek 2020-08-08 11:24:49 -07:00 committed by Tim Wojtulewicz
parent ccf8e271a1
commit ba1611c21f
2 changed files with 20 additions and 19 deletions

View file

@ -66,7 +66,7 @@ public:
ASSERT_VALID(this); ASSERT_VALID(this);
if( robust ) if( robust )
{ {
d->cookies->erase(std::remove(d->cookies->begin(), d->cookies->end(), this),d->cookies->end()); d->cookies->erase(std::remove(d->cookies->begin(), d->cookies->end(), this), d->cookies->end());
delete inserted; delete inserted;
delete visited; delete visited;
} }
@ -302,7 +302,7 @@ int Dictionary::EndOfClusterByBucket(int bucket) const
{ {
ASSERT(bucket>=0 && bucket < Buckets()); ASSERT(bucket>=0 && bucket < Buckets());
int i = bucket; int i = bucket;
while ( i < Capacity() && ! table[i].Empty() && BucketByPosition(i) <= bucket) while ( i < Capacity() && ! table[i].Empty() && BucketByPosition(i) <= bucket )
i++; i++;
return i; return i;
} }
@ -369,7 +369,7 @@ void Dictionary::AssertValid() const
bool valid = true; bool valid = true;
int n = num_entries; int n = num_entries;
for ( int i = Capacity()-1; i >= 0; i-- ) for ( int i = Capacity()-1; i >= 0; i-- )
if ( table && ! table[i].Empty()) if ( table && ! table[i].Empty() )
n--; n--;
ASSERT((valid = (n==0))); ASSERT((valid = (n==0)));
@ -444,7 +444,7 @@ void Dictionary::DumpKeys() const
int max_distance = 0; int max_distance = 0;
DistanceStats(max_distance); DistanceStats(max_distance);
if( binary ) if ( binary )
{ {
sprintf(key_file, "%d.%d.%lu-%c.key", Length(), max_distance, MemoryAllocation()/Length(), rand()%26 + 'A'); sprintf(key_file, "%d.%d.%lu-%c.key", Length(), max_distance, MemoryAllocation()/Length(), rand()%26 + 'A');
std::ofstream f(key_file, std::ios::binary|std::ios::out|std::ios::trunc); std::ofstream f(key_file, std::ios::binary|std::ios::out|std::ios::trunc);
@ -493,7 +493,7 @@ void Dictionary::DistanceStats(int& max_distance, int* distances, int num_distan
void Dictionary::Dump(int level) const void Dictionary::Dump(int level) const
{ {
int key_size = 0; int key_size = 0;
for (int i=0; i<Capacity(); i++) for ( int i = 0; i < Capacity(); i++ )
{ {
if ( table[i].Empty() ) if ( table[i].Empty() )
continue; continue;
@ -561,7 +561,7 @@ void Dictionary::Clear()
{ {
for ( int i = Capacity() - 1; i >= 0; i-- ) for ( int i = Capacity() - 1; i >= 0; i-- )
{ {
if ( table[i].Empty()) if ( table[i].Empty() )
continue; continue;
if ( delete_func ) if ( delete_func )
delete_func(table[i].value); delete_func(table[i].value);
@ -592,8 +592,8 @@ void Dictionary::Clear()
void Dictionary::Init() void Dictionary::Init()
{ {
ASSERT(! table); ASSERT(! table);
table = (detail::DictEntry*)malloc(sizeof(detail::DictEntry)*Capacity(true)); table = (detail::DictEntry*)malloc(sizeof(detail::DictEntry) * Capacity(true));
for (int i = Capacity()-1; i >= 0; i--) for ( int i = Capacity() - 1; i >= 0; i-- )
table[i].SetEmpty(); table[i].SetEmpty();
} }
@ -635,7 +635,7 @@ int Dictionary::LinearLookupIndex(const void* key, int key_size, zeek::detail::h
int Dictionary::LookupIndex(const void* key, int key_size, zeek::detail::hash_t hash, int* insert_position, int* insert_distance) int Dictionary::LookupIndex(const void* key, int key_size, zeek::detail::hash_t hash, int* insert_position, int* insert_distance)
{ {
ASSERT_VALID(this); ASSERT_VALID(this);
if ( ! table) if ( ! table )
return -1; return -1;
int bucket = BucketByHash(hash, log2_buckets); int bucket = BucketByHash(hash, log2_buckets);
@ -661,7 +661,7 @@ int Dictionary::LookupIndex(const void* key, int key_size, zeek::detail::hash_t
{ {
ASSERT(position == linear_position);//same as linearLookup ASSERT(position == linear_position);//same as linearLookup
//remap immediately if no iteration is on. //remap immediately if no iteration is on.
if ( !num_iterators ) if ( ! num_iterators )
{ {
Remap(position, &position); Remap(position, &position);
ASSERT(position == LookupIndex(key, key_size, hash)); ASSERT(position == LookupIndex(key, key_size, hash));
@ -814,7 +814,7 @@ void Dictionary::InsertAndRelocate(detail::DictEntry& entry, int insert_position
if ( table[insert_position].Empty() ) if ( table[insert_position].Empty() )
{ //the condition to end the loop. { //the condition to end the loop.
table[insert_position] = entry; table[insert_position] = entry;
if (last_affected_position) if ( last_affected_position )
*last_affected_position = insert_position; *last_affected_position = insert_position;
return; return;
} }
@ -851,7 +851,7 @@ void Dictionary::SizeUp()
int prev_capacity = Capacity(); int prev_capacity = Capacity();
log2_buckets++; log2_buckets++;
int capacity = Capacity(); int capacity = Capacity();
table = (detail::DictEntry*)realloc(table, capacity*sizeof(detail::DictEntry)); table = (detail::DictEntry*)realloc(table, capacity * sizeof(detail::DictEntry));
for ( int i = prev_capacity; i < capacity; i++ ) for ( int i = prev_capacity; i < capacity; i++ )
table[i].SetEmpty(); table[i].SetEmpty();
@ -1118,17 +1118,18 @@ void* Dictionary::NextEntryNonConst(zeek::detail::HashKey*& h, IterCookie*& c, b
return v; return v;
} }
IterCookie* Dictionary::InitForIteration() const IterCookie* Dictionary::InitForIteration() const
{ {
Dictionary* dp = const_cast<Dictionary*>(this); Dictionary* dp = const_cast<Dictionary*>(this);
return dp->InitForIterationNonConst(); return dp->InitForIterationNonConst();
} }
void* Dictionary::NextEntry(zeek::detail::HashKey*& h, IterCookie*& cookie, bool return_hash) const void* Dictionary::NextEntry(zeek::detail::HashKey*& h, IterCookie*& cookie, bool return_hash) const
{ {
Dictionary* dp = const_cast<Dictionary*>(this); Dictionary* dp = const_cast<Dictionary*>(this);
return dp->NextEntryNonConst(h, cookie, return_hash); return dp->NextEntryNonConst(h, cookie, return_hash);
} }
void Dictionary::StopIteration(IterCookie* cookie) const void Dictionary::StopIteration(IterCookie* cookie) const
{ {
Dictionary* dp = const_cast<Dictionary*>(this); Dictionary* dp = const_cast<Dictionary*>(this);

View file

@ -59,7 +59,7 @@ namespace detail {
/** /**
* An entry stored in the dictionary. * An entry stored in the dictionary.
*/ */
class DictEntry{ class DictEntry {
public: public:
#ifdef DEBUG #ifdef DEBUG
@ -127,7 +127,7 @@ public:
SetEmpty(); SetEmpty();
} }
const char* GetKey() const { return key_size <= 8? key_here : key; } const char* GetKey() const { return key_size <= 8 ? key_here : key; }
bool Equal(const char* arg_key, int arg_key_size, zeek::detail::hash_t arg_hash) const bool Equal(const char* arg_key, int arg_key_size, zeek::detail::hash_t arg_hash) const
{//only 40-bit hash comparison. {//only 40-bit hash comparison.
@ -158,7 +158,7 @@ public:
* the keys but not the values. The dictionary size will be bounded at around 100K. 1M * the keys but not the values. The dictionary size will be bounded at around 100K. 1M
* entries is the absolute limit. Only Connections use that many entries, and that is rare. * entries is the absolute limit. Only Connections use that many entries, and that is rare.
*/ */
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 = DEFAULT_DICT_SIZE);
~Dictionary(); ~Dictionary();
@ -249,21 +249,21 @@ public:
size_t MemoryAllocation() const; size_t MemoryAllocation() const;
/// The capacity of the table, Buckets + Overflow Size. /// The capacity of the table, Buckets + Overflow Size.
int Capacity(bool expected=false) const; int Capacity(bool expected = false) const;
//Debugging //Debugging
#ifdef DEBUG #ifdef DEBUG
void AssertValid() const; void AssertValid() const;
#endif//DEBUG #endif//DEBUG
void Dump(int level=0) const; void Dump(int level=0) const;
void DistanceStats(int& max_distance, int* distances=0, int num_distances=0) const; void DistanceStats(int& max_distance, int* distances = 0, int num_distances = 0) const;
void DumpKeys() const; void DumpKeys() const;
private: private:
friend zeek::IterCookie; friend zeek::IterCookie;
/// Buckets of the table, not including overflow size. /// Buckets of the table, not including overflow size.
int Buckets(bool expected=false) const; int Buckets(bool expected = false) const;
//bucket math //bucket math
int Log2(int num) const; int Log2(int num) const;