segmented_array - Accessing The Segments (Advanced)

If perhaps you wish to do some kind of optimized segmented copy for example and you need access to the segmented_array's segments then you can do the following:

template <typename T, std::size_t N = 64, typename A = std::allocator<T> >
class segmented_iterator : public lib::segmented_array<T, N, A>::iterator
{
    typedef typename lib::segmented_array<T, N, A>::iterator base;
public:
    segmented_iterator(const base& x) : base(x) {}
    typename base::segment_type& segment() { return base::seg(); }
    typename base::segment_list_iterator_type list_iterator() { return base::iter(); }
};


int main()
{
    lib::segmented_array<double, 3> sa;
    for (double i = 42; i < 43; i+= 0.1)
        sa.push_back(i);

    std::cout << "Number of segments: " <<
        std::distance(segmented_iterator<double, 3>(sa.begin()).list_iterator(),
            segmented_iterator<double, 3>(sa.end()).list_iterator()) << std::endl;

    std::cout << "Contents of the first segment: " << std::endl;
    lib::segmented_array<double, 3>::segment& segment = segmented_iterator<double, 3>(sa.begin()).segment();
    std::copy(segment.begin(), segment.end(), std::ostream_iterator<double>(std::cout, "\n"));

    return 0;
}

The output of which is:

Number of segments: 4
Contents of the first segment:
42
42.1
42.2


You can e-mail comments to the author.

Back