17 #ifndef dealii__aligned_vector_h 18 #define dealii__aligned_vector_h 20 #include <deal.II/base/config.h> 21 #include <deal.II/base/std_cxx11/type_traits.h> 22 #include <deal.II/base/exceptions.h> 23 #include <deal.II/base/memory_consumption.h> 24 #include <deal.II/base/utilities.h> 25 #include <deal.II/base/parallel.h> 26 #include <boost/serialization/array.hpp> 27 #include <boost/serialization/split_member.hpp> 33 DEAL_II_NAMESPACE_OPEN
60 typedef value_type *pointer;
61 typedef const value_type *const_pointer;
62 typedef value_type *iterator;
63 typedef const value_type *const_iterator;
64 typedef value_type &reference;
65 typedef const value_type &const_reference;
66 typedef std::size_t size_type;
120 void resize (
const size_type size_in,
121 const T &init = T());
131 void reserve (
const size_type size_alloc);
154 const_reference
back ()
const;
160 template <
typename ForwardIterator>
162 ForwardIterator
end);
172 void fill (
const T &element);
187 size_type
size ()
const;
204 const_reference
operator [] (
const size_type index)
const;
219 const_iterator
begin ()
const;
224 const_iterator
end ()
const;
237 template <
class Archive>
238 void save (Archive &ar,
const unsigned int version)
const;
244 template <
class Archive>
245 void load (Archive &ar,
const unsigned int version);
247 BOOST_SERIALIZATION_SPLIT_MEMBER()
292 template <
typename T>
295 static const std::size_t minimum_parallel_grain_size = 160000/
sizeof(T)+1;
310 const bool copy_source)
312 source_ (source_begin),
313 destination_ (destination),
314 copy_source_ (copy_source)
316 Assert (source_end >= source_begin, ExcInternalError());
317 const std::size_t size = source_end - source_begin;
318 if (size < minimum_parallel_grain_size)
319 apply_to_subrange (0, size);
321 apply_parallel (0, size, minimum_parallel_grain_size);
329 const std::size_t
end)
const 335 if (std_cxx11::is_trivial<T>::value ==
true)
336 std::memcpy ((
void *)(destination_+begin), source_+begin,
337 (end-begin)*
sizeof(T));
338 else if (copy_source_ ==
false)
339 for (std::size_t i=begin; i<
end; ++i)
343 new (&destination_[i]) T(source_[i]);
347 for (std::size_t i=begin; i<
end; ++i)
348 new (&destination_[i]) T(source_[i]);
354 const bool copy_source_;
368 template <
typename T,
bool initialize_memory>
371 static const std::size_t minimum_parallel_grain_size = 160000/
sizeof(T)+1;
382 destination_ (destination),
383 trivial_element (false)
391 if (std_cxx11::is_trivial<T>::value ==
true &&
394 const unsigned char zero [
sizeof(T)] = {};
398 if (std::memcmp(zero, (
void *)&element,
sizeof(T)) == 0)
399 trivial_element =
true;
401 if (size < minimum_parallel_grain_size)
402 apply_to_subrange (0, size);
404 apply_parallel (0, size, minimum_parallel_grain_size);
411 const std::size_t
end)
const 417 if (std_cxx11::is_trivial<T>::value ==
true && trivial_element)
418 std::memset ((
void *)(destination_+begin), 0, (end-begin)*
sizeof(T));
420 copy_construct_or_assign(begin, end,
426 mutable T *destination_;
427 bool trivial_element;
430 void copy_construct_or_assign(
const std::size_t
begin,
431 const std::size_t
end,
434 for (std::size_t i=begin; i<
end; ++i)
435 destination_[i] = element_;
439 void copy_construct_or_assign(
const std::size_t begin,
440 const std::size_t end,
443 for (std::size_t i=begin; i<
end; ++i)
444 new (&destination_[i]) T(element_);
523 const size_type old_size =
size();
524 if (std_cxx11::is_trivial<T>::value ==
false && size_in < old_size)
537 if (std_cxx11::is_trivial<T>::value ==
false && size_in > old_size)
549 const size_type old_size =
size();
550 if (std_cxx11::is_trivial<T>::value ==
false && size_in < old_size)
562 if (size_in > old_size)
575 if (size_alloc > allocated_size)
580 size_type new_size = size_alloc;
581 if (size_alloc < (2 * allocated_size))
582 new_size = 2 * allocated_size;
584 const size_type size_actual_allocate = new_size *
sizeof(T);
593 std::swap (_data, new_data);
603 Assert(new_data == 0, ExcInternalError());
605 else if (size_alloc == 0)
618 if (std_cxx11::is_trivial<T>::value ==
false)
639 if (std_cxx11::is_trivial<T>::value ==
false)
648 typename AlignedVector<T>::reference
660 typename AlignedVector<T>::const_reference
671 template <
typename ForwardIterator>
677 const unsigned int old_size =
size();
678 reserve (old_size + (end-begin));
681 if (std_cxx11::is_trivial<T>::value ==
false)
723 typename AlignedVector<T>::size_type
733 typename AlignedVector<T>::size_type
743 typename AlignedVector<T>::reference
754 typename AlignedVector<T>::const_reference
765 typename AlignedVector<T>::iterator
775 typename AlignedVector<T>::iterator
785 typename AlignedVector<T>::const_iterator
795 typename AlignedVector<T>::const_iterator
804 template <
class Archive >
809 size_type vec_size (
size());
812 ar &boost::serialization::make_array(
_data, vec_size);
818 template <
class Archive >
823 size_type vec_size = 0;
829 ar &boost::serialization::make_array(
_data, vec_size);
838 typename AlignedVector<T>::size_type
841 size_type memory =
sizeof(*this);
849 #endif // ifndef DOXYGEN 863 for (
typename AlignedVector<T>::const_iterator lit = lhs.
begin(),
864 rit = rhs.
begin(); lit != lhs.
end(); ++lit, ++rit)
886 DEAL_II_NAMESPACE_CLOSE
#define AssertIndexRange(index, range)
void load(Archive &ar, const unsigned int version)
AlignedVector & operator=(const AlignedVector< T > &vec)
void reserve(const size_type size_alloc)
void push_back(const T in_data)
reference operator[](const size_type index)
AlignedVectorSet(const std::size_t size, const T &element, T *destination)
bool operator==(const AlignedVector< T > &lhs, const AlignedVector< T > &rhs)
#define Assert(cond, exc)
virtual void apply_to_subrange(const std::size_t begin, const std::size_t end) const
void resize(const size_type size_in, const T &init=T())
void posix_memalign(void **memptr, size_t alignment, size_t size)
void insert_back(ForwardIterator begin, ForwardIterator end)
size_type memory_consumption() const
void swap(AlignedVector< T > &vec)
std_cxx11::enable_if< std_cxx11::is_fundamental< T >::value, std::size_t >::type memory_consumption(const T &t)
void resize_fast(const size_type size)
void save(Archive &ar, const unsigned int version) const
virtual void apply_to_subrange(const std::size_t begin, const std::size_t end) const
bool operator!=(const AlignedVector< T > &lhs, const AlignedVector< T > &rhs)
void fill(const T &element)
AlignedVectorMove(T *source_begin, T *source_end, T *destination, const bool copy_source)
size_type capacity() const