Quellcode durchsuchen

adjacency_matrix:
* make swap accept the right type
* make resize work properly

Michele Caini vor 3 Jahren
Ursprung
Commit
473c36aba5
2 geänderte Dateien mit 11 neuen und 6 gelöschten Zeilen
  1. 8 3
      src/entt/graph/adjacency_matrix.hpp
  2. 3 3
      test/entt/graph/adjacency_matrix.cpp

+ 8 - 3
src/entt/graph/adjacency_matrix.hpp

@@ -274,7 +274,7 @@ public:
      * @brief Exchanges the contents with those of a given adjacency matrix.
      * @brief Exchanges the contents with those of a given adjacency matrix.
      * @param other Adjacency matrix to exchange the content with.
      * @param other Adjacency matrix to exchange the content with.
      */
      */
-    void swap(adjacency_matrix &other) {
+    void swap(basic_adjacency_matrix &other) {
         using std::swap;
         using std::swap;
         swap(matrix, other.matrix);
         swap(matrix, other.matrix);
         swap(vert, other.vert);
         swap(vert, other.vert);
@@ -335,8 +335,13 @@ public:
      * @param vertices The new number of vertices.
      * @param vertices The new number of vertices.
      */
      */
     void resize(const size_type vertices) {
     void resize(const size_type vertices) {
-        matrix.resize(vertices * vertices);
-        vert = vertices;
+        basic_adjacency_matrix other{vertices, get_allocator()};
+
+        for(auto [lhs, rhs]: edges()) {
+            other.insert(lhs, rhs);
+        }
+
+        other.swap(*this);
     }
     }
 
 
     /**
     /**

+ 3 - 3
test/entt/graph/adjacency_matrix.cpp

@@ -6,15 +6,15 @@
 
 
 TEST(AdjacencyMatrix, Resize) {
 TEST(AdjacencyMatrix, Resize) {
     entt::adjacency_matrix adjacency_matrix{2};
     entt::adjacency_matrix adjacency_matrix{2};
-    adjacency_matrix.insert(0u, 1u);
+    adjacency_matrix.insert(1u, 0u);
 
 
     ASSERT_EQ(adjacency_matrix.size(), 2u);
     ASSERT_EQ(adjacency_matrix.size(), 2u);
-    ASSERT_TRUE(adjacency_matrix.contains(0u, 1u));
+    ASSERT_TRUE(adjacency_matrix.contains(1u, 0u));
 
 
     adjacency_matrix.resize(3u);
     adjacency_matrix.resize(3u);
 
 
     ASSERT_EQ(adjacency_matrix.size(), 3u);
     ASSERT_EQ(adjacency_matrix.size(), 3u);
-    ASSERT_TRUE(adjacency_matrix.contains(0u, 1u));
+    ASSERT_TRUE(adjacency_matrix.contains(1u, 0u));
 }
 }
 
 
 TEST(AdjacencyMatrix, Constructors) {
 TEST(AdjacencyMatrix, Constructors) {