Daniele Bartolini 8 лет назад
Родитель
Сommit
b112ee20d6
2 измененных файлов с 38 добавлено и 4 удалено
  1. 1 4
      src/core/containers/hash_map.h
  2. 37 0
      src/core/pair.h

+ 1 - 4
src/core/containers/hash_map.h

@@ -118,10 +118,7 @@ namespace hash_map_internal
 
 				std::swap(hash, m._index[hash_i].hash);
 				m._index[hash_i].index = 0x0123abcd;
-				PAIR(TKey, TValue) tmp(*m._allocator);
-				memcpy(&tmp, &m._data[hash_i].pair, sizeof(new_item));
-				memcpy(&m._data[hash_i].pair, &new_item, sizeof(new_item));
-				memcpy(&new_item, &tmp, sizeof(new_item));
+				swap(new_item, m._data[hash_i].pair);
 
 				dist = existing_elem_probe_dist;
 			}

+ 37 - 0
src/core/pair.h

@@ -6,6 +6,7 @@
 #pragma once
 
 #include "memory.h"
+#include <string.h> // memcpy
 
 namespace crown
 {
@@ -97,4 +98,40 @@ PAIR(T1, T2) make_pair(Allocator& a, T1 first, T2 second)
 	return pair;
 }
 
+template <typename T1, typename T2>
+inline void swap(Pair<T1, T2, 0, 0>& a, Pair<T1, T2, 0, 0>& b)
+{
+	char c[sizeof(a)];
+	memcpy( c, &a, sizeof(a));
+	memcpy(&a, &b, sizeof(a));
+	memcpy(&b, &c, sizeof(a));
+}
+
+template <typename T1, typename T2>
+inline void swap(Pair<T1, T2, 0, 1>& a, Pair<T1, T2, 0, 1>& b)
+{
+	char c[sizeof(a)];
+	memcpy( c, &a, sizeof(a));
+	memcpy(&a, &b, sizeof(a));
+	memcpy(&b, &c, sizeof(a));
+}
+
+template <typename T1, typename T2>
+inline void swap(Pair<T1, T2, 1, 0>& a, Pair<T1, T2, 1, 0>& b)
+{
+	char c[sizeof(a)];
+	memcpy( c, &a, sizeof(a));
+	memcpy(&a, &b, sizeof(a));
+	memcpy(&b, &c, sizeof(a));
+}
+
+template <typename T1, typename T2>
+inline void swap(Pair<T1, T2, 1, 1>& a, Pair<T1, T2, 1, 1>& b)
+{
+	char c[sizeof(a)];
+	memcpy( c, &a, sizeof(a));
+	memcpy(&a, &b, sizeof(a));
+	memcpy(&b, &c, sizeof(a));
+}
+
 } // namespace crown