OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

ListOps.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "ListOps.H"
00028 
00029 // * * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * //
00030 
00031 Foam::labelList Foam::invert
00032 (
00033     const label len,
00034     const UList<label>& map
00035 )
00036 {
00037     labelList inverse(len, -1);
00038 
00039     forAll(map, i)
00040     {
00041         label newPos = map[i];
00042 
00043         if (newPos >= 0)
00044         {
00045             if (inverse[newPos] >= 0)
00046             {
00047                 FatalErrorIn("invert(const label, const UList<label>&)")
00048                     << "Map is not one-to-one. At index " << i
00049                     << " element " << newPos << " has already occurred before"
00050                     << nl << "Please use invertOneToMany instead"
00051                     << abort(FatalError);
00052             }
00053 
00054             inverse[newPos] = i;
00055         }
00056     }
00057     return inverse;
00058 }
00059 
00060 
00061 Foam::labelListList Foam::invertOneToMany
00062 (
00063     const label len,
00064     const UList<label>& map
00065 )
00066 {
00067     labelList nElems(len, 0);
00068 
00069     forAll(map, i)
00070     {
00071         if (map[i] >= 0)
00072         {
00073             nElems[map[i]]++;
00074         }
00075     }
00076 
00077     labelListList inverse(len);
00078 
00079     forAll(nElems, i)
00080     {
00081         inverse[i].setSize(nElems[i]);
00082         nElems[i] = 0;
00083     }
00084 
00085     forAll(map, i)
00086     {
00087         label newI = map[i];
00088 
00089         if (newI >= 0)
00090         {
00091             inverse[newI][nElems[newI]++] = i;
00092         }
00093     }
00094 
00095     return inverse;
00096 }
00097 
00098 
00099 Foam::labelList Foam::identity(const label len)
00100 {
00101     labelList map(len);
00102 
00103     forAll(map, i)
00104     {
00105         map[i] = i;
00106     }
00107     return map;
00108 }
00109 
00110 
00111 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd