# HG changeset patch # User frosch # Date 2015-02-22 23:04:02 # Node ID e2c664f87e92ed92b0d9efff083833ca004c5241 # Parent 37511f59b0de579a3556b413a6cd719d945c1d68 (svn r27166) -Codechange: Optimise ScriptList by making use of iterators instead of looking up map-items multiple times per API call. diff --git a/src/script/api/script_list.cpp b/src/script/api/script_list.cpp --- a/src/script/api/script_list.cpp +++ b/src/script/api/script_list.cpp @@ -440,24 +440,25 @@ void ScriptList::AddItem(int64 item, int if (this->HasItem(item)) return; - this->items[item] = 0; - this->buckets[0].insert(item); - - this->SetValue(item, value); + this->items[item] = value; + this->buckets[value].insert(item); } void ScriptList::RemoveItem(int64 item) { this->modifications++; - if (!this->HasItem(item)) return; + ScriptListMap::iterator item_iter = this->items.find(item); + if (item_iter == this->items.end()) return; - int64 value = this->GetValue(item); + int64 value = item_iter->second; this->sorter->Remove(item); - this->buckets[value].erase(item); - if (this->buckets[value].empty()) this->buckets.erase(value); - this->items.erase(item); + ScriptListBucket::iterator bucket_iter = this->buckets.find(value); + assert(bucket_iter != this->buckets.end()); + bucket_iter->second.erase(item); + if (bucket_iter->second.empty()) this->buckets.erase(bucket_iter); + this->items.erase(item_iter); } int64 ScriptList::Begin() @@ -496,24 +497,26 @@ int32 ScriptList::Count() int64 ScriptList::GetValue(int64 item) { - if (!this->HasItem(item)) return 0; - - return this->items[item]; + ScriptListMap::const_iterator item_iter = this->items.find(item); + return item_iter == this->items.end() ? 0 : item_iter->second; } bool ScriptList::SetValue(int64 item, int64 value) { this->modifications++; - if (!this->HasItem(item)) return false; + ScriptListMap::iterator item_iter = this->items.find(item); + if (item_iter == this->items.end()) return false; - int64 value_old = this->GetValue(item); + int64 value_old = item_iter->second; if (value_old == value) return true; this->sorter->Remove(item); - this->buckets[value_old].erase(item); - if (this->buckets[value_old].empty()) this->buckets.erase(value_old); - this->items[item] = value; + ScriptListBucket::iterator bucket_iter = this->buckets.find(value_old); + assert(bucket_iter != this->buckets.end()); + bucket_iter->second.erase(item); + if (bucket_iter->second.empty()) this->buckets.erase(bucket_iter); + item_iter->second = value; this->buckets[value].insert(item); return true; @@ -772,9 +775,10 @@ SQInteger ScriptList::_get(HSQUIRRELVM v SQInteger idx; sq_getinteger(vm, 2, &idx); - if (!this->HasItem(idx)) return SQ_ERROR; + ScriptListMap::const_iterator item_iter = this->items.find(idx); + if (item_iter == this->items.end()) return SQ_ERROR; - sq_pushinteger(vm, this->GetValue(idx)); + sq_pushinteger(vm, item_iter->second); return 1; }