eset.cpp 55.6 KB
Newer Older
1
//#define _HAS_ITERATOR_DEBUGGING 0
2

Kirill Terekhov's avatar
Kirill Terekhov committed
3 4
#include "inmost.h"
#if defined(USE_MESH)
Kirill Terekhov's avatar
Kirill Terekhov committed
5

Kirill Terekhov's avatar
Kirill Terekhov committed
6 7
namespace INMOST
{
Kirill Terekhov's avatar
Kirill Terekhov committed
8
	//shortcuts
Kirill Terekhov's avatar
Kirill Terekhov committed
9
	
Kirill Terekhov's avatar
Kirill Terekhov committed
10 11

	ElementSet::iterator & ElementSet::iterator::operator ++() 
Kirill Terekhov's avatar
Kirill Terekhov committed
12
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
13 14 15 16 17 18 19 20 21 22 23 24
		++pos;
		Element::adj_type::size_type end = ptr->size();
		if( m->isMeshModified() )
		{
			MarkerType hm = m->HideMarker();
			while(pos < end && (ptr->at(pos) == InvalidHandle() || m->GetMarker(ptr->at(pos),hm)) ) ++pos; 
		}
		else 
		{
			while(pos < end && ptr->at(pos) == InvalidHandle()) ++pos; 
		}
		return *this;
Kirill Terekhov's avatar
Kirill Terekhov committed
25
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
26 27 28


	Storage::enumerator ElementSet::nbSorted() const
Kirill Terekhov's avatar
Kirill Terekhov committed
29
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
30
		return hSorted(GetMeshLink()->HighConn(GetHandle()));
Kirill Terekhov's avatar
Kirill Terekhov committed
31
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
32
	ElementSet ElementSet::GetParent() const
Kirill Terekhov's avatar
Kirill Terekhov committed
33
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
34 35 36
		assert(GetElementType() == ESET);
		Element::adj_type & hc = GetMeshLink()->HighConn(GetHandle());
		return ElementSet(GetMeshLink(),hParent(hc));
Kirill Terekhov's avatar
Kirill Terekhov committed
37 38
	}

Kirill Terekhov's avatar
Kirill Terekhov committed
39
	ElementSet ElementSet::GetSibling() const
Kirill Terekhov's avatar
Kirill Terekhov committed
40
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
41 42 43
		assert(GetElementType() == ESET);
		Element::adj_type & hc = GetMeshLink()->HighConn(GetHandle());
		return ElementSet(GetMeshLink(),hSibling(hc));
Kirill Terekhov's avatar
Kirill Terekhov committed
44
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
45 46

	ElementSet ElementSet::GetChild() const
Kirill Terekhov's avatar
Kirill Terekhov committed
47
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
48 49 50
		assert(GetElementType() == ESET);
		Element::adj_type & hc = GetMeshLink()->HighConn(GetHandle());
		return ElementSet(GetMeshLink(),hChild(hc));
Kirill Terekhov's avatar
Kirill Terekhov committed
51 52 53
	}


Kirill Terekhov's avatar
Kirill Terekhov committed
54
	bool ElementSet::HaveParent() const
Kirill Terekhov's avatar
Kirill Terekhov committed
55
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
56 57 58
		assert(GetElementType() == ESET);
		Element::adj_type & hc = GetMeshLink()->HighConn(GetHandle());
		return hParent(hc) != InvalidHandle();
Kirill Terekhov's avatar
Kirill Terekhov committed
59
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
60 61

	bool ElementSet::HaveSibling() const
Kirill Terekhov's avatar
Kirill Terekhov committed
62
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
63 64 65
		assert(GetElementType() == ESET);
		Element::adj_type & hc = GetMeshLink()->HighConn(GetHandle());
		return hSibling(hc) != InvalidHandle();
Kirill Terekhov's avatar
Kirill Terekhov committed
66
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
67 68

	bool ElementSet::HaveChild() const
Kirill Terekhov's avatar
Kirill Terekhov committed
69
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
70 71 72
		assert(GetElementType() == ESET);
		Element::adj_type & hc = GetMeshLink()->HighConn(GetHandle());
		return hChild(hc) != InvalidHandle();
Kirill Terekhov's avatar
Kirill Terekhov committed
73
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
74 75 76 77

	void ElementSet::AddChild(const ElementSet & child) const
	{
		assert(GetElementType() == ESET);
Kirill Terekhov's avatar
Kirill Terekhov committed
78
		assert(child->GetElementType() == ESET);
Kirill Terekhov's avatar
Kirill Terekhov committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
		Mesh * m = GetMeshLink();
		Element::adj_type & parent_hc = m->HighConn(GetHandle());
		assert(isValid());
		if(hChild(parent_hc) == InvalidHandle()) 
		{
			Element::adj_type & child_hc = m->HighConn(child->GetHandle());
			assert(hParent(child_hc) == InvalidHandle()); // child is not connected to another parent
			hParent(child_hc) = GetHandle(); //connect child to me
			hChild(parent_hc) = child->GetHandle(); //connect me to child
		}
		else 
			ElementSet(m,hChild(parent_hc))->AddSibling(child); //connect as sibling to my children
	}

	void ElementSet::AddSibling(const ElementSet & sibling) const
Kirill Terekhov's avatar
Kirill Terekhov committed
94
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
95
		assert(GetElementType() == ESET);
Kirill Terekhov's avatar
Kirill Terekhov committed
96
		assert(sibling->GetElementType() == ESET);
Kirill Terekhov's avatar
Kirill Terekhov committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110
		Mesh * m = GetMeshLink();
		//perform checks
		Element::adj_type & current_hc = m->HighConn(GetHandle());
		assert(isValid());
		assert( hParent(current_hc) != InvalidHandle() ); //no parent for current set
		if( hSibling(current_hc) == InvalidHandle() ) //no sibling here
		{
			Element::adj_type & sibling_hc = m->HighConn(sibling->GetHandle());
			assert( hParent(sibling_hc) == InvalidHandle() );
			hSibling(current_hc) = sibling->GetHandle(); //connect sibling to me
			hParent(sibling_hc) = hParent(current_hc); //connect sibling to my parent
		}
		else 
			ElementSet(m,hSibling(current_hc))->AddSibling(sibling);
Kirill Terekhov's avatar
Kirill Terekhov committed
111
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
112 113

	void ElementSet::RemChild(const ElementSet & child) const
Kirill Terekhov's avatar
Kirill Terekhov committed
114
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
115 116 117 118 119 120 121 122 123 124 125 126 127
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		Element::adj_type & parent_hc = m->HighConn(GetHandle());
		assert(isValid());
		assert(hChild(parent_hc) != InvalidHandle());
		if( hChild(parent_hc) == child->GetHandle() )
		{
			Element::adj_type & child_hc = m->HighConn(child->GetHandle());
			assert(hParent(child_hc) == GetHandle()); //was connected to me
			hParent(child_hc) = InvalidHandle();
			hChild(parent_hc) = hSibling(child_hc); //get next sibling as my child
		}
		else ElementSet(m,hChild(parent_hc))->RemSibling(child);
Kirill Terekhov's avatar
Kirill Terekhov committed
128
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
129 130

	void ElementSet::RemSibling(const ElementSet & sibling) const
Kirill Terekhov's avatar
Kirill Terekhov committed
131
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
132 133 134 135 136 137
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		Element::adj_type & current_hc = m->HighConn(GetHandle());
		assert(isValid());
		assert(hSibling(current_hc) != InvalidHandle());
		if( hSibling(current_hc) == sibling->GetHandle())
Kirill Terekhov's avatar
Kirill Terekhov committed
138
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
139 140 141 142
			Element::adj_type & sibling_hc = m->HighConn(sibling->GetHandle());
			assert(hParent(sibling_hc) == hParent(current_hc));
			hParent(sibling_hc) = InvalidHandle(); //remove it's parent
			hSibling(current_hc) = hSibling(sibling_hc); //get next sibling as my sibling
