CircularBuffer.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) 2022 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
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// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
29
30template<class T>
32(
33 const bool nocopy,
34 const label len
35)
36{
37 if (storage_.size() < len)
38 {
39 // Increase capacity (doubling)
40 const label newCapacity =
41 max(min_size(), max(len+1, label(2*storage_.size())));
42
43 if (nocopy || empty())
44 {
45 // Simple - no content to preserve
46
47 clear(); // Reset begin/end
48 storage_.resize_nocopy(newCapacity);
49 }
50 else
51 {
52 // Preserve content
53 const labelRange range1 = range_one();
54 const labelRange range2 = range_two();
55
56 List<T> old(newCapacity);
57 storage_.swap(old);
58 begin_ = 0;
59 end_ = 0;
60
61 for (const label i : range1)
62 {
63 storage_[end_++] = std::move(old[i]);
64 }
65 for (const label i : range2)
66 {
67 storage_[end_++] = std::move(old[i]);
68 }
69 }
70 }
71}
72
73
74// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
75
76template<class T>
78{
79 const label len = size_one();
80 return (len ? storage_.slice(begin_, len) : SubList<T>());
81}
82
83
84template<class T>
86{
87 const label len = size_two();
88 return (len ? storage_.slice(0, len) : SubList<T>());
89}
90
91
92template<class T>
94{
95 const label len = size_one();
96 return (len ? storage_.slice(begin_, len) : SubList<T>());
97}
98
99
100template<class T>
102{
103 const label len = size_two();
104 return (len ? storage_.slice(0, len) : SubList<T>());
105}
106
107
108template<class T>
109Foam::label Foam::CircularBuffer<T>::find(const T& val, label pos) const
110{
111 label i = -1;
112
113 const auto list1 = this->array_one();
114
115 if (pos < list1.size())
116 {
117 i = list1.find(val, pos);
118 }
119
120 if (i < 0)
121 {
122 // Not found - search the second list
123 return this->array_two().find(val, 0);
124 }
125
126 return i;
127}
128
129
130template<class T>
132{
133 const label n = this->size();
134 const label nBy2 = n/2;
135
136 for (label i = 0; i < nBy2; ++i)
137 {
138 Foam::Swap(operator[](i), operator[](n-1-i));
139 }
140}
141
142
143// ************************************************************************* //
label n
Y[inertIndex] max(0.0)
A simple list of objects of type <T> that is intended to be used as a circular buffer (eg,...
void reverse()
Reverse the buffer order, swapping elements.
label find(const T &val, label pos=0) const
Find index of the first occurrence of the value.
SubList< T > array_two()
The contents of the first internal array.
SubList< T > array_one()
The contents of the first internal array.
A List obtained as a section of another List.
Definition: SubList.H:70
const volScalarField & T
patchWriters clear()
dimensionedScalar pos(const dimensionedScalar &ds)
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Definition: DynamicList.H:408