FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include <OpenFOAM/ListOps.H>
00027 
00028 // * * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * //
00029 
00030 Foam::labelList Foam::invert
00031 (
00032     const label len,
00033     const UList<label>& map
00034 )
00035 {
00036     labelList inverse(len, -1);
00037 
00038     forAll(map, i)
00039     {
00040         label newPos = map[i];
00041 
00042         if (newPos >= 0)
00043         {
00044             if (inverse[newPos] >= 0)
00045             {
00046                 FatalErrorIn("invert(const label, const UList<label>&)")
00047                     << "Map is not one-to-one. At index " << i
00048                     << " element " << newPos << " has already occurred before"
00049                     << nl << "Please use invertOneToMany instead"
00050                     << abort(FatalError);
00051             }
00052 
00053             inverse[newPos] = i;
00054         }
00055     }
00056     return inverse;
00057 }
00058 
00059 
00060 Foam::labelListList Foam::invertOneToMany
00061 (
00062     const label len,
00063     const UList<label>& map
00064 )
00065 {
00066     labelList nElems(len, 0);
00067 
00068     forAll(map, i)
00069     {
00070         if (map[i] >= 0)
00071         {
00072             nElems[map[i]]++;
00073         }
00074     }
00075 
00076     labelListList inverse(len);
00077 
00078     forAll(nElems, i)
00079     {
00080         inverse[i].setSize(nElems[i]);
00081         nElems[i] = 0;
00082     }
00083 
00084     forAll(map, i)
00085     {
00086         label newI = map[i];
00087 
00088         if (newI >= 0)
00089         {
00090             inverse[newI][nElems[newI]++] = i;
00091         }
00092     }
00093 
00094     return inverse;
00095 }
00096 
00097 
00098 Foam::labelList Foam::identity(const label len)
00099 {
00100     labelList map(len);
00101 
00102     forAll(map, i)
00103     {
00104         map[i] = i;
00105     }
00106     return map;
00107 }
00108 
00109 
00110 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines