File tree Expand file tree Collapse file tree
src/main/java/pascal/taie/util/collection Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2626import java .util .HashMap ;
2727import java .util .Map ;
2828import java .util .Set ;
29+ import java .util .concurrent .atomic .AtomicInteger ;
2930import java .util .stream .Collectors ;
3031
3132public class UnionFindSet <E > {
@@ -38,11 +39,11 @@ public class UnionFindSet<E> {
3839 /**
3940 * Number of disjoint sets.
4041 */
41- private int setCount ;
42+ private final AtomicInteger setCount = new AtomicInteger () ;
4243
4344 public UnionFindSet (Collection <E > elems ) {
4445 elems .forEach (elem -> entries .put (elem , new Entry (elem )));
45- setCount = entries .size ();
46+ setCount . set ( entries .size () );
4647 }
4748
4849 /**
@@ -65,7 +66,7 @@ public boolean union(E e1, E e2) {
6566 root2 .parent = root1 ;
6667 ++root2 .rank ;
6768 }
68- -- setCount ;
69+ setCount . decrementAndGet () ;
6970 return true ;
7071 }
7172 }
@@ -90,7 +91,7 @@ public E findRoot(E e) {
9091 * @return number of disjoint sets in this union-find set.
9192 */
9293 public int numberOfSets () {
93- return setCount ;
94+ return setCount . get () ;
9495 }
9596
9697 /**
You can’t perform that action at this time.
0 commit comments