Debian Codegraph @

tools/physicalModeling/mesh2faust/vega/libraries/objMesh/objMesh-disjointSet.cpp

raw symbols SCIP
1 /*************************************************************************
2 * *
3 * Vega FEM Simulation Library Version 3.1 *
4 * *
5 * "objMesh" library , Copyright (C) 2007 CMU, 2009 MIT, 2016 USC *
6 * All rights reserved. *
7 * *
8 * Code authors: Yili Zhao, Jernej Barbic *
9 * http://www.jernejbarbic.com/code *
10 * *
11 * Research: Jernej Barbic, Fun Shing Sin, Daniel Schroeder, *
12 * Doug L. James, Jovan Popovic *
13 * *
14 * Funding: National Science Foundation, Link Foundation, *
15 * Singapore-MIT GAMBIT Game Lab, *
16 * Zumberge Research and Innovation Fund at USC *
17 * *
18 * This library is free software; you can redistribute it and/or *
19 * modify it under the terms of the BSD-style license that is *
20 * included with this library in the file LICENSE.txt *
21 * *
22 * This library is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file *
25 * LICENSE.TXT for more details. *
26 * *
27 *************************************************************************/
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include "objMesh-disjointSet.h"
32
33 DisjointSet::DisjointSet(int num):disjointSetParent(NULL),size(0)
34 {
35 Resize(num);
36 }
37
38 DisjointSet::~DisjointSet(void)
39 {
40 Destroy();
41 }
42
43 void DisjointSet::MakeSet(void)
44 {
45 for(int i=0; i<size; i++)
46 disjointSetParent[i] = -1; // each object is initially a root of a tree
47 }
48
49 void DisjointSet::Resize(int num)
50 {
51 if (num < 0)
52 {
53 printf("Error in DisjointSet::Resize(int num): problem scale cannot be negtive.\n");
54 exit(0);
55 }
56 if (size >= num)
57 return;
58
59 if (size != 0)
60 free(disjointSetParent);
61 size = num;
62 disjointSetParent = (int *) malloc (sizeof(int) * size);
63 }
64
65 void DisjointSet::Destroy(void)
66 {
67 if (size == 0)
68 return;
69 size = 0;
70 free(disjointSetParent);
71 disjointSetParent = NULL;
72 }
73
74 int DisjointSet::FindSet(int x)
75 {
76 if (x < 0 || x >= size)
77 {
78 printf("Error in DisjointSet::FindSet(int x): the x value is illegal.\n");
79 exit(0);
80 }
81 int ancestor = x;
82 while(disjointSetParent[ancestor] >= 0)
83 ancestor = disjointSetParent[ancestor]; // backtrack the ancestor
84 //path compression
85 while(x != ancestor)
86 {
87 int temp = disjointSetParent[x];
88 disjointSetParent[x] = ancestor;
89 x = temp;
90 }
91 return ancestor; // here ancestor should be >=0
92 }
93
94 void DisjointSet::UnionSet(int x, int y)
95 {
96 if (x < 0 || x >= size)
97 {
98 printf("Error in DisjointSet::UnionSet(int x, int y): the x value is illegal.\n");
99 exit(0);
100 }
101 if (y < 0 || y >= size)
102 {
103 printf("Error in DisjointSet::UnionSet(int x, int y): the y value is illegal.\n");
104 exit(0);
105 }
106 int ancestor1 = FindSet(x);
107 int ancestor2 = FindSet(y);
108 if (ancestor1 == ancestor2)
109 return; // they are already in the same set
110
111 if (disjointSetParent[ancestor1] < disjointSetParent[ancestor2]) // size of set 1 is larger
112 {
113 disjointSetParent[ancestor1] += disjointSetParent[ancestor2];
114 disjointSetParent[ancestor2] = ancestor1;
115 }
116 else // size of set 2 is larger or equal to the size of set 1
117 {
118 disjointSetParent[ancestor2] += disjointSetParent[ancestor1];
119 disjointSetParent[ancestor1] = ancestor2;
120 }
121 }
122