mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 06:38:20 +00:00
use iterator-based idiom for check-if-present-then-access
This commit is contained in:
parent
735d584d9f
commit
84423369b4
12 changed files with 86 additions and 59 deletions
|
@ -13,9 +13,10 @@ shared_ptr<CPP_InitInfo> CPPCompile::RegisterAttributes(const AttributesPtr& att
|
|||
return nullptr;
|
||||
|
||||
auto a = attrs.get();
|
||||
auto pa = processed_attrs.find(a);
|
||||
|
||||
if ( processed_attrs.count(a) > 0 )
|
||||
return processed_attrs[a];
|
||||
if ( pa != processed_attrs.end() )
|
||||
return pa->second;
|
||||
|
||||
attributes.AddKey(attrs);
|
||||
|
||||
|
@ -41,9 +42,10 @@ shared_ptr<CPP_InitInfo> CPPCompile::RegisterAttributes(const AttributesPtr& att
|
|||
shared_ptr<CPP_InitInfo> CPPCompile::RegisterAttr(const AttrPtr& attr)
|
||||
{
|
||||
auto a = attr.get();
|
||||
auto pa = processed_attr.find(a);
|
||||
|
||||
if ( processed_attr.count(a) > 0 )
|
||||
return processed_attr[a];
|
||||
if ( pa != processed_attr.end() )
|
||||
return pa->second;
|
||||
|
||||
const auto& e = a->GetExpr();
|
||||
if ( e && ! IsSimpleInitExpr(e) )
|
||||
|
|
|
@ -212,26 +212,30 @@ public:
|
|||
// an offset into the global vector that will hold these.
|
||||
int TrackString(std::string s)
|
||||
{
|
||||
if ( tracked_strings.count(s) == 0 )
|
||||
{
|
||||
tracked_strings[s] = ordered_tracked_strings.size();
|
||||
ordered_tracked_strings.emplace_back(s);
|
||||
}
|
||||
auto ts = tracked_strings.find(s);
|
||||
if ( ts != tracked_strings.end() )
|
||||
return ts->second;
|
||||
|
||||
return tracked_strings[s];
|
||||
int offset = ordered_tracked_strings.size();
|
||||
tracked_strings[s] = offset;
|
||||
ordered_tracked_strings.emplace_back(s);
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
// Tracks a profile hash value needed for initialization. Returns
|
||||
// an offset into the global vector that will hold these.
|
||||
int TrackHash(p_hash_type h)
|
||||
{
|
||||
if ( tracked_hashes.count(h) == 0 )
|
||||
{
|
||||
tracked_hashes[h] = ordered_tracked_hashes.size();
|
||||
ordered_tracked_hashes.emplace_back(h);
|
||||
}
|
||||
auto th = tracked_hashes.find(h);
|
||||
if ( th != tracked_hashes.end() )
|
||||
return th->second;
|
||||
|
||||
return tracked_hashes[h];
|
||||
int offset = ordered_tracked_hashes.size();
|
||||
tracked_hashes[h] = offset;
|
||||
ordered_tracked_hashes.emplace_back(h);
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
// Returns the hash associated with a given function body.
|
||||
|
|
|
@ -17,12 +17,13 @@ shared_ptr<CPP_InitInfo> CPPCompile::RegisterConstant(const ValPtr& vp, int& con
|
|||
cv_indices.push_back(vp);
|
||||
|
||||
auto v = vp.get();
|
||||
auto cv = const_vals.find(v);
|
||||
|
||||
if ( const_vals.count(v) > 0 )
|
||||
if ( cv != const_vals.end() )
|
||||
{
|
||||
// Already did this one.
|
||||
consts_offset = const_offsets[v];
|
||||
return const_vals[v];
|
||||
return cv->second;
|
||||
}
|
||||
|
||||
// Formulate a key that's unique per distinct constant.
|
||||
|
@ -50,11 +51,12 @@ shared_ptr<CPP_InitInfo> CPPCompile::RegisterConstant(const ValPtr& vp, int& con
|
|||
c_desc = d.Description();
|
||||
}
|
||||
|
||||
if ( constants.count(c_desc) > 0 )
|
||||
auto c = constants.find(c_desc);
|
||||
if ( c != constants.end() )
|
||||
{
|
||||
const_vals[v] = constants[c_desc];
|
||||
const_vals[v] = c->second;
|
||||
consts_offset = const_offsets[v] = constants_offsets[c_desc];
|
||||
return const_vals[v];
|
||||
return c->second;
|
||||
}
|
||||
|
||||
auto tag = t->Tag();
|
||||
|
|
|
@ -258,8 +258,9 @@ void CPPCompile::RegisterCompiledBody(const string& f)
|
|||
|
||||
// Build up an initializer of the events relevant to the function.
|
||||
string events;
|
||||
if ( body_events.count(f) > 0 )
|
||||
for ( const auto& e : body_events[f] )
|
||||
auto be = body_events.find(f);
|
||||
if ( be != body_events.end() )
|
||||
for ( const auto& e : be->second )
|
||||
{
|
||||
if ( events.size() > 0 )
|
||||
events += ", ";
|
||||
|
@ -278,8 +279,9 @@ void CPPCompile::RegisterCompiledBody(const string& f)
|
|||
// same binary).
|
||||
h = merge_p_hashes(h, p_hash(cf_locs[f]));
|
||||
|
||||
ASSERT(func_index.count(f) > 0);
|
||||
auto type_signature = casting_index[func_index[f]];
|
||||
auto fi = func_index.find(f);
|
||||
ASSERT(fi != func_index.end());
|
||||
auto type_signature = casting_index[fi->second];
|
||||
Emit("\tCPP_RegisterBody(\"%s\", (void*) %s, %s, %s, %s, std::vector<std::string>(%s)),", f, f,
|
||||
Fmt(type_signature), Fmt(p), Fmt(h), events);
|
||||
}
|
||||
|
|
|
@ -1173,23 +1173,25 @@ string CPPCompile::GenField(const ExprPtr& rec, int field)
|
|||
// Need to dynamically map the field.
|
||||
int mapping_slot;
|
||||
|
||||
if ( record_field_mappings.count(rt) > 0 && record_field_mappings[rt].count(field) > 0 )
|
||||
auto rfm = record_field_mappings.find(rt);
|
||||
if ( rfm != record_field_mappings.end() && rfm->second.count(field) > 0 )
|
||||
// We're already tracking this field.
|
||||
mapping_slot = record_field_mappings[rt][field];
|
||||
mapping_slot = rfm->second[field];
|
||||
|
||||
else
|
||||
{
|
||||
// New mapping.
|
||||
mapping_slot = num_rf_mappings++;
|
||||
|
||||
ASSERT(processed_types.count(rt) > 0);
|
||||
auto rt_offset = processed_types[rt]->Offset();
|
||||
auto pt = processed_types.find(rt);
|
||||
ASSERT(pt != processed_types.end());
|
||||
auto rt_offset = pt->second->Offset();
|
||||
string field_name = rt->FieldName(field);
|
||||
field_decls.emplace_back(pair(rt_offset, rt->FieldDecl(field)));
|
||||
|
||||
if ( record_field_mappings.count(rt) > 0 )
|
||||
if ( rfm != record_field_mappings.end() )
|
||||
// We're already tracking this record.
|
||||
record_field_mappings[rt][field] = mapping_slot;
|
||||
rfm->second[field] = mapping_slot;
|
||||
else
|
||||
{
|
||||
// Need to start tracking this record.
|
||||
|
@ -1214,9 +1216,10 @@ string CPPCompile::GenEnum(const TypePtr& t, const ValPtr& ev)
|
|||
// Need to dynamically map the access.
|
||||
int mapping_slot;
|
||||
|
||||
if ( enum_val_mappings.count(et) > 0 && enum_val_mappings[et].count(v) > 0 )
|
||||
auto evm = enum_val_mappings.find(et);
|
||||
if ( evm != enum_val_mappings.end() && evm->second.count(v) > 0 )
|
||||
// We're already tracking this value.
|
||||
mapping_slot = enum_val_mappings[et][v];
|
||||
mapping_slot = evm->second[v];
|
||||
|
||||
else
|
||||
{
|
||||
|
@ -1226,10 +1229,10 @@ string CPPCompile::GenEnum(const TypePtr& t, const ValPtr& ev)
|
|||
string enum_name = et->Lookup(v);
|
||||
enum_names.emplace_back(pair(TypeOffset(t), move(enum_name)));
|
||||
|
||||
if ( enum_val_mappings.count(et) > 0 )
|
||||
if ( evm != enum_val_mappings.end() )
|
||||
{
|
||||
// We're already tracking this enum.
|
||||
enum_val_mappings[et][v] = mapping_slot;
|
||||
evm->second[v] = mapping_slot;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
@ -233,12 +233,14 @@ string CPPCompile::BodyName(const FuncInfo& func)
|
|||
|
||||
p_hash_type CPPCompile::BodyHash(const Stmt* body)
|
||||
{
|
||||
ASSERT(body_names.count(body) > 0);
|
||||
auto bn = body_names.find(body);
|
||||
ASSERT(bn != body_names.end());
|
||||
|
||||
auto& body_name = body_names[body];
|
||||
ASSERT(body_hashes.count(body_name) > 0);
|
||||
auto& body_name = bn->second;
|
||||
auto bh = body_hashes.find(body_name);
|
||||
ASSERT(bh != body_hashes.end());
|
||||
|
||||
return body_hashes[body_name];
|
||||
return bh->second;
|
||||
}
|
||||
|
||||
string CPPCompile::GenArgs(const RecordTypePtr& params, const Expr* e)
|
||||
|
|
|
@ -18,8 +18,9 @@ std::shared_ptr<CPP_InitInfo> CPPCompile::RegisterInitExpr(const ExprPtr& ep)
|
|||
{
|
||||
auto ename = InitExprName(ep);
|
||||
|
||||
if ( init_infos.count(ename) )
|
||||
return init_infos[ename];
|
||||
auto ii = init_infos.find(ename);
|
||||
if ( ii != init_infos.end() )
|
||||
return ii->second;
|
||||
|
||||
auto wrapper_cl = string("wrapper_") + ename + "_cl";
|
||||
|
||||
|
@ -247,8 +248,9 @@ void CPPCompile::GenStandaloneActivation()
|
|||
// We didn't wind up compiling it.
|
||||
continue;
|
||||
|
||||
ASSERT(body_hashes.count(bname) > 0);
|
||||
func_bodies[f].push_back(body_hashes[bname]);
|
||||
auto bh = body_hashes.find(bname);
|
||||
ASSERT(bh != body_hashes.end());
|
||||
func_bodies[f].push_back(bh->second);
|
||||
}
|
||||
|
||||
for ( auto& fb : func_bodies )
|
||||
|
|
|
@ -306,10 +306,13 @@ AttrInfo::AttrInfo(CPPCompile* _c, const AttrPtr& attr) : CompoundItemInfo(_c)
|
|||
|
||||
AttrsInfo::AttrsInfo(CPPCompile* _c, const AttributesPtr& _attrs) : CompoundItemInfo(_c)
|
||||
{
|
||||
const auto& pas = c->ProcessedAttr();
|
||||
|
||||
for ( const auto& a : _attrs->GetAttrs() )
|
||||
{
|
||||
ASSERT(c->ProcessedAttr().count(a.get()) > 0);
|
||||
const auto& gi = c->ProcessedAttr().at(a.get());
|
||||
auto pa = pas.find(a.get());
|
||||
ASSERT(pa != pas.end());
|
||||
const auto& gi = pa->second;
|
||||
init_cohort = max(init_cohort, gi->InitCohort() + 1);
|
||||
vals.emplace_back(Fmt(gi->Offset()));
|
||||
}
|
||||
|
|
|
@ -154,8 +154,9 @@ void activate_bodies__CPP(const char* fn, const char* module, bool exported, Typ
|
|||
continue;
|
||||
|
||||
// Add in the new body.
|
||||
ASSERT(compiled_scripts.count(h) > 0);
|
||||
auto cs = compiled_scripts[h];
|
||||
auto csi = compiled_scripts.find(h);
|
||||
ASSERT(csi != compiled_scripts.end());
|
||||
auto cs = csi->second;
|
||||
|
||||
f->AddBody(cs.body, no_inits, num_params, cs.priority);
|
||||
added_bodies[fn].insert(h);
|
||||
|
@ -220,9 +221,10 @@ FuncValPtr lookup_func__CPP(string name, vector<p_hash_type> hashes, const TypeP
|
|||
|
||||
for ( auto h : hashes )
|
||||
{
|
||||
ASSERT(compiled_scripts.count(h) > 0);
|
||||
auto cs = compiled_scripts.find(h);
|
||||
ASSERT(cs != compiled_scripts.end());
|
||||
|
||||
const auto& f = compiled_scripts[h];
|
||||
const auto& f = cs->second;
|
||||
bodies.push_back(f.body);
|
||||
priorities.push_back(f.priority);
|
||||
|
||||
|
|
|
@ -52,8 +52,9 @@ template <class T> string CPPTracker<T>::KeyName(const T* key)
|
|||
ASSERT(hash != 0);
|
||||
|
||||
auto rep = reps[hash];
|
||||
if ( gi_s.count(rep) > 0 )
|
||||
return gi_s[rep]->Name();
|
||||
auto gi = gi_s.find(rep);
|
||||
if ( gi != gi_s.end() )
|
||||
return gi->second->Name();
|
||||
|
||||
auto index = map2[hash];
|
||||
|
||||
|
|
|
@ -280,8 +280,9 @@ shared_ptr<CPP_InitInfo> CPPCompile::RegisterType(const TypePtr& tp)
|
|||
{
|
||||
auto t = TypeRep(tp);
|
||||
|
||||
if ( processed_types.count(t) > 0 )
|
||||
return processed_types[t];
|
||||
auto pt = processed_types.find(t);
|
||||
if ( pt != processed_types.end() )
|
||||
return pt->second;
|
||||
|
||||
processed_types[t] = nullptr;
|
||||
|
||||
|
|
|
@ -120,7 +120,9 @@ void CPPCompile::CreateGlobal(const ID* g)
|
|||
|
||||
std::shared_ptr<CPP_InitInfo> CPPCompile::RegisterGlobal(const ID* g)
|
||||
{
|
||||
if ( global_gis.count(g) == 0 )
|
||||
auto gg = global_gis.find(g);
|
||||
|
||||
if ( gg == global_gis.end() )
|
||||
{
|
||||
auto gn = string(g->Name());
|
||||
|
||||
|
@ -131,9 +133,10 @@ std::shared_ptr<CPP_InitInfo> CPPCompile::RegisterGlobal(const ID* g)
|
|||
auto gi = make_shared<GlobalInitInfo>(this, g, globals[gn]);
|
||||
global_id_info->AddInstance(gi);
|
||||
global_gis[g] = gi;
|
||||
return gi;
|
||||
}
|
||||
|
||||
return global_gis[g];
|
||||
else
|
||||
return gg->second;
|
||||
}
|
||||
|
||||
void CPPCompile::AddBiF(const ID* b, bool is_var)
|
||||
|
@ -184,9 +187,9 @@ const string& CPPCompile::IDNameStr(const ID* id)
|
|||
return globals[g];
|
||||
}
|
||||
|
||||
ASSERT(locals.count(id) > 0);
|
||||
|
||||
return locals[id];
|
||||
auto l = locals.find(id);
|
||||
ASSERT(l != locals.end());
|
||||
return l->second;
|
||||
}
|
||||
|
||||
string CPPCompile::LocalName(const ID* l) const
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue