From b62ccc5d017e54beecd72d64d2074473c49192a7 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 4 Jan 2018 09:49:34 +0200 Subject: Implement small_list, small_forward_list Note that with VC small_list is never "small" because of the extra "headnode" that this implementation allocates (see notes in small-list.mxx for details). --- libbutl/small-forward-list.mxx | 158 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 libbutl/small-forward-list.mxx (limited to 'libbutl/small-forward-list.mxx') diff --git a/libbutl/small-forward-list.mxx b/libbutl/small-forward-list.mxx new file mode 100644 index 0000000..9411367 --- /dev/null +++ b/libbutl/small-forward-list.mxx @@ -0,0 +1,158 @@ +// file : libbutl/small-forward-list.mxx -*- C++ -*- +// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#ifndef __cpp_modules +#pragma once +#endif + +#ifndef __cpp_lib_modules +#include // size_t +#include // move() +#include +#endif + +// Other includes. + +#ifdef __cpp_modules +export module butl.small_forward_list; +#ifdef __cpp_lib_modules +import std.core; +#endif +import butl.small_allocator; +#else +#include +#endif + +#include + +LIBBUTL_MODEXPORT namespace butl +{ + // Issues and limitations. + // + // - Because small_allocator currently expects us to allocate the entire + // small buffer and there is no reserve() in std::forward_list, we + // currently only support N==1 (which we static_assert). + // + // - swap() is deleted (see notes below). + // + // - The implementation doesn't allocate T but rather a "node" that normally + // consists of a pointers (next) and T. + // + template + using small_forward_list_node = +#if defined (_MSC_VER) + std::_Flist_node; +#elif defined (__GLIBCXX__) + std::_Fwd_list_node; +#elif defined (_LIBCPP_VERSION) + std::__forward_list_node; +#else +#error unknown standard library implementation +#endif + + template + using small_forward_list_buffer = + small_allocator_buffer, N>; + + template + class small_forward_list: + private small_forward_list_buffer, + public std::forward_list>> + { + static_assert (N == 1, "only small_forward_list or 1 currently supported"); + + public: + using buffer_type = small_forward_list_buffer; + using allocator_type = small_allocator; + using base_type = std::forward_list; + + small_forward_list () + : base_type (allocator_type (this)) {} + + small_forward_list (std::initializer_list v) + : base_type (allocator_type (this)) + { + static_cast (*this) = v; + } + + template + small_forward_list (I b, I e) + : base_type (allocator_type (this)) + { + this->assign (b, e); + } + + explicit + small_forward_list (std::size_t n) + : base_type (allocator_type (this)) + { + this->resize (n); + } + + small_forward_list (std::size_t n, const T& x) + : base_type (allocator_type (this)) + { + this->assign (n, x); + } + + small_forward_list (const small_forward_list& v) + : buffer_type (), base_type (allocator_type (this)) + { + static_cast (*this) = v; + } + + small_forward_list& + operator= (const small_forward_list& v) + { + // Note: propagate_on_container_copy_assignment = false + // + static_cast (*this) = v; + return *this; + } + + small_forward_list (small_forward_list&& v) + : base_type (allocator_type (this)) + { + *this = std::move (v); // Delegate to operator=(&&). + } + + small_forward_list& + operator= (small_forward_list&& v) + { + // VC14's implementation of operator=(&&) swaps pointers without regard + // for allocator (fixed in 15). + // +#if defined(_MSC_VER) && _MSC_VER <= 1900 + clear (); + for (T& x: v) + push_front (std::move (x)); + reverse (); + v.clear (); +#else + // Note: propagate_on_container_move_assignment = false + // + static_cast (*this) = std::move (v); +#endif + + return *this; + } + + small_forward_list& + operator= (std::initializer_list v) + { + static_cast (*this) = v; + return *this; + } + + // Implementing swap() under small buffer optimization is not trivial, to + // say the least (think of swapping two such buffers of different sizes). + // One easy option would be to force both in to the heap. + // + void + swap (small_forward_list&) = delete; + }; +} -- cgit v1.1