Daniele Bartolini 11 лет назад
Родитель
Сommit
4ac5b96cf4
2 измененных файлов с 15 добавлено и 9 удалено
  1. 2 2
      engine/core/containers/container_types.h
  2. 13 7
      engine/core/containers/map.h

+ 2 - 2
engine/core/containers/container_types.h

@@ -76,8 +76,6 @@ struct Queue
 
 	/// Random access by index
 	T& operator[](uint32_t index);
-
-	/// Random access by index
 	const T& operator[](uint32_t index) const;
 
 	uint32_t _read;
@@ -125,6 +123,8 @@ struct Map
 {
 	Map(Allocator& a);
 
+	const TValue& operator[](const TKey& key) const;
+
 	struct Node
 	{
 		TKey key;

+ 13 - 7
engine/core/containers/map.h

@@ -22,11 +22,11 @@ namespace map
 	template <typename TKey, typename TValue> uint32_t size(const Map<TKey, TValue>& m);
 
 	/// Returns whether the given @a key exists in the map @a m.
-	template <typename TKey, typename TValue> bool has(const Map<TKey, TValue>& m, const TKey key);
+	template <typename TKey, typename TValue> bool has(const Map<TKey, TValue>& m, const TKey& key);
 
 	/// Returns the value for the given @a key or @a deffault if
 	/// the key does not exist in the map.
-	template <typename TKey, typename TValue> const TValue& get(const Map<TKey, TValue>& m, const TKey key, const TValue& deffault);
+	template <typename TKey, typename TValue> const TValue& get(const Map<TKey, TValue>& m, const TKey& key, const TValue& deffault);
 
 	/// Sets the @a value for the @a key in the map.
 	template <typename TKey, typename TValue> void set(Map<TKey, TValue>& m, const TKey& key, const TValue& value);
@@ -391,7 +391,7 @@ namespace map_internal
 	}
 
 	template <typename TKey, typename TValue>
-	inline uint32_t inner_find(const Map<TKey, TValue>& m, const TKey key)
+	inline uint32_t inner_find(const Map<TKey, TValue>& m, const TKey& key)
 	{
 		uint32_t x = m._root;
 
@@ -425,7 +425,7 @@ namespace map_internal
 	}
 
 	template <typename TKey, typename TValue>
-	inline uint32_t find_or_fail(const Map<TKey, TValue>& m, const TKey key)
+	inline uint32_t find_or_fail(const Map<TKey, TValue>& m, const TKey& key)
 	{
 		uint32_t p = inner_find(m, key);
 
@@ -436,7 +436,7 @@ namespace map_internal
 	}
 
 	template <typename TKey, typename TValue>
-	inline uint32_t find_or_add(Map<TKey, TValue>& m, const TKey key)
+	inline uint32_t find_or_add(Map<TKey, TValue>& m, const TKey& key)
 	{
 		uint32_t p = inner_find(m, key);
 
@@ -490,13 +490,13 @@ namespace map
 	}
 
 	template <typename TKey, typename TValue>
-	inline bool has(const Map<TKey, TValue>& m, const TKey key)
+	inline bool has(const Map<TKey, TValue>& m, const TKey& key)
 	{
 		return map_internal::find_or_fail(m, key) != map_internal::NIL;
 	}
 
 	template <typename TKey, typename TValue>
-	inline const TValue& get(const Map<TKey, TValue>& m, const TKey key, const TValue& deffault)
+	inline const TValue& get(const Map<TKey, TValue>& m, const TKey& key, const TValue& deffault)
 	{
 		uint32_t p = map_internal::inner_find(m, key);
 
@@ -732,4 +732,10 @@ inline Map<TKey, TValue>::Map(Allocator& a)
 	map::clear(*this);
 }
 
+template <typename TKey, typename TValue>
+inline const TValue& Map<TKey, TValue>::operator[](const TKey& key) const
+{
+	return map::get(*this, key, TValue());
+}
+
 } // namespace crown