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

ListOps.H

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 InNamspace
00026     Foam
00027 
00028 Description
00029     Various functions to operate on Lists.
00030 
00031 SourceFiles
00032     ListOps.C
00033     ListOpsTemplates.C
00034 
00035 \*---------------------------------------------------------------------------*/
00036 
00037 #ifndef ListOps_H
00038 #define ListOps_H
00039 
00040 #include "labelList.H"
00041 
00042 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00043 
00044 namespace Foam
00045 {
00046 
00047 //- Renumber the values (not the indices) of a list.
00048 //  Negative ListType elements are left as is.
00049 template<class ListType>
00050 ListType renumber(const UList<label>& oldToNew, const ListType&);
00051 
00052 //- Inplace renumber the values of a list.
00053 //  Negative ListType elements are left as is.
00054 template<class ListType>
00055 void inplaceRenumber(const UList<label>& oldToNew, ListType&);
00056 
00057 
00058 //- Reorder the elements (indices, not values) of a list.
00059 //  Negative ListType elements are left as is.
00060 template<class ListType>
00061 ListType reorder(const UList<label>& oldToNew, const ListType&);
00062 
00063 //- Inplace reorder the elements of a list.
00064 //  Negative ListType elements are left as is.
00065 template<class ListType>
00066 void inplaceReorder(const UList<label>& oldToNew, ListType&);
00067 
00068 
00069 // Variants to work with iterators and sparse tables.
00070 // Need to have iterators and insert()
00071 
00072 //- Map values. Do not map negative values.
00073 template<class Container>
00074 void inplaceMapValue(const UList<label>& oldToNew, Container&);
00075 
00076 //- Recreate with mapped keys. Do not map elements with negative key.
00077 template<class Container>
00078 void inplaceMapKey(const UList<label>& oldToNew, Container&);
00079 
00080 
00081 //- Generate the (stable) sort order for the list
00082 template<class T>
00083 void sortedOrder(const UList<T>&, labelList& order);
00084 
00085 //- Generate (sorted) indices corresponding to duplicate list values
00086 template<class T>
00087 void duplicateOrder(const UList<T>&, labelList& order);
00088 
00089 //- Generate (sorted) indices corresponding to unique list values
00090 template<class T>
00091 void uniqueOrder(const UList<T>&, labelList& order);
00092 
00093 //- Extract elements of List when select is a certain value.
00094 //  eg, to extract all selected elements:
00095 //    subset<bool, labelList>(selectedElems, true, lst);
00096 template<class T, class ListType>
00097 ListType subset(const UList<T>& select, const T& value, const ListType&);
00098 
00099 //- Inplace extract elements of List when select is a certain value.
00100 //  eg, to extract all selected elements:
00101 //    inplaceSubset<bool, labelList>(selectedElems, true, lst);
00102 template<class T, class ListType>
00103 void inplaceSubset(const UList<T>& select, const T& value, ListType&);
00104 
00105 //- Extract elements of List when select is true
00106 //  eg, to extract all selected elements:
00107 //    subset<boolList, labelList>(selectedElems, lst);
00108 //  Note a labelHashSet could also be used for the bool-list
00109 template<class BoolListType, class ListType>
00110 ListType subset(const BoolListType& select, const ListType&);
00111 
00112 //- Inplace extract elements of List when select is true
00113 //  eg, to extract all selected elements:
00114 //    inplaceSubset<boolList, labelList>(selectedElems, lst);
00115 //  Note a labelHashSet could also be used for the bool-list
00116 template<class BoolListType, class ListType>
00117 void inplaceSubset(const BoolListType& select, ListType&);
00118 
00119 //- Invert one-to-one map. Unmapped elements will be -1.
00120 labelList invert(const label len, const UList<label>&);
00121 
00122 //- Invert one-to-many map. Unmapped elements will be size 0.
00123 labelListList invertOneToMany(const label len, const UList<label>&);
00124 
00125 //- Invert many-to-many.
00126 //  Input and output types need to be inherited from List.
00127 //  eg, faces to pointFaces.
00128 template<class InList, class OutList>
00129 void invertManyToMany(const label len, const UList<InList>&, List<OutList>&);
00130 
00131 template<class InList, class OutList>
00132 List<OutList> invertManyToMany(const label len, const UList<InList>& in)
00133 {
00134     List<OutList> out;
00135     invertManyToMany<InList,OutList>(len, in, out);
00136     return out;
00137 }
00138 
00139 //- Create identity map (map[i] == i) of given length
00140 labelList identity(const label len);
00141 
00142 //- Find first occurence of given element and return index,
00143 //  return -1 if not found. Linear search.
00144 template<class ListType>
00145 label findIndex
00146 (
00147     const ListType&,
00148     typename ListType::const_reference,
00149     const label start=0
00150 );
00151 
00152 //- Find all occurences of given element. Linear search.
00153 template<class ListType>
00154 labelList findIndices
00155 (
00156     const ListType&,
00157     typename ListType::const_reference,
00158     const label start=0
00159 );
00160 
00161 //- Opposite of findIndices: set values at indices to given value
00162 template<class ListType>
00163 void setValues
00164 (
00165     ListType&,
00166     const UList<label>& indices,
00167     typename ListType::const_reference
00168 );
00169 
00170 //- Opposite of findIndices: set values at indices to given value
00171 template<class ListType>
00172 ListType createWithValues
00173 (
00174     const label sz,
00175     const typename ListType::const_reference initValue,
00176     const UList<label>& indices,
00177     typename ListType::const_reference setValue
00178 );
00179 
00180 //- Find index of max element (and larger than given element).
00181 //  return -1 if not found. Linear search.
00182 template<class ListType>
00183 label findMax(const ListType&, const label start=0);
00184 
00185 
00186 //- Find index of min element (and less than given element).
00187 //  return -1 if not found. Linear search.
00188 template<class ListType>
00189 label findMin(const ListType&, const label start=0);
00190 
00191 
00192 //- Find first occurence of given element in sorted list and return index,
00193 //  return -1 if not found. Binary search.
00194 template<class ListType>
00195 label findSortedIndex
00196 (
00197     const ListType&,
00198     typename ListType::const_reference,
00199     const label start=0
00200 );
00201 
00202 
00203 //- Find last element < given value in sorted list and return index,
00204 //  return -1 if not found. Binary search.
00205 template<class ListType>
00206 label findLower
00207 (
00208     const ListType&,
00209     typename ListType::const_reference,
00210     const label start=0
00211 );
00212 
00213 
00214 //- To construct a List from a C array. Has extra Container type
00215 //  to initialise e.g. wordList from arrays of char*.
00216 template<class Container, class T, int nRows>
00217 List<Container> initList(const T[nRows]);
00218 
00219 
00220 //- To construct a (square) ListList from a C array. Has extra Container type
00221 //  to initialise e.g. faceList from arrays of labels.
00222 template<class Container, class T, int nRows, int nColumns>
00223 List<Container> initListList(const T[nRows][nColumns]);
00224 
00225 
00226 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00227 
00228 } // End namespace Foam
00229 
00230 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00231 
00232 #ifdef NoRepository
00233 #   include "ListOpsTemplates.C"
00234 #endif
00235 
00236 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00237 
00238 #endif
00239 
00240 // ************************************************************************* //
00241 
Copyright © 2000-2009 OpenCFD Ltd