From 9bd818137970b25d8e9ee3846df5484ba8de5b9b Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Fri, 2 Jan 2015 01:44:57 +0000 Subject: [PATCH] base: fix map behaviour in release mode by moving code out of asserts. --- src/lol/base/map.h | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/lol/base/map.h b/src/lol/base/map.h index fb6718d6..37c2a338 100644 --- a/src/lol/base/map.h +++ b/src/lol/base/map.h @@ -1,7 +1,8 @@ // // Lol Engine // -// Copyright: (c) 2010-2013 Sam Hocevar +// Copyright: (c) 2010-2015 Sam Hocevar +// (c) 2013-2015 Guillaume Bittoun // This program is free software; you can redistribute it and/or // modify it under the terms of the Do What The Fuck You Want To // Public License, Version 2, as published by Sam Hocevar. See @@ -22,7 +23,6 @@ namespace lol { -/* A stupidly linear map for now. */ template class map : protected hash { public: @@ -36,8 +36,10 @@ public: inline V const& operator[] (E const &key) const { /* Look for the hash in our table and return the value. */ - V * value_ptr = nullptr; - ASSERT(m_tree.TryGetValue(key, value_ptr), "trying to read a nonexistent key in map"); + V *value_ptr = nullptr; + bool found = m_tree.TryGetValue(key, value_ptr); + + ASSERT(found, "trying to read a nonexistent key in map"); return *value_ptr; } @@ -46,19 +48,21 @@ public: inline V & operator[] (E const &key) { /* Look for the hash in our table and return the value if found. */ - K typed_key(key); - V * value_ptr = nullptr; + V *value_ptr = nullptr; - if (!m_tree.TryGetValue(typed_key, value_ptr)) + bool found = m_tree.TryGetValue(key, value_ptr); + if (!found) { - V default_value = V(); - m_tree.Insert(typed_key, default_value); + /* If not found, insert a new value. */ + m_tree.Insert(typed_key, V()); + found = m_tree.TryGetValue(key, value_ptr); } - ASSERT(m_tree.TryGetValue(typed_key, value_ptr), "inserted key can’t be retrieved. key operator < must behave as a comparator (a !b !b