From e9ef31035a0d0ed422ad9aba1b9dcaebe147cd21 Mon Sep 17 00:00:00 2001
From: dboe <dboe@ipp.mpg.de>
Date: Sat, 4 Dec 2021 22:55:01 +0100
Subject: [PATCH] used sets.remap in cleaned for significant speedup

---
 tfields/core.py | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)

diff --git a/tfields/core.py b/tfields/core.py
index d2f5921..3f42c52 100644
--- a/tfields/core.py
+++ b/tfields/core.py
@@ -2757,20 +2757,21 @@ class TensorMaps(TensorFields):
                 inst = self.copy()
             remove_mask = np.full(inst.shape[0], False, dtype=bool)
             duplicates = tfields.lib.util.duplicates(inst, axis=0)
-            for tensor_index, duplicate_index in zip(range(inst.shape[0]), duplicates):
-                if duplicate_index != tensor_index:
-                    # mark duplicate at tensor_index for removal
-                    remove_mask[tensor_index] = True
-                    # redirect maps. Note: work on inst.maps instead of
-                    # self.maps in case stale vertices where removed
-                    for map_dim in inst.maps:
-                        for face_index in range(len(inst.maps[map_dim])):  # face index
-                            map_ = np.array(inst.maps[map_dim], dtype=int)
-                            if tensor_index in map_[face_index]:
-                                index = tfields.lib.util.index(
-                                    map_[face_index], tensor_index
-                                )
-                                inst.maps[map_dim][face_index][index] = duplicate_index
+            tensor_indices = np.arange(inst.shape[0])
+            duplicates_mask = duplicates != tensor_indices
+            if duplicates_mask.any():
+                # redirect maps. Note: work on inst.maps instead of
+                # self.maps in case stale vertices where removed
+                keys = tensor_indices[duplicates_mask]
+                values = duplicates[duplicates_mask]
+                for map_dim in inst.maps:
+                    tfields.lib.sets.remap(
+                        inst.maps[map_dim], keys, values, inplace=True
+                    )
+
+                # mark duplicates for removal
+                remove_mask[keys] = True
+
             if remove_mask.any():
                 # prevent another copy
                 inst = inst.removed(remove_mask)
-- 
GitLab