From 695e36765b59cfefe85a90f167949e4afe3ee8b4 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Tue, 2 Apr 2019 08:35:11 +0200
Subject: [PATCH] overload map at with a default parameter

---
 alib2std/src/extensions/container/map.hpp     | 18 ++++++++++++++++++
 .../test-src/extensions/container/MapTest.cpp | 19 +++++++++++++++++++
 2 files changed, 37 insertions(+)

diff --git a/alib2std/src/extensions/container/map.hpp b/alib2std/src/extensions/container/map.hpp
index 80c4fc087a..d8b670d5b5 100644
--- a/alib2std/src/extensions/container/map.hpp
+++ b/alib2std/src/extensions/container/map.hpp
@@ -163,6 +163,24 @@ public:
 		return insert ( std::make_pair ( std::move ( key ), std::move ( value ) ) );
 	}
 
+	using std::map< T, R, Cmp, Alloc >::at;
+
+	R & at ( const T & key, R & defaultValue ) {
+		auto value = this->find ( key );
+		if ( value == end ( ) )
+			return defaultValue;
+		else
+			return value->second;
+	}
+
+	const R & at ( const T & key, const R & defaultValue ) const {
+		auto value = this->find ( key );
+		if ( value == end ( ) )
+			return defaultValue;
+		else
+			return value->second;
+	}
+
 	/**
 	 * \brief
 	 * Inherited behavior of begin for non-const instance.
diff --git a/alib2std/test-src/extensions/container/MapTest.cpp b/alib2std/test-src/extensions/container/MapTest.cpp
index d61eca037e..f3990ffad6 100644
--- a/alib2std/test-src/extensions/container/MapTest.cpp
+++ b/alib2std/test-src/extensions/container/MapTest.cpp
@@ -55,4 +55,23 @@ TEST_CASE ( "Map", "[unit][std][container]" ) {
 
 		CHECK ( copies == 0 );
 	}
+
+	SECTION ( "At" ) {
+		ext::map < int, std::string > map;
+		map.insert ( 1, "one" );
+
+		{
+			std::string defaultValue = "default";
+			std::string & value = map.at ( 0, defaultValue );
+
+			CHECK ( value == "default" );
+		}
+
+		{
+			const std::string defaultValue = "default";
+			const std::string & value = map.at ( 0, defaultValue );
+
+			CHECK ( value == "default" );
+		}
+	}
 }
-- 
GitLab