initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / containers / Lists / ListOps / ListOps.C
blob24e340d200b61374f84de87a3b75aab7c369ccde
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "ListOps.H"
29 // * * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * //
31 Foam::labelList Foam::invert
33     const label len,
34     const UList<label>& map
37     labelList inverse(len, -1);
39     forAll(map, i)
40     {
41         label newPos = map[i];
43         if (newPos >= 0)
44         {
45             if (inverse[newPos] >= 0)
46             {
47                 FatalErrorIn("invert(const label, const UList<label>&)")
48                     << "Map is not one-to-one. At index " << i
49                     << " element " << newPos << " has already occurred before"
50                     << nl << "Please use invertOneToMany instead"
51                     << abort(FatalError);
52             }
54             inverse[newPos] = i;
55         }
56     }
57     return inverse;
61 Foam::labelListList Foam::invertOneToMany
63     const label len,
64     const UList<label>& map
67     labelList nElems(len, 0);
69     forAll(map, i)
70     {
71         if (map[i] >= 0)
72         {
73             nElems[map[i]]++;
74         }
75     }
77     labelListList inverse(len);
79     forAll(nElems, i)
80     {
81         inverse[i].setSize(nElems[i]);
82         nElems[i] = 0;
83     }
85     forAll(map, i)
86     {
87         label newI = map[i];
89         if (newI >= 0)
90         {
91             inverse[newI][nElems[newI]++] = i;
92         }
93     }
95     return inverse;
99 Foam::labelList Foam::identity(const label len)
101     labelList map(len);
103     forAll(map, i)
104     {
105         map[i] = i;
106     }
107     return map;
111 // ************************************************************************* //