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-2023 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  List<T*>& ptrs = *this;
103  const label len = ptrs.size();
104 
105  // Presume they were allocated from front to back...
106  for (label i = len - 1; i >= 0; --i)
107  {
108  delete ptrs[i];
109  ptrs[i] = nullptr;
110  }
111 }
112 
113 
114 template<class T>
115 template<class... Args>
118 {
119  const List<T*>& ptrs = *this;
120  const label len = ptrs.size();
121 
122  PtrListDetail<T> cloned(len);
123 
124  for (label i = 0; i < len; ++i)
125  {
126  const T* ptr = ptrs[i];
127 
128  if (ptr)
129  {
130  cloned[i] = ptr->clone(std::forward<Args>(args)...).ptr();
131  }
132  }
133 
134  return cloned;
135 }
136 
137 
138 // ************************************************************************* //
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
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)
const direction noexcept
Definition: Scalar.H:258
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 count() const noexcept
Return the number of non-null entries.
Definition: PtrListDetail.C:26
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:121
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