From bfd33e96f6c566704f9b2c4a05ccf47241831323 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Fonseca?= Date: Sun, 28 Nov 2010 12:32:12 +0000 Subject: [PATCH] Use vectors instead of maps. --- trace_parser.hpp | 51 ++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/trace_parser.hpp b/trace_parser.hpp index f8eb9b5..6b519a0 100644 --- a/trace_parser.hpp +++ b/trace_parser.hpp @@ -54,16 +54,16 @@ protected: typedef std::list CallList; CallList calls; - typedef std::map FunctionMap; + typedef std::vector FunctionMap; FunctionMap functions; - typedef std::map StructMap; + typedef std::vector StructMap; StructMap structs; - typedef std::map EnumMap; + typedef std::vector EnumMap; EnumMap enums; - typedef std::map BitmaskMap; + typedef std::vector BitmaskMap; BitmaskMap bitmasks; unsigned next_call_no; @@ -123,13 +123,25 @@ public: } } while(true); } - + + /** + * Helper function to lookup an ID in a vector, resizing the vector if it doesn't fit. + */ + template + T *lookup(std::vector &map, size_t index) { + if (index >= map.size()) { + map.resize(index + 1); + return NULL; + } else { + return map[index]; + } + } + void parse_enter(void) { size_t id = read_uint(); - Call::Signature *sig; - FunctionMap::const_iterator it = functions.find(id); - if (it == functions.end()) { + Call::Signature *sig = lookup(functions, id); + if (!sig) { sig = new Call::Signature; sig->name = read_string(); unsigned size = read_uint(); @@ -137,8 +149,6 @@ public: sig->arg_names.push_back(read_string()); } functions[id] = sig; - } else { - sig = it->second; } assert(sig); @@ -265,15 +275,12 @@ public: Value *parse_enum() { size_t id = read_uint(); - Enum *sig; - EnumMap::const_iterator it = enums.find(id); - if (it == enums.end()) { + Enum *sig = lookup(enums, id); + if (!sig) { std::string name = read_string(); Value *value = parse_value(); sig = new Enum(name, value); enums[id] = sig; - } else { - sig = it->second; } assert(sig); return sig; @@ -281,9 +288,8 @@ public: Value *parse_bitmask() { size_t id = read_uint(); - Bitmask::Signature *sig; - BitmaskMap::const_iterator it = bitmasks.find(id); - if (it == bitmasks.end()) { + Bitmask::Signature *sig = lookup(bitmasks, id); + if (!sig) { size_t size = read_uint(); sig = new Bitmask::Signature(size); for (Bitmask::Signature::iterator it = sig->begin(); it != sig->end(); ++it) { @@ -292,8 +298,6 @@ public: assert(it->second); } bitmasks[id] = sig; - } else { - sig = it->second; } assert(sig); @@ -323,9 +327,8 @@ public: Value *parse_struct() { size_t id = read_uint(); - Struct::Signature *sig; - StructMap::const_iterator it = structs.find(id); - if (it == structs.end()) { + Struct::Signature *sig = lookup(structs, id); + if (!sig) { sig = new Struct::Signature; sig->name = read_string(); unsigned size = read_uint(); @@ -333,8 +336,6 @@ public: sig->member_names.push_back(read_string()); } structs[id] = sig; - } else { - sig = it->second; } assert(sig); -- 2.45.2