00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "ListOps.H"
00028
00029
00030
00031 template<class ListType>
00032 ListType Foam::renumber
00033 (
00034 const UList<label>& oldToNew,
00035 const ListType& lst
00036 )
00037 {
00038
00039 ListType newLst(lst.size());
00040
00041 forAll(lst, elemI)
00042 {
00043 if (lst[elemI] >= 0)
00044 {
00045 newLst[elemI] = oldToNew[lst[elemI]];
00046 }
00047 }
00048
00049 return newLst;
00050 }
00051
00052
00053 template<class ListType>
00054 void Foam::inplaceRenumber
00055 (
00056 const UList<label>& oldToNew,
00057 ListType& lst
00058 )
00059 {
00060 forAll(lst, elemI)
00061 {
00062 if (lst[elemI] >= 0)
00063 {
00064 lst[elemI] = oldToNew[lst[elemI]];
00065 }
00066 }
00067 }
00068
00069
00070 template<class ListType>
00071 ListType Foam::reorder
00072 (
00073 const UList<label>& oldToNew,
00074 const ListType& lst
00075 )
00076 {
00077
00078 ListType newLst(lst.size());
00079
00080 forAll(lst, elemI)
00081 {
00082 if (oldToNew[elemI] >= 0)
00083 {
00084 newLst[oldToNew[elemI]] = lst[elemI];
00085 }
00086 else
00087 {
00088 newLst[elemI] = lst[elemI];
00089 }
00090 }
00091 return newLst;
00092 }
00093
00094
00095 template<class ListType>
00096 void Foam::inplaceReorder
00097 (
00098 const UList<label>& oldToNew,
00099 ListType& lst
00100 )
00101 {
00102
00103 ListType newLst(lst.size());
00104
00105 forAll(lst, elemI)
00106 {
00107 if (oldToNew[elemI] >= 0)
00108 {
00109 newLst[oldToNew[elemI]] = lst[elemI];
00110 }
00111 else
00112 {
00113 newLst[elemI] = lst[elemI];
00114 }
00115 }
00116
00117 lst.transfer(newLst);
00118 }
00119
00120
00121 template<class Container>
00122 void Foam::inplaceMapValue
00123 (
00124 const UList<label>& oldToNew,
00125 Container& lst
00126 )
00127 {
00128 for
00129 (
00130 typename Container::iterator iter = lst.begin();
00131 iter != lst.end();
00132 ++iter
00133 )
00134 {
00135 if (iter() >= 0)
00136 {
00137 iter() = oldToNew[iter()];
00138 }
00139 }
00140 }
00141
00142
00143 template<class Container>
00144 void Foam::inplaceMapKey
00145 (
00146 const UList<label>& oldToNew,
00147 Container& lst
00148 )
00149 {
00150 Container newLst(lst.size());
00151
00152 for
00153 (
00154 typename Container::iterator iter = lst.begin();
00155 iter != lst.end();
00156 ++iter
00157 )
00158 {
00159 if (iter.key() >= 0)
00160 {
00161 newLst.insert(oldToNew[iter.key()], iter());
00162 }
00163 }
00164
00165 lst.transfer(newLst);
00166 }
00167
00168
00169 template<class T>
00170 void Foam::sortedOrder
00171 (
00172 const UList<T>& lst,
00173 labelList& order
00174 )
00175 {
00176
00177 if (order.size() != lst.size())
00178 {
00179
00180 order.clear();
00181 order.setSize(lst.size());
00182 }
00183
00184 forAll(order, elemI)
00185 {
00186 order[elemI] = elemI;
00187 }
00188 Foam::stableSort(order, typename UList<T>::less(lst));
00189 }
00190
00191
00192 template<class T>
00193 void Foam::duplicateOrder
00194 (
00195 const UList<T>& lst,
00196 labelList& order
00197 )
00198 {
00199 if (lst.size() < 2)
00200 {
00201 order.clear();
00202 return;
00203 }
00204
00205 sortedOrder(lst, order);
00206
00207 label n = 0;
00208 for (label i = 0; i < order.size() - 1; ++i)
00209 {
00210 if (lst[order[i]] == lst[order[i+1]])
00211 {
00212 order[n++] = order[i];
00213 }
00214 }
00215 order.setSize(n);
00216 }
00217
00218
00219 template<class T>
00220 void Foam::uniqueOrder
00221 (
00222 const UList<T>& lst,
00223 labelList& order
00224 )
00225 {
00226 sortedOrder(lst, order);
00227
00228 if (order.size() > 1)
00229 {
00230 label n = 0;
00231 for (label i = 0; i < order.size() - 1; ++i)
00232 {
00233 if (lst[order[i]] != lst[order[i+1]])
00234 {
00235 order[n++] = order[i];
00236 }
00237 }
00238 order.setSize(n);
00239 }
00240 }
00241
00242
00243 template<class T, class ListType>
00244 ListType Foam::subset
00245 (
00246 const UList<T>& select,
00247 const T& value,
00248 const ListType& lst
00249 )
00250 {
00251
00252 if (select.size() < lst.size())
00253 {
00254 FatalErrorIn("subset(const UList<T>&, const T&, const ListType&)")
00255 << "select is of size " << select.size()
00256 << "; but it must index a list of size " << lst.size()
00257 << abort(FatalError);
00258 }
00259
00260 ListType newLst(lst.size());
00261
00262 label nElem = 0;
00263 forAll(lst, elemI)
00264 {
00265 if (select[elemI] == value)
00266 {
00267 newLst[nElem++] = lst[elemI];
00268 }
00269 }
00270 newLst.setSize(nElem);
00271
00272 return newLst;
00273 }
00274
00275
00276 template<class T, class ListType>
00277 void Foam::inplaceSubset
00278 (
00279 const UList<T>& select,
00280 const T& value,
00281 ListType& lst
00282 )
00283 {
00284
00285 if (select.size() < lst.size())
00286 {
00287 FatalErrorIn("inplaceSubset(const UList<T>&, const T&, ListType&)")
00288 << "select is of size " << select.size()
00289 << "; but it must index a list of size " << lst.size()
00290 << abort(FatalError);
00291 }
00292
00293 label nElem = 0;
00294 forAll(lst, elemI)
00295 {
00296 if (select[elemI] == value)
00297 {
00298 if (nElem != elemI)
00299 {
00300 lst[nElem] = lst[elemI];
00301 }
00302 ++nElem;
00303 }
00304 }
00305
00306 lst.setSize(nElem);
00307 }
00308
00309
00310 template<class BoolListType, class ListType>
00311 ListType Foam::subset
00312 (
00313 const BoolListType& select,
00314 const ListType& lst
00315 )
00316 {
00317
00318
00319
00320 ListType newLst(lst.size());
00321
00322 label nElem = 0;
00323 forAll(lst, elemI)
00324 {
00325 if (select[elemI])
00326 {
00327 newLst[nElem++] = lst[elemI];
00328 }
00329 }
00330 newLst.setSize(nElem);
00331
00332 return newLst;
00333 }
00334
00335
00336 template<class BoolListType, class ListType>
00337 void Foam::inplaceSubset
00338 (
00339 const BoolListType& select,
00340 ListType& lst
00341 )
00342 {
00343
00344
00345
00346 label nElem = 0;
00347 forAll(lst, elemI)
00348 {
00349 if (select[elemI])
00350 {
00351 if (nElem != elemI)
00352 {
00353 lst[nElem] = lst[elemI];
00354 }
00355 ++nElem;
00356 }
00357 }
00358
00359 lst.setSize(nElem);
00360 }
00361
00362
00363
00364
00365 template<class InList, class OutList>
00366 void Foam::invertManyToMany
00367 (
00368 const label nEdges,
00369 const UList<InList>& pointEdges,
00370 List<OutList>& edges
00371 )
00372 {
00373
00374 labelList nPointsPerEdge(nEdges, 0);
00375
00376 forAll(pointEdges, pointI)
00377 {
00378 const InList& pEdges = pointEdges[pointI];
00379
00380 forAll(pEdges, j)
00381 {
00382 nPointsPerEdge[pEdges[j]]++;
00383 }
00384 }
00385
00386
00387 edges.setSize(nEdges);
00388
00389 forAll(nPointsPerEdge, edgeI)
00390 {
00391 edges[edgeI].setSize(nPointsPerEdge[edgeI]);
00392 }
00393 nPointsPerEdge = 0;
00394
00395
00396 forAll(pointEdges, pointI)
00397 {
00398 const InList& pEdges = pointEdges[pointI];
00399
00400 forAll(pEdges, j)
00401 {
00402 label edgeI = pEdges[j];
00403
00404 edges[edgeI][nPointsPerEdge[edgeI]++] = pointI;
00405 }
00406 }
00407 }
00408
00409
00410 template<class ListType>
00411 Foam::label Foam::findIndex
00412 (
00413 const ListType& l,
00414 typename ListType::const_reference t,
00415 const label start
00416 )
00417 {
00418 label index = -1;
00419
00420 for (label i = start; i < l.size(); i++)
00421 {
00422 if (l[i] == t)
00423 {
00424 index = i;
00425 break;
00426 }
00427 }
00428
00429 return index;
00430 }
00431
00432
00433 template<class ListType>
00434 Foam::labelList Foam::findIndices
00435 (
00436 const ListType& l,
00437 typename ListType::const_reference t,
00438 const label start
00439 )
00440 {
00441
00442 label n = 0;
00443
00444 for (label i = start; i < l.size(); i++)
00445 {
00446 if (l[i] == t)
00447 {
00448 n++;
00449 }
00450 }
00451
00452
00453 labelList indices(n);
00454 n = 0;
00455
00456 for (label i = start; i < l.size(); i++)
00457 {
00458 if (l[i] == t)
00459 {
00460 indices[n++] = i;
00461 }
00462 }
00463
00464 return indices;
00465 }
00466
00467
00468 template<class ListType>
00469 void Foam::setValues
00470 (
00471 ListType& l,
00472 const UList<label>& indices,
00473 typename ListType::const_reference t
00474 )
00475 {
00476 forAll(indices, i)
00477 {
00478 l[indices[i]] = t;
00479 }
00480 }
00481
00482
00483 template<class ListType>
00484 ListType Foam::createWithValues
00485 (
00486 const label sz,
00487 const typename ListType::const_reference initValue,
00488 const UList<label>& indices,
00489 typename ListType::const_reference setValue
00490 )
00491 {
00492 ListType l(sz, initValue);
00493 setValues(l, indices, setValue);
00494 return l;
00495 }
00496
00497
00498 template<class ListType>
00499 Foam::label Foam::findMax(const ListType& l, const label start)
00500 {
00501 if (start >= l.size())
00502 {
00503 return -1;
00504 }
00505
00506 label index = start;
00507
00508 for (label i = start+1; i < l.size(); i++)
00509 {
00510 if (l[i] > l[index])
00511 {
00512 index = i;
00513 }
00514 }
00515
00516 return index;
00517 }
00518
00519
00520 template<class ListType>
00521 Foam::label Foam::findMin(const ListType& l, const label start)
00522 {
00523 if (start >= l.size())
00524 {
00525 return -1;
00526 }
00527
00528 label index = start;
00529
00530 for (label i = start+1; i < l.size(); i++)
00531 {
00532 if (l[i] < l[index])
00533 {
00534 index = i;
00535 }
00536 }
00537
00538 return index;
00539 }
00540
00541
00542 template<class ListType>
00543 Foam::label Foam::findSortedIndex
00544 (
00545 const ListType& l,
00546 typename ListType::const_reference t,
00547 const label start
00548 )
00549 {
00550 if (start >= l.size())
00551 {
00552 return -1;
00553 }
00554
00555 label low = start;
00556 label high = l.size() - 1;
00557
00558 while (low <= high)
00559 {
00560 label mid = (low + high)/2;
00561
00562 if (t < l[mid])
00563 {
00564 high = mid - 1;
00565 }
00566 else if (t > l[mid])
00567 {
00568 low = mid + 1;
00569 }
00570 else
00571 {
00572 return mid;
00573 }
00574 }
00575
00576 return -1;
00577 }
00578
00579
00580 template<class ListType>
00581 Foam::label Foam::findLower
00582 (
00583 const ListType& l,
00584 typename ListType::const_reference t,
00585 const label start
00586 )
00587 {
00588 if (start >= l.size())
00589 {
00590 return -1;
00591 }
00592
00593 label low = start;
00594 label high = l.size() - 1;
00595
00596 while ((high - low) > 1)
00597 {
00598 label mid = (low + high)/2;
00599
00600 if (l[mid] < t)
00601 {
00602 low = mid;
00603 }
00604 else
00605 {
00606 high = mid;
00607 }
00608 }
00609
00610 if (l[high] < t)
00611 {
00612 return high;
00613 }
00614 else
00615 {
00616 if (l[low] < t)
00617 {
00618 return low;
00619 }
00620 else
00621 {
00622 return -1;
00623 }
00624 }
00625 }
00626
00627
00628 template<class Container, class T, int nRows>
00629 Foam::List<Container> Foam::initList(const T elems[nRows])
00630 {
00631 List<Container> lst(nRows);
00632
00633 forAll(lst, rowI)
00634 {
00635 lst[rowI] = Container(elems[rowI]);
00636 }
00637 return lst;
00638 }
00639
00640
00641 template<class Container, class T, int nRows, int nColumns>
00642 Foam::List<Container> Foam::initListList(const T elems[nRows][nColumns])
00643 {
00644 List<Container> lst(nRows);
00645
00646 Container cols(nColumns);
00647 forAll(lst, rowI)
00648 {
00649 forAll(cols, colI)
00650 {
00651 cols[colI] = elems[rowI][colI];
00652 }
00653 lst[rowI] = cols;
00654 }
00655 return lst;
00656 }
00657
00658
00659