@@ -119,15 +119,29 @@ class SimpleArray
119119 }
120120 }
121121
122+ // NOLINTNEXTLINE(modernize-pass-by-value)
123+ explicit SimpleArray (small_vector<size_t > const & shape, value_type const & value)
124+ : SimpleArray(shape)
125+ {
126+ std::fill (begin (), end (), value);
127+ }
128+
122129 explicit SimpleArray (std::vector<size_t > const & shape)
123130 : m_shape(shape), m_stride(calc_stride(m_shape))
124131 {
125- if (!m_shape.empty ()) {
132+ if (!m_shape.empty ())
133+ {
126134 m_buffer = buffer_type::construct (m_shape[0 ] * m_stride[0 ] * ITEMSIZE);
127135 m_body = m_buffer->data <T>();
128136 }
129137 }
130138
139+ explicit SimpleArray (std::vector<size_t > const & shape, value_type const & value)
140+ : SimpleArray(shape)
141+ {
142+ std::fill (begin (), end (), value);
143+ }
144+
131145 explicit SimpleArray (std::shared_ptr<buffer_type> const & buffer)
132146 {
133147 if (buffer)
@@ -169,20 +183,6 @@ class SimpleArray
169183 }
170184 }
171185
172- static shape_type calc_stride (shape_type const & shape)
173- {
174- shape_type stride (shape.size ());
175- if (!shape.empty ())
176- {
177- stride[shape.size ()-1 ] = 1 ;
178- for (size_t it=shape.size ()-1 ; it>0 ; --it)
179- {
180- stride[it-1 ] = stride[it] * shape[it];
181- }
182- }
183- return stride;
184- }
185-
186186 SimpleArray (std::initializer_list<T> init)
187187 : SimpleArray(init.size())
188188 {
@@ -199,21 +199,6 @@ class SimpleArray
199199 , m_body(calc_body(m_buffer->data<T>(), m_stride, other.m_nghost))
200200 {}
201201
202- static T * calc_body (T * data, shape_type const & stride, size_t nghost)
203- {
204- if (nullptr == data || stride.empty () || 0 == nghost)
205- {
206- // Do nothing.
207- }
208- else
209- {
210- shape_type shape (stride.size (), 0 );
211- shape[0 ] = nghost;
212- data += buffer_offset (stride, shape);
213- }
214- return data;
215- }
216-
217202 SimpleArray (SimpleArray && other) noexcept
218203 : m_buffer(std::move(other.m_buffer))
219204 , m_shape(std::move(other.m_shape))
@@ -248,6 +233,42 @@ class SimpleArray
248233
249234 ~SimpleArray () = default ;
250235
236+ template < typename ... Args >
237+ SimpleArray & remake (Args && ... args)
238+ {
239+ SimpleArray (args ...).swap (*this );
240+ return *this ;
241+ }
242+
243+ static shape_type calc_stride (shape_type const & shape)
244+ {
245+ shape_type stride (shape.size ());
246+ if (!shape.empty ())
247+ {
248+ stride[shape.size ()-1 ] = 1 ;
249+ for (size_t it=shape.size ()-1 ; it>0 ; --it)
250+ {
251+ stride[it-1 ] = stride[it] * shape[it];
252+ }
253+ }
254+ return stride;
255+ }
256+
257+ static T * calc_body (T * data, shape_type const & stride, size_t nghost)
258+ {
259+ if (nullptr == data || stride.empty () || 0 == nghost)
260+ {
261+ // Do nothing.
262+ }
263+ else
264+ {
265+ shape_type shape (stride.size (), 0 );
266+ shape[0 ] = nghost;
267+ data += buffer_offset (stride, shape);
268+ }
269+ return data;
270+ }
271+
251272 explicit operator bool () const noexcept { return bool (m_buffer) && bool (*m_buffer); }
252273
253274 size_t nbytes () const noexcept { return m_buffer ? m_buffer->nbytes () : 0 ; }
@@ -359,7 +380,7 @@ class SimpleArray
359380 return SimpleArray (m_shape, m_buffer);
360381 }
361382
362- void swap (SimpleArray<T> && other)
383+ void swap (SimpleArray & other) noexcept
363384 {
364385 if (this != &other)
365386 {
@@ -372,10 +393,14 @@ class SimpleArray
372393 }
373394
374395 template < typename ... Args >
375- value_type const & operator ()(Args ... args) const { return m_body[buffer_offset (m_stride, args...)]; }
396+ value_type const & operator ()(Args ... args) const { return *vptr (args...); }
397+ template < typename ... Args >
398+ value_type & operator ()(Args ... args) { return *vptr (args...); }
376399
377400 template < typename ... Args >
378- value_type & operator ()(Args ... args) { return m_body[buffer_offset (m_stride, args...)]; }
401+ value_type const * vptr (Args ... args) const { return m_body + buffer_offset (m_stride, args...); }
402+ template < typename ... Args >
403+ value_type * vptr (Args ... args) { return m_body + buffer_offset (m_stride, args...); }
379404
380405 /* Backdoor */
381406 value_type const & data (size_t it) const { return data ()[it]; }
0 commit comments