40 #ifndef FIXEDVECTOR_HPP
41 #define FIXEDVECTOR_HPP 1
43 #include <boost/aligned_storage.hpp>
44 #include <boost/serialization/nvp.hpp>
76 typename Initializer >
81 typedef const T* const_iterator;
94 m_data = allocData(sz);
97 Initializer::initialize(
this);
111 template <
typename U>
130 template <
typename V>
134 new (m_data) T(source[index]);
135 Initializer::initialize(
this);
155 template <
typename FwdIter>
167 return m_data[index];
179 return m_data[index];
217 const_iterator
end()
const
234 void setSize(std::size_t new_size);
251 if (m_data != singleton())
258 friend class boost::serialization::access;
268 boost::aligned_storage<sizeof(T), boost::alignment_of<T>::value>
277 static T* allocData(std::size_t sz);
279 static void constructElements(iterator from, iterator to);
281 void destructElements();
284 void detachElements();
286 template<
class Archive>
287 void load(Archive & ar,
const unsigned int version);
289 template<
class Archive>
290 void save(Archive & ar,
const unsigned int version)
const;
292 template<
class Archive>
293 void serialize(Archive & ar,
const unsigned int version)
295 boost::serialization::split_member(ar, *
this, version);
301 return static_cast<T*
>(
static_cast<void*
>(&m_singleton_buf));
305 void visitElements(const_visitor* v)
const;
312 namespace serialization {
340 template<
class Archive,
class T, SEXPTYPE ST,
typename Initr>
341 void load_construct_data(Archive& ar,
343 const unsigned int version)
346 ar >> BOOST_SERIALIZATION_NVP(size);
379 template<
class Archive,
class T, SEXPTYPE ST,
typename Initr>
380 void save_construct_data(Archive& ar,
382 const unsigned int version)
384 std::size_t size = t->
size();
385 ar << BOOST_SERIALIZATION_NVP(size);
394 #include "R_ext/Error.h"
396 template <
typename T, SEXPTYPE ST,
typename Initr>
397 template <
typename U>
403 m_data = allocData(sz);
404 for (T *p = m_data, *pend = m_data + sz; p != pend; ++p)
405 new (p) T(fill_value);
406 Initr::initialize(
this);
409 template <
typename T, SEXPTYPE ST,
typename Initr>
413 std::size_t sz =
size();
415 m_data = allocData(sz);
417 for (const_iterator it = pattern.
begin(),
end = pattern.
end();
420 Initr::initialize(
this);
423 template <
typename T, SEXPTYPE ST,
typename Initr>
424 template <
typename FwdIter>
426 :
VectorBase(ST, std::distance(from, to)), m_data(singleton())
429 m_data = allocData(
size());
431 for (const_iterator it = from; it != to; ++it)
433 Initr::initialize(
this);
436 template <
typename T, SEXPTYPE ST,
typename Initr>
439 std::size_t blocksize = sz*
sizeof(T);
441 if (blocksize/
sizeof(T) != sz)
442 Rf_error(_(
"request to create impossibly large vector."));
446 template <
typename T, SEXPTYPE ST,
typename Initr>
453 template <
typename T, SEXPTYPE ST,
typename Initr>
457 for (iterator p = from; p != to; ++p)
461 template <
typename T, SEXPTYPE ST,
typename Initr>
465 for (T* p = m_data + size() - 1; p >= m_data; --p)
469 template <
typename T, SEXPTYPE ST,
typename Initr>
472 std::for_each(begin(), end(), ElementTraits::DetachReferents<T>());
475 template <
typename T, SEXPTYPE ST,
typename Initr>
483 template <
typename T, SEXPTYPE ST,
typename Initr>
484 template<
class Archive>
486 const unsigned int version)
488 ar >> BOOST_SERIALIZATION_BASE_OBJECT_NVP(
VectorBase);
491 ar >> BOOST_SERIALIZATION_NVP(numnas);
492 std::vector<unsigned int> na_indices;
495 unsigned int idx = 0;
496 for (
unsigned int i = 0; i < numnas; ++i) {
498 ar >> BOOST_SERIALIZATION_NVP(ii);
500 m_data[idx] = NA<T>();
501 na_indices.push_back(idx);
503 na_indices.push_back(size());
509 for (std::vector<unsigned int>::const_iterator it = na_indices.begin();
510 it != na_indices.end(); ++it) {
511 unsigned int stop = *it;
513 ElementTraits::Serialize<T>()(ar, m_data[i]);
525 template <
typename T, SEXPTYPE ST,
typename Initr>
526 template<
class Archive>
528 const unsigned int version)
const
530 ar << BOOST_SERIALIZATION_BASE_OBJECT_NVP(VectorBase);
532 std::vector<unsigned int> na_indices;
535 for (
unsigned int i = 0; i < size(); ++i)
537 na_indices.push_back(i);
541 size_t numnas = na_indices.size();
542 ar << BOOST_SERIALIZATION_NVP(numnas);
543 unsigned int last_idx = 0;
544 for (std::vector<unsigned int>::const_iterator it = na_indices.begin();
545 it != na_indices.end(); ++it) {
546 unsigned int idx = *it;
547 unsigned int ii = idx - last_idx;
548 ar << BOOST_SERIALIZATION_NVP(ii);
554 for (
unsigned int i = 0; i < size(); ++i)
555 if (!
isNA(m_data[i]))
556 ElementTraits::Serialize<T>()(ar, m_data[i]);
559 template <
typename T, SEXPTYPE ST,
typename Initr>
562 std::size_t copysz = std::min(size(), new_size);
563 T* newblock = singleton();
565 newblock = allocData(new_size);
569 constructElements(newblock, newblock + copysz);
570 T* p = std::copy(begin(), begin() + copysz, newblock);
571 T* newblockend = newblock + new_size;
572 for (; p != newblockend; ++p)
576 if (m_data != singleton())
579 adjustSize(new_size);
582 template <
typename T, SEXPTYPE ST,
typename Initr>
588 template <
typename T, SEXPTYPE ST,
typename Initr>
594 template <
typename T, SEXPTYPE ST,
typename Initr>
602 #endif // FIXEDVECTOR_HPP