comparator.cpp 9.62 KB
Newer Older
Kirill Terekhov's avatar
Kirill Terekhov committed
1
2
3
4
#include "inmost_mesh.h"
#if defined(USE_MESH)
namespace INMOST
{
5
	int Mesh::CentroidComparator::Compare(const real * a, const real * b) const
Kirill Terekhov's avatar
Kirill Terekhov committed
6
7
8
9
10
	{
		real e = m->GetEpsilon();
		for(integer i = 0; i < m->GetDimensions(); i++) 
			if( fabs(a[i]-b[i]) > e ) 
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
11
				if( a[i] > b[i] ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
12
					return 1;
Kirill Terekhov's avatar
Kirill Terekhov committed
13
14
				else
					return -1;
Kirill Terekhov's avatar
Kirill Terekhov committed
15
16
17
18
			}
		return 0;
	}

19
	bool Mesh::CentroidComparator::operator () (HandleType a, HandleType b) const
Kirill Terekhov's avatar
Kirill Terekhov committed
20
21
22
23
	{
		if( a == InvalidHandle() || b == InvalidHandle() ) return a > b;
		real ca[3] = {0,0,0}, cb[3] = {0,0,0};
		m->GetGeometricData(a,CENTROID,ca);
Kirill Terekhov's avatar
Kirill Terekhov committed
24
25
		m->GetGeometricData(b,CENTROID,cb);
		return Compare(ca,cb) < 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
26
27
	}

28
	bool Mesh::CentroidComparator::operator () (HandleType a, const real * cb) const
Kirill Terekhov's avatar
Kirill Terekhov committed
29
30
31
32
	{
		if( a == InvalidHandle() ) return true;
		real ca[3] = {0,0,0}; 
		m->GetGeometricData(a,CENTROID,ca);
Kirill Terekhov's avatar
Kirill Terekhov committed
33
		return Compare(ca,cb) < 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
34
35
	}

36
37
38
39
40
41
42
43
	bool Mesh::CentroidComparator::operator () (const real * ca, HandleType b) const
	{
		if( b == InvalidHandle() ) return false;
		real cb[3] = {0,0,0}; 
		m->GetGeometricData(b,CENTROID,cb);
		return Compare(ca,cb) < 0;
	}

Kirill Terekhov's avatar
Kirill Terekhov committed
44
	
Kirill Terekhov's avatar
Kirill Terekhov committed
45
	int Mesh::HierarchyComparator::CompareNodes(HandleType a, HandleType b) const
Kirill Terekhov's avatar
Kirill Terekhov committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
	{
		real_array ca = m->RealArrayDF(a,m->CoordsTag());
		real_array cb = m->RealArrayDF(b,m->CoordsTag());
		real e = m->GetEpsilon();
		for(integer i = 0; i < m->GetDimensions(); ++i)
			if( fabs(ca[i]-cb[i]) > e )
			{
				if( ca[i] > cb[i] ) 
					return 1;
				else
					return -1;
			}
		return 0;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
60
	int Mesh::HierarchyComparator::CompareElements(HandleType a, HandleType b) const
Kirill Terekhov's avatar
Kirill Terekhov committed
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
	{
		integer ia = GetHandleElementNum(a);
		integer ib = GetHandleElementNum(b);
		if( ia == ib )
		{
			if( ia == 0 ) return CompareNodes(a,b);
			Element::adj_type const & adja = m->LowConn(a);
			Element::adj_type const & adjb = m->LowConn(b);
			if( adja.size() == adjb.size() )
			{
				for(Element::adj_type::size_type k = 0; k < adja.size(); ++k)
				{
					int ret = CompareElements(adja[k],adjb[k]);
					if( ret != 0 ) return ret;
				}
				return 0;
			}
			else return static_cast<int>(adja.size()) - static_cast<int>(adjb.size());
		}
		else return ia - ib;
	}

	void Mesh::SortByGlobalID(HandleType * h, enumerator n)
	{
		if( sizeof(integer) == 4 && n > 64 )
		{
			Element::adj_type::size_type i, num = static_cast<Element::adj_type::size_type>(n);
			array<HandleType> tmp(num);
			HandleType * ptra = h;
			HandleType * ptrb = tmp.data();
			const unsigned int kHist = 256;
			unsigned int b0[kHist * 4];
			unsigned int *b1 = b0 + kHist;
			unsigned int *b2 = b0 + kHist*2;
			unsigned int *b3 = b0 + kHist*3;
			memset(b0,0,sizeof(unsigned int)*kHist*4);
			for (i = 0; i < num; i++) 
			{
				unsigned char * rec = reinterpret_cast<unsigned char *>(&GlobalID(ptra[i]));
				++b0[rec[0]]; 
				++b1[rec[1]];
				++b2[rec[2]]; 
				++b3[rec[3]];
			}
			unsigned int sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0, tsum;
			for (i = 0; i < kHist; i++) 
			{
				tsum = b0[i] + sum0; b0[i] = sum0 - 1; sum0 = tsum;
				tsum = b1[i] + sum1; b1[i] = sum1 - 1; sum1 = tsum;
				tsum = b2[i] + sum2; b2[i] = sum2 - 1; sum2 = tsum;
				tsum = b3[i] + sum3; b3[i] = sum3 - 1; sum3 = tsum;
			}
			for (i = 0; i < num; i++) ptrb[++b0[reinterpret_cast<unsigned char *>(&GlobalID(ptra[i]))[0]]] = ptra[i];
			for (i = 0; i < num; i++) ptra[++b1[reinterpret_cast<unsigned char *>(&GlobalID(ptrb[i]))[1]]] = ptrb[i];
			for (i = 0; i < num; i++) ptrb[++b2[reinterpret_cast<unsigned char *>(&GlobalID(ptra[i]))[2]]] = ptra[i];
			for (i = 0; i < num; i++) ptra[++b3[reinterpret_cast<unsigned char *>(&GlobalID(ptrb[i]))[3]]] = ptrb[i];
		}
		else if( sizeof(integer) == 8 && n > 128 )
		{
			Element::adj_type::size_type i, num = static_cast<Element::adj_type::size_type>(n);
			array<HandleType> tmp(num);
			HandleType * ptra = h;
			HandleType * ptrb = tmp.data();
			const unsigned int kHist = 256;
			unsigned int b0[kHist * 8];
			unsigned int *b1 = b0 + kHist;
			unsigned int *b2 = b0 + kHist*2;
			unsigned int *b3 = b0 + kHist*3;
			unsigned int *b4 = b0 + kHist*4;
			unsigned int *b5 = b0 + kHist*5;
			unsigned int *b6 = b0 + kHist*6;
			unsigned int *b7 = b0 + kHist*7;
			memset(b0,0,sizeof(unsigned int)*kHist*8);
			for (i = 0; i < num; i++) 
			{
				unsigned char * rec = reinterpret_cast<unsigned char *>(&GlobalID(ptra[i]));
				++b0[rec[0]]; 
				++b1[rec[1]];
				++b2[rec[2]]; 
				++b3[rec[3]];
				++b4[rec[4]]; 
				++b5[rec[5]];
				++b6[rec[6]]; 
				++b7[rec[7]];
			}
			unsigned int sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0, tsum;
			unsigned int sum4 = 0, sum5 = 0, sum6 = 0, sum7 = 0;
			for (i = 0; i < kHist; i++) 
			{
				tsum = b0[i] + sum0; b0[i] = sum0 - 1; sum0 = tsum;
				tsum = b1[i] + sum1; b1[i] = sum1 - 1; sum1 = tsum;
				tsum = b2[i] + sum2; b2[i] = sum2 - 1; sum2 = tsum;
				tsum = b3[i] + sum3; b3[i] = sum3 - 1; sum3 = tsum;
				tsum = b4[i] + sum4; b4[i] = sum4 - 1; sum4 = tsum;
				tsum = b5[i] + sum5; b5[i] = sum5 - 1; sum5 = tsum;
				tsum = b6[i] + sum6; b6[i] = sum6 - 1; sum6 = tsum;
				tsum = b7[i] + sum7; b7[i] = sum7 - 1; sum7 = tsum;
			}
			for (i = 0; i < num; i++) ptrb[++b0[reinterpret_cast<unsigned char *>(&GlobalID(ptra[i]))[0]]] = ptra[i];
			for (i = 0; i < num; i++) ptra[++b1[reinterpret_cast<unsigned char *>(&GlobalID(ptrb[i]))[1]]] = ptrb[i];
			for (i = 0; i < num; i++) ptrb[++b2[reinterpret_cast<unsigned char *>(&GlobalID(ptra[i]))[2]]] = ptra[i];
			for (i = 0; i < num; i++) ptra[++b3[reinterpret_cast<unsigned char *>(&GlobalID(ptrb[i]))[3]]] = ptrb[i];
			for (i = 0; i < num; i++) ptrb[++b4[reinterpret_cast<unsigned char *>(&GlobalID(ptra[i]))[4]]] = ptra[i];
			for (i = 0; i < num; i++) ptra[++b5[reinterpret_cast<unsigned char *>(&GlobalID(ptrb[i]))[5]]] = ptrb[i];
			for (i = 0; i < num; i++) ptrb[++b6[reinterpret_cast<unsigned char *>(&GlobalID(ptra[i]))[6]]] = ptra[i];
			for (i = 0; i < num; i++) ptra[++b7[reinterpret_cast<unsigned char *>(&GlobalID(ptrb[i]))[7]]] = ptrb[i];
		}
		else std::sort(h,h+n,Mesh::GlobalIDComparator(this));
	}

	void Mesh::SortHandles(HandleType * h, enumerator n)
	{
		if( sizeof(HandleType) == 4 && n > 32 )
		{
			Element::adj_type::size_type i, num = static_cast<Element::adj_type::size_type>(n);
			array<HandleType> tmp(num);
			HandleType * ptra = h;
			HandleType * ptrb = tmp.data();
			//make so that invalid handles appear at he end
			for (i = 0; i < num; i++) if( ptra[i] == InvalidHandle() ) ptra[i] = ENUMUNDEF;
			const unsigned int kHist = 256;
			unsigned int b0[kHist * 4];
			unsigned int *b1 = b0 + kHist;
			unsigned int *b2 = b0 + kHist*2;
			unsigned int *b3 = b0 + kHist*3;
			unsigned char * l1 = reinterpret_cast<unsigned char *>(ptra);
			unsigned char * l2 = reinterpret_cast<unsigned char *>(ptrb);
			memset(b0,0,sizeof(unsigned int)*kHist*4);
			for (i = 0; i < num; i++) 
			{
				++b0[l1[4*i+0]]; 
				++b1[l1[4*i+1]];
				++b2[l1[4*i+2]]; 
				++b3[l1[4*i+3]];
			}
			unsigned int sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0, tsum;
			for (i = 0; i < kHist; i++) 
			{
				tsum = b0[i] + sum0; b0[i] = sum0 - 1; sum0 = tsum;
				tsum = b1[i] + sum1; b1[i] = sum1 - 1; sum1 = tsum;
				tsum = b2[i] + sum2; b2[i] = sum2 - 1; sum2 = tsum;
				tsum = b3[i] + sum3; b3[i] = sum3 - 1; sum3 = tsum;
			}
			for (i = 0; i < num; i++) ptrb[++b0[l1[4*i+0]]] = ptra[i];
			for (i = 0; i < num; i++) ptra[++b1[l2[4*i+1]]] = ptrb[i];
			for (i = 0; i < num; i++) ptrb[++b2[l1[4*i+2]]] = ptra[i];
			for (i = 0; i < num; i++) ptra[++b3[l2[4*i+3]]] = ptrb[i];

			for (i = 0; i < num; i++) if( ptra[i] == ENUMUNDEF ) ptra[i] = InvalidHandle();
		}
		else if( sizeof(HandleType) == 8 && n > 64 )
		{
			Element::adj_type::size_type i, num = static_cast<Element::adj_type::size_type>(n);
			array<HandleType> tmp(num);
			HandleType * ptra = h;
			HandleType * ptrb = tmp.data();
			//make so that invalid handles appear at he end
			for (i = 0; i < num; i++) if( ptra[i] == InvalidHandle() ) ptra[i] = ENUMUNDEF;
			const unsigned int kHist = 256;
			unsigned int b0[kHist * 8];
			unsigned int *b1 = b0 + kHist;
			unsigned int *b2 = b0 + kHist*2;
			unsigned int *b3 = b0 + kHist*3;
			unsigned int *b4 = b0 + kHist*4;
			unsigned int *b5 = b0 + kHist*5;
			unsigned int *b6 = b0 + kHist*6;
			unsigned int *b7 = b0 + kHist*7;
			unsigned char * l1 = reinterpret_cast<unsigned char *>(ptra);
			unsigned char * l2 = reinterpret_cast<unsigned char *>(ptrb);
			memset(b0,0,sizeof(unsigned int)*kHist*8);
			for (i = 0; i < num; i++) 
			{
				++b0[l1[8*i+0]]; 
				++b1[l1[8*i+1]];
				++b2[l1[8*i+2]]; 
				++b3[l1[8*i+3]];
				++b4[l1[8*i+4]]; 
				++b5[l1[8*i+5]];
				++b6[l1[8*i+6]]; 
				++b7[l1[8*i+7]];
			}
			unsigned int sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0, tsum;
			unsigned int sum4 = 0, sum5 = 0, sum6 = 0, sum7 = 0;
			for (i = 0; i < kHist; i++) 
			{
				tsum = b0[i] + sum0; b0[i] = sum0 - 1; sum0 = tsum;
				tsum = b1[i] + sum1; b1[i] = sum1 - 1; sum1 = tsum;
				tsum = b2[i] + sum2; b2[i] = sum2 - 1; sum2 = tsum;
				tsum = b3[i] + sum3; b3[i] = sum3 - 1; sum3 = tsum;
				tsum = b4[i] + sum4; b4[i] = sum4 - 1; sum4 = tsum;
				tsum = b5[i] + sum5; b5[i] = sum5 - 1; sum5 = tsum;
				tsum = b6[i] + sum6; b6[i] = sum6 - 1; sum6 = tsum;
				tsum = b7[i] + sum7; b7[i] = sum7 - 1; sum7 = tsum;
			}
			for (i = 0; i < num; i++) ptrb[++b0[l1[8*i+0]]] = ptra[i];
			for (i = 0; i < num; i++) ptra[++b1[l2[8*i+1]]] = ptrb[i];
			for (i = 0; i < num; i++) ptrb[++b2[l1[8*i+2]]] = ptra[i];
			for (i = 0; i < num; i++) ptra[++b3[l2[8*i+3]]] = ptrb[i];
			for (i = 0; i < num; i++) ptrb[++b4[l1[8*i+4]]] = ptra[i];
			for (i = 0; i < num; i++) ptra[++b5[l2[8*i+5]]] = ptrb[i];
			for (i = 0; i < num; i++) ptrb[++b6[l1[8*i+6]]] = ptra[i];
			for (i = 0; i < num; i++) ptra[++b7[l2[8*i+7]]] = ptrb[i];

			for (i = 0; i < num; i++) if( ptra[i] == ENUMUNDEF ) ptra[i] = InvalidHandle();
		}
		else std::sort(h,h+n);
	}
}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
269
#endif