auto
  B   ˆl–Ãa¾”êh¥2 ¸&®6â˜´o�þDàY¹H$qŒ@óÂƒ}ÖCÁ_‹I|¥�+ýÿ¹ä³ÁÀ &    /* Copyright 2016-2018 Joaquin M Lopez Munoz.
 * Distributed under the Boost Software License, Version 1.0.
 * (See accompanying file LICENSE_1_0.txt or copy at
 * http://www.boost.org/LICENSE_1_0.txt)
 *
 * See http://www.boost.org/libs/poly_collection for library home page.
 */

#ifndef BOOST_POLY_COLLECTION_DETAIL_SEGMENT_HPP
#define BOOST_POLY_COLLECTION_DETAIL_SEGMENT_HPP

#if defined(_MSC_VER)
#pragma once
#endif

#include <iterator>
#include <memory>
#include <type_traits>
#include <utility>

namespace boost{

namespace poly_collection{

namespace detail{

/* segment<Model,Allocator> encapsulates implementations of
 * Model::segment_backend virtual interface under a value-semantics type for
 * use by poly_collection. The techique is described by Sean Parent at slides
 * 157-205 of
 * https://github.com/sean-parent/sean-parent.github.com/wiki/
 *   presentations/2013-09-11-cpp-seasoning/cpp-seasoning.pdf
 * with one twist: when the type of the implementation can be known at compile
 * time, a downcast is done and non-virtual member functions (named with a nv_
 * prefix) are used: this increases the performance of some operations.
 */

template<typename Model,typename Allocator>
class segment
{
public:
  using value_type=typename Model::value_type;
  using allocator_type=Allocator; /* needed for uses-allocator construction */
  using base_iterator=typename Model::base_iterator;
  using const_base_iterator=typename Model::const_base_iterator;
  using base_sentinel=typename Model::base_sentinel;
  using const_base_sentinel=typename Model::const_base_sentinel;
  template<typename T>
  using iterator=typename Model::template iterator<T>;
  template<typename T>
  using const_iterator=typename Model::template const_iterator<T>;

  template<typename T>
  static segment make(const allocator_type& al)
  {
    return segment_backend_implementation<T>::make(al);
  }

  /* clones the implementation of x with no elements */

  static segment make_from_prototype(const segment& x,const allocator_type& al)
  {
    return {from_prototype{},x,al};
  }

  segment(const segment& x):
    pimpl{x.impl().copy()}{set_sentinel();}
  segment(segment&& x)=default;
  segment(const segment& x,const allocator_type& al):
    pimpl{x.impl().copy(al)}{set_sentinel();}

  /* TODO: try ptr-level move before impl().move() */
  segment(segment&& x,const allocator_type& al):
    pimpl{x.impl().move(al)}{set_sentinel();}

  segment& operator=(const segment& x)
  {
    pimpl=allocator_traits::propagate_on_container_copy_assignment::value?
      x.impl().copy():x.impl().copy(impl().get_allocator());
    set_sentinel();
    return *this;
  }
  
  segment& operator=(segment&& x)
  {
    pimpl=x.impl().move(
      allocator_traits::propagate_on_container_move_assignment::value?
      x.impl().get_allocator():impl().get_allocator());
    set_sentinel();
    return *this;
  }

  friend bool operator==(const segment& x,const segment& y)
  {
    if(typeid(*(x.pimpl))!=typeid(*(y.pimpl)))return false;
    else return x.impl().equal(y.impl());
  }

  friend bool operator!=(const segment& x,const segment& y){return !(x==y);}

  base_iterator        begin()const noexcept{return impl().begin();}
  template<typename U>
  base_iterator        begin()const noexcept{return impl<U>().nv_begin();}
  base_iterator        end()const noexcept{return impl().end();}
  template<typename U>
  base_iterator        end()const noexcept{return impl<U>().nv_end();}
  base_sentinel        sentinel()const noexcept{return snt;}
  bool                 empty()const noexcept{return impl().empty();}
  template<typename U>
  bool                 empty()const noexcept{return impl<U>().nv_empty();}
  std::size_t          size()const noexcept{return impl().size();}
  template<typename U>
  std::size_t          size()const noexcept{return impl<U>().nv_size();}
  std::size_t          max_size()const noexcept{return impl().max_size();}
  template<typename U>
  std::size_t          max_size()const noexcept
      