Use boost::container::vector instead of std::vector in test helpers

This commit is contained in:
Ion Gaztañaga
2026-04-04 11:02:22 +02:00
parent aab32bfa54
commit 7bd65d4dc3
+61 -35
View File
@@ -16,28 +16,31 @@
#define BOOST_CONTAINER_TEST_SEGMENTED_TEST_HELPER_HPP
#include <boost/container/experimental/segmented_iterator_traits.hpp>
#include <vector>
#include <boost/container/detail/iterator.hpp>
#include <boost/container/vector.hpp>
#include <cstddef>
#include <iterator>
#include <ostream>
#include <utility>
#include <iosfwd>
namespace test_detail {
class movable_int
{
BOOST_MOVABLE_BUT_NOT_COPYABLE(movable_int)
int val_;
public:
explicit movable_int(int v = 0) : val_(v) {}
movable_int(movable_int&& other) noexcept : val_(other.val_) { other.val_ = -1; }
movable_int& operator=(movable_int&& other) noexcept { val_ = other.val_; other.val_ = -1; return *this; }
movable_int(const movable_int&) = delete;
movable_int& operator=(const movable_int&) = delete;
movable_int(BOOST_RV_REF(movable_int) other) BOOST_NOEXCEPT
: val_(other.val_)
{ other.val_ = -1; }
movable_int& operator=(BOOST_RV_REF(movable_int) other) BOOST_NOEXCEPT
{ val_ = other.val_; other.val_ = -1; return *this; }
int value() const { return val_; }
friend bool operator==(const movable_int& a, const movable_int& b) { return a.val_ == b.val_; }
friend bool operator!=(const movable_int& a, const movable_int& b) { return a.val_ != b.val_; }
friend bool operator==(const movable_int& a, const movable_int& b) { return a.val_ == b.val_; }
friend bool operator!=(const movable_int& a, const movable_int& b) { return a.val_ != b.val_; }
friend std::ostream& operator<<(std::ostream& os, const movable_int& m) { return os << m.val_; }
};
@@ -51,9 +54,9 @@ public:
typedef std::ptrdiff_t difference_type;
typedef std::bidirectional_iterator_tag iterator_category;
typedef std::vector<std::vector<T> > segments_type;
typedef boost::container::vector<boost::container::vector<T> > segments_type;
typedef typename segments_type::iterator seg_iter_t;
typedef typename std::vector<T>::iterator local_iter_t;
typedef typename boost::container::vector<T>::iterator local_iter_t;
seg_iter_t seg_;
local_iter_t local_;
@@ -115,45 +118,67 @@ class seg_vector
{
// Last entry is always an empty sentinel segment, following
// the convention from Austern's segmented-iterator paper.
std::vector<std::vector<T> > segments_;
boost::container::vector<boost::container::vector<T> > segments_;
BOOST_COPYABLE_AND_MOVABLE(seg_vector)
public:
typedef seg_vector_iterator<T> iterator;
seg_vector()
{
segments_.push_back(std::vector<T>());
segments_.push_back(boost::container::vector<T>());
}
seg_vector(const seg_vector &x)
: segments_(x.segments_)
{}
seg_vector(BOOST_RV_REF(seg_vector) x)
: segments_(boost::move(x.segments_))
{}
seg_vector &operator= (BOOST_RV_REF(seg_vector) x)
{
segments_ = boost::move(x.segments_);
return *this;
}
seg_vector &operator= (BOOST_COPY_ASSIGN_REF(seg_vector) x)
{
segments_ = x.segments_;
return *this;
}
void add_segment(std::size_t n, const T& val)
{
segments_.insert(segments_.end() - 1, std::vector<T>(n, val));
segments_.insert(segments_.end() - 1, boost::container::vector<T>(n, val));
}
template<class InpIt>
void add_segment_range(InpIt first, InpIt last)
{
segments_.insert(segments_.end() - 1, std::vector<T>(first, last));
segments_.insert(segments_.end() - 1, boost::container::vector<T>(first, last));
}
void add_segment_from_ints(const int* first, const int* last)
{
std::vector<T> v;
boost::container::vector<T> v;
v.reserve(static_cast<std::size_t>(last - first));
for(; first != last; ++first)
v.push_back(T(*first));
segments_.insert(segments_.end() - 1, std::move(v));
segments_.insert(segments_.end() - 1, boost::move(v));
}
iterator begin()
{
typename std::vector<std::vector<T> >::iterator s = segments_.begin();
typename boost::container::vector<boost::container::vector<T> >::iterator s = segments_.begin();
return iterator(s, s->begin());
}
iterator end()
{
typename std::vector<std::vector<T> >::iterator s = segments_.end() - 1;
typename boost::container::vector<boost::container::vector<T> >::iterator s = segments_.end() - 1;
return iterator(s, s->begin());
}
@@ -165,8 +190,8 @@ public:
return n;
}
std::vector<std::vector<T> >& segments() { return segments_; }
const std::vector<std::vector<T> >& segments() const { return segments_; }
boost::container::vector<boost::container::vector<T> >& segments() { return segments_; }
const boost::container::vector<boost::container::vector<T> >& segments() const { return segments_; }
};
template<class Iter>
@@ -201,9 +226,10 @@ public:
friend bool operator==(const sized_sentinel_wrapper& a, const Iter& b) { return a.it_ == b; }
friend bool operator!=(const sized_sentinel_wrapper& a, const Iter& b) { return !(a.it_ == b); }
friend typename std::iterator_traits<Iter>::difference_type
friend typename boost::container::iterator_traits<Iter>::difference_type
operator-(const sized_sentinel_wrapper& a, const Iter& b) { return a.it_ - b; }
friend typename std::iterator_traits<Iter>::difference_type
friend typename boost::container::iterator_traits<Iter>::difference_type
operator-(const Iter& a, const sized_sentinel_wrapper& b) { return a - b.it_; }
};
@@ -220,7 +246,7 @@ public:
typedef std::ptrdiff_t difference_type;
typedef std::bidirectional_iterator_tag iterator_category;
typedef std::vector<seg_vector<T> > segments_type;
typedef boost::container::vector<seg_vector<T> > segments_type;
typedef typename segments_type::iterator seg_iter_t;
typedef seg_vector_iterator<T> local_iter_t;
@@ -282,7 +308,7 @@ public:
template<class T>
class seg2_vector
{
std::vector<seg_vector<T> > segments_;
boost::container::vector<seg_vector<T> > segments_;
public:
typedef seg2_vector_iterator<T> iterator;
@@ -302,25 +328,25 @@ public:
{
seg_vector<T> sv;
sv.add_segment_range(first, last);
segments_.insert(segments_.end() - 1, sv);
segments_.insert(segments_.end() - 1, boost::move(sv));
}
void add_flat_segment_from_ints(const int* first, const int* last)
{
seg_vector<T> sv;
sv.add_segment_from_ints(first, last);
segments_.insert(segments_.end() - 1, std::move(sv));
segments_.insert(segments_.end() - 1, boost::move(sv));
}
iterator begin()
{
typename std::vector<seg_vector<T> >::iterator s = segments_.begin();
typename boost::container::vector<seg_vector<T> >::iterator s = segments_.begin();
return iterator(s, s->begin());
}
iterator end()
{
typename std::vector<seg_vector<T> >::iterator s = segments_.end() - 1;
typename boost::container::vector<seg_vector<T> >::iterator s = segments_.end() - 1;
return iterator(s, s->begin());
}
@@ -332,8 +358,8 @@ public:
return n;
}
std::vector<seg_vector<T> >& segments() { return segments_; }
const std::vector<seg_vector<T> >& segments() const { return segments_; }
boost::container::vector<seg_vector<T> >& segments() { return segments_; }
const boost::container::vector<seg_vector<T> >& segments() const { return segments_; }
};
} // namespace test_detail
@@ -346,8 +372,8 @@ struct segmented_iterator_traits<test_detail::seg_vector_iterator<T> >
{
typedef segmented_iterator_tag is_segmented_iterator;
typedef typename std::vector<std::vector<T> >::iterator segment_iterator;
typedef typename std::vector<T>::iterator local_iterator;
typedef typename boost::container::vector<boost::container::vector<T> >::iterator segment_iterator;
typedef typename boost::container::vector<T>::iterator local_iterator;
typedef test_detail::seg_vector_iterator<T> iterator;
static segment_iterator segment(iterator it) { return it.seg_; }
@@ -371,7 +397,7 @@ struct segmented_iterator_traits<test_detail::seg2_vector_iterator<T> >
{
typedef segmented_iterator_tag is_segmented_iterator;
typedef typename std::vector<test_detail::seg_vector<T> >::iterator segment_iterator;
typedef typename boost::container::vector<test_detail::seg_vector<T> >::iterator segment_iterator;
typedef test_detail::seg_vector_iterator<T> local_iterator;
typedef test_detail::seg2_vector_iterator<T> iterator;