Kirill Terekhov's avatar
Kirill Terekhov committed
143
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
		else ElementSet(m,hSibling(current_hc))->RemSibling(sibling);
	}

	Storage::enumerator ElementSet::CountChildren() const
	{
		assert(GetElementType() == ESET);
		ElementSet child = GetChild();
		return child->isValid() ? child->CountSiblings() : 0;
	}

	Storage::enumerator ElementSet::CountSiblings() const
	{
		assert(GetElementType() == ESET);
		ElementSet next = GetSibling();
		return next->isValid() ? next->CountSiblings()+1 : 1;
Kirill Terekhov's avatar
Kirill Terekhov committed
159
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
160 161 162 163 164

	std::string ElementSet::GetName() const
	{
		assert(GetElementType() == ESET);
		std::string ret;
Kirill Terekhov's avatar
Kirill Terekhov committed
165
		bulk_array arr = BulkArrayDV(GetMeshLink()->SetNameTag());
Kirill Terekhov's avatar
Kirill Terekhov committed
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
		ret.resize(arr.size());
		for(bulk_array::size_type it = 0; it < arr.size(); ++it)
			ret[it] = arr[it];
		return ret;
	}

	HandleType * ElementSet::getHandles() const
	{
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		Element::adj_type & lc = m->LowConn(GetHandle());
		return lc.data();
	}

	Storage::enumerator ElementSet::nbHandles() const
	{
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		Element::adj_type & lc = m->LowConn(GetHandle());
		return static_cast<enumerator>(lc.size());
	}

	void ElementSet::SetMarkerElements(MarkerType mrk, ElementType etype) const
	{
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		Element::adj_type & lc = m->LowConn(GetHandle());
		if( m->isMeshModified() )
		{
			MarkerType hm = m->HideMarker();
			for(Element::adj_type::iterator it = lc.begin(); it != lc.end(); ++it)
				if( (GetHandleElementType(*it) & etype) && !m->GetMarker(*it,hm) ) //check for type filters invalid handles
					m->SetMarker(*it,mrk);
		}
		else
		{
			for(Element::adj_type::iterator it = lc.begin(); it != lc.end(); ++it)//check for type filters invalid handles
				if( GetHandleElementType(*it) & etype )
					m->SetMarker(*it,mrk);
		}
	}

Kirill Terekhov's avatar
Kirill Terekhov committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

  void ElementSet::SetPrivateMarkerElements(MarkerType mrk, ElementType etype) const
	{
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		Element::adj_type & lc = m->LowConn(GetHandle());
		if( m->isMeshModified() )
		{
			MarkerType hm = m->HideMarker();
			for(Element::adj_type::iterator it = lc.begin(); it != lc.end(); ++it)
				if( (GetHandleElementType(*it) & etype) && !m->GetMarker(*it,hm) ) //check for type filters invalid handles
					m->SetPrivateMarker(*it,mrk);
		}
		else
		{
			for(Element::adj_type::iterator it = lc.begin(); it != lc.end(); ++it)//check for type filters invalid handles
				if( GetHandleElementType(*it) & etype )
					m->SetPrivateMarker(*it,mrk);
		}
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
	void ElementSet::RemMarkerElements(MarkerType mrk, ElementType etype) const
	{
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		Element::adj_type & lc = m->LowConn(GetHandle());
		if( m->isMeshModified() )
		{
			MarkerType hm = m->HideMarker();
			for(Element::adj_type::iterator it = lc.begin(); it != lc.end(); ++it)
				if( (GetHandleElementType(*it) & etype) && !m->GetMarker(*it,hm) )//check for type filters invalid handles
					m->RemMarker(*it,mrk);
		}
		else
		{
			for(Element::adj_type::iterator it = lc.begin(); it != lc.end(); ++it)//check for type filters invalid handles
				if( GetHandleElementType(*it) & etype )
					m->RemMarker(*it,mrk);
		}
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
  void ElementSet::RemPrivateMarkerElements(MarkerType mrk, ElementType etype) const
	{
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		Element::adj_type & lc = m->LowConn(GetHandle());
		if( m->isMeshModified() )
		{
			MarkerType hm = m->HideMarker();
			for(Element::adj_type::iterator it = lc.begin(); it != lc.end(); ++it)
				if( (GetHandleElementType(*it) & etype) && !m->GetMarker(*it,hm) )//check for type filters invalid handles
					m->RemPrivateMarker(*it,mrk);
		}
		else
		{
			for(Element::adj_type::iterator it = lc.begin(); it != lc.end(); ++it)//check for type filters invalid handles
				if( GetHandleElementType(*it) & etype )
					m->RemPrivateMarker(*it,mrk);
		}
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
266

Kirill Terekhov's avatar
Kirill Terekhov committed
267
	
Kirill Terekhov's avatar
Kirill Terekhov committed
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
	ElementArray<Element> ElementSet::getAdjElements(ElementType etype) const
	{
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		ElementArray<Element> ret(m);
		Element::adj_type const & lc = m->LowConn(GetHandle());
		if( m->isMeshModified() )
		{
			MarkerType hm = m->HideMarker();
			for(Element::adj_type::size_type it = 0; it < lc.size(); ++it)
				if( (GetHandleElementType(lc[it]) & etype) && !m->GetMarker(lc[it],hm) )//check for type filters invalid handles
					ret.push_back(lc[it]);
		}
		else
		{
			for(Element::adj_type::size_type it = 0; it < lc.size(); ++it)//check for type filters invalid handles
				if( GetHandleElementType(lc[it]) & etype )
					ret.push_back(lc[it]);
		}
		return ret;
	}

	Storage::enumerator ElementSet::nbAdjElements(ElementType etype) const
	{
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		Element::adj_type const & lc = m->LowConn(GetHandle());
		enumerator ret = 0;
		if( m->isMeshModified() )
		{
			MarkerType hm = m->HideMarker();
			for(Element::adj_type::size_type it = 0; it < lc.size(); ++it)
				if( (GetHandleElementType(lc[it]) & etype) && !m->GetMarker(lc[it],hm) )//check for type filters invalid handles
					++ret;
		}
		else
		{
			for(Element::adj_type::size_type it = 0; it < lc.size(); ++it)//check for type filters invalid handles
				if( GetHandleElementType(lc[it]) & etype )
					++ret;
		}
		return ret;
	}

	ElementArray<Element> ElementSet::getAdjElements(ElementType etype, MarkerType select, bool invert) const
	{
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		ElementArray<Element> ret(m);
		Element::adj_type const & lc = m->LowConn(GetHandle());
		if( m->isMeshModified() )
		{
			MarkerType hm = m->HideMarker();
			for(Element::adj_type::size_type it = 0; it < lc.size(); ++it)
Kirill Terekhov's avatar
Kirill Terekhov committed
322
				if( (GetHandleElementType(lc[it]) & etype) && !m->GetMarker(lc[it],hm) && (invert ^ m->GetMarker(lc[it],select)) )//check for type filters invalid handles
Kirill Terekhov's avatar
Kirill Terekhov committed
323 324 325 326 327
					ret.push_back(lc[it]);
		}
		else
		{
			for(Element::adj_type::size_type it = 0; it < lc.size(); ++it)
Kirill Terekhov's avatar
Kirill Terekhov committed
328
				if( (GetHandleElementType(lc[it]) & etype) && (invert ^ m->GetMarker(lc[it],select)) )//check for type filters invalid handles
Kirill Terekhov's avatar
Kirill Terekhov committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
					ret.push_back(lc[it]);
		}
		return ret;
	}

	Storage::enumerator ElementSet::nbAdjElements(ElementType etype, MarkerType select, bool invert) const
	{
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		enumerator ret = 0;
		Element::adj_type const & lc = m->LowConn(GetHandle());
		if( m->isMeshModified() )
		{
			MarkerType hm = m->HideMarker();
			for(Element::adj_type::size_type it = 0; it < lc.size(); ++it)
Kirill Terekhov's avatar
Kirill Terekhov committed
344
				if( (GetHandleElementType(lc[it]) & etype) && !m->GetMarker(lc[it],hm) && (invert ^ m->GetMarker(lc[it],select)) )//check for type filters invalid handles
Kirill Terekhov's avatar
Kirill Terekhov committed
345 346 347 348 349
					++ret;
		}
		else
		{
			for(Element::adj_type::size_type it = 0; it < lc.size(); ++it)
Kirill Terekhov's avatar
Kirill Terekhov committed
350
				if( (GetHandleElementType(lc[it]) & etype) && (invert ^ m->GetMarker(lc[it],select)) )//check for type filters invalid handles
Kirill Terekhov's avatar
Kirill Terekhov committed
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
					++ret;
		}
		return ret;
	}

	ElementArray<Node> ElementSet::getNodes() const
	{
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		ElementArray<Node> ret(m);
		Element::adj_type const & lc = m->LowConn(GetHandle());
		if( m->isMeshModified() )
		{
			MarkerType hm = m->HideMarker();
			for(Element::adj_type::size_type it = 0; it < lc.size(); ++it)
				if( (GetHandleElementType(lc[it]) & NODE) && !m->GetMarker(lc[it],hm) )//check for type filters invalid handles
					ret.push_back(lc[it]);
		}
		else
		{
			for(Element::adj_type::size_type it = 0; it < lc.size(); ++it)//check for type filters invalid handles
				if( GetHandleElementType(lc[it]) & NODE )
					ret.push_back(lc[it]);
		}
		return ret;
	}

	ElementArray<Node> ElementSet::getNodes(MarkerType select, bool invert) const
	{
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		ElementArray<Node> ret(m);
		Element::adj_type const & lc = m->LowConn(GetHandle());
		if( m->isMeshModified() )
		{
			MarkerType hm = m->HideMarker();
			for(Element::adj_type::size_type it = 0; it < lc.size(); ++it)
Kirill Terekhov's avatar
Kirill Terekhov committed
388
				if( (GetHandleElementType(lc[it]) & NODE) && !m->GetMarker(lc[it],hm) && (invert ^ m->GetMarker(lc[it],select)) )//check for type filters invalid handles
Kirill Terekhov's avatar
Kirill Terekhov committed
389 390 391 392 393
					ret.push_back(lc[it]);
		}
		else
		{
			for(Element::adj_type::size_type it = 0; it < lc.size(); ++it)
Kirill Terekhov's avatar
Kirill Terekhov committed
394
				if( (GetHandleElementType(lc[it]) & NODE) && (invert ^ m->GetMarker(lc[it],select)) )//check for type filters invalid handles
Kirill Terekhov's avatar
Kirill Terekhov committed
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
					ret.push_back(lc[it]);
		}
		return ret;
	}

	ElementArray<Edge> ElementSet::getEdges() const
	{
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		ElementArray<Edge> ret(m);
		Element::adj_type const & lc = m->LowConn(GetHandle());
		if( m->isMeshModified() )
		{
			MarkerType hm = m->HideMarker();
			for(Element::adj_type::size_type it = 0; it < lc.size(); ++it)
				if( (GetHandleElementType(lc[it]) & EDGE) && !m->GetMarker(lc[it],hm) )//check for type filters invalid handles
					ret.push_back(lc[it]);
		}
		else
		{
			for(Element::adj_type::size_type it = 0; it < lc.size(); ++it)//check for type filters invalid handles
				if( GetHandleElementType(lc[it]) & EDGE )
					ret.push_back(lc[it]);
		}
		return ret;
	}

	ElementArray<Edge> ElementSet::getEdges(MarkerType select, bool invert) const
Kirill Terekhov's avatar
Kirill Terekhov committed
423
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
424 425 426 427 428 429 430 431
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		ElementArray<Edge> ret(m);
		Element::adj_type const & lc = m->LowConn(GetHandle());
		if( m->isMeshModified() )
		{
			MarkerType hm = m->HideMarker();
			for(Element::adj_type::size_type it = 0; it < lc.size(); ++it)
Kirill Terekhov's avatar
Kirill Terekhov committed
432
				if( (GetHandleElementType(lc[it]) & EDGE) && !m->GetMarker(lc[it],hm) && (invert ^ m->GetMarker(lc[it],select)) )//check for type filters invalid handles
Kirill Terekhov's avatar
Kirill Terekhov committed
433 434 435 436 437
					ret.push_back(lc[it]);
		}
		else
		{
			for(Element::adj_type::size_type it = 0; it < lc.size(); ++it)
Kirill Terekhov's avatar
Kirill Terekhov committed
438
				if( (GetHandleElementType(lc[it]) & EDGE) && (invert ^ m->GetMarker(lc[it],select)) )//check for type filters invalid handles
Kirill Terekhov's avatar
Kirill Terekhov committed
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
					ret.push_back(lc[it]);
		}
		return ret;
	}

	ElementArray<Face> ElementSet::getFaces() const
	{
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		ElementArray<Face> ret(m);
		Element::adj_type const & lc = m->LowConn(GetHandle());
		if( m->isMeshModified() )
		{
			MarkerType hm = m->HideMarker();
			for(Element::adj_type::size_type it = 0; it < lc.size(); ++it)
				if( (GetHandleElementType(lc[it]) & FACE) && !m->GetMarker(lc[it],hm) )//check for type filters invalid handles
					ret.push_back(lc[it]);
		}
		else
		{
			for(Element::adj_type::size_type it = 0; it < lc.size(); ++it)//check for type filters invalid handles
				if( GetHandleElementType(lc[it]) & FACE )
					ret.push_back(lc[it]);
		}
		return ret;
	}

	ElementArray<Face> ElementSet::getFaces(MarkerType select, bool invert) const
	{
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		ElementArray<Face> ret(m);
		Element::adj_type const & lc = m->LowConn(GetHandle());
		if( m->isMeshModified() )
		{
			MarkerType hm = m->HideMarker();
			for(Element::adj_type::size_type it = 0; it < lc.size(); ++it)
Kirill Terekhov's avatar
Kirill Terekhov committed
476
				if( (GetHandleElementType(lc[it]) & FACE) && !m->GetMarker(lc[it],hm) && (invert ^ m->GetMarker(lc[it],select)) )//check for type filters invalid handles
Kirill Terekhov's avatar
Kirill Terekhov committed
477 478 479 480 481
					ret.push_back(lc[it]);
		}
		else
		{
			for(Element::adj_type::size_type it = 0; it < lc.size(); ++it)
Kirill Terekhov's avatar
Kirill Terekhov committed
482
				if( (GetHandleElementType(lc[it]) & FACE) && (invert ^ m->GetMarker(lc[it],select)) )//check for type filters invalid handles
Kirill Terekhov's avatar
Kirill Terekhov committed
483 484 485
					ret.push_back(lc[it]);
		}
		return ret;
Kirill Terekhov's avatar
Kirill Terekhov committed
486
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
487 488

	ElementArray<Cell> ElementSet::getCells() const
Kirill Terekhov's avatar
Kirill Terekhov committed
489
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
490 491 492 493 494 495 496 497 498 499 500 501
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		ElementArray<Cell> ret(m);
		Element::adj_type const & lc = m->LowConn(GetHandle());
		if( m->isMeshModified() )
		{
			MarkerType hm = m->HideMarker();
			for(Element::adj_type::size_type it = 0; it < lc.size(); ++it)
				if( (GetHandleElementType(lc[it]) & CELL) && !m->GetMarker(lc[it],hm) )//check for type filters invalid handles
					ret.push_back(lc[it]);
		}
		else
Kirill Terekhov's avatar
Kirill Terekhov committed
502
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
			for(Element::adj_type::size_type it = 0; it < lc.size(); ++it)//check for type filters invalid handles
				if( GetHandleElementType(lc[it]) & CELL )
					ret.push_back(lc[it]);
		}
		return ret;
	}

	ElementArray<Cell> ElementSet::getCells(MarkerType select, bool invert) const
	{
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		ElementArray<Cell> ret(m);
		Element::adj_type const & lc = m->LowConn(GetHandle());
		if( m->isMeshModified() )
		{
			MarkerType hm = m->HideMarker();
			for(Element::adj_type::size_type it = 0; it < lc.size(); ++it)
Kirill Terekhov's avatar
Kirill Terekhov committed
520
				if( (GetHandleElementType(lc[it]) & CELL) && !m->GetMarker(lc[it],hm) && (invert ^ m->GetMarker(lc[it],select)) )//check for type filters invalid handles
Kirill Terekhov's avatar
Kirill Terekhov committed
521 522 523 524 525
					ret.push_back(lc[it]);
		}
		else
		{
			for(Element::adj_type::size_type it = 0; it < lc.size(); ++it)
Kirill Terekhov's avatar
Kirill Terekhov committed
526
				if( (GetHandleElementType(lc[it]) & CELL) && (invert ^ m->GetMarker(lc[it],select)) )//check for type filters invalid handles
Kirill Terekhov's avatar
Kirill Terekhov committed
527 528 529 530 531 532 533 534 535 536 537 538 539
					ret.push_back(lc[it]);
		}
		return ret;
	}

	void ElementSet::PutElement(HandleType e) const
	{
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		Element::adj_type & lc = m->LowConn(GetHandle());
		switch(BulkDF(m->SetComparatorTag()))
		{
		case UNSORTED_COMPARATOR:
Kirill Terekhov's avatar
Kirill Terekhov committed
540
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
541 542 543 544 545 546 547
				Element::adj_type & hc = m->HighConn(GetHandle());
				if( hc.size() > high_conn_reserved )
				{
					lc[static_cast<enumerator>(hc.back())] = e;
					hc.pop_back();
				}
				else lc.push_back(e);
Kirill Terekhov's avatar
Kirill Terekhov committed
548
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
549 550
			break;
		case   HANDLE_COMPARATOR:
Kirill Terekhov's avatar
Kirill Terekhov committed
551
		case  HIERARCHY_COMPARATOR:
Kirill Terekhov's avatar
Kirill Terekhov committed
552 553 554 555
		case GLOBALID_COMPARATOR:
		case CENTROID_COMPARATOR:
			lc.push_back(e);
			break;
Kirill Terekhov's avatar
Kirill Terekhov committed
556 557
		}
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
558 559

	void ElementSet::PutElements(const HandleType * h, enumerator num) const
Kirill Terekhov's avatar
Kirill Terekhov committed
560
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		Element::adj_type & lc = m->LowConn(GetHandle());
		switch(BulkDF(m->SetComparatorTag()))
		{
		case UNSORTED_COMPARATOR:
			{
				const HandleType * h2 = h;
				Element::adj_type & hc = m->HighConn(GetHandle());
				while( hc.size() > high_conn_reserved )
				{
					lc[static_cast<enumerator>(hc.back())] = *h2;
					hc.pop_back();
					h2++;
				}
				lc.insert(lc.end(),h2, h+num);
			}
			break;
		case   HANDLE_COMPARATOR:
Kirill Terekhov's avatar
Kirill Terekhov committed
580
		case HIERARCHY_COMPARATOR:
Kirill Terekhov's avatar
Kirill Terekhov committed
581 582 583 584 585
		case GLOBALID_COMPARATOR:
		case CENTROID_COMPARATOR:
			lc.insert(lc.end(),h,h+num);
			break;
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
586
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
587 588

	void ElementSet::AddElement(HandleType e) const
Kirill Terekhov's avatar
Kirill Terekhov committed
589
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
590 591 592 593 594 595 596
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		Element::adj_type & lc = m->LowConn(GetHandle());
		Element::adj_type & hc = m->HighConn(GetHandle());
		Element::adj_type::iterator find;
		Element::adj_type::size_type sorted = static_cast<Element::adj_type::size_type>(hSorted(hc));
		switch(BulkDF(m->SetComparatorTag()))
Kirill Terekhov's avatar
Kirill Terekhov committed
597
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
598 599 600 601
		case UNSORTED_COMPARATOR:
			for(Element::adj_type::iterator it = lc.begin(); it != lc.end(); ++it)
				if( *it == e ) return;
			if( hc.size() > high_conn_reserved )
Kirill Terekhov's avatar
Kirill Terekhov committed
602
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
603 604
				lc[static_cast<enumerator>(hc.back())] = e;
				hc.pop_back();
Kirill Terekhov's avatar
Kirill Terekhov committed
605
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
606 607 608 609 610
			else lc.push_back(e);
			return;
		case   HANDLE_COMPARATOR:
			find = std::lower_bound(lc.begin(),lc.begin()+sorted,e);
			break;
Kirill Terekhov's avatar
Kirill Terekhov committed
611
		case  HIERARCHY_COMPARATOR:
Kirill Terekhov's avatar
Kirill Terekhov committed
612
			find = std::lower_bound(lc.begin(),lc.begin()+sorted,e,Mesh::HierarchyComparator(m));
Kirill Terekhov's avatar
Kirill Terekhov committed
613 614 615 616 617 618 619 620 621 622 623 624
			break;
		case GLOBALID_COMPARATOR:
			find = std::lower_bound(lc.begin(),lc.begin()+sorted,e,Mesh::GlobalIDComparator(m));
			break;
		case CENTROID_COMPARATOR:
			find = std::lower_bound(lc.begin(),lc.begin()+sorted,e,Mesh::CentroidComparator(m));
			break;
		}
		if( find != lc.begin()+sorted )
		{
			if( *find == e )
				return;
Kirill Terekhov's avatar
Kirill Terekhov committed
625
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
626 627
		lc.insert(find,1,e);
		hSorted(hc)++;
Kirill Terekhov's avatar
Kirill Terekhov committed
628
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
629 630

	void ElementSet::AddElements(const HandleType * h, enumerator num) const
Kirill Terekhov's avatar
Kirill Terekhov committed
631
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
632 633 634 635 636 637 638 639 640
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		Element::adj_type & lc = m->LowConn(GetHandle());
		Element::adj_type & hc = m->HighConn(GetHandle());
		switch(BulkDF(m->SetComparatorTag()))
		{
		case UNSORTED_COMPARATOR:
			{
				Element::adj_type & hc = m->HighConn(GetHandle());
Kirill Terekhov's avatar
Kirill Terekhov committed
641 642
				MarkerType mrk = m->CreatePrivateMarker();
				SetPrivateMarkerElements(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
643 644 645
				enumerator it = 0;
				while( it != num )
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
646
					if( !m->GetPrivateMarker(h[it],mrk) )
Kirill Terekhov's avatar
Kirill Terekhov committed
647 648 649 650 651 652 653 654 655 656
					{
						if( hc.size() > high_conn_reserved )
						{
							lc[static_cast<enumerator>(hc.back())] = h[it];
							hc.pop_back();
						}
						else lc.push_back(hc[it]);
					}
					++it;
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
657 658
				RemPrivateMarkerElements(mrk);
				m->ReleasePrivateMarker(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
			}
			break;
		case HANDLE_COMPARATOR:
			{
				// sort 
				array<HandleType> sorted(h,h+num);
				m->SortHandles(sorted.data(),sorted.size());
				//now merge with sorted part
				Element::adj_type merge(lc.size()+sorted.size());
				Element::adj_type::iterator it = lc.begin(), iend = lc.begin()+hSorted(hc);
				Element::adj_type::iterator jt = sorted.begin(), jend = sorted.end();
				Element::adj_type::iterator mit = merge.begin(), mitprev = merge.begin();
				//insert first element
				while(it != iend && jt != jend)
				{
					if( *it == InvalidHandle() ) ++it;
					else if( *jt == InvalidHandle() ) ++jt;
					else if( *it < *jt ) {*mit++ = *it++; break;}
					else {*mit++ = *jt++; break;}
				}
				//now insertion checks for duplicates
				while(it != iend && jt != jend)
				{
					if( *it == InvalidHandle() || *it == *mitprev ) ++it;
					else if( *jt == InvalidHandle() || *jt == *mitprev ) ++jt;
					else if( *it < *jt ) {mitprev = mit; *mit = *it; ++mit; ++it;}
Kirill Terekhov's avatar
Kirill Terekhov committed
685
					else {mitprev = mit; *mit = *jt; ++mit; ++jt;}
Kirill Terekhov's avatar
Kirill Terekhov committed
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
				}
				//attach rest
				while(it != iend && *it != InvalidHandle() && *it != *mitprev )
				{
					mitprev = mit;
					*mit = *it;
					++mit;
					++it;
				}
				while(jt != jend && *jt != InvalidHandle() && *jt != *mitprev )
				{
					mitprev = mit;
					*mit = *jt;
					++mit;
					++jt;
				}
				//indicate end of sorted part
				hSorted(hc) = static_cast<HandleType>(mit-merge.begin());
				//attach unsorted part with control for duplicates
Kirill Terekhov's avatar
Kirill Terekhov committed
705 706 707
				MarkerType mrk = m->CreatePrivateMarker();
				m->SetPrivateMarkerArray(merge.data(),hSorted(hc),mrk);
				while(it != lc.end() && *it != InvalidHandle() && !m->GetPrivateMarker(*it,mrk) )
Kirill Terekhov's avatar
Kirill Terekhov committed
708
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
709
					m->SetPrivateMarker(*it,mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
710 711 712
					*mit++ = *it++;
				}
				merge.resize(static_cast<Element::adj_type::size_type>(mit-merge.begin()));
Kirill Terekhov's avatar
Kirill Terekhov committed
713 714
				m->RemPrivateMarkerArray(merge.data(),static_cast<enumerator>(merge.size()),mrk);
				m->ReleasePrivateMarker(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
				// set merged elemetns as current
				lc.swap(merge);
				//indicate that there are no empty spaces
				hc.resize(high_conn_reserved);
			}
			break;
		case GLOBALID_COMPARATOR:
			{
				// sort 
				array<HandleType> sorted(h,h+num);
				m->SortByGlobalID(sorted.data(),sorted.size());
				//now merge with sorted part
				Element::adj_type merge(lc.size()+sorted.size());
				Element::adj_type::iterator it = lc.begin(), iend = lc.begin()+hSorted(hc);
				Element::adj_type::iterator jt = sorted.begin(), jend = sorted.end();
				Element::adj_type::iterator mit = merge.begin();
Kirill Terekhov's avatar
Kirill Terekhov committed
731 732
				MarkerType mrk = m->CreatePrivateMarker();
        //now insertion checks for duplicates
Kirill Terekhov's avatar
Kirill Terekhov committed
733 734
				while(it != iend && jt != jend)
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
					if( *it == InvalidHandle() || m->GetPrivateMarker(*it,mrk) ) ++it;
					else if( *jt == InvalidHandle() || m->GetPrivateMarker(*jt,mrk) ) ++jt;
					else if( Mesh::GlobalIDComparator(m)(*it,*jt) ) 
          {
            *mit = *it; 
            m->SetPrivateMarker(*mit,mrk);
            ++mit; 
            ++it;
          }
					else 
          {
            *mit = *jt;
            m->SetPrivateMarker(*mit,mrk);
            ++mit; 
            ++jt;
          }
Kirill Terekhov's avatar
Kirill Terekhov committed
751 752
				}
				//attach rest
Kirill Terekhov's avatar
Kirill Terekhov committed
753
				while(it != iend && *it != InvalidHandle() && !m->GetPrivateMarker(*it,mrk) )
Kirill Terekhov's avatar
Kirill Terekhov committed
754 755
				{
					*mit = *it;
Kirill Terekhov's avatar
Kirill Terekhov committed
756
          m->SetPrivateMarker(*mit,mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
757 758 759
					++mit;
					++it;
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
760
				while(jt != jend && *jt != InvalidHandle() && !m->GetPrivateMarker(*jt,mrk) )
Kirill Terekhov's avatar
Kirill Terekhov committed
761 762
				{
					*mit = *jt;
Kirill Terekhov's avatar
Kirill Terekhov committed
763
          m->SetPrivateMarker(*mit,mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
764 765 766 767 768 769
					++mit;
					++jt;
				}
				//indicate end of sorted part
				hSorted(hc) = static_cast<HandleType>(mit-merge.begin());
				//attach unsorted part
Kirill Terekhov's avatar
Kirill Terekhov committed
770
				while(it != lc.end() && *it != InvalidHandle() && !m->GetPrivateMarker(*it,mrk) )
Kirill Terekhov's avatar
Kirill Terekhov committed
771
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
772
					m->SetPrivateMarker(*it,mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
773 774 775
					*mit++ = *it++;
				}
				merge.resize(static_cast<Element::adj_type::size_type>(mit-merge.begin()));
Kirill Terekhov's avatar
Kirill Terekhov committed
776 777
				m->RemPrivateMarkerArray(merge.data(),static_cast<enumerator>(merge.size()),mrk);
				m->ReleasePrivateMarker(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
778 779 780 781 782 783
				// set merged elemetns as current
				lc.swap(merge);
				//indicate that there are no empty spaces
				hc.resize(high_conn_reserved);
			}
			break;
Kirill Terekhov's avatar
Kirill Terekhov committed
784
		case HIERARCHY_COMPARATOR:
Kirill Terekhov's avatar
Kirill Terekhov committed
785 786 787
			{
				// sort 
				array<HandleType> sorted(h,h+num);
Kirill Terekhov's avatar
Kirill Terekhov committed
788
				std::sort(sorted.begin(),sorted.end(),Mesh::HierarchyComparator(m));
Kirill Terekhov's avatar
Kirill Terekhov committed
789 790 791 792 793
				//now merge with sorted part
				Element::adj_type merge(lc.size()+sorted.size());
				Element::adj_type::iterator it = lc.begin(), iend = lc.begin()+hSorted(hc);
				Element::adj_type::iterator jt = sorted.begin(), jend = sorted.end();
				Element::adj_type::iterator mit = merge.begin(), mitprev = merge.begin();
Kirill Terekhov's avatar
Kirill Terekhov committed
794
				MarkerType mrk = m->CreatePrivateMarker();
Kirill Terekhov's avatar
Kirill Terekhov committed
795 796 797
				//now insertion checks for duplicates
				while(it != iend && jt != jend)
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
798 799
					if( *it == InvalidHandle() || m->GetPrivateMarker(*it,mrk) ) ++it;
					else if( *jt == InvalidHandle() || m->GetPrivateMarker(*jt,mrk) ) ++jt;
Kirill Terekhov's avatar
Kirill Terekhov committed
800
					else if( Mesh::HierarchyComparator(m)(*it,*jt) ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
801 802 803 804 805 806 807 808 809 810 811
          {
            *mit = *it; 
            m->SetPrivateMarker(*mit,mrk);
            ++mit; ++it;
          }
					else 
          {
            *mit = *jt; 
            m->SetPrivateMarker(*mit,mrk);
            ++mit; ++jt;
          }
Kirill Terekhov's avatar
Kirill Terekhov committed
812 813
				}
				//attach rest
Kirill Terekhov's avatar
Kirill Terekhov committed
814
				while(it != iend && *it != InvalidHandle() && !m->GetPrivateMarker(*it,mrk) )
Kirill Terekhov's avatar
Kirill Terekhov committed
815 816
				{
					*mit = *it;
Kirill Terekhov's avatar
Kirill Terekhov committed
817
          m->SetPrivateMarker(*mit,mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
818 819 820
					++mit;
					++it;
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
821
				while(jt != jend && *jt != InvalidHandle() && !m->GetPrivateMarker(*jt,mrk) )
Kirill Terekhov's avatar
Kirill Terekhov committed
822 823
				{
					*mit = *jt;
Kirill Terekhov's avatar
Kirill Terekhov committed
824
          m->SetPrivateMarker(*mit,mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
825 826 827 828 829 830
					++mit;
					++jt;
				}
				//indicate end of sorted part
				hSorted(hc) = static_cast<HandleType>(mit-merge.begin());
				//attach unsorted part
Kirill Terekhov's avatar
Kirill Terekhov committed
831
				while(it != lc.end() && *it != InvalidHandle() && !m->GetPrivateMarker(*it,mrk) )
Kirill Terekhov's avatar
Kirill Terekhov committed
832
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
833
					m->SetPrivateMarker(*it,mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
834 835 836
					*mit++ = *it++;
				}
				merge.resize(static_cast<Element::adj_type::size_type>(mit-merge.begin()));
Kirill Terekhov's avatar
Kirill Terekhov committed
837 838
				m->RemPrivateMarkerArray(merge.data(),static_cast<enumerator>(merge.size()),mrk);
				m->ReleasePrivateMarker(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
				// set merged elemetns as current
				lc.swap(merge);
				//indicate that there are no empty spaces
				hc.resize(high_conn_reserved);
			}
			break;
		case CENTROID_COMPARATOR:
			{
				// sort 
				array<HandleType> sorted(h,h+num);
				std::sort(sorted.begin(),sorted.end(),Mesh::CentroidComparator(m));
				//now merge with sorted part
				Element::adj_type merge(lc.size()+sorted.size());
				Element::adj_type::iterator it = lc.begin(), iend = lc.begin()+hSorted(hc);
				Element::adj_type::iterator jt = sorted.begin(), jend = sorted.end();
				Element::adj_type::iterator mit = merge.begin(), mitprev = merge.begin();
Kirill Terekhov's avatar
Kirill Terekhov committed
855
				MarkerType mrk = m->CreatePrivateMarker();
Kirill Terekhov's avatar
Kirill Terekhov committed
856 857 858
				//now insertion checks for duplicates
				while(it != iend && jt != jend)
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
859 860 861 862 863 864 865 866 867 868 869 870 871 872
					if( *it == InvalidHandle() || m->GetPrivateMarker(*it,mrk) ) ++it;
					else if( *jt == InvalidHandle() || m->GetPrivateMarker(*jt,mrk) ) ++jt;
					else if( Mesh::CentroidComparator(m)(*it,*jt) ) 
          {
            *mit = *it; 
            m->SetPrivateMarker(*mit,mrk);
            ++mit; ++it;
          }
					else 
          {
            *mit = *jt; 
            m->SetPrivateMarker(*mit,mrk);
            ++mit; ++it;
          }
Kirill Terekhov's avatar
Kirill Terekhov committed
873 874
				}
				//attach rest
Kirill Terekhov's avatar
Kirill Terekhov committed
875
				while(it != iend && *it != InvalidHandle() && !m->GetPrivateMarker(*it,mrk) )
Kirill Terekhov's avatar
Kirill Terekhov committed
876 877
				{
					*mit = *it;
Kirill Terekhov's avatar
Kirill Terekhov committed
878
          m->SetPrivateMarker(*mit,mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
879 880 881
					++mit;
					++it;
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
882
				while(jt != jend && *jt != InvalidHandle() && !m->GetPrivateMarker(*jt,mrk) )
Kirill Terekhov's avatar
Kirill Terekhov committed
883 884
				{
					*mit = *jt;
Kirill Terekhov's avatar
Kirill Terekhov committed
885
          m->SetPrivateMarker(*mit,mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
886 887 888 889 890 891
					++mit;
					++jt;
				}
				//indicate end of sorted part
				hSorted(hc) = static_cast<HandleType>(mit-merge.begin());
				//attach unsorted part
Kirill Terekhov's avatar
Kirill Terekhov committed
892
				while(it != lc.end() && *it != InvalidHandle() && !m->GetPrivateMarker(*it,mrk) )
Kirill Terekhov's avatar
Kirill Terekhov committed
893
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
894
					m->SetPrivateMarker(*it,mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
895 896 897
					*mit++ = *it++;
				}
				merge.resize(static_cast<Element::adj_type::size_type>(mit-merge.begin()));
Kirill Terekhov's avatar
Kirill Terekhov committed
898 899
				m->RemPrivateMarkerArray(merge.data(),static_cast<enumerator>(merge.size()),mrk);
				m->ReleasePrivateMarker(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
900 901 902 903 904 905 906
				// set merged elemetns as current
				lc.swap(merge);
				//indicate that there are no empty spaces
				hc.resize(high_conn_reserved);
			}
			break;
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
907
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
908 909

	void ElementSet::RemoveElement(const Storage & e) const
Kirill Terekhov's avatar
Kirill Terekhov committed
910
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		Element::adj_type & lc = m->LowConn(GetHandle());
		Element::adj_type & hc = m->HighConn(GetHandle());
		Element::adj_type::iterator find;
		Element::adj_type::size_type i, sorted = static_cast<Element::adj_type::size_type>(hSorted(hc));
		switch(BulkDF(m->SetComparatorTag()))
		{
		case UNSORTED_COMPARATOR:
			for(Element::adj_type::size_type it = 0; it < lc.size(); ++it)
				if( lc[it] == e->GetHandle() )
				{
					lc[it] = InvalidHandle();
					hc.push_back(static_cast<HandleType>(it));
					break;
				}
			return;
		case   HANDLE_COMPARATOR:
			find = std::lower_bound(lc.begin(),lc.begin()+sorted,e->GetHandle());
			break;
Kirill Terekhov's avatar
Kirill Terekhov committed
931
		case  HIERARCHY_COMPARATOR:
Kirill Terekhov's avatar
Kirill Terekhov committed
932
			find = std::lower_bound(lc.begin(),lc.begin()+sorted,e->GetHandle(),Mesh::HierarchyComparator(m));
Kirill Terekhov's avatar
Kirill Terekhov committed
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
			break;
		case GLOBALID_COMPARATOR:
			find = std::lower_bound(lc.begin(),lc.begin()+sorted,e->GetHandle(),Mesh::GlobalIDComparator(m));
			break;
		case CENTROID_COMPARATOR:
			find = std::lower_bound(lc.begin(),lc.begin()+sorted,e->GetHandle(),Mesh::CentroidComparator(m));
			break;
		}
		if( find != lc.begin()+sorted )
		{
			if( *find == e->GetHandle() )
			{
				*find = InvalidHandle();
				hc.push_back(static_cast<HandleType>(find-lc.begin()));
			}
		}
		else for(i = sorted; i < lc.size(); ++i) //unsorted part
			if( lc[i] == e->GetHandle() )
			{
				lc[i] = InvalidHandle();
				hc.push_back(static_cast<HandleType>(i));
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
955
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
956 957

	void ElementSet::RemoveElements(const HandleType * h, enumerator num) const
Kirill Terekhov's avatar
Kirill Terekhov committed
958
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
959 960 961 962 963
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		Element::adj_type & lc = m->LowConn(GetHandle());
		Element::adj_type & hc = m->HighConn(GetHandle());
		Element::adj_type::size_type i;
Kirill Terekhov's avatar
Kirill Terekhov committed
964 965
		MarkerType mrk = m->CreatePrivateMarker();
		m->SetPrivateMarkerArray(h,num,mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
966 967
		for(i = 0; i < lc.size(); i++)
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
968
			if( m->GetPrivateMarker(lc[i],mrk) )
Kirill Terekhov's avatar
Kirill Terekhov committed
969 970 971 972 973
			{
				lc[i] = InvalidHandle();
				hc.push_back(i);
			}
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
974 975
		m->RemPrivateMarkerArray(h,num,mrk);
		m->ReleasePrivateMarker(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
976
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
977 978

	ElementArray<Element> ElementSet::Union(const ElementSet & other) const
Kirill Terekhov's avatar
Kirill Terekhov committed
979
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
980 981 982 983
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		ElementArray<Element> ret(m);
		Element::adj_type & lc = m->LowConn(GetHandle());
Kirill Terekhov's avatar
Kirill Terekhov committed
984
		MarkerType mrk = m->CreatePrivateMarker();
Kirill Terekhov's avatar
Kirill Terekhov committed
985 986
		for(Element::adj_type::size_type i = 0; i < lc.size(); ++i)
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
987
			if( lc[i] != InvalidHandle() && !m->GetPrivateMarker(lc[i],mrk))
Kirill Terekhov's avatar
Kirill Terekhov committed
988 989
			{
				ret.push_back(lc[i]);
Kirill Terekhov's avatar
Kirill Terekhov committed
990
				m->SetPrivateMarker(ret.atback(),mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
991 992 993
			}
		}
		Element::adj_type & other_lc = m->LowConn(other->GetHandle());
Kirill Terekhov's avatar
Kirill Terekhov committed
994
		for(const HandleType * h = other_lc.data(); h != other_lc.data() + other_lc.size(); ++h)
Kirill Terekhov's avatar
Kirill Terekhov committed
995
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
996
			if( *h != InvalidHandle() && !m->GetPrivateMarker(*h,mrk) )
Kirill Terekhov's avatar
Kirill Terekhov committed
997 998
			{
				ret.push_back(*h);
Kirill Terekhov's avatar
Kirill Terekhov committed
999
				m->SetPrivateMarker(ret.atback(),mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1000 1001
			}
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
1002 1003
		ret.RemPrivateMarker(mrk);
		m->ReleasePrivateMarker(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1004
		return ret;
Kirill Terekhov's avatar
Kirill Terekhov committed
1005
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
1006 1007

	ElementArray<Element> ElementSet::Union(const HandleType * handles, enumerator num) const
Kirill Terekhov's avatar
Kirill Terekhov committed
1008
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
1009 1010 1011 1012
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		ElementArray<Element> ret(m);
		Element::adj_type & lc = m->LowConn(GetHandle());
Kirill Terekhov's avatar
Kirill Terekhov committed
1013
		MarkerType mrk = m->CreatePrivateMarker();
Kirill Terekhov's avatar
Kirill Terekhov committed
1014 1015
		for(Element::adj_type::size_type i = 0; i < lc.size(); ++i)
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
1016
			if( lc[i] != InvalidHandle() && !m->GetPrivateMarker(lc[i],mrk))
Kirill Terekhov's avatar
Kirill Terekhov committed
1017 1018
			{
				ret.push_back(lc[i]);
Kirill Terekhov's avatar
Kirill Terekhov committed
1019
				m->SetPrivateMarker(ret.atback(),mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1020 1021 1022 1023
			}
		}
		for(const HandleType * h = handles; h != handles+num; ++h)
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
1024
			if( *h != InvalidHandle() && !m->GetPrivateMarker(*h,mrk) )
Kirill Terekhov's avatar
Kirill Terekhov committed
1025 1026
			{
				ret.push_back(*h);
Kirill Terekhov's avatar
Kirill Terekhov committed
1027
				m->SetPrivateMarker(ret.atback(),mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1028 1029
			}
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
1030 1031
		ret.RemPrivateMarker(mrk);
		m->ReleasePrivateMarker(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1032
		return ret;
Kirill Terekhov's avatar
Kirill Terekhov committed
1033
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
1034 1035

	ElementArray<Element> ElementSet::Difference(const ElementSet & other) const
Kirill Terekhov's avatar
Kirill Terekhov committed
1036
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
1037 1038 1039 1040
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		ElementArray<Element> ret(m);
		Element::adj_type & lc = m->LowConn(GetHandle());
Kirill Terekhov's avatar
Kirill Terekhov committed
1041 1042
		MarkerType mrk = m->CreatePrivateMarker();
		other->SetPrivateMarkerElements(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1043 1044
		for(Element::adj_type::size_type i = 0; i < lc.size(); ++i)
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
1045
			if( lc[i] != InvalidHandle() && !m->GetPrivateMarker(lc[i],mrk))
Kirill Terekhov's avatar
Kirill Terekhov committed
1046 1047 1048 1049 1050
			{
				ret.push_back(lc[i]);
				m->SetMarker(ret.atback(),mrk);
			}
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
1051 1052 1053
		other->RemPrivateMarkerElements(mrk);
		ret.RemPrivateMarker(mrk);
		m->ReleasePrivateMarker(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1054
		return ret;
Kirill Terekhov's avatar
Kirill Terekhov committed
1055
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
1056 1057

	ElementArray<Element> ElementSet::Difference(const HandleType * handles, enumerator num) const
Kirill Terekhov's avatar
Kirill Terekhov committed
1058
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
1059 1060 1061 1062
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		ElementArray<Element> ret(m);
		Element::adj_type & lc = m->LowConn(GetHandle());
Kirill Terekhov's avatar
Kirill Terekhov committed
1063 1064
		MarkerType mrk = m->CreatePrivateMarker();
		m->SetPrivateMarkerArray(handles,num,mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1065 1066
		for(Element::adj_type::size_type i = 0; i < lc.size(); ++i)
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
1067
			if( lc[i] != InvalidHandle() && !m->GetPrivateMarker(lc[i],mrk))
Kirill Terekhov's avatar
Kirill Terekhov committed
1068 1069
			{
				ret.push_back(lc[i]);
Kirill Terekhov's avatar
Kirill Terekhov committed
1070
				m->SetPrivateMarker(ret.atback(),mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1071 1072
			}
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
1073 1074 1075
		m->RemPrivateMarkerArray(handles,num,mrk);
		ret.RemPrivateMarker(mrk);
		m->ReleasePrivateMarker(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1076
		return ret;
Kirill Terekhov's avatar
Kirill Terekhov committed
1077
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
1078 1079

	ElementArray<Element> ElementSet::Intersection(const ElementSet & other) const
Kirill Terekhov's avatar
Kirill Terekhov committed
1080
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
1081 1082 1083 1084
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		ElementArray<Element> ret(m);
		Element::adj_type & lc = m->LowConn(GetHandle());
Kirill Terekhov's avatar
Kirill Terekhov committed
1085 1086
		MarkerType mrk = m->CreatePrivateMarker(), mrk2 = m->CreatePrivateMarker();
		other->SetPrivateMarkerElements(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1087 1088
		for(Element::adj_type::size_type i = 0; i < lc.size(); ++i)
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
1089
			if( lc[i] != InvalidHandle() && m->GetPrivateMarker(lc[i],mrk) && !m->GetPrivateMarker(lc[i],mrk2))
Kirill Terekhov's avatar
Kirill Terekhov committed
1090 1091
			{
				ret.push_back(lc[i]);
Kirill Terekhov's avatar
Kirill Terekhov committed
1092
				m->SetPrivateMarker(lc[i],mrk2);
Kirill Terekhov's avatar
Kirill Terekhov committed
1093 1094
			}
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
1095 1096 1097 1098
		other->RemPrivateMarkerElements(mrk);
		ret.RemPrivateMarker(mrk2);
		m->ReleasePrivateMarker(mrk2);
		m->ReleasePrivateMarker(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1099
		return ret;
Kirill Terekhov's avatar
Kirill Terekhov committed
1100
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
1101 1102 1103 1104 1105 1106 1107

	ElementArray<Element> ElementSet::Intersection(const HandleType * handles, enumerator num) const
	{
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		ElementArray<Element> ret(m);
		Element::adj_type & lc = m->LowConn(GetHandle());
Kirill Terekhov's avatar
Kirill Terekhov committed
1108 1109
		MarkerType mrk = m->CreatePrivateMarker(), mrk2 = m->CreatePrivateMarker();
		m->SetPrivateMarkerArray(handles,num,mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1110 1111
		for(Element::adj_type::size_type i = 0; i < lc.size(); ++i)
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
1112
			if( lc[i] != InvalidHandle() && m->GetPrivateMarker(lc[i],mrk) && !m->GetPrivateMarker(lc[i],mrk2))
Kirill Terekhov's avatar
Kirill Terekhov committed
1113 1114
			{
				ret.push_back(lc[i]);
Kirill Terekhov's avatar
Kirill Terekhov committed
1115
				m->SetPrivateMarker(lc[i],mrk2);
Kirill Terekhov's avatar
Kirill Terekhov committed
1116 1117
			}
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
1118 1119 1120 1121
		m->RemPrivateMarkerArray(handles,num,mrk);
		ret.RemPrivateMarker(mrk2);
		m->ReleasePrivateMarker(mrk2);
		m->ReleasePrivateMarker(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
		return ret;
	}


	void ElementSet::Unite(const ElementSet & other) const
	{
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		bulk cmp = BulkDF(m->SetComparatorTag());
		Element::adj_type & other_lc = m->LowConn(other->GetHandle());
		if( cmp == UNSORTED_COMPARATOR || other->BulkDF(m->SetComparatorTag()) != cmp )
			Unite(other_lc.data(),static_cast<enumerator>(other_lc.size()));
		else //other set sorted by the same comparator, just merge them
		{
			Element::adj_type & other_hc = m->HighConn(other->GetHandle());
			Element::adj_type & hc = m->HighConn(GetHandle());
			Element::adj_type & lc = m->LowConn(GetHandle());
			Element::adj_type merge(lc.size()+other_lc.size());
			Element::adj_type::iterator it = lc.begin(), iend = lc.begin()+hSorted(hc);
			Element::adj_type::iterator jt = other_lc.begin(), jend = other_lc.end()+hSorted(other_hc);
			Element::adj_type::iterator mit = merge.begin(), mitprev = merge.begin();
Kirill Terekhov's avatar
Kirill Terekhov committed
1143
			MarkerType mrk = m->CreatePrivateMarker();
Kirill Terekhov's avatar
Kirill Terekhov committed
1144 1145 1146
			//now insertion checks for duplicates
			while(it != iend && jt != jend)
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1147 1148
				if( *it == InvalidHandle() || m->GetPrivateMarker(*it,mrk) ) ++it;
				else if( *jt == InvalidHandle() || m->GetPrivateMarker(*jt,mrk) ) ++jt;
Kirill Terekhov's avatar
Kirill Terekhov committed
1149 1150 1151 1152 1153 1154
				else 
				{
					bool cmp_res = false;
					switch(cmp)
					{
					case HANDLE_COMPARATOR: cmp_res = *it < *jt; break;
Kirill Terekhov's avatar
Kirill Terekhov committed
1155
					case HIERARCHY_COMPARATOR: cmp_res = Mesh::HierarchyComparator(m)(*it,*jt); break;
Kirill Terekhov's avatar
Kirill Terekhov committed
1156 1157 1158 1159 1160 1161
					case CENTROID_COMPARATOR: cmp_res = Mesh::CentroidComparator(m)(*it,*jt); break;
					case GLOBALID_COMPARATOR: cmp_res = Mesh::GlobalIDComparator(m)(*it,*jt); break;
					default: assert(false);
					}
					if( cmp_res ) 
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
1162 1163 1164 1165
						*mit = *it;
            m->SetPrivateMarker(*mit,mrk);
						++mit; 
            ++it;
Kirill Terekhov's avatar
Kirill Terekhov committed
1166 1167 1168 1169
					}
					else 
					{
						*mit = *jt; 
Kirill Terekhov's avatar
Kirill Terekhov committed
1170
            m->SetPrivateMarker(*mit,mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1171
						++mit; 
Kirill Terekhov's avatar
Kirill Terekhov committed
1172
						++jt;
Kirill Terekhov's avatar
Kirill Terekhov committed
1173 1174 1175 1176
					}
				}
			}
			//attach rest one of the sets not reached it's end
Kirill Terekhov's avatar
Kirill Terekhov committed
1177
			while(it != iend && *it != InvalidHandle() && !m->GetPrivateMarker(*it,mrk) )
Kirill Terekhov's avatar
Kirill Terekhov committed
1178 1179
			{
				*mit = *it;
Kirill Terekhov's avatar
Kirill Terekhov committed
1180
        m->SetPrivateMarker(*mit,mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1181 1182 1183
				++mit;
				++it;
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
1184
			while(jt != jend && *jt != InvalidHandle() && !m->GetPrivateMarker(*jt,mrk) )
Kirill Terekhov's avatar
Kirill Terekhov committed
1185 1186
			{
				*mit = *jt;
Kirill Terekhov's avatar
Kirill Terekhov committed
1187
        m->SetPrivateMarker(*mit,mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1188 1189 1190 1191 1192 1193
				++mit;
				++jt;
			}
			//indicate end of sorted part
			hSorted(hc) = static_cast<HandleType>(mit-merge.begin());
			//attach unsorted part
Kirill Terekhov's avatar
Kirill Terekhov committed
1194
			while(it != lc.end() && *it != InvalidHandle() && !m->GetPrivateMarker(*it,mrk) )
Kirill Terekhov's avatar
Kirill Terekhov committed
1195
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1196
				m->SetPrivateMarker(*it,mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1197 1198 1199
				*mit++ = *it++;
			}
			//attach unsorted part
Kirill Terekhov's avatar
Kirill Terekhov committed
1200
			while(jt != other_lc.end() && *jt != InvalidHandle() && !m->GetPrivateMarker(*jt,mrk) )
Kirill Terekhov's avatar
Kirill Terekhov committed
1201
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1202
				m->SetPrivateMarker(*jt,mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1203 1204 1205
				*mit++ = *jt++;
			}
			merge.resize(static_cast<Element::adj_type::size_type>(mit-merge.begin()));
Kirill Terekhov's avatar
Kirill Terekhov committed
1206 1207
			m->RemPrivateMarkerArray(merge.data(),static_cast<enumerator>(merge.size()),mrk);
			m->ReleasePrivateMarker(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
			// set merged elemetns as current
			lc.swap(merge);
			//indicate that there are no empty spaces
			hc.resize(high_conn_reserved);
		}
	}

	void ElementSet::Unite(const HandleType * handles, enumerator num) const
	{
		assert(GetElementType() == ESET);
		AddElements(handles,num); //These algorithms are identical
	}


	void ElementSet::Subtract(const ElementSet & other) const
	{
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		Element::adj_type & lc = m->LowConn(GetHandle());
		Element::adj_type & hc = m->LowConn(GetHandle());
Kirill Terekhov's avatar
Kirill Terekhov committed
1228 1229
		MarkerType mrk = m->CreatePrivateMarker();
		other->SetPrivateMarkerElements(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1230 1231
		for(Element::adj_type::size_type i = 0; i < lc.size(); ++i)
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
1232
			if( lc[i] != InvalidHandle() && m->GetPrivateMarker(lc[i],mrk))
Kirill Terekhov's avatar
Kirill Terekhov committed
1233 1234 1235 1236 1237
			{
				lc[i] = InvalidHandle();
				hc.push_back(i);
			}
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
1238 1239
		other->RemPrivateMarkerElements(mrk);
		m->ReleasePrivateMarker(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1240 1241 1242 1243 1244 1245 1246 1247
	}

	void ElementSet::Subtract(const HandleType * handles, enumerator num) const
	{
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		Element::adj_type & lc = m->LowConn(GetHandle());
		Element::adj_type & hc = m->HighConn(GetHandle());
Kirill Terekhov's avatar
Kirill Terekhov committed
1248 1249
		MarkerType mrk = m->CreatePrivateMarker();
		m->SetPrivateMarkerArray(handles,num,mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1250 1251
		for(Element::adj_type::size_type i = 0; i < lc.size(); ++i)
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
1252
			if( lc[i] != InvalidHandle() && m->GetPrivateMarker(lc[i],mrk))
Kirill Terekhov's avatar
Kirill Terekhov committed
1253 1254 1255 1256 1257
			{
				lc[i] = InvalidHandle();
				hc.push_back(i);
			}
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
1258 1259
		m->RemPrivateMarkerArray(handles,num,mrk);
		m->ReleasePrivateMarker(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1260 1261 1262 1263 1264 1265 1266 1267
	}

	void ElementSet::Intersect(const ElementSet & other) const
	{
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		Element::adj_type & lc = m->LowConn(GetHandle());
		Element::adj_type & hc = m->HighConn(GetHandle());
Kirill Terekhov's avatar
Kirill Terekhov committed
1268 1269
		MarkerType mrk = m->CreatePrivateMarker();
		other->SetPrivateMarkerElements(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1270 1271
		for(Element::adj_type::size_type i = 0; i < lc.size(); ++i)
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
1272
			if( lc[i] != InvalidHandle() && !m->GetPrivateMarker(lc[i],mrk) )
Kirill Terekhov's avatar
Kirill Terekhov committed
1273 1274 1275 1276 1277
			{
				lc[i] = InvalidHandle();
				hc.push_back(i);
			}
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
1278 1279
		other->RemPrivateMarkerElements(mrk);
		m->ReleasePrivateMarker(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1280 1281 1282
	}

	void ElementSet::Intersect(const HandleType * handles, enumerator num) const
Kirill Terekhov's avatar
Kirill Terekhov committed
1283
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
1284 1285 1286 1287
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		Element::adj_type & lc = m->LowConn(GetHandle());
		Element::adj_type & hc = m->HighConn(GetHandle());
Kirill Terekhov's avatar
Kirill Terekhov committed
1288 1289
		MarkerType mrk = m->CreatePrivateMarker();
		m->SetPrivateMarkerArray(handles,num,mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1290 1291
		for(Element::adj_type::size_type i = 0; i < lc.size(); ++i)
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
1292
			if( lc[i] != InvalidHandle() && !m->GetPrivateMarker(lc[i],mrk) )
Kirill Terekhov's avatar
Kirill Terekhov committed
1293 1294 1295 1296 1297
			{
				lc[i] = InvalidHandle();
				hc.push_back(i);
			}
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
1298 1299
		m->RemPrivateMarkerArray(handles,num,mrk);
		m->ReleasePrivateMarker(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
	}

	void ElementSet::SortSet(ComparatorType comp) const
	{
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		Element::adj_type * lc;
		Element::adj_type & hc = m->HighConn(GetHandle());
		if( comp != UNSORTED_COMPARATOR )
			lc = &m->LowConn(GetHandle());
		bulk & comparator = BulkDF(m->SetComparatorTag());
		switch(comp)
		{
		case GLOBALID_COMPARATOR:
			{
				Element::adj_type::size_type start = hSorted(hc);
				Element::adj_type::size_type i, j = 0, k = 0, num = lc->size() - start;
				if( hc.size() > high_conn_reserved ) // there are invalid handles
				{
					//move invalid handles to the end
					for(i = start; i < lc->size(); i++)
					{
						if( lc->at(i) != InvalidHandle() )
						{
							lc->at(j++) = lc->at(i);
							lc->at(i) = InvalidHandle();
						}
						else k++;
					}
					num = lc->size() - start - k; //size of array to sort
				}
				m->SortByGlobalID(lc->data()+start,static_cast<enumerator>(num));
				if( start > 0 ) //need to merge two parts
				{
					Element::adj_type temp(lc->size());
					Element::adj_type::iterator it = lc->begin(), jt = lc->begin()+start;
					Element::adj_type::iterator iend = jt, jend = lc->end();
					Element::adj_type::iterator t = temp.begin();
					while(it != iend && jt != jend)
					{
						if( *it == InvalidHandle() ) ++it;
						else if( *jt == InvalidHandle() ) ++jt;
						else if( Mesh::GlobalIDComparator(m)(*it,*jt) ) *t++ = *it++;
						else *t++ = *jt++;
					}
					assert(it == iend || jt == jend);
					while(it != iend && *it != InvalidHandle() ) *t++ = *it++;
					while(jt != jend && *jt != InvalidHandle() ) *t++ = *jt++;
					//number of deleted items and invalid handles should match
					assert( (temp.size() - (t-temp.begin())) == (hc.size()-ElementSet::high_conn_reserved) );
					lc->swap(temp);
				}
			}
			break;
		case CENTROID_COMPARATOR:
			{
				Element::adj_type::size_type start = hSorted(hc);
				std::sort(lc->begin()+start,lc->end(),Mesh::CentroidComparator(m));
				if( start > 0 ) //need to merge two parts
				{
					Element::adj_type temp(lc->size());
					Element::adj_type::iterator it = lc->begin(), jt = lc->begin()+start;
					Element::adj_type::iterator iend = jt, jend = lc->end();
					Element::adj_type::iterator t = temp.begin();
					while(it != iend && jt != jend)
					{
						if( *it == InvalidHandle() ) ++it;
						else if( *jt == InvalidHandle() ) ++jt;
						else if( Mesh::CentroidComparator(m)(*it,*jt) ) *t++ = *it++;
						else *t++ = *jt++;
					}
					assert(it == iend || jt == jend);
					while(it != iend && *it != InvalidHandle() ) *t++ = *it++;
					while(jt != jend && *jt != InvalidHandle() ) *t++ = *jt++;
					//number of deleted items and invalid handles should match
					assert( (temp.size() - (t-temp.begin())) == (hc.size()-ElementSet::high_conn_reserved) );
					lc->swap(temp);
				}
			}
			break;
Kirill Terekhov's avatar
Kirill Terekhov committed
1380
		case  HIERARCHY_COMPARATOR:
Kirill Terekhov's avatar
Kirill Terekhov committed
1381 1382
			{
				Element::adj_type::size_type start = hSorted(hc);
Kirill Terekhov's avatar
Kirill Terekhov committed
1383
				std::sort(lc->begin()+start,lc->end(),Mesh::HierarchyComparator(m));
Kirill Terekhov's avatar
Kirill Terekhov committed
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
				if( start > 0 ) //need to merge two parts
				{
					Element::adj_type temp(lc->size());
					Element::adj_type::iterator it = lc->begin(), jt = lc->begin()+start;
					Element::adj_type::iterator iend = jt, jend = lc->end();
					Element::adj_type::iterator t = temp.begin();
					while(it != iend && jt != jend)
					{
						if( *it == InvalidHandle() ) ++it;
						else if( *jt == InvalidHandle() ) ++jt;
Kirill Terekhov's avatar
Kirill Terekhov committed
1394
						else if( Mesh::HierarchyComparator(m)(*it,*jt) ) *t++ = *it++;
Kirill Terekhov's avatar
Kirill Terekhov committed
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
						else *t++ = *jt++;
					}
					assert(it == iend || jt == jend);
					while(it != iend && *it != InvalidHandle() ) *t++ = *it++;
					while(jt != jend && *jt != InvalidHandle() ) *t++ = *jt++;
					//number of deleted items and invalid handles should match
					assert( (temp.size() - (t-temp.begin())) == (hc.size()-ElementSet::high_conn_reserved) );
					lc->swap(temp);
				}
			}
			break;
		case HANDLE_COMPARATOR:
			{
				Element::adj_type::size_type start = hSorted(hc);
				m->SortHandles(lc->data()+start,static_cast<enumerator>(lc->size()-start));
				if( start > 0 ) //need to merge two parts
				{
					Element::adj_type temp(lc->size());
					Element::adj_type::iterator it = lc->begin(), jt = lc->begin()+start;
					Element::adj_type::iterator iend = jt, jend = lc->end();
					Element::adj_type::iterator t = temp.begin();
					while(it != iend && jt != jend)
					{
						if( *it == InvalidHandle() ) ++it;
						else if( *jt == InvalidHandle() ) ++jt;
						else if( *it < *jt ) *t++ = *it++;
						else *t++ = *jt++;
					}
					assert(it == iend || jt == jend);
					while(it != iend && *it != InvalidHandle() ) *t++ = *it++;
					while(jt != jend && *jt != InvalidHandle() ) *t++ = *jt++;
					//number of deleted items and invalid handles should match
					assert( (temp.size() - (t-temp.begin())) == (hc.size()-ElementSet::high_conn_reserved) );
					lc->swap(temp);
				}
			}
			break;
		case UNSORTED_COMPARATOR:
			break;
		}
		if( comp != UNSORTED_COMPARATOR )
		{
			///all invalid handles moved to the end, cut them
			lc->resize(lc->size() - (hc.size()-ElementSet::high_conn_reserved) );
			hc.resize(ElementSet::high_conn_reserved);
			hSorted(hc) = lc->size();
		}
		else hSorted(hc) = 0;
		comparator = comp;
	}

Kirill Terekhov's avatar
Kirill Terekhov committed
1446 1447 1448 1449 1450
  ElementSet::ComparatorType ElementSet::GetComparator() const
  {
    return BulkDF(GetMeshLink()->SetComparatorTag());
  }

Kirill Terekhov's avatar
Kirill Terekhov committed
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
	Element ElementSet::FindElementByGlobalID(integer global_id) const
	{
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		Element::adj_type & lc = m->LowConn(GetHandle());
		ComparatorType cmp = BulkDF(m->SetComparatorTag());
		if( cmp == GLOBALID_COMPARATOR )
		{
			Element::adj_type::size_type sorted = static_cast<Element::adj_type::size_type>(hSorted(m->HighConn(GetHandle())));
			Element::adj_type::iterator find = std::lower_bound(lc.begin(),lc.begin()+sorted,global_id,Mesh::GlobalIDComparator(m));
			if( find != lc.begin()+sorted ) 
			{
				if(*find != InvalidHandle() && m->GlobalID(*find) == global_id)
					return Element(m,*find);
			}
			else for(Element::adj_type::size_type k = sorted; k < lc.size(); k++)
				if( lc[k] != InvalidHandle() && m->GlobalID(lc[k]) == global_id )
					return Element(m,lc[k]);
		}
		else
		{
			for(Element::adj_type::size_type it = 0; it < lc.size(); ++it)
				if( lc[it] != InvalidHandle() && m->GlobalID(lc[it]) == global_id )
					return Element(m,lc[it]);
		}
		return Element(m,InvalidHandle());
Kirill Terekhov's avatar
Kirill Terekhov committed
1477
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
1478 1479

	Element ElementSet::FindElementByCentroid(real * centroid) const
Kirill Terekhov's avatar
Kirill Terekhov committed
1480
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		Element::adj_type & lc = m->LowConn(GetHandle());
		ComparatorType cmp = BulkDF(m->SetComparatorTag());
		if( cmp == CENTROID_COMPARATOR )
		{
			Element::adj_type::size_type sorted = static_cast<Element::adj_type::size_type>(hSorted(m->HighConn(GetHandle())));
			Element::adj_type::iterator find = std::lower_bound(lc.begin(),lc.begin()+sorted,centroid,Mesh::CentroidComparator(m));
			if( find != lc.begin()+sorted )
			{
				Storage::real cnt[3];
				m->GetGeometricData(*find,CENTROID,cnt);
				if( *find != InvalidHandle() &&  Mesh::CentroidComparator(m).Compare(cnt,centroid) == 0 )
					return Element(m,*find);
			}
			else for(Element::adj_type::size_type k = sorted; k < lc.size(); k++)
				if( lc[k] != InvalidHandle() )
				{
					Storage::real cnt[3];
					m->GetGeometricData(lc[k],CENTROID,cnt);
					if( Mesh::CentroidComparator(m).Compare(cnt,centroid) == 0 )
						return Element(m,lc[k]);
				}
		}
		else
		{
			for(Element::adj_type::size_type it = 0; it < lc.size(); ++it)
				if( lc[it] != InvalidHandle() )
				{
					Storage::real cnt[3];
					m->GetGeometricData(lc[it],CENTROID,cnt);
					if( Mesh::CentroidComparator(m).Compare(cnt,centroid) == 0 )
						return Element(m,lc[it]);
				}
		}
		return Element(m,InvalidHandle());
	}

	bool ElementSet::FindHandle(HandleType h, bool use_cmp) const
	{
		assert(GetElementType() == ESET);
		Mesh * m = GetMeshLink();
		Element::adj_type & lc = m->LowConn(GetHandle());
		ComparatorType cmp = BulkDF(m->SetComparatorTag());
		if( cmp == HANDLE_COMPARATOR  && use_cmp)
		{
			Element::adj_type::size_type sorted = static_cast<Element::adj_type::size_type>(hSorted(m->HighConn(GetHandle())));
			Element::adj_type::iterator find = std::lower_bound(lc.begin(),lc.begin()+sorted,h);
			if( find != lc.begin()+sorted )
			{
				if( *find == h )
					return true;
			}
			else for(Element::adj_type::size_type i = sorted; i < lc.size(); i++)
				if( lc[i] == h ) return true;
		}
		else if( cmp == GLOBALID_COMPARATOR && use_cmp)
		{
			integer gid = m->GlobalID(h);
			Element::adj_type::