mirror of
https://github.com/zeek/zeek.git
synced 2025-10-08 09:38:19 +00:00
Code cleanup in Dict.h
This commit is contained in:
parent
817eb2fd87
commit
a69463ac46
2 changed files with 30 additions and 50 deletions
44
src/Dict.cc
44
src/Dict.cc
|
@ -27,8 +27,7 @@ TEST_SUITE_BEGIN("Dict");
|
|||
|
||||
class DictEntry {
|
||||
public:
|
||||
DictEntry(void* k, int l, hash_t h, void* val)
|
||||
{ key = k; len = l; hash = h; value = val; }
|
||||
DictEntry(void* k, int l, hash_t h, void* val) : key(k), len(l), hash(h), value(val) {}
|
||||
|
||||
~DictEntry()
|
||||
{
|
||||
|
@ -45,17 +44,11 @@ public:
|
|||
// bucket at which to start looking for the next value to return.
|
||||
class IterCookie {
|
||||
public:
|
||||
IterCookie(int b, int o)
|
||||
{
|
||||
bucket = b;
|
||||
offset = o;
|
||||
ttbl = 0;
|
||||
num_buckets_p = 0;
|
||||
}
|
||||
IterCookie(int b, int o) : bucket(b), offset(o) {}
|
||||
|
||||
int bucket, offset;
|
||||
PList<DictEntry>** ttbl;
|
||||
const int* num_buckets_p;
|
||||
PList<DictEntry>** ttbl = nullptr;
|
||||
const int* num_buckets_p = 0;
|
||||
PList<DictEntry> inserted; // inserted while iterating
|
||||
};
|
||||
|
||||
|
@ -195,22 +188,8 @@ TEST_CASE("dict iteration")
|
|||
|
||||
Dictionary::Dictionary(dict_order ordering, int initial_size)
|
||||
{
|
||||
tbl = 0;
|
||||
tbl2 = 0;
|
||||
|
||||
if ( ordering == ORDERED )
|
||||
order = new PList<DictEntry>;
|
||||
else
|
||||
order = 0;
|
||||
|
||||
delete_func = 0;
|
||||
tbl_next_ind = 0;
|
||||
|
||||
cumulative_entries = 0;
|
||||
num_buckets = num_entries = max_num_entries = thresh_entries = 0;
|
||||
den_thresh = 0;
|
||||
num_buckets2 = num_entries2 = max_num_entries2 = thresh_entries2 = 0;
|
||||
den_thresh2 = 0;
|
||||
|
||||
if ( initial_size > 0 )
|
||||
Init(initial_size);
|
||||
|
@ -225,8 +204,8 @@ Dictionary::~Dictionary()
|
|||
void Dictionary::Clear()
|
||||
{
|
||||
DeInit();
|
||||
tbl = 0;
|
||||
tbl2 = 0;
|
||||
tbl = nullptr;
|
||||
tbl2 = nullptr;
|
||||
}
|
||||
|
||||
void Dictionary::DeInit()
|
||||
|
@ -249,8 +228,9 @@ void Dictionary::DeInit()
|
|||
}
|
||||
|
||||
delete [] tbl;
|
||||
tbl = nullptr;
|
||||
|
||||
if ( tbl2 == 0 )
|
||||
if ( ! tbl2 )
|
||||
return;
|
||||
|
||||
for ( int i = 0; i < num_buckets2; ++i )
|
||||
|
@ -268,7 +248,7 @@ void Dictionary::DeInit()
|
|||
}
|
||||
|
||||
delete [] tbl2;
|
||||
tbl2 = 0;
|
||||
tbl2 = nullptr;
|
||||
}
|
||||
|
||||
void* Dictionary::Lookup(const void* key, int key_size, hash_t hash) const
|
||||
|
@ -524,7 +504,7 @@ void Dictionary::Init(int size)
|
|||
tbl = new PList<DictEntry>*[num_buckets];
|
||||
|
||||
for ( int i = 0; i < num_buckets; ++i )
|
||||
tbl[i] = 0;
|
||||
tbl[i] = nullptr;
|
||||
|
||||
max_num_entries = num_entries = 0;
|
||||
SetDensityThresh(DEFAULT_DENSITY_THRESH);
|
||||
|
@ -536,7 +516,7 @@ void Dictionary::Init2(int size)
|
|||
tbl2 = new PList<DictEntry>*[num_buckets2];
|
||||
|
||||
for ( int i = 0; i < num_buckets2; ++i )
|
||||
tbl2[i] = 0;
|
||||
tbl2[i] = nullptr;
|
||||
|
||||
max_num_entries2 = num_entries2 = 0;
|
||||
}
|
||||
|
@ -713,7 +693,7 @@ void Dictionary::FinishChangeSize()
|
|||
delete [] tbl;
|
||||
|
||||
tbl = tbl2;
|
||||
tbl2 = 0;
|
||||
tbl2 = nullptr;
|
||||
|
||||
num_buckets = num_buckets2;
|
||||
num_entries = num_entries2;
|
||||
|
|
36
src/Dict.h
36
src/Dict.h
|
@ -11,7 +11,7 @@ class IterCookie;
|
|||
|
||||
// Type indicating whether the dictionary should keep track of the order
|
||||
// of insertions.
|
||||
typedef enum { ORDERED, UNORDERED } dict_order;
|
||||
enum dict_order { ORDERED, UNORDERED };
|
||||
|
||||
// Type for function to be called when deleting elements.
|
||||
typedef void (*dict_delete_func)(void*);
|
||||
|
@ -70,7 +70,7 @@ public:
|
|||
}
|
||||
|
||||
// True if the dictionary is ordered, false otherwise.
|
||||
int IsOrdered() const { return order != 0; }
|
||||
bool IsOrdered() const { return order != 0; }
|
||||
|
||||
// If the dictionary is ordered then returns the n'th entry's value;
|
||||
// the second method also returns the key. The first entry inserted
|
||||
|
@ -158,26 +158,26 @@ private:
|
|||
// When we're resizing, we'll have tbl (old) and tbl2 (new)
|
||||
// tbl_next_ind keeps track of how much we've moved to tbl2
|
||||
// (it's the next index we're going to move).
|
||||
PList<DictEntry>** tbl;
|
||||
int num_buckets;
|
||||
int num_entries;
|
||||
int max_num_entries;
|
||||
uint64_t cumulative_entries;
|
||||
double den_thresh;
|
||||
int thresh_entries;
|
||||
PList<DictEntry>** tbl = nullptr;
|
||||
int num_buckets = 0;
|
||||
int num_entries = 0;
|
||||
int max_num_entries = 0;
|
||||
uint64_t cumulative_entries = 0;
|
||||
double den_thresh = 0.0;
|
||||
int thresh_entries = 0;
|
||||
|
||||
// Resizing table (replicates tbl above).
|
||||
PList<DictEntry>** tbl2;
|
||||
int num_buckets2;
|
||||
int num_entries2;
|
||||
int max_num_entries2;
|
||||
double den_thresh2;
|
||||
int thresh_entries2;
|
||||
PList<DictEntry>** tbl2 = nullptr;
|
||||
int num_buckets2 = 0;
|
||||
int num_entries2 = 0;
|
||||
int max_num_entries2 = 0;
|
||||
double den_thresh2 = 0;
|
||||
int thresh_entries2 = 0;
|
||||
|
||||
hash_t tbl_next_ind;
|
||||
hash_t tbl_next_ind = 0;
|
||||
|
||||
PList<DictEntry>* order;
|
||||
dict_delete_func delete_func;
|
||||
PList<DictEntry>* order = nullptr;
|
||||
dict_delete_func delete_func = nullptr;
|
||||
|
||||
PList<IterCookie> cookies;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue