File: disjoint_set.h

package info (click to toggle)
meshlab 1.3.2%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 21,096 kB
  • ctags: 33,630
  • sloc: cpp: 224,813; ansic: 8,170; xml: 119; makefile: 80
file content (166 lines) | stat: -rw-r--r-- 5,440 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/****************************************************************************
* VCGLib                                                            o o     *
* Visual and Computer Graphics Library                            o     o   *
*                                                                _   O  _   *
* Copyright(C) 2004                                                \/)\/    *
* Visual Computing Lab                                            /\/|      *
* ISTI - Italian National Research Council                           |      *
*                                                                    \      *
* All rights reserved.                                                      *
*                                                                           *
* This program is free software; you can redistribute it and/or modify      *   
* it under the terms of the GNU General Public License as published by      *
* the Free Software Foundation; either version 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program is distributed in the hope that it will be useful,           *
* but WITHOUT ANY WARRANTY; without even the implied warranty of            *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/

#ifndef VCG_MATH_UNIONSET_H
#define VCG_MATH_UNIONSET_H


// some stuff for portable hashes...
#ifdef WIN32
 #ifndef __MINGW32__
  #include <hash_map>
  #include <hash_set>
  #define STDEXT stdext
 #else
  #include <ext/hash_map>
  #include <ext/hash_set>
  #define STDEXT __gnu_cxx
 #endif
#else
 #include <ext/hash_map>
 #include <ext/hash_set>
 #define STDEXT __gnu_cxx
// It's terrible but gnu's hash_map needs an instantiation of hash() for
// every key type! So we cast the pointer to void*
namespace __gnu_cxx
{
	template <> class hash<void *>: private hash<unsigned long>
	{
	public:
		size_t operator()(const void *ptr) const { return hash<unsigned long>::operator()((unsigned long)ptr); }
	};
}
#endif

#include <vector>
#include <assert.h>

namespace vcg
{
	/*!
	* Given a set of elements, it is often useful to break them up or partition them into a number of separate, nonoverlapping groups. 
	* A disjoint-set data structure is a data structure that keeps track of such a partitioning. See 
	* <a href="http://en.wikipedia.org/wiki/Disjoint-set_data_structure">Diskoint-set data structure on Wikipedia </a> for more details.
	*/
	template<class OBJECT_TYPE>
	class DisjointSet
	{
		/*************************************************
		*		Inner class definitions
		**************************************************/
		struct DisjointSetNode
		{
			DisjointSetNode(OBJECT_TYPE *x) {obj=x; parent=obj; rank=0;}
			OBJECT_TYPE	*obj;
			OBJECT_TYPE *parent;
			int					 rank;
		};

		typedef OBJECT_TYPE*																							ObjectPointer;
		typedef std::pair< ObjectPointer, int >														hPair;

		struct SimpleObjHashFunc{
			inline	size_t	operator ()(const ObjectPointer &p) const {return size_t(p);}
		};

#ifdef _MSC_VER
		STDEXT::hash_map< OBJECT_TYPE*, int > inserted_objects;
	  typedef typename STDEXT::hash_map< ObjectPointer, int >::iterator	hIterator;
#else
		STDEXT::hash_map< OBJECT_TYPE*, int, SimpleObjHashFunc > inserted_objects;
		typedef typename STDEXT::hash_map< ObjectPointer, int, SimpleObjHashFunc  >::iterator	hIterator;
#endif

		typedef std::pair< hIterator, bool >															hInsertResult;






	public:
		/*!
		* Default constructor
		*/
		DisjointSet() {}

		/*!
		* Makes a group containing only a given element (a singleton).
		*/
		void MakeSet(OBJECT_TYPE *x)
		{
			int object_count		= int(inserted_objects.size());
			assert(inserted_objects.find(x)==inserted_objects.end()); //the map mustn't already contain the object x
			nodes.push_back(DisjointSetNode(x));
			inserted_objects.insert( hPair(x,object_count) );
		}

		/*!
		* Combine or merge two groups into a single group.
		*/
		void Union(OBJECT_TYPE *x, OBJECT_TYPE *y)
		{
			OBJECT_TYPE *s0 = FindSet(x);
			OBJECT_TYPE *s1 = FindSet(y);
			Link(s0, s1);
		}

		/*!
		* Determine which group a particular element is in.
		*/
		OBJECT_TYPE* FindSet(OBJECT_TYPE *x)
		{
			hIterator pos = inserted_objects.find(x);
			assert(pos!=inserted_objects.end());
			DisjointSetNode *node = &nodes[pos->second];
			if (node->parent!=x)
				node->parent = FindSet(node->parent);
			return node->parent;
		}

	private:
		/*
		*/
		void Link(OBJECT_TYPE *x, OBJECT_TYPE *y)
		{
			hIterator xPos = inserted_objects.find(x);
			hIterator yPos = inserted_objects.find(y);
			assert(xPos!=inserted_objects.end() && yPos!=inserted_objects.end());
			DisjointSetNode *xNode = &nodes[xPos->second];
			DisjointSetNode *yNode = &nodes[yPos->second];
			if (xNode->rank>yNode->rank)
				xNode->parent = y;
			else
			{
				yNode->parent = x;
				if (xNode->rank==yNode->rank)
					yNode->rank++;
			}
		}

	protected:
		std::vector< DisjointSetNode >				nodes;
	};
};// end of namespace vcg

#endif //VCG_MATH_UNIONSET_H