Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
MultiBuffer.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* MultiBuffer.h (C) 2000-2011 */
9/* */
10/* Template class of an array with a buffer. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_UTILS_MULTIBUFFER_H
13#define ARCANE_UTILS_MULTIBUFFER_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/utils/ArrayView.h"
18
19/*---------------------------------------------------------------------------*/
20/*---------------------------------------------------------------------------*/
21
22namespace Arcane
23{
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
43template <class T>
44class MultiBufferT
45{
46 public:
47
48 typedef UniqueArray<T> BufferType;
49
50 public:
51
52 MultiBufferT()
53 : m_buffer_size(1000)
57 {
58 }
59 MultiBufferT(Integer buf_size)
60 : m_buffer_size(buf_size)
64 {
65 }
66
68 MultiBufferT(const MultiBufferT<T>& ref)
73 {
74 }
76 {
77 _freeAllocatedBuffers();
78 }
79
80 public:
81
83 void operator=(const MultiBufferT<T>& ref)
84 {
85 if (&ref == this)
86 return;
87 clear();
89 }
90
91 public:
92
95 {
97 _allocateCurrentBuffer();
98 T* v = &(*m_current_buffer)[m_nb_in_buffer];
101 m_current_buffer = 0; // Indicates that the current buffer is full
102 return v;
103 }
104
107 {
108 // If the desired number of elements is greater than the size
109 // of the buffer, specifically allocate a buffer of the correct size
110 if (n > m_current_buffer_size) {
111 BufferType* bt = new BufferType(n);
112 m_allocated_buffers.add(bt);
113 return *bt;
114 }
115 if (!m_current_buffer)
116 _allocateCurrentBuffer();
117 // If the current buffer is not large enough to contain the
118 // \a n desired elements, allocate another one
120 _allocateCurrentBuffer();
121 T* v = &(*m_current_buffer)[m_nb_in_buffer];
122 m_nb_in_buffer += n;
124 m_current_buffer = 0; // Indicates that the current buffer is full
125 return ArrayView<T>(n, v);
126 }
127 void clear()
128 {
129 m_nb_in_buffer = 0;
132 _freeAllocatedBuffers();
133 }
134 Integer nbAllocatedBuffer() { return m_allocated_buffers.size(); }
135 Integer bufferSize() const { return m_current_buffer_size; }
136
137 protected:
138
139 void _freeAllocatedBuffers()
140 {
141 for (Integer i = 0, s = m_allocated_buffers.size(); i < s; ++i)
142 delete m_allocated_buffers[i];
143 m_allocated_buffers.clear();
144 }
145
146 private:
147
151 BufferType* m_current_buffer;
153 private:
154
155 void _allocateCurrentBuffer()
156 {
157 m_current_buffer = new BufferType(m_buffer_size);
160 m_nb_in_buffer = 0;
161 }
162};
163
164/*---------------------------------------------------------------------------*/
165/*---------------------------------------------------------------------------*/
166
167} // namespace Arcane
168
169/*---------------------------------------------------------------------------*/
170/*---------------------------------------------------------------------------*/
171
172#endif
Modifiable view of an array of type T.
Buffer for multiple allocation.
Definition MultiBuffer.h:45
MultiBufferT(const MultiBufferT< T > &ref)
Copy constructor.
Definition MultiBuffer.h:68
Integer m_buffer_size
Number of elements in a buffer.
Integer m_nb_in_buffer
Number of elements in the current buffer.
BufferType * m_current_buffer
Current buffer.
UniqueArray< BufferType * > m_allocated_buffers
List of all buffers.
T * allocOne()
Allocates a new element.
Definition MultiBuffer.h:94
Integer m_current_buffer_size
Maximum number of elements in the current buffer.
ArrayView< T > allocMany(Integer n)
Allocates n elements.
void operator=(const MultiBufferT< T > &ref)
Copy assignment operator (forbidden).
Definition MultiBuffer.h:83
1D data vector with value semantics (STL style).
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.