PtrListDetail.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2018-2025 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "PtrListDetail.H"
29 
30 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
31 
32 template<class T>
34 {
35  label n = 0;
36 
37  for (const T* ptr : *this)
38  {
39  if (ptr)
40  {
41  ++n;
42  }
43  }
44 
45  return n;
46 }
47 
48 
49 template<class T>
51 {
52  return this->find_next(-1);
53 }
54 
55 
56 template<class T>
58 {
59  return this->find_next_not(-1);
60 }
61 
62 
63 template<class T>
64 Foam::label Foam::Detail::PtrListDetail<T>::find_next(label pos) const
65 {
66  const label len = this->size();
67 
68  // Start search after the given position (input of -1 is also valid)
69  for (++pos; pos < len; ++pos)
70  {
71  if ((*this)[pos])
72  {
73  return pos;
74  }
75  }
76 
77  return -1;
78 }
79 
80 
81 template<class T>
83 {
84  const label len = this->size();
85 
86  // Start search after the given position (input of -1 is also valid)
87  for (++pos; pos < len; ++pos)
88  {
89  if (!(*this)[pos])
90  {
91  return pos;
92  }
93  }
94 
95  return -1;
96 }
97 
98 
99 template<class T>
101 {
102  // Presume they were allocated from front to back...
103  List<T*>& ptrs = *this;
104 
105  for (auto i = this->size()-1; i >= 0; --i)
106  {
107  delete ptrs[i];
108  ptrs[i] = nullptr;
109  }
110 }
111 
112 
113 template<class T>
114 template<class... Args>
117 {
118  const List<T*>& ptrs = *this;
119  const label len = ptrs.size();
120 
121  PtrListDetail<T> cloned(len);
122 
123  for (label i = 0; i < len; ++i)
124  {
125  if (const T* ptr = ptrs[i]; ptr)
126  {
127  cloned[i] = ptr->clone(std::forward<Args>(args)...).ptr();
128  }
129  }
130 
131  return cloned;
132 }
133 
134 
135 // ************************************************************************* //
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:119
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
label find_first_not() const
Locate the first entry that is null, -1 if there are none (or empty list)
Definition: PtrListDetail.C:50
A rudimentary list of pointers used for PtrList, UPtrList, etc. This class is considered implementati...
Definition: PtrListDetail.H:57
dimensionedScalar pos(const dimensionedScalar &ds)
label count_nonnull() const noexcept
The number of non-nullptr entries in the list.
Definition: PtrListDetail.C:26
const direction noexcept
Definition: scalarImpl.H:265
const volScalarField & T
label find_next_not(label pos) const
Locate the next null entry, starting one beyond the specified position.
Definition: PtrListDetail.C:75
label find_first() const
Locate the first entry that is non-null.
Definition: PtrListDetail.C:43
autoPtr< List< T * > > clone() const
Clone.
Definition: ListI.H:124
label n
void free()
Delete allocated entries and reassign to nullptr. Does not affect the list size.
Definition: PtrListDetail.C:93
Foam::argList args(argc, argv)
label find_next(label pos) const
Locate the next non-null entry, starting one beyond the specified position.
Definition: PtrListDetail.C:57