Attention une mise à jour du serveur va être effectuée le lundi 17 mai entre 13h et 13h30. Cette mise à jour va générer une interruption du service de quelques minutes.

Commit 7c2112b9 authored by BRAMAS Berenger's avatar BRAMAS Berenger

Add functeur to octree using lambda function to make it clean

parent d74460b9
......@@ -16,6 +16,7 @@
#ifndef FOCTREE_HPP
#define FOCTREE_HPP
#include <functional>
#include "FSubOctree.hpp"
#include "FTreeCoordinate.hpp"
......@@ -1082,6 +1083,43 @@ public:
return idxNeighbors;
}
/////////////////////////////////////////////////////////
// Lambda function to apply to all member
/////////////////////////////////////////////////////////
/**
* @brief forEachLeaf iterate on the leaf and apply the function
* @param function
*/
void forEachLeaf(std::function<void(LeafClass*)> function){
Iterator octreeIterator(this);
octreeIterator.gotoBottomLeft();
do{
function(octreeIterator.getCurrentLeaf());
} while(octreeIterator.moveRight());
}
/**
* @brief forEachLeaf iterate on the cell and apply the function
* @param function
*/
void forEachCell(std::function<void(CellClass*)> function){
Iterator octreeIterator(this);
octreeIterator.gotoBottomLeft();
Iterator avoidGoLeft(octreeIterator);
for(int idx = 0 ; idx < this->height - 1 ; ++idx ){
do{
function(octreeIterator.getCurrentCell());
} while(octreeIterator.moveRight());
avoidGoLeft.moveUp();
octreeIterator = avoidGoLeft;
}
}
};
#endif //FOCTREE_HPP
......
// ===================================================================================
// Copyright ScalFmm 2011 INRIA, Olivier Coulaud, Bérenger Bramas, Matthias Messner
// olivier.coulaud@inria.fr, berenger.bramas@inria.fr
// This software is a computer program whose purpose is to compute the FMM.
//
// This software is governed by the CeCILL-C and LGPL licenses and
// abiding by the rules of distribution of free software.
//
// 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 and CeCILL-C Licenses for more details.
// "http://www.cecill.info".
// "http://www.gnu.org/licenses".
// ===================================================================================
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include "../../Src/Utils/FTic.hpp"
#include "../../Src/Utils/FParameters.hpp"
#include "../../Src/Containers/FOctree.hpp"
#include "../../Src/Containers/FVector.hpp"
#include "../../Src/Utils/FAssertable.hpp"
#include "../../Src/Utils/FPoint.hpp"
#include "../../Src/Components/FBasicParticle.hpp"
#include "../../Src/Components/FBasicCell.hpp"
#include "../../Src/Components/FSimpleLeaf.hpp"
#include "../../Src/Files/FRandomLoader.hpp"
/**
* In this file we show how to use octree's functeur
*/
int main(int argc, char ** argv){
typedef FVector<FBasicParticle> ContainerClass;
typedef FSimpleLeaf<FBasicParticle, ContainerClass > LeafClass;
typedef FOctree<FBasicParticle, FBasicCell, ContainerClass , LeafClass > OctreeClass;
///////////////////////What we do/////////////////////////////
std::cout << ">> This executable is useless to execute.\n";
std::cout << ">> It is only interesting to wath the code to understand\n";
std::cout << ">> how to use the Octree\n";
//////////////////////////////////////////////////////////////
const int NbPart = FParameters::getValue(argc,argv,"-nb", 2000);
FTic counter;
FRandomLoader<FBasicParticle> loader(NbPart, 1, FPoint(0.5,0.5,0.5), 1);
OctreeClass tree(10, 3, loader.getBoxWidth(), loader.getCenterOfBox());
// -----------------------------------------------------
std::cout << "Creating and inserting " << NbPart << " particles ..." << std::endl;
counter.tic();
loader.fillTree(tree);
counter.tac();
std::cout << "Done " << "(" << counter.elapsed() << ")." << std::endl;
// -----------------------------------------------------
// Call a function on each leaf
long nbParticles = 0;
tree.forEachLeaf([&](LeafClass* leaf){
nbParticles += leaf->getSrc()->getSize();
});
std::cout << "There are " << nbParticles << " particles " << std::endl;
// Call a function on each cell
long nbCells = 0;
tree.forEachCell([&nbCells](FBasicCell* /*cell*/){
nbCells += 1;
});
std::cout << "There are " << nbCells << " cells " << std::endl;
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment