From d63a82e609d0babbaa43c22ff8a7653e8640e9b8 Mon Sep 17 00:00:00 2001
From: Thomas Fischer <thomas.fischer@ufz.de>
Date: Fri, 10 Aug 2012 12:33:19 +0200
Subject: [PATCH] [class Element] - optimized addNeighbor()

[Mesh]
- optimized setNeighborInformationForElements()
---
 MeshLib/Elements/Element.cpp                |  3 ++-
 MeshLib/Mesh.cpp                            | 28 +++++++--------------
 MeshLib/Node.h                              |  2 +-
 SimpleTests/MeshTests/CollapseMeshNodes.cpp |  1 -
 4 files changed, 12 insertions(+), 22 deletions(-)

diff --git a/MeshLib/Elements/Element.cpp b/MeshLib/Elements/Element.cpp
index 87165e7a6c7..a8572c56414 100644
--- a/MeshLib/Elements/Element.cpp
+++ b/MeshLib/Elements/Element.cpp
@@ -52,12 +52,13 @@ bool Element::addNeighbor(Element* e)
 		const unsigned eNodes (e->getNNodes());
 		const Node* const* e_nodes = e->getNodes();
 		unsigned count(0);
+		const unsigned dim (this->getDimension());
 		for (unsigned i(0); i<nNodes; i++)
 			for (unsigned j(0); j<eNodes; j++)
 				if (_nodes[i] == e_nodes[j])
 					//std::cout << _nodes[i]->getID() << " == " << e_nodes[j]->getID() << std::endl;
 					// increment shared nodes counter and check if enough nodes are similar to be sure e is a neighbour of this
-					if ((++count)>=this->getDimension())
+					if ((++count)>=dim)
 					{
 						_neighbors[n]=e;
 						return true;
diff --git a/MeshLib/Mesh.cpp b/MeshLib/Mesh.cpp
index 03701131dd2..a8bdfd13f28 100644
--- a/MeshLib/Mesh.cpp
+++ b/MeshLib/Mesh.cpp
@@ -112,34 +112,24 @@ void Mesh::setNeighborInformationForElements()
 	{
 		// create vector with all elements connected to current element (includes lots of doubles!)
 		std::vector<Element*> neighbors;
-		const size_t nNodes (_elements[m]->getNNodes());
+		Element *const element (_elements[m]);
+		const size_t nNodes (element->getNNodes());
 		for (unsigned n(0); n<nNodes; n++)
 		{
-			const std::vector<Element*> conn_elems (_elements[m]->getNode(n)->getElements());
+			std::vector<Element*> const& conn_elems ((element->getNode(n)->getElements()));
 			neighbors.insert(neighbors.end(), conn_elems.begin(), conn_elems.end());
 		}
-		
+
 		const unsigned nNeighbors ( neighbors.size() );
-		/*std::vector<bool> done (nNeighbors, false);
-		// mark off the element itself
-		for (unsigned j(0); j<nNeighbors; j++)
-			if (neighbors[j] == _elements[m])
-				done[j] = true;
-				*/
+
 		// check if connected element is indeed a neighbour and mark all doubles of that element as 'done'
 		for (unsigned i(0); i<nNeighbors; i++)
-			//if (!done[i])
+		{
+			if (element->addNeighbor(neighbors[i]))
 			{
-				if (_elements[m]->addNeighbor(neighbors[i]))
-				{
-					neighbors[i]->addNeighbor(_elements[m]);
-				}/*
-					for (unsigned j(0); j<nNeighbors; j++)
-						if (!done[j] && (neighbors[j] == neighbors[i]))
-							done[j] = true;
-							*/
-					
+				neighbors[i]->addNeighbor(element);
 			}
+		}
 	}
 }
 
diff --git a/MeshLib/Node.h b/MeshLib/Node.h
index 0514b963d87..07362e911ba 100644
--- a/MeshLib/Node.h
+++ b/MeshLib/Node.h
@@ -48,7 +48,7 @@ public:
 	const Element* getElement(unsigned idx) const { return _elements[idx]; };
 
 	/// Get all elements the node is part of.
-	const std::vector<Element*> getElements() const { return _elements; };
+	const std::vector<Element*>& getElements() const { return _elements; };
 
 	/// Get number of elements the node is part of.
 	size_t getNElements() const { return _elements.size(); };
diff --git a/SimpleTests/MeshTests/CollapseMeshNodes.cpp b/SimpleTests/MeshTests/CollapseMeshNodes.cpp
index add35350f61..5fcbf1f9d9b 100644
--- a/SimpleTests/MeshTests/CollapseMeshNodes.cpp
+++ b/SimpleTests/MeshTests/CollapseMeshNodes.cpp
@@ -17,7 +17,6 @@
 // BaseLib/logog
 #include "logog.hpp"
 
-
 // MeshLib
 #include "Node.h"
 #include "Elements/Element.h"
-- 
GitLab