krishnarb3 commented on code in PR #609:
URL: https://github.com/apache/incubator-sedona/pull/609#discussion_r851511902


##########
core/src/main/java/org/apache/sedona/core/dbscanJudgement/UnionFindImpl.java:
##########
@@ -0,0 +1,134 @@
+package org.apache.sedona.core.dbscanJudgement;
+
+import java.util.Arrays;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+
+public class UnionFindImpl implements UnionFind {
+    private List<Integer> clusters;
+    private List<Integer> clusterSizes;
+    private int numClusters;
+    private final int n;
+
+    public UnionFindImpl(int n) {
+        this.n = n;
+        this.numClusters = n;
+        this.clusters = new ArrayList<>();
+        this.clusterSizes = new ArrayList<>();
+        for (int i = 0; i < n; i++) {
+            clusters.add(i);
+            clusterSizes.add(1);
+        }
+    }
+
+    @Override
+    public int find(int i) {
+        int base = i;
+        while (!clusters.get(base).equals(base)) {
+            base = clusters.get(base);
+        }
+        while (i != base) {
+            int next = clusters.get(i);
+            clusters.set(i, base);
+            i = next;
+        }
+        return i;
+    }
+
+    @Override
+    public int size(int i) {
+        return clusterSizes.get(find(i));
+    }
+
+    @Override
+    public void union(int i, int j) {
+        int a = find(i);
+        int b = find(j);
+        if (a == b) {
+            return;
+        }
+        if (clusterSizes.get(a) < clusterSizes.get(b) ||
+                (Objects.equals(clusterSizes.get(a), clusterSizes.get(b)) && a 
> b)) {
+            clusters.set(a, clusters.get(b));
+            clusterSizes.set(b, clusterSizes.get(a) + clusterSizes.get(b));
+            clusterSizes.set(a, 0);
+        } else {
+            clusters.set(b, clusters.get(a));
+            clusterSizes.set(a, clusterSizes.get(a) + clusterSizes.get(b));
+            clusterSizes.set(b, 0);
+        }
+        numClusters--;
+    }
+
+    @Override
+    public Integer[] orderedByCluster() {
+        Integer[] clusterIdByElemId = new Integer[n];
+        for (int i = 0; i < n; i++) {
+            find(i);
+            clusterIdByElemId[i] = i;
+        }
+        Arrays.sort(clusterIdByElemId, Comparator.comparing(index -> 
clusters.get(index)));
+        return clusterIdByElemId;
+    }
+
+    @Override
+    public Integer[] getCollapsedClusterIds(Set<Integer> isInCluster) {
+        Integer[] orderedComponents = orderedByCluster();
+        Integer[] newIds = new Integer[n];
+        int lastOldId = 0, currentNewId = 0;
+        boolean encounteredCluster = false;
+
+        if (isInCluster == null) {
+            isInCluster = new HashSet<>();
+        }
+
+        for (int i = 0; i < n; i++) {

Review Comment:
   As per POSTGIS `getCollapsedClusterIds` will set the cluster id only for 
indexes that are part of isInCluster or all the indexes if isInCluster is empty.
   @jiayuasu Is this the expected behavior?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to