From 8c3142ad610fe387a9a536b64e98d53925218fc2 Mon Sep 17 00:00:00 2001 From: Kern Handa Date: Wed, 30 Sep 2015 18:50:07 +0000 Subject: [PATCH] Add .clang-format file and format source files accordingly. To apply .clang-format to the codebase, the following command can be run: git ls-files -- *.cpp *.h | xargs clang-format -i -style=file --- .clang-format | 47 + include/array_view.h | 4125 ++++++++++++++++++----------------- include/fail_fast.h | 61 +- include/gsl.h | 353 ++- include/string_view.h | 163 +- tests/array_view_tests.cpp | 2977 ++++++++++++------------- tests/assertion_tests.cpp | 34 +- tests/at_tests.cpp | 40 +- tests/bounds_tests.cpp | 162 +- tests/maybenull_tests.cpp | 117 +- tests/notnull_tests.cpp | 85 +- tests/owner_tests.cpp | 32 +- tests/string_view_tests.cpp | 44 +- tests/utils_tests.cpp | 53 +- 14 files changed, 4380 insertions(+), 3913 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..8da26e3 --- /dev/null +++ b/.clang-format @@ -0,0 +1,47 @@ +AccessModifierOffset: 0 +AlignEscapedNewlinesLeft: true +AlignTrailingComments: false +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortFunctionsOnASingleLine: false +AllowShortIfStatementsOnASingleLine: true +AllowShortLoopsOnASingleLine: false +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: false +BinPackParameters: false +BreakBeforeBinaryOperators: false +BreakBeforeBraces: Allman +BreakBeforeTernaryOperators: false +BreakConstructorInitializersBeforeComma: false +ColumnLimit: 100 +CommentPragmas: '' +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 0 +ContinuationIndentWidth: 0 +Cpp11BracedListStyle: false +DerivePointerBinding: false +IndentCaseLabels: false +IndentFunctionDeclarationAfterType: false +IndentWidth: 4 +Language: Cpp +MaxEmptyLinesToKeep: 2 +NamespaceIndentation: None +ObjCSpaceAfterProperty: true +ObjCSpaceBeforeProtocolList: true +PenaltyBreakBeforeFirstCallParameter: 100 +PenaltyBreakComment: 100 +PenaltyBreakFirstLessLess: 0 +PenaltyBreakString: 100 +PenaltyExcessCharacter: 1 +PenaltyReturnTypeOnItsOwnLine: 20 +PointerBindsToType: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeParens: ControlStatements +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInCStyleCastParentheses: false +SpacesInContainerLiterals: false +SpacesInParentheses: false +Standard: Cpp11 +TabWidth: 4 +UseTab: Never diff --git a/include/array_view.h b/include/array_view.h index c581760..789a5b5 100644 --- a/include/array_view.h +++ b/include/array_view.h @@ -1,17 +1,17 @@ -/////////////////////////////////////////////////////////////////////////////// -// -// Copyright (c) 2015 Microsoft Corporation. All rights reserved. -// -// This code is licensed under the MIT License (MIT). -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// +/////////////////////////////////////////////////////////////////////////////// +// +// Copyright (c) 2015 Microsoft Corporation. All rights reserved. +// +// This code is licensed under the MIT License (MIT). +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// /////////////////////////////////////////////////////////////////////////////// #pragma once @@ -40,453 +40,467 @@ #ifndef _NOEXCEPT #ifdef GSL_THROWS_FOR_TESTING -#define _NOEXCEPT -#else +#define _NOEXCEPT +#else #define _NOEXCEPT noexcept -#endif +#endif #else // _NOEXCEPT #ifdef GSL_THROWS_FOR_TESTING #undef _NOEXCEPT -#define _NOEXCEPT -#endif +#define _NOEXCEPT +#endif #endif // _NOEXCEPT #if defined(_MSC_VER) && _MSC_VER <= 1800 #pragma warning(push) -#pragma warning(disable: 4351) // warns about newly introduced aggregate initializer behavior +#pragma warning(disable : 4351) // warns about newly introduced aggregate initializer behavior #endif // _MSC_VER <= 1800 -namespace gsl { +namespace gsl +{ /* ** begin definitions of index and bounds */ namespace details { - template - struct SizeTypeTraits - { - static const size_t max_value = std::is_signed::value ? static_cast::type>(-1) / 2 : static_cast(-1); - }; +template struct SizeTypeTraits +{ + static const size_t max_value = std::is_signed::value ? + static_cast::type>(-1) / 2 : + static_cast(-1); +}; - template - class coordinate_facade - { - static_assert(std::is_integral::value - && sizeof(ValueType) <= sizeof(size_t), "ValueType must be an integral type!"); - static_assert(Rank > 0, "Rank must be greater than 0!"); +template class coordinate_facade +{ + static_assert(std::is_integral::value && sizeof(ValueType) <= sizeof(size_t), + "ValueType must be an integral type!"); + static_assert(Rank > 0, "Rank must be greater than 0!"); - template - friend class coordinate_facade; - public: - using reference = ValueType&; - using const_reference = const ValueType&; - using value_type = ValueType; - static const size_t rank = Rank; - constexpr coordinate_facade() _NOEXCEPT - { - static_assert(std::is_base_of::value, "ConcreteType must be derived from coordinate_facade."); - } - constexpr coordinate_facade(const value_type(&values)[rank]) _NOEXCEPT - { - static_assert(std::is_base_of::value, "ConcreteType must be derived from coordinate_facade."); - for (size_t i = 0; i < rank; ++i) - elems[i] = values[i]; - } - constexpr coordinate_facade(value_type e0) _NOEXCEPT - { - static_assert(std::is_base_of::value, "ConcreteType must be derived from coordinate_facade."); - static_assert(rank == 1, "This constructor can only be used with rank == 1."); - elems[0] = e0; - } - // Preconditions: il.size() == rank - constexpr coordinate_facade(std::initializer_list il) - { - static_assert(std::is_base_of::value, "ConcreteType must be derived from coordinate_facade."); - fail_fast_assert(il.size() == rank, "The size of the initializer list must match the rank of the array"); - for (size_t i = 0; i < rank; ++i) - { - elems[i] = begin(il)[i]; - } - } + template + friend class coordinate_facade; - constexpr coordinate_facade(const coordinate_facade & other) = default; + public: + using reference = ValueType&; + using const_reference = const ValueType&; + using value_type = ValueType; + static const size_t rank = Rank; + constexpr coordinate_facade() _NOEXCEPT + { + static_assert(std::is_base_of::value, + "ConcreteType must be derived from coordinate_facade."); + } + constexpr coordinate_facade(const value_type(&values)[rank]) _NOEXCEPT + { + static_assert(std::is_base_of::value, + "ConcreteType must be derived from coordinate_facade."); + for (size_t i = 0; i < rank; ++i) + elems[i] = values[i]; + } + constexpr coordinate_facade(value_type e0) _NOEXCEPT + { + static_assert(std::is_base_of::value, + "ConcreteType must be derived from coordinate_facade."); + static_assert(rank == 1, "This constructor can only be used with rank == 1."); + elems[0] = e0; + } + // Preconditions: il.size() == rank + constexpr coordinate_facade(std::initializer_list il) + { + static_assert(std::is_base_of::value, + "ConcreteType must be derived from coordinate_facade."); + fail_fast_assert(il.size() == rank, + "The size of the initializer list must match the rank of the array"); + for (size_t i = 0; i < rank; ++i) + { + elems[i] = begin(il)[i]; + } + } - template - constexpr coordinate_facade(const coordinate_facade & other) - { - for (size_t i = 0; i < rank; ++i) - { - fail_fast_assert(static_cast(other.elems[i]) <= SizeTypeTraits::max_value); - elems[i] = static_cast(other.elems[i]); - } - } - protected: - coordinate_facade& operator=(const coordinate_facade& rhs) = default; - // Preconditions: component_idx < rank - constexpr reference operator[](size_t component_idx) - { - fail_fast_assert(component_idx < rank, "Component index must be less than rank"); - return elems[component_idx]; - } - // Preconditions: component_idx < rank - constexpr const_reference operator[](size_t component_idx) const - { - fail_fast_assert(component_idx < rank, "Component index must be less than rank"); - return elems[component_idx]; - } - constexpr bool operator==(const ConcreteType& rhs) const _NOEXCEPT - { - for (size_t i = 0; i < rank; ++i) - { - if (elems[i] != rhs.elems[i]) - return false; - } - return true; - } - constexpr bool operator!=(const ConcreteType& rhs) const _NOEXCEPT - { - return !(to_concrete() == rhs); - } - constexpr ConcreteType operator+() const _NOEXCEPT - { - return to_concrete(); - } - constexpr ConcreteType operator-() const - { - ConcreteType ret = to_concrete(); - for (size_t i = 0; i < rank; ++i) - ret.elems[i] = -ret.elems[i]; - return ret; - } - constexpr ConcreteType operator+(const ConcreteType& rhs) const - { - ConcreteType ret = to_concrete(); - ret += rhs; - return ret; - } - constexpr ConcreteType operator-(const ConcreteType& rhs) const - { - ConcreteType ret = to_concrete(); - ret -= rhs; - return ret; - } - constexpr ConcreteType& operator+=(const ConcreteType& rhs) - { - for (size_t i = 0; i < rank; ++i) - elems[i] += rhs.elems[i]; - return to_concrete(); - } - constexpr ConcreteType& operator-=(const ConcreteType& rhs) - { - for (size_t i = 0; i < rank; ++i) - elems[i] -= rhs.elems[i]; - return to_concrete(); - } - constexpr ConcreteType& operator++() - { - static_assert(rank == 1, "This operator can only be used with rank == 1."); - ++elems[0]; - return to_concrete(); - } - constexpr ConcreteType operator++(int) - { - static_assert(rank == 1, "This operator can only be used with rank == 1."); - ConcreteType ret = to_concrete(); - ++(*this); - return ret; - } - constexpr ConcreteType& operator--() - { - static_assert(rank == 1, "This operator can only be used with rank == 1."); - --elems[0]; - return to_concrete(); - } - constexpr ConcreteType operator--(int) - { - static_assert(rank == 1, "This operator can only be used with rank == 1."); - ConcreteType ret = to_concrete(); - --(*this); - return ret; - } - constexpr ConcreteType operator*(value_type v) const - { - ConcreteType ret = to_concrete(); - ret *= v; - return ret; - } - constexpr ConcreteType operator/(value_type v) const - { - ConcreteType ret = to_concrete(); - ret /= v; - return ret; - } - friend constexpr ConcreteType operator*(value_type v, const ConcreteType& rhs) - { - return rhs * v; - } - constexpr ConcreteType& operator*=(value_type v) - { - for (size_t i = 0; i < rank; ++i) - elems[i] *= v; - return to_concrete(); - } - constexpr ConcreteType& operator/=(value_type v) - { - for (size_t i = 0; i < rank; ++i) - elems[i] /= v; - return to_concrete(); - } - value_type elems[rank] = {}; - private: - constexpr const ConcreteType& to_concrete() const _NOEXCEPT - { - return static_cast(*this); - } - constexpr ConcreteType& to_concrete() _NOEXCEPT - { - return static_cast(*this); - } - }; - template - class arrow_proxy - { - public: - explicit arrow_proxy(T t) - : val(t) - {} - const T operator*() const _NOEXCEPT - { - return val; - } - const T* operator->() const _NOEXCEPT - { - return &val; - } - private: - T val; - }; + constexpr coordinate_facade(const coordinate_facade& other) = default; + + template + constexpr coordinate_facade(const coordinate_facade& other) + { + for (size_t i = 0; i < rank; ++i) + { + fail_fast_assert(static_cast(other.elems[i]) <= SizeTypeTraits::max_value); + elems[i] = static_cast(other.elems[i]); + } + } + + protected: + coordinate_facade& operator=(const coordinate_facade& rhs) = default; + // Preconditions: component_idx < rank + constexpr reference operator[](size_t component_idx) + { + fail_fast_assert(component_idx < rank, "Component index must be less than rank"); + return elems[component_idx]; + } + // Preconditions: component_idx < rank + constexpr const_reference operator[](size_t component_idx) const + { + fail_fast_assert(component_idx < rank, "Component index must be less than rank"); + return elems[component_idx]; + } + constexpr bool operator==(const ConcreteType& rhs) const _NOEXCEPT + { + for (size_t i = 0; i < rank; ++i) + { + if (elems[i] != rhs.elems[i]) return false; + } + return true; + } + constexpr bool operator!=(const ConcreteType& rhs) const _NOEXCEPT + { + return !(to_concrete() == rhs); + } + constexpr ConcreteType operator+() const _NOEXCEPT + { + return to_concrete(); + } + constexpr ConcreteType operator-() const + { + ConcreteType ret = to_concrete(); + for (size_t i = 0; i < rank; ++i) + ret.elems[i] = -ret.elems[i]; + return ret; + } + constexpr ConcreteType operator+(const ConcreteType& rhs) const + { + ConcreteType ret = to_concrete(); + ret += rhs; + return ret; + } + constexpr ConcreteType operator-(const ConcreteType& rhs) const + { + ConcreteType ret = to_concrete(); + ret -= rhs; + return ret; + } + constexpr ConcreteType& operator+=(const ConcreteType& rhs) + { + for (size_t i = 0; i < rank; ++i) + elems[i] += rhs.elems[i]; + return to_concrete(); + } + constexpr ConcreteType& operator-=(const ConcreteType& rhs) + { + for (size_t i = 0; i < rank; ++i) + elems[i] -= rhs.elems[i]; + return to_concrete(); + } + constexpr ConcreteType& operator++() + { + static_assert(rank == 1, "This operator can only be used with rank == 1."); + ++elems[0]; + return to_concrete(); + } + constexpr ConcreteType operator++(int) + { + static_assert(rank == 1, "This operator can only be used with rank == 1."); + ConcreteType ret = to_concrete(); + ++(*this); + return ret; + } + constexpr ConcreteType& operator--() + { + static_assert(rank == 1, "This operator can only be used with rank == 1."); + --elems[0]; + return to_concrete(); + } + constexpr ConcreteType operator--(int) + { + static_assert(rank == 1, "This operator can only be used with rank == 1."); + ConcreteType ret = to_concrete(); + --(*this); + return ret; + } + constexpr ConcreteType operator*(value_type v) const + { + ConcreteType ret = to_concrete(); + ret *= v; + return ret; + } + constexpr ConcreteType operator/(value_type v) const + { + ConcreteType ret = to_concrete(); + ret /= v; + return ret; + } + friend constexpr ConcreteType operator*(value_type v, const ConcreteType& rhs) + { + return rhs * v; + } + constexpr ConcreteType& operator*=(value_type v) + { + for (size_t i = 0; i < rank; ++i) + elems[i] *= v; + return to_concrete(); + } + constexpr ConcreteType& operator/=(value_type v) + { + for (size_t i = 0; i < rank; ++i) + elems[i] /= v; + return to_concrete(); + } + value_type elems[rank] = {}; + + private: + constexpr const ConcreteType& to_concrete() const _NOEXCEPT + { + return static_cast(*this); + } + constexpr ConcreteType& to_concrete() _NOEXCEPT + { + return static_cast(*this); + } +}; +template class arrow_proxy +{ + public: + explicit arrow_proxy(T t) : val(t) + { + } + const T operator*() const _NOEXCEPT + { + return val; + } + const T* operator->() const _NOEXCEPT + { + return &val; + } + + private: + T val; +}; } template class index : private details::coordinate_facade, ValueType, Rank> { - using Base = details::coordinate_facade, ValueType, Rank>; - friend Base; - template - friend class index; -public: - using Base::rank; - using reference = typename Base::reference; - using const_reference = typename Base::const_reference; - using size_type = typename Base::value_type; - using value_type = typename Base::value_type; - constexpr index() _NOEXCEPT : Base(){} - constexpr index(const value_type (&values)[rank]) _NOEXCEPT : Base(values) {} - constexpr index(std::initializer_list il) : Base(il) {} + using Base = details::coordinate_facade, ValueType, Rank>; + friend Base; + template friend class index; - constexpr index(const index &) = default; + public: + using Base::rank; + using reference = typename Base::reference; + using const_reference = typename Base::const_reference; + using size_type = typename Base::value_type; + using value_type = typename Base::value_type; + constexpr index() _NOEXCEPT : Base() + { + } + constexpr index(const value_type(&values)[rank]) _NOEXCEPT : Base(values) + { + } + constexpr index(std::initializer_list il) : Base(il) + { + } - template - constexpr index(const index &other) : Base(other) - { - } - constexpr static index shift_left(const index& other) _NOEXCEPT - { - value_type (&arr)[rank] = (value_type(&)[rank])(*(other.elems + 1)); - return index(arr); - } + constexpr index(const index&) = default; - using Base::operator[]; - using Base::operator==; - using Base::operator!=; - using Base::operator+; - using Base::operator-; - using Base::operator+=; - using Base::operator-=; - using Base::operator++; - using Base::operator--; - using Base::operator*; - using Base::operator/; - using Base::operator*=; - using Base::operator/=; + template + constexpr index(const index& other) + : Base(other) + { + } + constexpr static index shift_left(const index& other) _NOEXCEPT + { + value_type(&arr)[rank] = (value_type(&)[rank])(*(other.elems + 1)); + return index(arr); + } + + using Base::operator[]; + using Base::operator==; + using Base::operator!=; + using Base::operator+; + using Base::operator-; + using Base::operator+=; + using Base::operator-=; + using Base::operator++; + using Base::operator--; + using Base::operator*; + using Base::operator/; + using Base::operator*=; + using Base::operator/=; }; -template -class index<1, ValueType> +template class index<1, ValueType> { - template - friend class index; -public: - static const size_t rank = 1; - using reference = ValueType&; - using const_reference = const ValueType&; - using size_type = ValueType; - using value_type = ValueType; - - constexpr index() _NOEXCEPT : value(0) - { - } - constexpr index(value_type e0) _NOEXCEPT : value(e0) - { - } - constexpr index(const value_type(&values)[1]) _NOEXCEPT : index(values[0]) - { - } - // Preconditions: il.size() == rank - constexpr index(std::initializer_list il) - { - fail_fast_assert(il.size() == rank, "Size of the initializer list must match the rank of the array"); - value = begin(il)[0]; - } + template friend class index; - constexpr index(const index &) = default; + public: + static const size_t rank = 1; + using reference = ValueType&; + using const_reference = const ValueType&; + using size_type = ValueType; + using value_type = ValueType; - template - constexpr index(const index<1, OtherValueType> & other) - { - fail_fast_assert(other.value <= details::SizeTypeTraits::max_value); - value = static_cast(other.value); - } + constexpr index() _NOEXCEPT : value(0) + { + } + constexpr index(value_type e0) _NOEXCEPT : value(e0) + { + } + constexpr index(const value_type(&values)[1]) _NOEXCEPT : index(values[0]) + { + } + // Preconditions: il.size() == rank + constexpr index(std::initializer_list il) + { + fail_fast_assert(il.size() == rank, + "Size of the initializer list must match the rank of the array"); + value = begin(il)[0]; + } - constexpr static index shift_left(const index& other) _NOEXCEPT - { - return other.elems[1]; - } - // Preconditions: component_idx < rank - constexpr reference operator[](size_type component_idx) _NOEXCEPT - { - fail_fast_assert(component_idx == 0, "Component index must be less than rank"); - (void)(component_idx); - return value; - } - // Preconditions: component_idx < rank - constexpr const_reference operator[](size_type component_idx) const _NOEXCEPT - { - fail_fast_assert(component_idx == 0, "Component index must be less than rank"); - (void)(component_idx); - return value; - } - constexpr bool operator==(const index& rhs) const _NOEXCEPT - { - return value == rhs.value; - } - constexpr bool operator!=(const index& rhs) const _NOEXCEPT - { - return !(*this == rhs); - } - constexpr index operator+() const _NOEXCEPT - { - return *this; - } - constexpr index operator-() const _NOEXCEPT - { - return index(-value); - } - constexpr index operator+(const index& rhs) const _NOEXCEPT - { - return index(value + rhs.value); - } - constexpr index operator-(const index& rhs) const _NOEXCEPT - { - return index(value - rhs.value); - } - constexpr index& operator+=(const index& rhs) _NOEXCEPT - { - value += rhs.value; - return *this; - } - constexpr index& operator-=(const index& rhs) _NOEXCEPT - { - value -= rhs.value; - return *this; - } - constexpr index& operator++() _NOEXCEPT - { - ++value; - return *this; - } - constexpr index operator++(int) _NOEXCEPT - { - index ret = *this; - ++(*this); - return ret; - } - constexpr index& operator--() _NOEXCEPT - { - --value; - return *this; - } - constexpr index operator--(int) _NOEXCEPT - { - index ret = *this; - --(*this); - return ret; - } - constexpr index operator*(value_type v) const _NOEXCEPT - { - return index(value * v); - } - constexpr index operator/(value_type v) const _NOEXCEPT - { - return index(value / v); - } - constexpr index& operator*=(value_type v) _NOEXCEPT - { - value *= v; - return *this; - } - constexpr index& operator/=(value_type v) _NOEXCEPT - { - value /= v; - return *this; - } - friend constexpr index operator*(value_type v, const index& rhs) _NOEXCEPT - { - return index(rhs * v); - } -private: - value_type value; + constexpr index(const index&) = default; + + template constexpr index(const index<1, OtherValueType>& other) + { + fail_fast_assert(other.value <= details::SizeTypeTraits::max_value); + value = static_cast(other.value); + } + + constexpr static index shift_left(const index& other) _NOEXCEPT + { + return other.elems[1]; + } + // Preconditions: component_idx < rank + constexpr reference operator[](size_type component_idx)_NOEXCEPT + { + fail_fast_assert(component_idx == 0, "Component index must be less than rank"); + (void)(component_idx); + return value; + } + // Preconditions: component_idx < rank + constexpr const_reference operator[](size_type component_idx) const _NOEXCEPT + { + fail_fast_assert(component_idx == 0, "Component index must be less than rank"); + (void)(component_idx); + return value; + } + constexpr bool operator==(const index& rhs) const _NOEXCEPT + { + return value == rhs.value; + } + constexpr bool operator!=(const index& rhs) const _NOEXCEPT + { + return !(*this == rhs); + } + constexpr index operator+() const _NOEXCEPT + { + return *this; + } + constexpr index operator-() const _NOEXCEPT + { + return index(-value); + } + constexpr index operator+(const index& rhs) const _NOEXCEPT + { + return index(value + rhs.value); + } + constexpr index operator-(const index& rhs) const _NOEXCEPT + { + return index(value - rhs.value); + } + constexpr index& operator+=(const index& rhs) _NOEXCEPT + { + value += rhs.value; + return *this; + } + constexpr index& operator-=(const index& rhs) _NOEXCEPT + { + value -= rhs.value; + return *this; + } + constexpr index& operator++() _NOEXCEPT + { + ++value; + return *this; + } + constexpr index operator++(int)_NOEXCEPT + { + index ret = *this; + ++(*this); + return ret; + } + constexpr index& operator--() _NOEXCEPT + { + --value; + return *this; + } + constexpr index operator--(int)_NOEXCEPT + { + index ret = *this; + --(*this); + return ret; + } + constexpr index operator*(value_type v) const _NOEXCEPT + { + return index(value * v); + } + constexpr index operator/(value_type v) const _NOEXCEPT + { + return index(value / v); + } + constexpr index& operator*=(value_type v) _NOEXCEPT + { + value *= v; + return *this; + } + constexpr index& operator/=(value_type v) _NOEXCEPT + { + value /= v; + return *this; + } + friend constexpr index operator*(value_type v, const index& rhs)_NOEXCEPT + { + return index(rhs * v); + } + + private: + value_type value; }; #ifndef _MSC_VER struct static_bounds_dynamic_range_t { - template ::value>> - constexpr operator T() const noexcept - { - return static_cast(-1); - } + template ::value>> + constexpr operator T() const noexcept + { + return static_cast(-1); + } - template ::value>> - constexpr bool operator ==(T other) const noexcept - { - return static_cast(-1) == other; - } - - template ::value>> - constexpr bool operator !=(T other) const noexcept - { - return static_cast(-1) != other; - } + template ::value>> + constexpr bool operator==(T other) const noexcept + { + return static_cast(-1) == other; + } + template ::value>> + constexpr bool operator!=(T other) const noexcept + { + return static_cast(-1) != other; + } }; template ::value>> -constexpr bool operator ==(T left, static_bounds_dynamic_range_t right) noexcept +constexpr bool operator==(T left, static_bounds_dynamic_range_t right) noexcept { - return right == left; + return right == left; } template ::value>> -constexpr bool operator !=(T left, static_bounds_dynamic_range_t right) noexcept +constexpr bool operator!=(T left, static_bounds_dynamic_range_t right) noexcept { - return right != left; + return right != left; } constexpr static_bounds_dynamic_range_t dynamic_range{}; @@ -494,826 +508,873 @@ constexpr static_bounds_dynamic_range_t dynamic_range{}; const char dynamic_range = -1; #endif -struct generalized_mapping_tag {}; -struct contiguous_mapping_tag : generalized_mapping_tag {}; +struct generalized_mapping_tag +{ +}; +struct contiguous_mapping_tag : generalized_mapping_tag +{ +}; namespace details { - template - struct StaticSizeHelperImpl - { - static_assert(static_cast(Fact1) * static_cast(Fact2) <= SizeTypeTraits::max_value, "Value out of the range of SizeType"); - static const SizeType value = Fact1 * Fact2; - }; +template +struct StaticSizeHelperImpl +{ + static_assert(static_cast(Fact1) * static_cast(Fact2) <= SizeTypeTraits::max_value, + "Value out of the range of SizeType"); + static const SizeType value = Fact1 * Fact2; +}; - template - struct StaticSizeHelperImpl - { - static const SizeType value = ConstBound; - }; +template +struct StaticSizeHelperImpl +{ + static const SizeType value = ConstBound; +}; - template - struct StaticSizeHelperImpl - { - static const SizeType value = ConstBound; - }; +template +struct StaticSizeHelperImpl +{ + static const SizeType value = ConstBound; +}; - template - struct StaticSizeHelperImpl - { - static const SizeType value = static_cast(ConstBound); - }; +template +struct StaticSizeHelperImpl +{ + static const SizeType value = static_cast(ConstBound); +}; - template - struct StaticSizeHelper - { - static const SizeType value = StaticSizeHelperImpl(Fact1), static_cast(Fact2), static_cast(dynamic_range)>::value; - }; +template struct StaticSizeHelper +{ + static const SizeType value = + StaticSizeHelperImpl(Fact1), static_cast(Fact2), static_cast(dynamic_range)>::value; +}; - template - struct LessThan - { - static const bool value = Left < Right; - }; +template struct LessThan +{ + static const bool value = Left < Right; +}; - template - struct BoundsRanges { - static const size_t Depth = 0; - static const size_t DynamicNum = 0; - static const SizeType CurrentRange = 1; - static const SizeType TotalSize = 1; +template struct BoundsRanges +{ + static const size_t Depth = 0; + static const size_t DynamicNum = 0; + static const SizeType CurrentRange = 1; + static const SizeType TotalSize = 1; - BoundsRanges (const BoundsRanges &) = default; + BoundsRanges(const BoundsRanges&) = default; - // TODO : following signature is for work around VS bug - template - BoundsRanges (const OtherType &, bool /* firstLevel */) {} - BoundsRanges(const SizeType * const) { } - BoundsRanges() = default; + // TODO : following signature is for work around VS bug + template BoundsRanges(const OtherType&, bool /* firstLevel */) + { + } + BoundsRanges(const SizeType* const) + { + } + BoundsRanges() = default; - template - void serialize(T &) const { - } - template - SizeType linearize(const T &) const { - return 0; - } - template - ptrdiff_t contains(const T &) const { - return 0; - } + template void serialize(T&) const + { + } + template SizeType linearize(const T&) const + { + return 0; + } + template ptrdiff_t contains(const T&) const + { + return 0; + } - size_t totalSize() const _NOEXCEPT { - return TotalSize; - } + size_t totalSize() const _NOEXCEPT + { + return TotalSize; + } - bool operator == (const BoundsRanges &) const _NOEXCEPT - { - return true; - } - }; + bool operator==(const BoundsRanges&) const _NOEXCEPT + { + return true; + } +}; - template - struct BoundsRanges : BoundsRanges{ - using Base = BoundsRanges ; - static const size_t Depth = Base::Depth + 1; - static const size_t DynamicNum = Base::DynamicNum + 1; - static const SizeType CurrentRange = dynamic_range; - static const SizeType TotalSize = dynamic_range; - const SizeType m_bound; +template +struct BoundsRanges : BoundsRanges +{ + using Base = BoundsRanges; + static const size_t Depth = Base::Depth + 1; + static const size_t DynamicNum = Base::DynamicNum + 1; + static const SizeType CurrentRange = dynamic_range; + static const SizeType TotalSize = dynamic_range; + const SizeType m_bound; - BoundsRanges (const BoundsRanges &) = default; - BoundsRanges(const SizeType * const arr) : Base(arr + 1), m_bound(static_cast(*arr * this->Base::totalSize())) - { - fail_fast_assert(0 <= *arr); - fail_fast_assert(*arr * this->Base::totalSize() <= details::SizeTypeTraits::max_value); - } - BoundsRanges() : m_bound(0) {} + BoundsRanges(const BoundsRanges&) = default; + BoundsRanges(const SizeType* const arr) + : Base(arr + 1), m_bound(static_cast(*arr * this->Base::totalSize())) + { + fail_fast_assert(0 <= *arr); + fail_fast_assert(*arr * this->Base::totalSize() <= details::SizeTypeTraits::max_value); + } + BoundsRanges() : m_bound(0) + { + } - template - BoundsRanges(const BoundsRanges &other, bool /* firstLevel */ = true) : - Base(static_cast&>(other), false), m_bound (static_cast(other.totalSize())) - { - } + template + BoundsRanges(const BoundsRanges& other, + bool /* firstLevel */ = true) + : Base(static_cast&>(other), false), + m_bound(static_cast(other.totalSize())) + { + } - template - void serialize(T & arr) const { - arr[Dim] = elementNum(); - this->Base::template serialize(arr); - } - template - SizeType linearize(const T & arr) const { - const size_t index = this->Base::totalSize() * arr[Dim]; - fail_fast_assert(index < static_cast(m_bound)); - return static_cast(index) + this->Base::template linearize(arr); - } - - template - ptrdiff_t contains(const T & arr) const { - const ptrdiff_t last = this->Base::template contains(arr); - if (last == -1) - return -1; - const ptrdiff_t cur = this->Base::totalSize() * arr[Dim]; - return static_cast(cur) < static_cast(m_bound) ? cur + last : -1; - } - - size_t totalSize() const _NOEXCEPT { - return m_bound; - } - - SizeType elementNum() const _NOEXCEPT { - return static_cast(totalSize() / this->Base::totalSize()); - } - - SizeType elementNum(size_t dim) const _NOEXCEPT{ - if (dim > 0) - return this->Base::elementNum(dim - 1); - else - return elementNum(); - } + template void serialize(T& arr) const + { + arr[Dim] = elementNum(); + this->Base::template serialize(arr); + } + template SizeType linearize(const T& arr) const + { + const size_t index = this->Base::totalSize() * arr[Dim]; + fail_fast_assert(index < static_cast(m_bound)); + return static_cast(index) + this->Base::template linearize(arr); + } - bool operator == (const BoundsRanges & rhs) const _NOEXCEPT - { - return m_bound == rhs.m_bound && static_cast(*this) == static_cast(rhs); - } - }; + template ptrdiff_t contains(const T& arr) const + { + const ptrdiff_t last = this->Base::template contains(arr); + if (last == -1) return -1; + const ptrdiff_t cur = this->Base::totalSize() * arr[Dim]; + return static_cast(cur) < static_cast(m_bound) ? cur + last : -1; + } - template - struct BoundsRanges : BoundsRanges{ - using Base = BoundsRanges ; - static const size_t Depth = Base::Depth + 1; - static const size_t DynamicNum = Base::DynamicNum; - static const SizeType CurrentRange = static_cast(CurRange); - static const SizeType TotalSize = StaticSizeHelper::value; - static_assert (CurRange <= SizeTypeTraits::max_value, "CurRange must be smaller than SizeType limits"); + size_t totalSize() const _NOEXCEPT + { + return m_bound; + } - BoundsRanges (const BoundsRanges &) = default; - BoundsRanges(const SizeType * const arr) : Base(arr) { } - BoundsRanges() = default; + SizeType elementNum() const _NOEXCEPT + { + return static_cast(totalSize() / this->Base::totalSize()); + } - template - BoundsRanges(const BoundsRanges &other, bool firstLevel = true) : Base(static_cast&>(other), false) - { - fail_fast_assert((firstLevel && totalSize() <= other.totalSize()) || totalSize() == other.totalSize()); - } + SizeType elementNum(size_t dim) const _NOEXCEPT + { + if (dim > 0) + return this->Base::elementNum(dim - 1); + else + return elementNum(); + } - template - void serialize(T & arr) const { - arr[Dim] = elementNum(); - this->Base::template serialize(arr); - } + bool operator==(const BoundsRanges& rhs) const _NOEXCEPT + { + return m_bound == rhs.m_bound && static_cast(*this) == static_cast(rhs); + } +}; - template - SizeType linearize(const T & arr) const { - fail_fast_assert(arr[Dim] < CurrentRange, "Index is out of range"); - return static_cast(this->Base::totalSize()) * arr[Dim] + this->Base::template linearize(arr); - } +template +struct BoundsRanges : BoundsRanges +{ + using Base = BoundsRanges; + static const size_t Depth = Base::Depth + 1; + static const size_t DynamicNum = Base::DynamicNum; + static const SizeType CurrentRange = static_cast(CurRange); + static const SizeType TotalSize = StaticSizeHelper::value; + static_assert(CurRange <= SizeTypeTraits::max_value, + "CurRange must be smaller than SizeType limits"); - template - ptrdiff_t contains(const T & arr) const { - if (static_cast(arr[Dim]) >= CurrentRange) - return -1; - const ptrdiff_t last = this->Base::template contains(arr); - if (last == -1) - return -1; - return static_cast(this->Base::totalSize() * arr[Dim]) + last; - } + BoundsRanges(const BoundsRanges&) = default; + BoundsRanges(const SizeType* const arr) : Base(arr) + { + } + BoundsRanges() = default; - size_t totalSize() const _NOEXCEPT{ - return CurrentRange * this->Base::totalSize(); - } + template + BoundsRanges(const BoundsRanges& other, bool firstLevel = true) + : Base(static_cast&>(other), false) + { + fail_fast_assert((firstLevel && totalSize() <= other.totalSize()) || totalSize() == other.totalSize()); + } - SizeType elementNum() const _NOEXCEPT{ - return CurrentRange; - } + template void serialize(T& arr) const + { + arr[Dim] = elementNum(); + this->Base::template serialize(arr); + } - SizeType elementNum(size_t dim) const _NOEXCEPT{ - if (dim > 0) - return this->Base::elementNum(dim - 1); - else - return elementNum(); - } + template SizeType linearize(const T& arr) const + { + fail_fast_assert(arr[Dim] < CurrentRange, "Index is out of range"); + return static_cast(this->Base::totalSize()) * arr[Dim] + + this->Base::template linearize(arr); + } - bool operator == (const BoundsRanges & rhs) const _NOEXCEPT - { - return static_cast(*this) == static_cast(rhs); - } - }; + template ptrdiff_t contains(const T& arr) const + { + if (static_cast(arr[Dim]) >= CurrentRange) return -1; + const ptrdiff_t last = this->Base::template contains(arr); + if (last == -1) return -1; + return static_cast(this->Base::totalSize() * arr[Dim]) + last; + } - template - struct BoundsRangeConvertible2; + size_t totalSize() const _NOEXCEPT + { + return CurrentRange * this->Base::totalSize(); + } - // TODO: I have to rewrite BoundsRangeConvertible into following way to workaround VS 2013 bugs - template > - auto helpBoundsRangeConvertible(SourceType, TargetType, std::true_type) -> Ret; + SizeType elementNum() const _NOEXCEPT + { + return CurrentRange; + } - template - auto helpBoundsRangeConvertible(SourceType, TargetType, ...) -> std::false_type; - - template - struct BoundsRangeConvertible2 : decltype(helpBoundsRangeConvertible(SourceType(), TargetType(), - std::integral_constant())) - {}; + SizeType elementNum(size_t dim) const _NOEXCEPT + { + if (dim > 0) + return this->Base::elementNum(dim - 1); + else + return elementNum(); + } - template - struct BoundsRangeConvertible2 : std::true_type {}; + bool operator==(const BoundsRanges& rhs) const _NOEXCEPT + { + return static_cast(*this) == static_cast(rhs); + } +}; - template - struct BoundsRangeConvertible : decltype(helpBoundsRangeConvertible(SourceType(), TargetType(), - std::integral_constant::value || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>())) - {}; - template - struct BoundsRangeConvertible : std::true_type {}; +template struct BoundsRangeConvertible2; - template - struct TypeListIndexer - { - const TypeChain & obj; - TypeListIndexer(const TypeChain & obj) :obj(obj){} - template - const TypeChain & getObj(std::true_type) - { - return obj; - } - template - auto getObj(std::false_type) -> decltype(TypeListIndexer(static_cast(obj)).template get()) - { - return TypeListIndexer(static_cast(obj)).template get(); - } - template - auto get() -> decltype(getObj(std::integral_constant())) - { - return getObj(std::integral_constant()); - } - }; +// TODO: I have to rewrite BoundsRangeConvertible into following way to workaround VS 2013 bugs +template > +auto helpBoundsRangeConvertible(SourceType, TargetType, std::true_type) -> Ret; - template - TypeListIndexer createTypeListIndexer(const TypeChain &obj) - { - return TypeListIndexer(obj); - } +template +auto helpBoundsRangeConvertible(SourceType, TargetType, ...) -> std::false_type; + +template +struct BoundsRangeConvertible2 +: decltype(helpBoundsRangeConvertible( + SourceType(), + TargetType(), + std::integral_constant())) +{ +}; + +template +struct BoundsRangeConvertible2 : std::true_type +{ +}; + +template +struct BoundsRangeConvertible +: decltype(helpBoundsRangeConvertible( + SourceType(), + TargetType(), + std::integral_constant::value || TargetType::CurrentRange == dynamic_range || + SourceType::CurrentRange == dynamic_range)>())) +{ +}; +template +struct BoundsRangeConvertible : std::true_type +{ +}; + +template struct TypeListIndexer +{ + const TypeChain& obj; + TypeListIndexer(const TypeChain& obj) : obj(obj) + { + } + template const TypeChain& getObj(std::true_type) + { + return obj; + } + template + auto getObj(std::false_type) + -> decltype(TypeListIndexer(static_cast(obj)).template get()) + { + return TypeListIndexer(static_cast(obj)).template get(); + } + template auto get() -> decltype(getObj(std::integral_constant())) + { + return getObj(std::integral_constant()); + } +}; + +template TypeListIndexer createTypeListIndexer(const TypeChain& obj) +{ + return TypeListIndexer(obj); +} } -template -class bounds_iterator; +template class bounds_iterator; -template -class static_bounds { -public: - static_bounds(const details::BoundsRanges &) { - } +template class static_bounds +{ + public: + static_bounds(const details::BoundsRanges&) + { + } }; template class static_bounds { - using MyRanges = details::BoundsRanges ; - static_assert(std::is_integral::value - && details::SizeTypeTraits::max_value <= SIZE_MAX, "SizeType must be an integral type and its numeric limits must be smaller than SIZE_MAX"); + using MyRanges = details::BoundsRanges; + static_assert( + std::is_integral::value && details::SizeTypeTraits::max_value <= SIZE_MAX, + "SizeType must be an integral type and its numeric limits must be smaller than SIZE_MAX"); - MyRanges m_ranges; - constexpr static_bounds(const MyRanges & range) : m_ranges(range) { } - - template - friend class static_bounds; -public: - static const size_t rank = MyRanges::Depth; - static const size_t dynamic_rank = MyRanges::DynamicNum; - static const SizeType static_size = static_cast(MyRanges::TotalSize); + MyRanges m_ranges; + constexpr static_bounds(const MyRanges& range) : m_ranges(range) + { + } - using size_type = SizeType; - using index_type = index; - using iterator = bounds_iterator; - using const_iterator = bounds_iterator; - using difference_type = ptrdiff_t; - using sliced_type = static_bounds; - using mapping_type = contiguous_mapping_tag; -public: - constexpr static_bounds(const static_bounds &) = default; - - template , details::BoundsRanges >::value>> - constexpr static_bounds(const static_bounds &other): - m_ranges(other.m_ranges) - { - } + template friend class static_bounds; - constexpr static_bounds(std::initializer_list il) : m_ranges(il.begin()) - { - fail_fast_assert(MyRanges::DynamicNum == il.size(), "Size of the initializer list must match the rank of the array"); - fail_fast_assert(m_ranges.totalSize() <= details::SizeTypeTraits::max_value, "Size of the range is larger than the max element of the size type"); - } - - constexpr static_bounds() = default; + public: + static const size_t rank = MyRanges::Depth; + static const size_t dynamic_rank = MyRanges::DynamicNum; + static const SizeType static_size = static_cast(MyRanges::TotalSize); - constexpr static_bounds & operator = (const static_bounds & otherBounds) - { - new(&m_ranges) MyRanges (otherBounds.m_ranges); - return *this; - } + using size_type = SizeType; + using index_type = index; + using iterator = bounds_iterator; + using const_iterator = bounds_iterator; + using difference_type = ptrdiff_t; + using sliced_type = static_bounds; + using mapping_type = contiguous_mapping_tag; - constexpr sliced_type slice() const _NOEXCEPT - { - return sliced_type{static_cast &>(m_ranges)}; - } + public: + constexpr static_bounds(const static_bounds&) = default; - constexpr size_type stride() const _NOEXCEPT - { - return rank > 1 ? slice().size() : 1; - } - - constexpr size_type size() const _NOEXCEPT - { - return static_cast(m_ranges.totalSize()); - } + template , details::BoundsRanges>::value>> + constexpr static_bounds(const static_bounds& other) + : m_ranges(other.m_ranges) + { + } - constexpr size_type total_size() const _NOEXCEPT - { - return static_cast(m_ranges.totalSize()); - } - - constexpr size_type linearize(const index_type & idx) const - { - return m_ranges.linearize(idx); - } - - constexpr bool contains(const index_type& idx) const _NOEXCEPT - { - return m_ranges.contains(idx) != -1; - } - - constexpr size_type operator[](size_t index) const _NOEXCEPT - { - return m_ranges.elementNum(index); - } - - template - constexpr size_type extent() const _NOEXCEPT - { - static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)"); - return details::createTypeListIndexer(m_ranges).template get().elementNum(); - } - - constexpr index_type index_bounds() const _NOEXCEPT - { - index_type extents; - m_ranges.serialize(extents); - return extents; - } - - template - constexpr bool operator == (const static_bounds & rhs) const _NOEXCEPT - { - return this->size() == rhs.size(); - } - - template - constexpr bool operator != (const static_bounds & rhs) const _NOEXCEPT - { - return !(*this == rhs); - } - - constexpr const_iterator begin() const _NOEXCEPT - { - return const_iterator(*this); - } - - constexpr const_iterator end() const _NOEXCEPT - { - index_type boundary; - m_ranges.serialize(boundary); - return const_iterator(*this, this->index_bounds()); - } + constexpr static_bounds(std::initializer_list il) : m_ranges(il.begin()) + { + fail_fast_assert(MyRanges::DynamicNum == il.size(), + "Size of the initializer list must match the rank of the array"); + fail_fast_assert(m_ranges.totalSize() <= details::SizeTypeTraits::max_value, + "Size of the range is larger than the max element of the size type"); + } + + constexpr static_bounds() = default; + + constexpr static_bounds& operator=(const static_bounds& otherBounds) + { + new (&m_ranges) MyRanges(otherBounds.m_ranges); + return *this; + } + + constexpr sliced_type slice() const _NOEXCEPT + { + return sliced_type{ static_cast&>(m_ranges) }; + } + + constexpr size_type stride() const _NOEXCEPT + { + return rank > 1 ? slice().size() : 1; + } + + constexpr size_type size() const _NOEXCEPT + { + return static_cast(m_ranges.totalSize()); + } + + constexpr size_type total_size() const _NOEXCEPT + { + return static_cast(m_ranges.totalSize()); + } + + constexpr size_type linearize(const index_type& idx) const + { + return m_ranges.linearize(idx); + } + + constexpr bool contains(const index_type& idx) const _NOEXCEPT + { + return m_ranges.contains(idx) != -1; + } + + constexpr size_type operator[](size_t index) const _NOEXCEPT + { + return m_ranges.elementNum(index); + } + + template constexpr size_type extent() const _NOEXCEPT + { + static_assert(Dim < rank, + "dimension should be less than rank (dimension count starts from 0)"); + return details::createTypeListIndexer(m_ranges).template get().elementNum(); + } + + constexpr index_type index_bounds() const _NOEXCEPT + { + index_type extents; + m_ranges.serialize(extents); + return extents; + } + + template + constexpr bool operator==(const static_bounds& rhs) const _NOEXCEPT + { + return this->size() == rhs.size(); + } + + template + constexpr bool operator!=(const static_bounds& rhs) const _NOEXCEPT + { + return !(*this == rhs); + } + + constexpr const_iterator begin() const _NOEXCEPT + { + return const_iterator(*this); + } + + constexpr const_iterator end() const _NOEXCEPT + { + index_type boundary; + m_ranges.serialize(boundary); + return const_iterator(*this, this->index_bounds()); + } }; template class strided_bounds : private details::coordinate_facade, SizeType, Rank> { - using Base = details::coordinate_facade, SizeType, Rank>; - friend Base; - template - friend class strided_bounds; + using Base = details::coordinate_facade, SizeType, Rank>; + friend Base; + template friend class strided_bounds; -public: - using Base::rank; - using reference = typename Base::reference; - using const_reference = typename Base::const_reference; - using size_type = typename Base::value_type; - using difference_type = typename Base::value_type; - using value_type = typename Base::value_type; - using index_type = index; - using iterator = bounds_iterator; - using const_iterator = bounds_iterator; - static const int dynamic_rank = rank; - static const size_t static_size = dynamic_range; - using sliced_type = std::conditional_t, void>; - using mapping_type = generalized_mapping_tag; - constexpr strided_bounds(const strided_bounds &) = default; + public: + using Base::rank; + using reference = typename Base::reference; + using const_reference = typename Base::const_reference; + using size_type = typename Base::value_type; + using difference_type = typename Base::value_type; + using value_type = typename Base::value_type; + using index_type = index; + using iterator = bounds_iterator; + using const_iterator = bounds_iterator; + static const int dynamic_rank = rank; + static const size_t static_size = dynamic_range; + using sliced_type = std::conditional_t, void>; + using mapping_type = generalized_mapping_tag; + constexpr strided_bounds(const strided_bounds&) = default; - template - constexpr strided_bounds(const strided_bounds &other) - : Base(other), m_strides(other.strides) - { - } + template + constexpr strided_bounds(const strided_bounds& other) + : Base(other), m_strides(other.strides) + { + } - constexpr strided_bounds(const index_type &extents, const index_type &strides) - : m_strides(strides) - { - for (size_t i = 0; i < rank; i++) - Base::elems[i] = extents[i]; - } - constexpr strided_bounds(const value_type(&values)[rank], index_type strides) - : Base(values), m_strides(std::move(strides)) - { - } - constexpr index_type strides() const _NOEXCEPT - { - return m_strides; - } - constexpr size_type total_size() const _NOEXCEPT - { - size_type ret = 0; - for (size_t i = 0; i < rank; ++i) - ret += (Base::elems[i] - 1) * m_strides[i]; - return ret + 1; - } - constexpr size_type size() const _NOEXCEPT - { - size_type ret = 1; - for (size_t i = 0; i < rank; ++i) - ret *= Base::elems[i]; - return ret; - } - constexpr bool contains(const index_type& idx) const _NOEXCEPT - { - for (size_t i = 0; i < rank; ++i) - { - if (idx[i] < 0 || idx[i] >= Base::elems[i]) - return false; - } - return true; - } - constexpr size_type linearize(const index_type & idx) const - { - size_type ret = 0; - for (size_t i = 0; i < rank; i++) - { - fail_fast_assert(idx[i] < Base::elems[i], "index is out of bounds of the array"); - ret += idx[i] * m_strides[i]; - } - return ret; - } - constexpr size_type stride() const _NOEXCEPT - { - return m_strides[0]; - } - template 1), typename Ret = std::enable_if_t> - constexpr sliced_type slice() const - { - return{ (value_type(&)[rank - 1])Base::elems[1], sliced_type::index_type::shift_left(m_strides) }; - } - template - constexpr size_type extent() const _NOEXCEPT - { - static_assert(Dim < Rank, "dimension should be less than rank (dimension count starts from 0)"); - return Base::elems[Dim]; - } - constexpr index_type index_bounds() const _NOEXCEPT - { - return index_type(Base::elems); - } - const_iterator begin() const _NOEXCEPT - { - return const_iterator{ *this }; - } - const_iterator end() const _NOEXCEPT - { - return const_iterator{ *this, index_bounds() }; - } -private: - index_type m_strides; + constexpr strided_bounds(const index_type& extents, const index_type& strides) + : m_strides(strides) + { + for (size_t i = 0; i < rank; i++) + Base::elems[i] = extents[i]; + } + constexpr strided_bounds(const value_type(&values)[rank], index_type strides) + : Base(values), m_strides(std::move(strides)) + { + } + constexpr index_type strides() const _NOEXCEPT + { + return m_strides; + } + constexpr size_type total_size() const _NOEXCEPT + { + size_type ret = 0; + for (size_t i = 0; i < rank; ++i) + ret += (Base::elems[i] - 1) * m_strides[i]; + return ret + 1; + } + constexpr size_type size() const _NOEXCEPT + { + size_type ret = 1; + for (size_t i = 0; i < rank; ++i) + ret *= Base::elems[i]; + return ret; + } + constexpr bool contains(const index_type& idx) const _NOEXCEPT + { + for (size_t i = 0; i < rank; ++i) + { + if (idx[i] < 0 || idx[i] >= Base::elems[i]) return false; + } + return true; + } + constexpr size_type linearize(const index_type& idx) const + { + size_type ret = 0; + for (size_t i = 0; i < rank; i++) + { + fail_fast_assert(idx[i] < Base::elems[i], "index is out of bounds of the array"); + ret += idx[i] * m_strides[i]; + } + return ret; + } + constexpr size_type stride() const _NOEXCEPT + { + return m_strides[0]; + } + template 1), typename Ret = std::enable_if_t> + constexpr sliced_type slice() const + { + return { (value_type(&)[rank - 1])Base::elems[1], sliced_type::index_type::shift_left(m_strides) }; + } + template constexpr size_type extent() const _NOEXCEPT + { + static_assert(Dim < Rank, + "dimension should be less than rank (dimension count starts from 0)"); + return Base::elems[Dim]; + } + constexpr index_type index_bounds() const _NOEXCEPT + { + return index_type(Base::elems); + } + const_iterator begin() const _NOEXCEPT + { + return const_iterator{ *this }; + } + const_iterator end() const _NOEXCEPT + { + return const_iterator{ *this, index_bounds() }; + } + + private: + index_type m_strides; }; -template -struct is_bounds : std::integral_constant {}; +template struct is_bounds : std::integral_constant +{ +}; template -struct is_bounds> : std::integral_constant {}; +struct is_bounds> : std::integral_constant +{ +}; template -struct is_bounds> : std::integral_constant {}; +struct is_bounds> : std::integral_constant +{ +}; template class bounds_iterator - : public std::iterator, - const IndexType> +: public std::iterator, const IndexType> { -private: - using Base = std::iterator , const IndexType>; -public: - static const size_t rank = IndexType::rank; - using typename Base::reference; - using typename Base::pointer; - using typename Base::difference_type; - using typename Base::value_type; - using index_type = value_type; - using index_size_type = typename IndexType::size_type; - template - explicit bounds_iterator(const Bounds & bnd, value_type curr = value_type{}) _NOEXCEPT - : boundary(bnd.index_bounds()) - , curr( std::move(curr) ) - { - static_assert(is_bounds::value, "Bounds type must be provided"); - } - reference operator*() const _NOEXCEPT - { - return curr; - } - pointer operator->() const _NOEXCEPT - { - return details::arrow_proxy{ curr }; - } - bounds_iterator& operator++() _NOEXCEPT - { - for (size_t i = rank; i-- > 0;) - { - if (++curr[i] < boundary[i]) - { - return *this; - } - else - { - curr[i] = 0; - } - } - // If we're here we've wrapped over - set to past-the-end. - for (size_t i = 0; i < rank; ++i) - { - curr[i] = boundary[i]; - } - return *this; - } - bounds_iterator operator++(int) _NOEXCEPT - { - auto ret = *this; - ++(*this); - return ret; - } - bounds_iterator& operator--() _NOEXCEPT - { - for (size_t i = rank; i-- > 0;) - { - if (curr[i]-- > 0) - { - return *this; - } - else - { - curr[i] = boundary[i] - 1; - } - } - // If we're here the preconditions were violated - // "pre: there exists s such that r == ++s" - fail_fast_assert(false); - return *this; - } - bounds_iterator operator--(int) _NOEXCEPT - { - auto ret = *this; - --(*this); - return ret; - } - bounds_iterator operator+(difference_type n) const _NOEXCEPT - { - bounds_iterator ret{ *this }; - return ret += n; - } - bounds_iterator& operator+=(difference_type n) _NOEXCEPT - { - auto linear_idx = linearize(curr) + n; - value_type stride; - stride[rank - 1] = 1; - for (size_t i = rank - 1; i-- > 0;) - { - stride[i] = stride[i + 1] * boundary[i + 1]; - } - for (size_t i = 0; i < rank; ++i) - { - curr[i] = linear_idx / stride[i]; - linear_idx = linear_idx % stride[i]; - } - return *this; - } - bounds_iterator operator-(difference_type n) const _NOEXCEPT - { - bounds_iterator ret{ *this }; - return ret -= n; - } - bounds_iterator& operator-=(difference_type n) _NOEXCEPT - { - return *this += -n; - } - difference_type operator-(const bounds_iterator& rhs) const _NOEXCEPT - { - return linearize(curr) - linearize(rhs.curr); - } - reference operator[](difference_type n) const _NOEXCEPT - { - return *(*this + n); - } - bool operator==(const bounds_iterator& rhs) const _NOEXCEPT - { - return curr == rhs.curr; - } - bool operator!=(const bounds_iterator& rhs) const _NOEXCEPT - { - return !(*this == rhs); - } - bool operator<(const bounds_iterator& rhs) const _NOEXCEPT - { - for (size_t i = 0; i < rank; ++i) - { - if (curr[i] < rhs.curr[i]) - return true; - } - return false; - } - bool operator<=(const bounds_iterator& rhs) const _NOEXCEPT - { - return !(rhs < *this); - } - bool operator>(const bounds_iterator& rhs) const _NOEXCEPT - { - return rhs < *this; - } - bool operator>=(const bounds_iterator& rhs) const _NOEXCEPT - { - return !(rhs > *this); - } - void swap(bounds_iterator& rhs) _NOEXCEPT - { - std::swap(boundary, rhs.boundary); - std::swap(curr, rhs.curr); - } -private: - index_size_type linearize(const value_type& idx) const _NOEXCEPT - { - // TODO: Smarter impl. - // Check if past-the-end - bool pte = true; - for (size_t i = 0; i < rank; ++i) - { - if (idx[i] != boundary[i]) - { - pte = false; - break; - } - } - index_size_type multiplier = 1; - index_size_type res = 0; - if (pte) - { - res = 1; - for (size_t i = rank; i-- > 0;) - { - res += (idx[i] - 1) * multiplier; - multiplier *= boundary[i]; - } - } - else - { - for (size_t i = rank; i-- > 0;) - { - res += idx[i] * multiplier; - multiplier *= boundary[i]; - } - } - return res; - } - value_type boundary; - value_type curr; + private: + using Base = + std::iterator, const IndexType>; + + public: + static const size_t rank = IndexType::rank; + using typename Base::reference; + using typename Base::pointer; + using typename Base::difference_type; + using typename Base::value_type; + using index_type = value_type; + using index_size_type = typename IndexType::size_type; + template + explicit bounds_iterator(const Bounds& bnd, value_type curr = value_type{}) _NOEXCEPT + : boundary(bnd.index_bounds()), + curr(std::move(curr)) + { + static_assert(is_bounds::value, "Bounds type must be provided"); + } + reference operator*() const _NOEXCEPT + { + return curr; + } + pointer operator->() const _NOEXCEPT + { + return details::arrow_proxy{ curr }; + } + bounds_iterator& operator++() _NOEXCEPT + { + for (size_t i = rank; i-- > 0;) + { + if (++curr[i] < boundary[i]) + { + return *this; + } + else + { + curr[i] = 0; + } + } + // If we're here we've wrapped over - set to past-the-end. + for (size_t i = 0; i < rank; ++i) + { + curr[i] = boundary[i]; + } + return *this; + } + bounds_iterator operator++(int)_NOEXCEPT + { + auto ret = *this; + ++(*this); + return ret; + } + bounds_iterator& operator--() _NOEXCEPT + { + for (size_t i = rank; i-- > 0;) + { + if (curr[i]-- > 0) + { + return *this; + } + else + { + curr[i] = boundary[i] - 1; + } + } + // If we're here the preconditions were violated + // "pre: there exists s such that r == ++s" + fail_fast_assert(false); + return *this; + } + bounds_iterator operator--(int)_NOEXCEPT + { + auto ret = *this; + --(*this); + return ret; + } + bounds_iterator operator+(difference_type n) const _NOEXCEPT + { + bounds_iterator ret{ *this }; + return ret += n; + } + bounds_iterator& operator+=(difference_type n) _NOEXCEPT + { + auto linear_idx = linearize(curr) + n; + value_type stride; + stride[rank - 1] = 1; + for (size_t i = rank - 1; i-- > 0;) + { + stride[i] = stride[i + 1] * boundary[i + 1]; + } + for (size_t i = 0; i < rank; ++i) + { + curr[i] = linear_idx / stride[i]; + linear_idx = linear_idx % stride[i]; + } + return *this; + } + bounds_iterator operator-(difference_type n) const _NOEXCEPT + { + bounds_iterator ret{ *this }; + return ret -= n; + } + bounds_iterator& operator-=(difference_type n) _NOEXCEPT + { + return * this += -n; + } + difference_type operator-(const bounds_iterator& rhs) const _NOEXCEPT + { + return linearize(curr) - linearize(rhs.curr); + } + reference operator[](difference_type n) const _NOEXCEPT + { + return *(*this + n); + } + bool operator==(const bounds_iterator& rhs) const _NOEXCEPT + { + return curr == rhs.curr; + } + bool operator!=(const bounds_iterator& rhs) const _NOEXCEPT + { + return !(*this == rhs); + } + bool operator<(const bounds_iterator& rhs) const _NOEXCEPT + { + for (size_t i = 0; i < rank; ++i) + { + if (curr[i] < rhs.curr[i]) return true; + } + return false; + } + bool operator<=(const bounds_iterator& rhs) const _NOEXCEPT + { + return !(rhs < *this); + } + bool operator>(const bounds_iterator& rhs) const _NOEXCEPT + { + return rhs < *this; + } + bool operator>=(const bounds_iterator& rhs) const _NOEXCEPT + { + return !(rhs > *this); + } + void swap(bounds_iterator& rhs) _NOEXCEPT + { + std::swap(boundary, rhs.boundary); + std::swap(curr, rhs.curr); + } + + private: + index_size_type linearize(const value_type& idx) const _NOEXCEPT + { + // TODO: Smarter impl. + // Check if past-the-end + bool pte = true; + for (size_t i = 0; i < rank; ++i) + { + if (idx[i] != boundary[i]) + { + pte = false; + break; + } + } + index_size_type multiplier = 1; + index_size_type res = 0; + if (pte) + { + res = 1; + for (size_t i = rank; i-- > 0;) + { + res += (idx[i] - 1) * multiplier; + multiplier *= boundary[i]; + } + } + else + { + for (size_t i = rank; i-- > 0;) + { + res += idx[i] * multiplier; + multiplier *= boundary[i]; + } + } + return res; + } + value_type boundary; + value_type curr; }; template class bounds_iterator> - : public std::iterator, - ptrdiff_t, - const details::arrow_proxy>, - const index<1, SizeType>> +: public std::iterator, ptrdiff_t, const details::arrow_proxy>, const index<1, SizeType>> { - using Base = std::iterator, ptrdiff_t, const details::arrow_proxy>, const index<1, SizeType>>; + using Base = + std::iterator, ptrdiff_t, const details::arrow_proxy>, const index<1, SizeType>>; -public: - using typename Base::reference; - using typename Base::pointer; - using typename Base::difference_type; - using typename Base::value_type; - using index_type = value_type; - using index_size_type = typename index_type::size_type; + public: + using typename Base::reference; + using typename Base::pointer; + using typename Base::difference_type; + using typename Base::value_type; + using index_type = value_type; + using index_size_type = typename index_type::size_type; - template - explicit bounds_iterator(const Bounds &, value_type curr = value_type{}) _NOEXCEPT - : curr( std::move(curr) ) - {} - reference operator*() const _NOEXCEPT - { - return curr; - } - pointer operator->() const _NOEXCEPT - { - return details::arrow_proxy{ curr }; - } - bounds_iterator& operator++() _NOEXCEPT - { - ++curr; - return *this; - } - bounds_iterator operator++(int) _NOEXCEPT - { - auto ret = *this; - ++(*this); - return ret; - } - bounds_iterator& operator--() _NOEXCEPT - { - curr--; - return *this; - } - bounds_iterator operator--(int) _NOEXCEPT - { - auto ret = *this; - --(*this); - return ret; - } - bounds_iterator operator+(difference_type n) const _NOEXCEPT - { - bounds_iterator ret{ *this }; - return ret += n; - } - bounds_iterator& operator+=(difference_type n) _NOEXCEPT - { - curr += n; - return *this; - } - bounds_iterator operator-(difference_type n) const _NOEXCEPT - { - bounds_iterator ret{ *this }; - return ret -= n; - } - bounds_iterator& operator-=(difference_type n) _NOEXCEPT - { - return *this += -n; - } - difference_type operator-(const bounds_iterator& rhs) const _NOEXCEPT - { - return curr[0] - rhs.curr[0]; - } - reference operator[](difference_type n) const _NOEXCEPT - { - return curr + n; - } - bool operator==(const bounds_iterator& rhs) const _NOEXCEPT - { - return curr == rhs.curr; - } - bool operator!=(const bounds_iterator& rhs) const _NOEXCEPT - { - return !(*this == rhs); - } - bool operator<(const bounds_iterator& rhs) const _NOEXCEPT - { - return curr[0] < rhs.curr[0]; - } - bool operator<=(const bounds_iterator& rhs) const _NOEXCEPT - { - return !(rhs < *this); - } - bool operator>(const bounds_iterator& rhs) const _NOEXCEPT - { - return rhs < *this; - } - bool operator>=(const bounds_iterator& rhs) const _NOEXCEPT - { - return !(rhs > *this); - } - void swap(bounds_iterator& rhs) _NOEXCEPT - { - std::swap(curr, rhs.curr); - } -private: - value_type curr; + template + explicit bounds_iterator(const Bounds&, value_type curr = value_type{}) _NOEXCEPT + : curr(std::move(curr)) + { + } + reference operator*() const _NOEXCEPT + { + return curr; + } + pointer operator->() const _NOEXCEPT + { + return details::arrow_proxy{ curr }; + } + bounds_iterator& operator++() _NOEXCEPT + { + ++curr; + return *this; + } + bounds_iterator operator++(int)_NOEXCEPT + { + auto ret = *this; + ++(*this); + return ret; + } + bounds_iterator& operator--() _NOEXCEPT + { + curr--; + return *this; + } + bounds_iterator operator--(int)_NOEXCEPT + { + auto ret = *this; + --(*this); + return ret; + } + bounds_iterator operator+(difference_type n) const _NOEXCEPT + { + bounds_iterator ret{ *this }; + return ret += n; + } + bounds_iterator& operator+=(difference_type n) _NOEXCEPT + { + curr += n; + return *this; + } + bounds_iterator operator-(difference_type n) const _NOEXCEPT + { + bounds_iterator ret{ *this }; + return ret -= n; + } + bounds_iterator& operator-=(difference_type n) _NOEXCEPT + { + return * this += -n; + } + difference_type operator-(const bounds_iterator& rhs) const _NOEXCEPT + { + return curr[0] - rhs.curr[0]; + } + reference operator[](difference_type n) const _NOEXCEPT + { + return curr + n; + } + bool operator==(const bounds_iterator& rhs) const _NOEXCEPT + { + return curr == rhs.curr; + } + bool operator!=(const bounds_iterator& rhs) const _NOEXCEPT + { + return !(*this == rhs); + } + bool operator<(const bounds_iterator& rhs) const _NOEXCEPT + { + return curr[0] < rhs.curr[0]; + } + bool operator<=(const bounds_iterator& rhs) const _NOEXCEPT + { + return !(rhs < *this); + } + bool operator>(const bounds_iterator& rhs) const _NOEXCEPT + { + return rhs < *this; + } + bool operator>=(const bounds_iterator& rhs) const _NOEXCEPT + { + return !(rhs > *this); + } + void swap(bounds_iterator& rhs) _NOEXCEPT + { + std::swap(curr, rhs.curr); + } + + private: + value_type curr; }; template -bounds_iterator operator+(typename bounds_iterator::difference_type n, const bounds_iterator& rhs) _NOEXCEPT +bounds_iterator operator+(typename bounds_iterator::difference_type n, + const bounds_iterator& rhs) _NOEXCEPT { - return rhs + n; + return rhs + n; } /* @@ -1321,962 +1382,1080 @@ bounds_iterator operator+(typename bounds_iterator::differ */ namespace details { - template - constexpr std::enable_if_t::value, typename Bounds::index_type> make_stride(const Bounds& bnd) _NOEXCEPT - { - return bnd.strides(); - } +template +constexpr std::enable_if_t::value, typename Bounds::index_type> +make_stride(const Bounds& bnd) _NOEXCEPT +{ + return bnd.strides(); +} - // Make a stride vector from bounds, assuming contiguous memory. - template - constexpr std::enable_if_t::value, typename Bounds::index_type> make_stride(const Bounds& bnd) _NOEXCEPT - { - auto extents = bnd.index_bounds(); - typename Bounds::index_type stride; - stride[Bounds::rank - 1] = 1; - for (size_t i = Bounds::rank - 1; Bounds::rank > 1 && i > 0; --i) - stride[i-1] = stride[i] * extents[i]; - return stride; - } +// Make a stride vector from bounds, assuming contiguous memory. +template +constexpr std::enable_if_t::value, typename Bounds::index_type> +make_stride(const Bounds& bnd) _NOEXCEPT +{ + auto extents = bnd.index_bounds(); + typename Bounds::index_type stride; + stride[Bounds::rank - 1] = 1; + for (size_t i = Bounds::rank - 1; Bounds::rank > 1 && i > 0; --i) + stride[i - 1] = stride[i] * extents[i]; + return stride; +} - template - void verifyBoundsReshape(const BoundsSrc &src, const BoundsDest &dest) - { - static_assert(is_bounds::value && is_bounds::value, "The src type and dest type must be bounds"); - static_assert(std::is_same::value, "The source type must be a contiguous bounds"); - static_assert(BoundsDest::static_size == dynamic_range || BoundsSrc::static_size == dynamic_range || BoundsDest::static_size == BoundsSrc::static_size, "The source bounds must have same size as dest bounds"); - fail_fast_assert(src.size() == dest.size()); - } +template +void verifyBoundsReshape(const BoundsSrc& src, const BoundsDest& dest) +{ + static_assert(is_bounds::value && is_bounds::value, + "The src type and dest type must be bounds"); + static_assert(std::is_same::value, + "The source type must be a contiguous bounds"); + static_assert(BoundsDest::static_size == dynamic_range || BoundsSrc::static_size == dynamic_range || + BoundsDest::static_size == BoundsSrc::static_size, + "The source bounds must have same size as dest bounds"); + fail_fast_assert(src.size() == dest.size()); +} } // namespace details -template -class contiguous_array_view_iterator; -template -class general_array_view_iterator; -enum class byte : std::uint8_t {}; - -template -class basic_array_view +template class contiguous_array_view_iterator; +template class general_array_view_iterator; +enum class byte : std::uint8_t { -public: - static const size_t rank = BoundsType::rank; - using bounds_type = BoundsType; - using size_type = typename bounds_type::size_type; - using index_type = typename bounds_type::index_type; - using value_type = ValueType; - using pointer = ValueType*; - using reference = ValueType&; - using iterator = std::conditional_t::value, contiguous_array_view_iterator, general_array_view_iterator>; - using const_iterator = std::conditional_t::value, contiguous_array_view_iterator>, general_array_view_iterator>>; - using reverse_iterator = std::reverse_iterator; - using const_reverse_iterator = std::reverse_iterator; - using sliced_type = std::conditional_t>; - -private: - pointer m_pdata; - bounds_type m_bounds; - -public: - constexpr bounds_type bounds() const _NOEXCEPT - { - return m_bounds; - } - template - constexpr size_type extent() const _NOEXCEPT - { - static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)"); - return m_bounds.template extent(); - } - constexpr size_type size() const _NOEXCEPT - { - return m_bounds.size(); - } - constexpr reference operator[](const index_type& idx) const - { - return m_pdata[m_bounds.linearize(idx)]; - } - constexpr pointer data() const _NOEXCEPT - { - return m_pdata; - } - template 1), typename Ret = std::enable_if_t> - constexpr Ret operator[](size_type idx) const - { - fail_fast_assert(idx < m_bounds.size(), "index is out of bounds of the array"); - const size_type ridx = idx * m_bounds.stride(); - - fail_fast_assert(ridx < m_bounds.total_size(), "index is out of bounds of the underlying data"); - return Ret {m_pdata + ridx, m_bounds.slice()}; - } - - constexpr operator bool () const _NOEXCEPT - { - return m_pdata != nullptr; - } - - constexpr iterator begin() const - { - return iterator {this, true}; - } - constexpr iterator end() const - { - return iterator {this}; - } - constexpr const_iterator cbegin() const - { - return const_iterator {reinterpret_cast *>(this), true}; - } - constexpr const_iterator cend() const - { - return const_iterator {reinterpret_cast *>(this)}; - } - - constexpr reverse_iterator rbegin() const - { - return reverse_iterator {end()}; - } - constexpr reverse_iterator rend() const - { - return reverse_iterator {begin()}; - } - constexpr const_reverse_iterator crbegin() const - { - return const_reverse_iterator {cend()}; - } - constexpr const_reverse_iterator crend() const - { - return const_reverse_iterator {cbegin()}; - } - - template , std::remove_cv_t>::value>> - constexpr bool operator== (const basic_array_view & other) const _NOEXCEPT - { - return m_bounds.size() == other.m_bounds.size() && - (m_pdata == other.m_pdata || std::equal(this->begin(), this->end(), other.begin())); - } - - template , std::remove_cv_t>::value>> - constexpr bool operator!= (const basic_array_view & other) const _NOEXCEPT - { - return !(*this == other); - } - - template , std::remove_cv_t>::value>> - constexpr bool operator< (const basic_array_view & other) const _NOEXCEPT - { - return std::lexicographical_compare(this->begin(), this->end(), other.begin(), other.end()); - } - - template , std::remove_cv_t>::value>> - constexpr bool operator<= (const basic_array_view & other) const _NOEXCEPT - { - return !(other < *this); - } - - template , std::remove_cv_t>::value>> - constexpr bool operator> (const basic_array_view & other) const _NOEXCEPT - { - return (other < *this); - } - - template , std::remove_cv_t>::value>> - constexpr bool operator>= (const basic_array_view & other) const _NOEXCEPT - { - return !(*this < other); - } - -public: - template ::value - && std::is_convertible::value>> - constexpr basic_array_view(const basic_array_view & other ) _NOEXCEPT - : m_pdata(other.m_pdata), m_bounds(other.m_bounds) - { - } -protected: - - constexpr basic_array_view(pointer data, bounds_type bound) _NOEXCEPT - : m_pdata(data) - , m_bounds(std::move(bound)) - { - fail_fast_assert((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0); - } - template - constexpr basic_array_view(T *data, std::enable_if_t>::value, bounds_type> bound) _NOEXCEPT - : m_pdata(reinterpret_cast(data)) - , m_bounds(std::move(bound)) - { - fail_fast_assert((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0); - } - template - constexpr basic_array_view as_array_view(const DestBounds &bounds) - { - details::verifyBoundsReshape(m_bounds, bounds); - return {m_pdata, bounds}; - } -private: - - friend iterator; - friend const_iterator; - template - friend class basic_array_view; }; -template -struct dim +template class basic_array_view { - static const size_t value = DimSize; + public: + static const size_t rank = BoundsType::rank; + using bounds_type = BoundsType; + using size_type = typename bounds_type::size_type; + using index_type = typename bounds_type::index_type; + using value_type = ValueType; + using pointer = ValueType*; + using reference = ValueType&; + using iterator = + std::conditional_t::value, + contiguous_array_view_iterator, + general_array_view_iterator>; + using const_iterator = + std::conditional_t::value, + contiguous_array_view_iterator>, + general_array_view_iterator>>; + using reverse_iterator = std::reverse_iterator; + using const_reverse_iterator = std::reverse_iterator; + using sliced_type = + std::conditional_t>; + + private: + pointer m_pdata; + bounds_type m_bounds; + + public: + constexpr bounds_type bounds() const _NOEXCEPT + { + return m_bounds; + } + template constexpr size_type extent() const _NOEXCEPT + { + static_assert(Dim < rank, + "dimension should be less than rank (dimension count starts from 0)"); + return m_bounds.template extent(); + } + constexpr size_type size() const _NOEXCEPT + { + return m_bounds.size(); + } + constexpr reference operator[](const index_type& idx) const + { + return m_pdata[m_bounds.linearize(idx)]; + } + constexpr pointer data() const _NOEXCEPT + { + return m_pdata; + } + template 1), typename Ret = std::enable_if_t> + constexpr Ret operator[](size_type idx) const + { + fail_fast_assert(idx < m_bounds.size(), "index is out of bounds of the array"); + const size_type ridx = idx * m_bounds.stride(); + + fail_fast_assert(ridx < m_bounds.total_size(), + "index is out of bounds of the underlying data"); + return Ret{ m_pdata + ridx, m_bounds.slice() }; + } + + constexpr operator bool() const _NOEXCEPT + { + return m_pdata != nullptr; + } + + constexpr iterator begin() const + { + return iterator{ this, true }; + } + constexpr iterator end() const + { + return iterator{ this }; + } + constexpr const_iterator cbegin() const + { + return const_iterator{ reinterpret_cast*>(this), true }; + } + constexpr const_iterator cend() const + { + return const_iterator{ reinterpret_cast*>(this) }; + } + + constexpr reverse_iterator rbegin() const + { + return reverse_iterator{ end() }; + } + constexpr reverse_iterator rend() const + { + return reverse_iterator{ begin() }; + } + constexpr const_reverse_iterator crbegin() const + { + return const_reverse_iterator{ cend() }; + } + constexpr const_reverse_iterator crend() const + { + return const_reverse_iterator{ cbegin() }; + } + + template , std::remove_cv_t>::value>> + constexpr bool operator==(const basic_array_view& other) const _NOEXCEPT + { + return m_bounds.size() == other.m_bounds.size() && + (m_pdata == other.m_pdata || std::equal(this->begin(), this->end(), other.begin())); + } + + template , std::remove_cv_t>::value>> + constexpr bool operator!=(const basic_array_view& other) const _NOEXCEPT + { + return !(*this == other); + } + + template , std::remove_cv_t>::value>> + constexpr bool operator<(const basic_array_view& other) const _NOEXCEPT + { + return std::lexicographical_compare(this->begin(), this->end(), other.begin(), other.end()); + } + + template , std::remove_cv_t>::value>> + constexpr bool operator<=(const basic_array_view& other) const _NOEXCEPT + { + return !(other < *this); + } + + template , std::remove_cv_t>::value>> + constexpr bool operator>(const basic_array_view& other) const _NOEXCEPT + { + return (other < *this); + } + + template , std::remove_cv_t>::value>> + constexpr bool operator>=(const basic_array_view& other) const _NOEXCEPT + { + return !(*this < other); + } + + public: + template ::value && + std::is_convertible::value>> + constexpr basic_array_view(const basic_array_view& other) _NOEXCEPT + : m_pdata(other.m_pdata), + m_bounds(other.m_bounds) + { + } + + protected: + constexpr basic_array_view(pointer data, bounds_type bound) _NOEXCEPT : m_pdata(data), + m_bounds(std::move(bound)) + { + fail_fast_assert((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0); + } + template + constexpr basic_array_view(T* data, + std::enable_if_t>::value, bounds_type> bound) _NOEXCEPT + : m_pdata(reinterpret_cast(data)), + m_bounds(std::move(bound)) + { + fail_fast_assert((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0); + } + template + constexpr basic_array_view as_array_view(const DestBounds& bounds) + { + details::verifyBoundsReshape(m_bounds, bounds); + return { m_pdata, bounds }; + } + + private: + friend iterator; + friend const_iterator; + template friend class basic_array_view; }; -template <> -struct dim + +template struct dim { - static const size_t value = dynamic_range; - const size_t dvalue; - dim(size_t size) : dvalue(size) {} + static const size_t value = DimSize; +}; +template <> struct dim +{ + static const size_t value = dynamic_range; + const size_t dvalue; + dim(size_t size) : dvalue(size) + { + } }; template class array_view; -template -class strided_array_view; +template class strided_array_view; namespace details { - template - struct ArrayViewTypeTraits - { - using value_type = T; - using size_type = size_t; - }; +template struct ArrayViewTypeTraits +{ + using value_type = T; + using size_type = size_t; +}; - template - struct ArrayViewTypeTraits::type> - { - using value_type = typename Traits::array_view_traits::value_type; - using size_type = typename Traits::array_view_traits::size_type; - }; +template +struct ArrayViewTypeTraits::type> +{ + using value_type = typename Traits::array_view_traits::value_type; + using size_type = typename Traits::array_view_traits::size_type; +}; - template - struct ArrayViewArrayTraits { - using type = array_view; - using value_type = T; - using bounds_type = static_bounds; - using pointer = T*; - using reference = T&; - }; - template - struct ArrayViewArrayTraits : ArrayViewArrayTraits {}; +template struct ArrayViewArrayTraits +{ + using type = array_view; + using value_type = T; + using bounds_type = static_bounds; + using pointer = T*; + using reference = T&; +}; +template +struct ArrayViewArrayTraits : ArrayViewArrayTraits +{ +}; - template - BoundsType newBoundsHelperImpl(size_t totalSize, std::true_type) // dynamic size - { - fail_fast_assert(totalSize <= details::SizeTypeTraits::max_value); - return BoundsType{static_cast(totalSize)}; - } - template - BoundsType newBoundsHelperImpl(size_t totalSize, std::false_type) // static size - { - fail_fast_assert(BoundsType::static_size == totalSize); - return {}; - } - template - BoundsType newBoundsHelper(size_t totalSize) - { - static_assert(BoundsType::dynamic_rank <= 1, "dynamic rank must less or equal to 1"); - return newBoundsHelperImpl(totalSize, std::integral_constant()); - } - - struct Sep{}; - - template - T static_as_array_view_helper(Sep, Args... args) - { - return T{static_cast(args)...}; - } - template - std::enable_if_t>::value && !std::is_same::value, T> static_as_array_view_helper(Arg, Args... args) - { - return static_as_array_view_helper(args...); - } - template - T static_as_array_view_helper(dim val, Args ... args) - { - return static_as_array_view_helper(args..., val.dvalue); - } +template +BoundsType newBoundsHelperImpl(size_t totalSize, std::true_type) // dynamic size +{ + fail_fast_assert(totalSize <= details::SizeTypeTraits::max_value); + return BoundsType{ static_cast(totalSize) }; +} +template +BoundsType newBoundsHelperImpl(size_t totalSize, std::false_type) // static size +{ + fail_fast_assert(BoundsType::static_size == totalSize); + return {}; +} +template BoundsType newBoundsHelper(size_t totalSize) +{ + static_assert(BoundsType::dynamic_rank <= 1, "dynamic rank must less or equal to 1"); + return newBoundsHelperImpl(totalSize, + std::integral_constant()); +} - template - struct static_as_array_view_static_bounds_helper - { - using type = static_bounds; - }; +struct Sep +{ +}; - template - struct is_array_view_oracle : std::false_type - {}; - template - struct is_array_view_oracle> : std::true_type - {}; - template - struct is_array_view_oracle> : std::true_type - {}; - template - struct is_array_view : is_array_view_oracle> - {}; +template T static_as_array_view_helper(Sep, Args... args) +{ + return T{ static_cast(args)... }; +} +template +std::enable_if_t>::value && !std::is_same::value, T> +static_as_array_view_helper(Arg, Args... args) +{ + return static_as_array_view_helper(args...); +} +template +T static_as_array_view_helper(dim val, Args... args) +{ + return static_as_array_view_helper(args..., val.dvalue); +} +template +struct static_as_array_view_static_bounds_helper +{ + using type = static_bounds; +}; + +template struct is_array_view_oracle : std::false_type +{ +}; +template +struct is_array_view_oracle> : std::true_type +{ +}; +template +struct is_array_view_oracle> : std::true_type +{ +}; +template struct is_array_view : is_array_view_oracle> +{ +}; } -template -struct array_view_options +template struct array_view_options { - struct array_view_traits - { - using value_type = ValueType; - using size_type = SizeType; - }; + struct array_view_traits + { + using value_type = ValueType; + using size_type = SizeType; + }; }; template -class array_view : public basic_array_view::value_type, - static_bounds::size_type, FirstDimension, RestDimensions...>> +class array_view +: public basic_array_view::value_type, + static_bounds::size_type, FirstDimension, RestDimensions...>> { - template - friend class array_view; - using Base = basic_array_view::value_type, - static_bounds::size_type, FirstDimension, RestDimensions...>>; + template + friend class array_view; + using Base = + basic_array_view::value_type, + static_bounds::size_type, FirstDimension, RestDimensions...>>; -public: - using typename Base::bounds_type; - using typename Base::size_type; - using typename Base::pointer; - using typename Base::value_type; - using typename Base::index_type; - using typename Base::iterator; - using typename Base::const_iterator; - using typename Base::reference; - using Base::rank; + public: + using typename Base::bounds_type; + using typename Base::size_type; + using typename Base::pointer; + using typename Base::value_type; + using typename Base::index_type; + using typename Base::iterator; + using typename Base::const_iterator; + using typename Base::reference; + using Base::rank; -public: - // basic - constexpr array_view(pointer ptr, bounds_type bounds) : Base(ptr, std::move(bounds)) - { - } + public: + // basic + constexpr array_view(pointer ptr, bounds_type bounds) : Base(ptr, std::move(bounds)) + { + } - constexpr array_view(std::nullptr_t) : Base(nullptr, bounds_type{}) - { - } + constexpr array_view(std::nullptr_t) : Base(nullptr, bounds_type{}) + { + } - constexpr array_view(std::nullptr_t, size_type size) : Base(nullptr, bounds_type{}) - { - fail_fast_assert(size == 0); - } + constexpr array_view(std::nullptr_t, size_type size) : Base(nullptr, bounds_type{}) + { + fail_fast_assert(size == 0); + } - // default - template > - constexpr array_view() : Base(nullptr, bounds_type()) - { - } + // default + template > + constexpr array_view() + : Base(nullptr, bounds_type()) + { + } - // from n-dimensions dynamic array (e.g. new int[m][4]) (precedence will be lower than the 1-dimension pointer) - template , - typename Dummy = std::enable_if_t::value - && std::is_convertible::value>> - constexpr array_view(T * const & data, size_type size) : Base(data, typename Helper::bounds_type{size}) - { - } + // from n-dimensions dynamic array (e.g. new int[m][4]) (precedence will be lower than the + // 1-dimension pointer) + template , + typename Dummy = std::enable_if_t< + std::is_convertible::value && + std::is_convertible::value>> + constexpr array_view(T* const& data, size_type size) + : Base(data, typename Helper::bounds_type{ size }) + { + } - // from n-dimensions static array - template , - typename Dummy = std::enable_if_t::value - && std::is_convertible::value>> - constexpr array_view (T (&arr)[N]) : Base(arr, typename Helper::bounds_type()) - { - } + // from n-dimensions static array + template , + typename Dummy = std::enable_if_t< + std::is_convertible::value && + std::is_convertible::value>> + constexpr array_view(T(&arr)[N]) + : Base(arr, typename Helper::bounds_type()) + { + } - // from n-dimensions static array with size - template , - typename Dummy = std::enable_if_t::value - && std::is_convertible::value >> - constexpr array_view(T(&arr)[N], size_type size) : Base(arr, typename Helper::bounds_type{ size }) - { - fail_fast_assert(size <= N); - } + // from n-dimensions static array with size + template , + typename Dummy = std::enable_if_t< + std::is_convertible::value && + std::is_convertible::value>> + constexpr array_view(T(&arr)[N], size_type size) + : Base(arr, typename Helper::bounds_type{ size }) + { + fail_fast_assert(size <= N); + } - // from std array - template , typename Base::bounds_type>::value>> - constexpr array_view (std::array, N> & arr) : Base(arr.data(), static_bounds()) - { - } + // from std array + template , typename Base::bounds_type>::value>> + constexpr array_view(std::array, N>& arr) + : Base(arr.data(), static_bounds()) + { + } - template , typename Base::bounds_type>::value && std::is_const::value>> - constexpr array_view (const std::array, N> & arr) : Base(arr.data(), static_bounds()) - { - } + template , typename Base::bounds_type>::value && + std::is_const::value>> + constexpr array_view(const std::array, N>& arr) + : Base(arr.data(), static_bounds()) + { + } - // from begin, end pointers. We don't provide iterator pair since no way to guarantee the contiguity - template ::value - && details::LessThan::value>> // remove literal 0 case - constexpr array_view (pointer begin, Ptr end) : Base(begin, details::newBoundsHelper(static_cast(end) - begin)) - { - } + // from begin, end pointers. We don't provide iterator pair since no way to guarantee the + // contiguity + template ::value && + details::LessThan::value>> // remove literal 0 case + constexpr array_view(pointer begin, Ptr end) + : Base(begin, details::newBoundsHelper(static_cast(end) - begin)) + { + } - // from containers. It must has .size() and .data() two function signatures - template ::value - && std::is_convertible::value - && std::is_convertible, typename Base::bounds_type>::value - && std::is_same().size(), *std::declval().data())>, DataType>::value> - > - constexpr array_view (Cont& cont) : Base(static_cast(cont.data()), details::newBoundsHelper(cont.size())) - { + // from containers. It must has .size() and .data() two function signatures + template ::value && std::is_convertible::value && + std::is_convertible, typename Base::bounds_type>::value && + std::is_same().size(), *std::declval().data())>, DataType>::value>> + constexpr array_view(Cont& cont) + : Base(static_cast(cont.data()), + details::newBoundsHelper(cont.size())) + { + } - } + constexpr array_view(const array_view&) = default; - constexpr array_view(const array_view &) = default; + // convertible + template ::value_type, + static_bounds::size_type, FirstDimension, RestDimensions...>>, + typename OtherBaseType = + basic_array_view::value_type, + static_bounds::size_type, OtherDimensions...>>, + typename Dummy = std::enable_if_t::value>> + constexpr array_view(const array_view& av) + : Base(static_cast::Base&>(av)) + { + } // static_cast is required - // convertible - template ::value_type, static_bounds::size_type, FirstDimension, RestDimensions...>>, - typename OtherBaseType = basic_array_view::value_type, static_bounds::size_type, OtherDimensions...>>, - typename Dummy = std::enable_if_t::value> - > - constexpr array_view(const array_view &av) : Base(static_cast::Base &>(av)) {} // static_cast is required + // reshape + template + constexpr array_view as_array_view(Dimensions2... dims) + { + static_assert(sizeof...(Dimensions2) > 0, + "the target array_view must have at least one dimension."); + using BoundsType = typename array_view::bounds_type; + auto tobounds = details::static_as_array_view_helper(dims..., details::Sep{}); + details::verifyBoundsReshape(this->bounds(), tobounds); + return { this->data(), tobounds }; + } - // reshape - template - constexpr array_view as_array_view(Dimensions2... dims) - { - static_assert(sizeof...(Dimensions2) > 0, "the target array_view must have at least one dimension."); - using BoundsType = typename array_view::bounds_type; - auto tobounds = details::static_as_array_view_helper(dims..., details::Sep{}); - details::verifyBoundsReshape(this->bounds(), tobounds); - return {this->data(), tobounds}; - } + // to bytes array + template ::value_type>>::value> + constexpr auto as_bytes() const _NOEXCEPT + -> array_view, + static_cast(details::StaticSizeHelper::value)> + { + static_assert(Enabled, "The value_type of array_view must be standarded layout"); + return { reinterpret_cast(this->data()), this->bytes() }; + } - // to bytes array - template ::value_type>>::value> - constexpr auto as_bytes() const _NOEXCEPT -> - array_view, static_cast(details::StaticSizeHelper::value)> - { - static_assert(Enabled, "The value_type of array_view must be standarded layout"); - return { reinterpret_cast(this->data()), this->bytes() }; - } - - template ::value_type>>::value> - constexpr auto as_writeable_bytes() const _NOEXCEPT -> - array_view, static_cast(details::StaticSizeHelper::value)> - { - static_assert(Enabled, "The value_type of array_view must be standarded layout"); - return { reinterpret_cast(this->data()), this->bytes() }; - } + template ::value_type>>::value> + constexpr auto as_writeable_bytes() const _NOEXCEPT + -> array_view, + static_cast(details::StaticSizeHelper::value)> + { + static_assert(Enabled, "The value_type of array_view must be standarded layout"); + return { reinterpret_cast(this->data()), this->bytes() }; + } - // from bytes array - template::value, typename Dummy = std::enable_if_t> - constexpr auto as_array_view() const _NOEXCEPT -> array_view(dynamic_range))> - { - static_assert(std::is_standard_layout::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % sizeof(U) == 0), - "Target type must be standard layout and its size must match the byte array size"); - fail_fast_assert((this->bytes() % sizeof(U)) == 0); - return { reinterpret_cast(this->data()), this->bytes() / sizeof(U) }; - } + // from bytes array + template ::value, + typename Dummy = std::enable_if_t> + constexpr auto as_array_view() const _NOEXCEPT + -> array_view(dynamic_range))> + { + static_assert( + std::is_standard_layout::value && (Base::bounds_type::static_size == dynamic_range || + Base::bounds_type::static_size % sizeof(U) == 0), + "Target type must be standard layout and its size must match the byte array size"); + fail_fast_assert((this->bytes() % sizeof(U)) == 0); + return { reinterpret_cast(this->data()), this->bytes() / sizeof(U) }; + } - template::value, typename Dummy = std::enable_if_t> - constexpr auto as_array_view() const _NOEXCEPT -> array_view(dynamic_range))> - { - static_assert(std::is_standard_layout::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % sizeof(U) == 0), - "Target type must be standard layout and its size must match the byte array size"); - fail_fast_assert((this->bytes() % sizeof(U)) == 0); - return { reinterpret_cast(this->data()), this->bytes() / sizeof(U) }; - } + template ::value, + typename Dummy = std::enable_if_t> + constexpr auto as_array_view() const _NOEXCEPT + -> array_view(dynamic_range))> + { + static_assert( + std::is_standard_layout::value && (Base::bounds_type::static_size == dynamic_range || + Base::bounds_type::static_size % sizeof(U) == 0), + "Target type must be standard layout and its size must match the byte array size"); + fail_fast_assert((this->bytes() % sizeof(U)) == 0); + return { reinterpret_cast(this->data()), this->bytes() / sizeof(U) }; + } - // section on linear space - template - constexpr array_view first() const _NOEXCEPT - { - static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound"); - fail_fast_assert(bounds_type::static_size != dynamic_range || Count <= this->size()); // ensures we only check condition when needed - return { this->data(), Count }; - } + // section on linear space + template constexpr array_view first() const _NOEXCEPT + { + static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, + "Index is out of bound"); + fail_fast_assert(bounds_type::static_size != dynamic_range || + Count <= this->size()); // ensures we only check condition when needed + return { this->data(), Count }; + } - constexpr array_view first(size_type count) const _NOEXCEPT - { - fail_fast_assert(count <= this->size()); - return { this->data(), count }; - } + constexpr array_view first(size_type count) const _NOEXCEPT + { + fail_fast_assert(count <= this->size()); + return { this->data(), count }; + } - template - constexpr array_view last() const _NOEXCEPT - { - static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound"); - fail_fast_assert(bounds_type::static_size != dynamic_range || Count <= this->size()); - return { this->data() + this->size() - Count, Count }; - } + template constexpr array_view last() const _NOEXCEPT + { + static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, + "Index is out of bound"); + fail_fast_assert(bounds_type::static_size != dynamic_range || Count <= this->size()); + return { this->data() + this->size() - Count, Count }; + } - constexpr array_view last(size_type count) const _NOEXCEPT - { - fail_fast_assert(count <= this->size()); - return { this->data() + this->size() - count, count }; - } + constexpr array_view last(size_type count) const _NOEXCEPT + { + fail_fast_assert(count <= this->size()); + return { this->data() + this->size() - count, count }; + } - template - constexpr array_view sub() const _NOEXCEPT - { - static_assert(bounds_type::static_size == dynamic_range || ((Offset == 0 || Offset <= bounds_type::static_size) && Offset + Count <= bounds_type::static_size), "Index is out of bound"); - fail_fast_assert(bounds_type::static_size != dynamic_range || ((Offset == 0 || Offset <= this->size()) && Offset + Count <= this->size())); - return { this->data() + Offset, Count }; - } + template + constexpr array_view sub() const _NOEXCEPT + { + static_assert(bounds_type::static_size == dynamic_range || + ((Offset == 0 || Offset <= bounds_type::static_size) && Offset + Count <= bounds_type::static_size), + "Index is out of bound"); + fail_fast_assert(bounds_type::static_size != dynamic_range || + ((Offset == 0 || Offset <= this->size()) && Offset + Count <= this->size())); + return { this->data() + Offset, Count }; + } - constexpr array_view sub(size_type offset, size_type count = dynamic_range) const _NOEXCEPT - { - fail_fast_assert((offset == 0 || offset <= this->size()) && (count == dynamic_range || (offset + count) <= this->size())); - return { this->data() + offset, count == dynamic_range ? this->length() - offset : count }; - } + constexpr array_view sub(size_type offset, size_type count = dynamic_range) const _NOEXCEPT + { + fail_fast_assert((offset == 0 || offset <= this->size()) && + (count == dynamic_range || (offset + count) <= this->size())); + return { this->data() + offset, count == dynamic_range ? this->length() - offset : count }; + } - // size - constexpr size_type length() const _NOEXCEPT - { - return this->size(); - } - constexpr size_type used_length() const _NOEXCEPT - { - return length(); - } - constexpr size_type bytes() const _NOEXCEPT - { - return sizeof(value_type) * this->size(); - } - constexpr size_type used_bytes() const _NOEXCEPT - { - return bytes(); - } + // size + constexpr size_type length() const _NOEXCEPT + { + return this->size(); + } + constexpr size_type used_length() const _NOEXCEPT + { + return length(); + } + constexpr size_type bytes() const _NOEXCEPT + { + return sizeof(value_type) * this->size(); + } + constexpr size_type used_bytes() const _NOEXCEPT + { + return bytes(); + } - // section - constexpr strided_array_view section(index_type origin, index_type extents) const - { - size_type size = this->bounds().total_size() - this->bounds().linearize(origin); - return{ &this->operator[](origin), size, strided_bounds {extents, details::make_stride(Base::bounds())} }; - } - - constexpr reference operator[](const index_type& idx) const - { - return Base::operator[](idx); - } - - template 1), typename Dummy = std::enable_if_t> - constexpr array_view operator[](size_type idx) const - { - auto ret = Base::operator[](idx); - return{ ret.data(), ret.bounds() }; - } + // section + constexpr strided_array_view section(index_type origin, index_type extents) const + { + size_type size = this->bounds().total_size() - this->bounds().linearize(origin); + return { &this->operator[](origin), + size, + strided_bounds{ extents, details::make_stride(Base::bounds()) } }; + } - using Base::operator==; - using Base::operator!=; - using Base::operator<; - using Base::operator<=; - using Base::operator>; - using Base::operator>=; + constexpr reference operator[](const index_type& idx) const + { + return Base::operator[](idx); + } + + template 1), typename Dummy = std::enable_if_t> + constexpr array_view operator[](size_type idx) const + { + auto ret = Base::operator[](idx); + return { ret.data(), ret.bounds() }; + } + + using Base::operator==; + using Base::operator!=; + using Base::operator<; + using Base::operator<=; + using Base::operator>; + using Base::operator>=; }; template -constexpr auto as_array_view(T * const & ptr, dim... args) -> array_view, Dimensions...> +constexpr auto as_array_view(T* const& ptr, dim... args) +-> array_view, Dimensions...> { - return {reinterpret_cast*>(ptr), details::static_as_array_view_helper>(args..., details::Sep{})}; + return { reinterpret_cast*>(ptr), + details::static_as_array_view_helper>(args..., details::Sep{}) }; } template -constexpr auto as_array_view (T * arr, size_t len) -> typename details::ArrayViewArrayTraits::type +constexpr auto as_array_view(T* arr, size_t len) -> +typename details::ArrayViewArrayTraits::type { - return {arr, len}; + return { arr, len }; } template -constexpr auto as_array_view (T (&arr)[N]) -> typename details::ArrayViewArrayTraits::type +constexpr auto as_array_view(T(&arr)[N]) -> typename details::ArrayViewArrayTraits::type { - return {arr}; + return { arr }; } template -constexpr array_view as_array_view(const std::array &arr) +constexpr array_view as_array_view(const std::array& arr) { - return {arr}; + return { arr }; } template -constexpr array_view as_array_view(const std::array &&) = delete; +constexpr array_view as_array_view(const std::array&&) = delete; -template -constexpr array_view as_array_view(std::array &arr) +template constexpr array_view as_array_view(std::array& arr) { - return {arr}; + return { arr }; } -template -constexpr array_view as_array_view(T *begin, T *end) +template constexpr array_view as_array_view(T* begin, T* end) { - return {begin, end}; + return { begin, end }; } template -constexpr auto as_array_view(Cont &arr) -> std::enable_if_t>::value, - array_view, dynamic_range>> +constexpr auto as_array_view(Cont& arr) +-> std::enable_if_t>::value, + array_view, dynamic_range>> { - return {arr.data(), arr.size()}; + return { arr.data(), arr.size() }; } template -constexpr auto as_array_view(Cont &&arr) -> std::enable_if_t>::value, - array_view, dynamic_range>> = delete; +constexpr auto as_array_view(Cont&& arr) +-> std::enable_if_t>::value, + array_view, dynamic_range>> = delete; template -class strided_array_view : public basic_array_view::value_type, strided_bounds::size_type>> +class strided_array_view +: public basic_array_view::value_type, + strided_bounds::size_type>> { - using Base = basic_array_view::value_type, strided_bounds::size_type>>; + using Base = + basic_array_view::value_type, + strided_bounds::size_type>>; - template - friend class strided_array_view; -public: - using Base::rank; - using typename Base::bounds_type; - using typename Base::size_type; - using typename Base::pointer; - using typename Base::value_type; - using typename Base::index_type; - using typename Base::iterator; - using typename Base::const_iterator; - using typename Base::reference; - - // from static array of size N - template - strided_array_view(value_type(&values)[N], bounds_type bounds) : Base(values, std::move(bounds)) - { - fail_fast_assert(this->bounds().total_size() <= N, "Bounds cross data boundaries"); - } + template friend class strided_array_view; - // from raw data - strided_array_view(pointer ptr, size_type size, bounds_type bounds): Base(ptr, std::move(bounds)) - { - fail_fast_assert(this->bounds().total_size() <= size, "Bounds cross data boundaries"); - } + public: + using Base::rank; + using typename Base::bounds_type; + using typename Base::size_type; + using typename Base::pointer; + using typename Base::value_type; + using typename Base::index_type; + using typename Base::iterator; + using typename Base::const_iterator; + using typename Base::reference; - // from array view - template > - strided_array_view(array_view av, bounds_type bounds) : Base(av.data(), std::move(bounds)) - { - fail_fast_assert(this->bounds().total_size() <= av.bounds().total_size(), "Bounds cross data boundaries"); - } - - // convertible - template ::value_type, strided_bounds::size_type>>, - typename OtherBaseType = basic_array_view::value_type, strided_bounds::size_type>>, - typename Dummy = std::enable_if_t::value> - > - constexpr strided_array_view(const strided_array_view &av): Base(static_cast::Base &>(av)) // static_cast is required - { - } + // from static array of size N + template + strided_array_view(value_type(&values)[N], bounds_type bounds) + : Base(values, std::move(bounds)) + { + fail_fast_assert(this->bounds().total_size() <= N, "Bounds cross data boundaries"); + } - // convert from bytes - template - strided_array_view::value, OtherValueType>::type, rank> as_strided_array_view() const - { - static_assert((sizeof(OtherValueType) >= sizeof(value_type)) && (sizeof(OtherValueType) % sizeof(value_type) == 0), "OtherValueType should have a size to contain a multiple of ValueTypes"); - auto d = sizeof(OtherValueType) / sizeof(value_type); + // from raw data + strided_array_view(pointer ptr, size_type size, bounds_type bounds) + : Base(ptr, std::move(bounds)) + { + fail_fast_assert(this->bounds().total_size() <= size, "Bounds cross data boundaries"); + } - size_type size = this->bounds().total_size() / d; - return{ (OtherValueType*)this->data(), size, bounds_type{ resize_extent(this->bounds().index_bounds(), d), resize_stride(this->bounds().strides(), d)} }; - } + // from array view + template > + strided_array_view(array_view av, bounds_type bounds) + : Base(av.data(), std::move(bounds)) + { + fail_fast_assert(this->bounds().total_size() <= av.bounds().total_size(), + "Bounds cross data boundaries"); + } - strided_array_view section(index_type origin, index_type extents) const - { - size_type size = this->bounds().total_size() - this->bounds().linearize(origin); - return { &this->operator[](origin), size, bounds_type {extents, details::make_stride(Base::bounds())}}; - } + // convertible + template ::value_type, + strided_bounds::size_type>>, + typename OtherBaseType = basic_array_view::value_type, + strided_bounds::size_type>>, + typename Dummy = std::enable_if_t::value>> + constexpr strided_array_view(const strided_array_view& av) + : Base(static_cast::Base&>( + av)) // static_cast is required + { + } - constexpr reference operator[](const index_type& idx) const - { - return Base::operator[](idx); - } + // convert from bytes + template + strided_array_view::value, OtherValueType>::type, rank> + as_strided_array_view() const + { + static_assert((sizeof(OtherValueType) >= sizeof(value_type)) && + (sizeof(OtherValueType) % sizeof(value_type) == 0), + "OtherValueType should have a size to contain a multiple of ValueTypes"); + auto d = sizeof(OtherValueType) / sizeof(value_type); - template 1), typename Dummy = std::enable_if_t> - constexpr strided_array_view operator[](size_type idx) const - { - auto ret = Base::operator[](idx); - return{ ret.data(), ret.bounds().total_size(), ret.bounds() }; - } + size_type size = this->bounds().total_size() / d; + return { (OtherValueType*)this->data(), + size, + bounds_type{ resize_extent(this->bounds().index_bounds(), d), + resize_stride(this->bounds().strides(), d) } }; + } -private: - static index_type resize_extent(const index_type& extent, size_t d) - { - fail_fast_assert(extent[rank - 1] >= d && (extent[rank-1] % d == 0), "The last dimension of the array needs to contain a multiple of new type elements"); + strided_array_view section(index_type origin, index_type extents) const + { + size_type size = this->bounds().total_size() - this->bounds().linearize(origin); + return { &this->operator[](origin), size, bounds_type{ extents, details::make_stride(Base::bounds()) } }; + } - index_type ret = extent; - ret[rank - 1] /= d; + constexpr reference operator[](const index_type& idx) const + { + return Base::operator[](idx); + } - return ret; - } + template 1), typename Dummy = std::enable_if_t> + constexpr strided_array_view operator[](size_type idx) const + { + auto ret = Base::operator[](idx); + return { ret.data(), ret.bounds().total_size(), ret.bounds() }; + } - template > - static index_type resize_stride(const index_type& strides, size_t , void * = 0) - { - fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized"); + private: + static index_type resize_extent(const index_type& extent, size_t d) + { + fail_fast_assert(extent[rank - 1] >= d && (extent[rank - 1] % d == 0), + "The last dimension of the array needs to contain a multiple of new type " + "elements"); - return strides; - } + index_type ret = extent; + ret[rank - 1] /= d; - template 1), typename Dummy = std::enable_if_t> - static index_type resize_stride(const index_type& strides, size_t d) - { - fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized"); - fail_fast_assert(strides[rank - 2] >= d && (strides[rank - 2] % d == 0), "The strides must have contiguous chunks of memory that can contain a multiple of new type elements"); + return ret; + } - for (size_t i = rank - 1; i > 0; --i) - fail_fast_assert((strides[i-1] >= strides[i]) && (strides[i-1] % strides[i] == 0), "Only strided arrays with regular strides can be resized"); + template > + static index_type resize_stride(const index_type& strides, size_t, void* = 0) + { + fail_fast_assert(strides[rank - 1] == 1, + "Only strided arrays with regular strides can be resized"); - index_type ret = strides / d; - ret[rank - 1] = 1; + return strides; + } - return ret; - } + template 1), typename Dummy = std::enable_if_t> + static index_type resize_stride(const index_type& strides, size_t d) + { + fail_fast_assert(strides[rank - 1] == 1, + "Only strided arrays with regular strides can be resized"); + fail_fast_assert(strides[rank - 2] >= d && (strides[rank - 2] % d == 0), + "The strides must have contiguous chunks of memory that can contain a " + "multiple of new type elements"); + + for (size_t i = rank - 1; i > 0; --i) + fail_fast_assert((strides[i - 1] >= strides[i]) && (strides[i - 1] % strides[i] == 0), + "Only strided arrays with regular strides can be resized"); + + index_type ret = strides / d; + ret[rank - 1] = 1; + + return ret; + } }; template -class contiguous_array_view_iterator : public std::iterator +class contiguous_array_view_iterator +: public std::iterator { - using Base = std::iterator; -public: - using typename Base::reference; - using typename Base::pointer; - using typename Base::difference_type; -private: - template - friend class basic_array_view; - pointer m_pdata; - const ArrayView * m_validator; - void validateThis() const - { - fail_fast_assert(m_pdata >= m_validator->m_pdata && m_pdata < m_validator->m_pdata + m_validator->size(), "iterator is out of range of the array"); - } - contiguous_array_view_iterator (const ArrayView *container, bool isbegin = false) : - m_pdata(isbegin ? container->m_pdata : container->m_pdata + container->size()), m_validator(container) { } -public: - reference operator*() const _NOEXCEPT - { - validateThis(); - return *m_pdata; - } - pointer operator->() const _NOEXCEPT - { - validateThis(); - return m_pdata; - } - contiguous_array_view_iterator& operator++() _NOEXCEPT - { - ++m_pdata; - return *this; - } - contiguous_array_view_iterator operator++(int)_NOEXCEPT - { - auto ret = *this; - ++(*this); - return ret; - } - contiguous_array_view_iterator& operator--() _NOEXCEPT - { - --m_pdata; - return *this; - } - contiguous_array_view_iterator operator--(int)_NOEXCEPT - { - auto ret = *this; - --(*this); - return ret; - } - contiguous_array_view_iterator operator+(difference_type n) const _NOEXCEPT - { - contiguous_array_view_iterator ret{ *this }; - return ret += n; - } - contiguous_array_view_iterator& operator+=(difference_type n) _NOEXCEPT - { - m_pdata += n; - return *this; - } - contiguous_array_view_iterator operator-(difference_type n) const _NOEXCEPT - { - contiguous_array_view_iterator ret{ *this }; - return ret -= n; - } - contiguous_array_view_iterator& operator-=(difference_type n) _NOEXCEPT - { - return *this += -n; - } - difference_type operator-(const contiguous_array_view_iterator& rhs) const _NOEXCEPT - { - fail_fast_assert(m_validator == rhs.m_validator); - return m_pdata - rhs.m_pdata; - } - reference operator[](difference_type n) const _NOEXCEPT - { - return *(*this + n); - } - bool operator==(const contiguous_array_view_iterator& rhs) const _NOEXCEPT - { - fail_fast_assert(m_validator == rhs.m_validator); - return m_pdata == rhs.m_pdata; - } - bool operator!=(const contiguous_array_view_iterator& rhs) const _NOEXCEPT - { - return !(*this == rhs); - } - bool operator<(const contiguous_array_view_iterator& rhs) const _NOEXCEPT - { - fail_fast_assert(m_validator == rhs.m_validator); - return m_pdata < rhs.m_pdata; - } - bool operator<=(const contiguous_array_view_iterator& rhs) const _NOEXCEPT - { - return !(rhs < *this); - } - bool operator>(const contiguous_array_view_iterator& rhs) const _NOEXCEPT - { - return rhs < *this; - } - bool operator>=(const contiguous_array_view_iterator& rhs) const _NOEXCEPT - { - return !(rhs > *this); - } - void swap(contiguous_array_view_iterator& rhs) _NOEXCEPT - { - std::swap(m_pdata, rhs.m_pdata); - std::swap(m_validator, rhs.m_validator); - } + using Base = std::iterator; + + public: + using typename Base::reference; + using typename Base::pointer; + using typename Base::difference_type; + + private: + template friend class basic_array_view; + pointer m_pdata; + const ArrayView* m_validator; + void validateThis() const + { + fail_fast_assert(m_pdata >= m_validator->m_pdata && m_pdata < m_validator->m_pdata + m_validator->size(), + "iterator is out of range of the array"); + } + contiguous_array_view_iterator(const ArrayView* container, bool isbegin = false) + : m_pdata(isbegin ? container->m_pdata : container->m_pdata + container->size()), m_validator(container) + { + } + + public: + reference operator*() const _NOEXCEPT + { + validateThis(); + return *m_pdata; + } + pointer operator->() const _NOEXCEPT + { + validateThis(); + return m_pdata; + } + contiguous_array_view_iterator& operator++() _NOEXCEPT + { + ++m_pdata; + return *this; + } + contiguous_array_view_iterator operator++(int)_NOEXCEPT + { + auto ret = *this; + ++(*this); + return ret; + } + contiguous_array_view_iterator& operator--() _NOEXCEPT + { + --m_pdata; + return *this; + } + contiguous_array_view_iterator operator--(int)_NOEXCEPT + { + auto ret = *this; + --(*this); + return ret; + } + contiguous_array_view_iterator operator+(difference_type n) const _NOEXCEPT + { + contiguous_array_view_iterator ret{ *this }; + return ret += n; + } + contiguous_array_view_iterator& operator+=(difference_type n) _NOEXCEPT + { + m_pdata += n; + return *this; + } + contiguous_array_view_iterator operator-(difference_type n) const _NOEXCEPT + { + contiguous_array_view_iterator ret{ *this }; + return ret -= n; + } + contiguous_array_view_iterator& operator-=(difference_type n) _NOEXCEPT + { + return * this += -n; + } + difference_type operator-(const contiguous_array_view_iterator& rhs) const _NOEXCEPT + { + fail_fast_assert(m_validator == rhs.m_validator); + return m_pdata - rhs.m_pdata; + } + reference operator[](difference_type n) const _NOEXCEPT + { + return *(*this + n); + } + bool operator==(const contiguous_array_view_iterator& rhs) const _NOEXCEPT + { + fail_fast_assert(m_validator == rhs.m_validator); + return m_pdata == rhs.m_pdata; + } + bool operator!=(const contiguous_array_view_iterator& rhs) const _NOEXCEPT + { + return !(*this == rhs); + } + bool operator<(const contiguous_array_view_iterator& rhs) const _NOEXCEPT + { + fail_fast_assert(m_validator == rhs.m_validator); + return m_pdata < rhs.m_pdata; + } + bool operator<=(const contiguous_array_view_iterator& rhs) const _NOEXCEPT + { + return !(rhs < *this); + } + bool operator>(const contiguous_array_view_iterator& rhs) const _NOEXCEPT + { + return rhs < *this; + } + bool operator>=(const contiguous_array_view_iterator& rhs) const _NOEXCEPT + { + return !(rhs > *this); + } + void swap(contiguous_array_view_iterator& rhs) _NOEXCEPT + { + std::swap(m_pdata, rhs.m_pdata); + std::swap(m_validator, rhs.m_validator); + } }; template -contiguous_array_view_iterator operator+(typename contiguous_array_view_iterator::difference_type n, const contiguous_array_view_iterator& rhs) _NOEXCEPT +contiguous_array_view_iterator +operator+(typename contiguous_array_view_iterator::difference_type n, + const contiguous_array_view_iterator& rhs) _NOEXCEPT { - return rhs + n; + return rhs + n; } template -class general_array_view_iterator : public std::iterator +class general_array_view_iterator +: public std::iterator { - using Base = std::iterator; -public: - using typename Base::reference; - using typename Base::pointer; - using typename Base::difference_type; - using typename Base::value_type; -private: - template - friend class basic_array_view; - const ArrayView * m_container; - typename ArrayView::bounds_type::iterator m_itr; - general_array_view_iterator(const ArrayView *container, bool isbegin = false) : - m_container(container), m_itr(isbegin ? m_container->bounds().begin() : m_container->bounds().end()) - { - } -public: - reference operator*() const _NOEXCEPT - { - return (*m_container)[*m_itr]; - } - pointer operator->() const _NOEXCEPT - { - return &(*m_container)[*m_itr]; - } - general_array_view_iterator& operator++() _NOEXCEPT - { - ++m_itr; - return *this; - } - general_array_view_iterator operator++(int)_NOEXCEPT - { - auto ret = *this; - ++(*this); - return ret; - } - general_array_view_iterator& operator--() _NOEXCEPT - { - --m_itr; - return *this; - } - general_array_view_iterator operator--(int)_NOEXCEPT - { - auto ret = *this; - --(*this); - return ret; - } - general_array_view_iterator operator+(difference_type n) const _NOEXCEPT - { - general_array_view_iterator ret{ *this }; - return ret += n; - } - general_array_view_iterator& operator+=(difference_type n) _NOEXCEPT - { - m_itr += n; - return *this; - } - general_array_view_iterator operator-(difference_type n) const _NOEXCEPT - { - general_array_view_iterator ret{ *this }; - return ret -= n; - } - general_array_view_iterator& operator-=(difference_type n) _NOEXCEPT - { - return *this += -n; - } - difference_type operator-(const general_array_view_iterator& rhs) const _NOEXCEPT - { - fail_fast_assert(m_container == rhs.m_container); - return m_itr - rhs.m_itr; - } - value_type operator[](difference_type n) const _NOEXCEPT - { - return (*m_container)[m_itr[n]];; - } - bool operator==(const general_array_view_iterator& rhs) const _NOEXCEPT - { - fail_fast_assert(m_container == rhs.m_container); - return m_itr == rhs.m_itr; - } - bool operator !=(const general_array_view_iterator& rhs) const _NOEXCEPT - { - return !(*this == rhs); - } - bool operator<(const general_array_view_iterator& rhs) const _NOEXCEPT - { - fail_fast_assert(m_container == rhs.m_container); - return m_itr < rhs.m_itr; - } - bool operator<=(const general_array_view_iterator& rhs) const _NOEXCEPT - { - return !(rhs < *this); - } - bool operator>(const general_array_view_iterator& rhs) const _NOEXCEPT - { - return rhs < *this; - } - bool operator>=(const general_array_view_iterator& rhs) const _NOEXCEPT - { - return !(rhs > *this); - } - void swap(general_array_view_iterator& rhs) _NOEXCEPT - { - std::swap(m_itr, rhs.m_itr); - std::swap(m_container, rhs.m_container); - } + using Base = std::iterator; + + public: + using typename Base::reference; + using typename Base::pointer; + using typename Base::difference_type; + using typename Base::value_type; + + private: + template friend class basic_array_view; + const ArrayView* m_container; + typename ArrayView::bounds_type::iterator m_itr; + general_array_view_iterator(const ArrayView* container, bool isbegin = false) + : m_container(container), m_itr(isbegin ? m_container->bounds().begin() : m_container->bounds().end()) + { + } + + public: + reference operator*() const _NOEXCEPT + { + return (*m_container)[*m_itr]; + } + pointer operator->() const _NOEXCEPT + { + return &(*m_container)[*m_itr]; + } + general_array_view_iterator& operator++() _NOEXCEPT + { + ++m_itr; + return *this; + } + general_array_view_iterator operator++(int)_NOEXCEPT + { + auto ret = *this; + ++(*this); + return ret; + } + general_array_view_iterator& operator--() _NOEXCEPT + { + --m_itr; + return *this; + } + general_array_view_iterator operator--(int)_NOEXCEPT + { + auto ret = *this; + --(*this); + return ret; + } + general_array_view_iterator operator+(difference_type n) const _NOEXCEPT + { + general_array_view_iterator ret{ *this }; + return ret += n; + } + general_array_view_iterator& operator+=(difference_type n) _NOEXCEPT + { + m_itr += n; + return *this; + } + general_array_view_iterator operator-(difference_type n) const _NOEXCEPT + { + general_array_view_iterator ret{ *this }; + return ret -= n; + } + general_array_view_iterator& operator-=(difference_type n) _NOEXCEPT + { + return * this += -n; + } + difference_type operator-(const general_array_view_iterator& rhs) const _NOEXCEPT + { + fail_fast_assert(m_container == rhs.m_container); + return m_itr - rhs.m_itr; + } + value_type operator[](difference_type n) const _NOEXCEPT + { + return (*m_container)[m_itr[n]]; + ; + } + bool operator==(const general_array_view_iterator& rhs) const _NOEXCEPT + { + fail_fast_assert(m_container == rhs.m_container); + return m_itr == rhs.m_itr; + } + bool operator!=(const general_array_view_iterator& rhs) const _NOEXCEPT + { + return !(*this == rhs); + } + bool operator<(const general_array_view_iterator& rhs) const _NOEXCEPT + { + fail_fast_assert(m_container == rhs.m_container); + return m_itr < rhs.m_itr; + } + bool operator<=(const general_array_view_iterator& rhs) const _NOEXCEPT + { + return !(rhs < *this); + } + bool operator>(const general_array_view_iterator& rhs) const _NOEXCEPT + { + return rhs < *this; + } + bool operator>=(const general_array_view_iterator& rhs) const _NOEXCEPT + { + return !(rhs > *this); + } + void swap(general_array_view_iterator& rhs) _NOEXCEPT + { + std::swap(m_itr, rhs.m_itr); + std::swap(m_container, rhs.m_container); + } }; template -general_array_view_iterator operator+(typename general_array_view_iterator::difference_type n, const general_array_view_iterator& rhs) _NOEXCEPT +general_array_view_iterator +operator+(typename general_array_view_iterator::difference_type n, + const general_array_view_iterator& rhs) _NOEXCEPT { - return rhs + n; + return rhs + n; } } // namespace gsl diff --git a/include/fail_fast.h b/include/fail_fast.h index 477bace..e098175 100644 --- a/include/fail_fast.h +++ b/include/fail_fast.h @@ -1,17 +1,17 @@ -/////////////////////////////////////////////////////////////////////////////// -// -// Copyright (c) 2015 Microsoft Corporation. All rights reserved. -// -// This code is licensed under the MIT License (MIT). -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// +/////////////////////////////////////////////////////////////////////////////// +// +// Copyright (c) 2015 Microsoft Corporation. All rights reserved. +// +// This code is licensed under the MIT License (MIT). +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// /////////////////////////////////////////////////////////////////////////////// #pragma once @@ -27,26 +27,41 @@ namespace gsl // // Having "fail fast" result in an exception makes unit testing -// the GSL classes that rely upon it much simpler. +// the GSL classes that rely upon it much simpler. // #if defined(GSL_THROWS_FOR_TESTING) -struct fail_fast : public std::runtime_error +struct fail_fast : public std::runtime_error { - fail_fast() : std::runtime_error("") {} - explicit fail_fast(char const* const message) : std::runtime_error(message) {} + fail_fast() : std::runtime_error("") + { + } + explicit fail_fast(char const* const message) : std::runtime_error(message) + { + } }; -inline void fail_fast_assert(bool cond) { if (!cond) throw fail_fast(); } -inline void fail_fast_assert(bool cond, const char* const message) { if (!cond) throw fail_fast(message); } +inline void fail_fast_assert(bool cond) +{ + if (!cond) throw fail_fast(); +} +inline void fail_fast_assert(bool cond, const char* const message) +{ + if (!cond) throw fail_fast(message); +} #else -inline void fail_fast_assert(bool cond) { if (!cond) std::terminate(); } -inline void fail_fast_assert(bool cond, const char* const) { if (!cond) std::terminate(); } +inline void fail_fast_assert(bool cond) +{ + if (!cond) std::terminate(); +} +inline void fail_fast_assert(bool cond, const char* const) +{ + if (!cond) std::terminate(); +} #endif // GSL_THROWS_FOR_TESTING - } #endif // GSL_FAIL_FAST_H diff --git a/include/gsl.h b/include/gsl.h index 5ae4df6..1ddaed7 100644 --- a/include/gsl.h +++ b/include/gsl.h @@ -1,17 +1,17 @@ -/////////////////////////////////////////////////////////////////////////////// -// -// Copyright (c) 2015 Microsoft Corporation. All rights reserved. -// -// This code is licensed under the MIT License (MIT). -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// +/////////////////////////////////////////////////////////////////////////////// +// +// Copyright (c) 2015 Microsoft Corporation. All rights reserved. +// +// This code is licensed under the MIT License (MIT). +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// /////////////////////////////////////////////////////////////////////////////// #pragma once @@ -19,141 +19,194 @@ #ifndef GSL_GSL_H #define GSL_GSL_H -#include "array_view.h" // array_view, strided_array_view... -#include "string_view.h" // zstring, string_view, zstring_builder... +#include "array_view.h" // array_view, strided_array_view... +#include "string_view.h" // zstring, string_view, zstring_builder... #include namespace gsl { // -// GSL.owner: ownership pointers +// GSL.owner: ownership pointers // using std::unique_ptr; using std::shared_ptr; -template -using owner = T; +template using owner = T; // // GSL.assert: assertions // -#define Expects(x) gsl::fail_fast_assert((x)) -#define Ensures(x) gsl::fail_fast_assert((x)) +#define Expects(x) gsl::fail_fast_assert((x)) +#define Ensures(x) gsl::fail_fast_assert((x)) // // GSL.util: utilities // // Final_act allows you to ensure something gets run at the end of a scope -template -class Final_act +template class Final_act { -public: - explicit Final_act(F f) : f_(std::move(f)), invoke_(true) {} + public: + explicit Final_act(F f) : f_(std::move(f)), invoke_(true) + { + } - Final_act(Final_act&& other) : f_(std::move(other.f_)), invoke_(true) { other.invoke_ = false; } + Final_act(Final_act&& other) : f_(std::move(other.f_)), invoke_(true) + { + other.invoke_ = false; + } Final_act(const Final_act&) = delete; Final_act& operator=(const Final_act&) = delete; - ~Final_act() { if (invoke_) f_(); } + ~Final_act() + { + if (invoke_) f_(); + } -private: + private: F f_; bool invoke_; }; // finally() - convenience function to generate a Final_act -template -Final_act finally(const F &f) { return Final_act(f); } +template Final_act finally(const F& f) +{ + return Final_act(f); +} -template -Final_act finally(F &&f) { return Final_act(std::forward(f)); } +template Final_act finally(F&& f) +{ + return Final_act(std::forward(f)); +} // narrow_cast(): a searchable way to do narrowing casts of values -template -T narrow_cast(U u) { return static_cast(u); } +template T narrow_cast(U u) +{ + return static_cast(u); +} -struct narrowing_error : public std::exception {}; +struct narrowing_error : public std::exception +{ +}; // narrow() : a checked version of narrow_cast() that throws if the cast changed the value -template -T narrow(U u) { T t = narrow_cast(u); if (static_cast(t) != u) throw narrowing_error(); return t; } +template T narrow(U u) +{ + T t = narrow_cast(u); + if (static_cast(t) != u) throw narrowing_error(); + return t; +} // // at() - Bounds-checked way of accessing static arrays, std::array, std::vector // -template -T& at(T(&arr)[N], size_t index) { fail_fast_assert(index < N); return arr[index]; } +template T& at(T(&arr)[N], size_t index) +{ + fail_fast_assert(index < N); + return arr[index]; +} -template -T& at(std::array& arr, size_t index) { fail_fast_assert(index < N); return arr[index]; } +template T& at(std::array& arr, size_t index) +{ + fail_fast_assert(index < N); + return arr[index]; +} -template -typename Cont::value_type& at(Cont& cont, size_t index) { fail_fast_assert(index < cont.size()); return cont[index]; } +template typename Cont::value_type& at(Cont& cont, size_t index) +{ + fail_fast_assert(index < cont.size()); + return cont[index]; +} // // not_null // // Restricts a pointer or smart pointer to only hold non-null values. -// +// // Has zero size overhead over T. // -// If T is a pointer (i.e. T == U*) then -// - allow construction from U* or U& +// If T is a pointer (i.e. T == U*) then +// - allow construction from U* or U& // - disallow construction from nullptr_t // - disallow default construction // - ensure construction from U* fails with nullptr // - allow implicit conversion to U* // -template -class not_null +template class not_null { static_assert(std::is_assignable::value, "T cannot be assigned nullptr."); -public: - not_null(T t) : ptr_(t) { ensure_invariant(); } - not_null& operator=(const T& t) { ptr_ = t; ensure_invariant(); return *this; } - not_null(const not_null &other) = default; - not_null& operator=(const not_null &other) = default; + public: + not_null(T t) : ptr_(t) + { + ensure_invariant(); + } + not_null& operator=(const T& t) + { + ptr_ = t; + ensure_invariant(); + return *this; + } + + not_null(const not_null& other) = default; + not_null& operator=(const not_null& other) = default; template ::value>> - not_null(const not_null &other) + not_null(const not_null& other) { *this = other; } template ::value>> - not_null& operator=(const not_null &other) + not_null& operator=(const not_null& other) { ptr_ = other.get(); return *this; } - // prevents compilation when someone attempts to assign a nullptr + // prevents compilation when someone attempts to assign a nullptr not_null(std::nullptr_t) = delete; not_null(int) = delete; not_null& operator=(std::nullptr_t) = delete; - not_null& operator=(int) = delete; - - T get() const { + not_null& operator=(int) = delete; + + T get() const + { #ifdef _MSC_VER __assume(ptr_ != nullptr); #endif return ptr_; } // the assume() should help the optimizer - operator T() const { return get(); } - T operator->() const { return get(); } + operator T() const + { + return get(); + } + T operator->() const + { + return get(); + } - bool operator==(const T& rhs) const { return ptr_ == rhs; } - bool operator!=(const T& rhs) const { return !(*this == rhs); } -private: + bool operator==(const T& rhs) const + { + return ptr_ == rhs; + } + bool operator!=(const T& rhs) const + { + return !(*this == rhs); + } + + private: T ptr_; - // we assume that the compiler can hoist/prove away most of the checks inlined from this function + // we assume that the compiler can hoist/prove away most of the checks inlined from this + // function // if not, we could make them optional via conditional compilation - void ensure_invariant() const { fail_fast_assert(ptr_ != nullptr); } + void ensure_invariant() const + { + fail_fast_assert(ptr_ != nullptr); + } // unwanted operators...pointers only point to single objects! // TODO ensure all arithmetic ops on this type are unavailable @@ -168,26 +221,30 @@ private: }; -// +// // maybe_null // // Describes an optional pointer - provides symmetry with not_null // -template -class maybe_null_ret; +template class maybe_null_ret; -template -class maybe_null_dbg +template class maybe_null_dbg { - template - friend class maybe_null_dbg; + template friend class maybe_null_dbg; static_assert(std::is_assignable::value, "T cannot be assigned nullptr."); -public: - maybe_null_dbg() : ptr_(nullptr), tested_(false) {} - maybe_null_dbg(std::nullptr_t) : ptr_(nullptr), tested_(false) {} - maybe_null_dbg(const T& p) : ptr_(p), tested_(false) {} + public: + maybe_null_dbg() : ptr_(nullptr), tested_(false) + { + } + maybe_null_dbg(std::nullptr_t) : ptr_(nullptr), tested_(false) + { + } + + maybe_null_dbg(const T& p) : ptr_(p), tested_(false) + { + } maybe_null_dbg& operator=(const T& p) { if (ptr_ != p) @@ -199,7 +256,9 @@ public: } - maybe_null_dbg(const maybe_null_dbg& rhs) : ptr_(rhs.ptr_), tested_(false) {} + maybe_null_dbg(const maybe_null_dbg& rhs) : ptr_(rhs.ptr_), tested_(false) + { + } maybe_null_dbg& operator=(const maybe_null_dbg& rhs) { if (this != &rhs) @@ -212,10 +271,13 @@ public: template ::value>> - maybe_null_dbg(const not_null &other) : ptr_(other.get()), tested_(false) {} + maybe_null_dbg(const not_null& other) + : ptr_(other.get()), tested_(false) + { + } template ::value>> - maybe_null_dbg& operator=(const not_null &other) + maybe_null_dbg& operator=(const not_null& other) { ptr_ = other.get(); tested_ = false; @@ -224,10 +286,13 @@ public: template ::value>> - maybe_null_dbg(const maybe_null_dbg &other) : ptr_(other.ptr_), tested_(false) {} + maybe_null_dbg(const maybe_null_dbg& other) + : ptr_(other.ptr_), tested_(false) + { + } template ::value>> - maybe_null_dbg& operator=(const maybe_null_dbg &other) + maybe_null_dbg& operator=(const maybe_null_dbg& other) { ptr_ = other.ptr_; tested_ = false; @@ -236,10 +301,13 @@ public: template ::value>> - maybe_null_dbg(const maybe_null_ret &other) : ptr_(other.get()), tested_(false) {} + maybe_null_dbg(const maybe_null_ret& other) + : ptr_(other.get()), tested_(false) + { + } template ::value>> - maybe_null_dbg& operator=(const maybe_null_ret &other) + maybe_null_dbg& operator=(const maybe_null_ret& other) { ptr_ = other.get(); tested_ = false; @@ -247,27 +315,53 @@ public: } - bool present() const { tested_ = true; return ptr_ != nullptr; } + bool present() const + { + tested_ = true; + return ptr_ != nullptr; + } - bool operator==(const T& rhs) const { tested_ = true; return ptr_ == rhs; } - bool operator!=(const T& rhs) const { return !(*this == rhs); } + bool operator==(const T& rhs) const + { + tested_ = true; + return ptr_ == rhs; + } + bool operator!=(const T& rhs) const + { + return !(*this == rhs); + } template ::value>> - bool operator==(const maybe_null_dbg& rhs) const { tested_ = true; rhs.tested_ = true; return ptr_ == rhs.ptr_; } + bool operator==(const maybe_null_dbg& rhs) const + { + tested_ = true; + rhs.tested_ = true; + return ptr_ == rhs.ptr_; + } template ::value>> - bool operator!=(const maybe_null_dbg& rhs) const { return !(*this == rhs); } + bool operator!=(const maybe_null_dbg& rhs) const + { + return !(*this == rhs); + } - T get() const { + T get() const + { fail_fast_assert(tested_); #ifdef _MSC_VER __assume(ptr_ != nullptr); #endif - return ptr_; + return ptr_; } - operator T() const { return get(); } - T operator->() const { return get(); } + operator T() const + { + return get(); + } + T operator->() const + { + return get(); + } -private: + private: // unwanted operators...pointers only point to single objects! // TODO ensure all arithmetic ops on this type are unavailable maybe_null_dbg& operator++() = delete; @@ -283,25 +377,38 @@ private: mutable bool tested_; }; -template -class maybe_null_ret +template class maybe_null_ret { static_assert(std::is_assignable::value, "T cannot be assigned nullptr."); -public: - maybe_null_ret() : ptr_(nullptr) {} - maybe_null_ret(std::nullptr_t) : ptr_(nullptr) {} - maybe_null_ret(const T& p) : ptr_(p) {} - maybe_null_ret& operator=(const T& p) { ptr_ = p; return *this; } - + public: + maybe_null_ret() : ptr_(nullptr) + { + } + maybe_null_ret(std::nullptr_t) : ptr_(nullptr) + { + } + + maybe_null_ret(const T& p) : ptr_(p) + { + } + maybe_null_ret& operator=(const T& p) + { + ptr_ = p; + return *this; + } + maybe_null_ret(const maybe_null_ret& rhs) = default; maybe_null_ret& operator=(const maybe_null_ret& rhs) = default; template ::value>> - maybe_null_ret(const not_null &other) : ptr_(other.get()) {} + maybe_null_ret(const not_null& other) + : ptr_(other.get()) + { + } template ::value>> - maybe_null_ret& operator=(const not_null &other) + maybe_null_ret& operator=(const not_null& other) { ptr_ = other.get(); return *this; @@ -309,10 +416,13 @@ public: template ::value>> - maybe_null_ret(const maybe_null_ret &other) : ptr_(other.get()) {} + maybe_null_ret(const maybe_null_ret& other) + : ptr_(other.get()) + { + } template ::value>> - maybe_null_ret& operator=(const maybe_null_ret &other) + maybe_null_ret& operator=(const maybe_null_ret& other) { ptr_ = other.get(); return *this; @@ -320,24 +430,39 @@ public: template ::value>> - maybe_null_ret(const maybe_null_dbg &other) : ptr_(other.get()) {} + maybe_null_ret(const maybe_null_dbg& other) + : ptr_(other.get()) + { + } template ::value>> - maybe_null_ret& operator=(const maybe_null_dbg &other) + maybe_null_ret& operator=(const maybe_null_dbg& other) { ptr_ = other.get(); return *this; } - bool present() const { return ptr_ != nullptr; } + bool present() const + { + return ptr_ != nullptr; + } - T get() const { return ptr_; } + T get() const + { + return ptr_; + } - operator T() const { return get(); } - T operator->() const { return get(); } + operator T() const + { + return get(); + } + T operator->() const + { + return get(); + } -private: + private: // unwanted operators...pointers only point to single objects! // TODO ensure all arithmetic ops on this type are unavailable maybe_null_ret& operator++() = delete; @@ -352,7 +477,7 @@ private: T ptr_; }; -template using maybe_null = maybe_null_ret; +template using maybe_null = maybe_null_ret; } // namespace gsl diff --git a/include/string_view.h b/include/string_view.h index 4076d52..37998a9 100644 --- a/include/string_view.h +++ b/include/string_view.h @@ -1,17 +1,17 @@ -/////////////////////////////////////////////////////////////////////////////// -// -// Copyright (c) 2015 Microsoft Corporation. All rights reserved. -// -// This code is licensed under the MIT License (MIT). -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// +/////////////////////////////////////////////////////////////////////////////// +// +// Copyright (c) 2015 Microsoft Corporation. All rights reserved. +// +// This code is licensed under the MIT License (MIT). +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// /////////////////////////////////////////////////////////////////////////////// #pragma once @@ -27,24 +27,20 @@ namespace gsl // // czstring and wzstring // -// These are "tag" typedef's for C-style strings (i.e. null-terminated character arrays) +// These are "tag" typedef's for C-style strings (i.e. null-terminated character arrays) // that allow static analysis to help find bugs. // // There are no additional features/semantics that we can find a way to add inside the // type system for these types that will not either incur significant runtime costs or // (sometimes needlessly) break existing programs when introduced. // -template -using czstring = const char*; +template using czstring = const char*; -template -using cwzstring = const wchar_t*; +template using cwzstring = const wchar_t*; -template -using zstring = char*; +template using zstring = char*; -template -using wzstring = wchar_t*; +template using wzstring = wchar_t*; // // string_view and relatives @@ -57,34 +53,33 @@ using wzstring = wchar_t*; template using basic_string_view = array_view; -template -using string_view = basic_string_view; +template using string_view = basic_string_view; -template -using cstring_view = basic_string_view; +template using cstring_view = basic_string_view; -template -using wstring_view = basic_string_view; +template using wstring_view = basic_string_view; -template +template using cwstring_view = basic_string_view; // -// ensure_sentinel() +// ensure_sentinel() // // Provides a way to obtain an array_view from a contiguous sequence // that ends with a (non-inclusive) sentinel value. // // Will fail-fast if sentinel cannot be found before max elements are examined. // -template -array_view ensure_sentinel(const T* seq, SizeType max = std::numeric_limits::max()) +template +array_view +ensure_sentinel(const T* seq, SizeType max = std::numeric_limits::max()) { auto cur = seq; - while ((cur - seq) < max && *cur != Sentinel) ++cur; + while ((cur - seq) < max && *cur != Sentinel) + ++cur; fail_fast_assert(*cur == Sentinel); - return{ seq, cur - seq }; + return { seq, cur - seq }; } @@ -93,42 +88,51 @@ array_view ensure_sentinel(const T* seq, SizeType max = std::n // Will fail fast if a null-terminator cannot be found before // the limit of size_type. // -template -inline basic_string_view ensure_z(T* const & sz, size_t max = std::numeric_limits::max()) +template +inline basic_string_view +ensure_z(T* const& sz, size_t max = std::numeric_limits::max()) { return ensure_sentinel<0>(sz, max); } -// TODO (neilmac) there is probably a better template-magic way to get the const and non-const overloads to share an implementation -inline basic_string_view ensure_z(char* const & sz, size_t max) +// TODO (neilmac) there is probably a better template-magic way to get the const and non-const +// overloads to share an implementation +inline basic_string_view ensure_z(char* const& sz, size_t max) { auto len = strnlen(sz, max); - fail_fast_assert(sz[len] == 0); return{ sz, len }; + fail_fast_assert(sz[len] == 0); + return { sz, len }; } inline basic_string_view ensure_z(const char* const& sz, size_t max) { auto len = strnlen(sz, max); - fail_fast_assert(sz[len] == 0); return{ sz, len }; + fail_fast_assert(sz[len] == 0); + return { sz, len }; } -inline basic_string_view ensure_z(wchar_t* const & sz, size_t max) +inline basic_string_view ensure_z(wchar_t* const& sz, size_t max) { auto len = wcsnlen(sz, max); - fail_fast_assert(sz[len] == 0); return{ sz, len }; + fail_fast_assert(sz[len] == 0); + return { sz, len }; } -inline basic_string_view ensure_z(const wchar_t* const & sz, size_t max) +inline basic_string_view ensure_z(const wchar_t* const& sz, size_t max) { auto len = wcsnlen(sz, max); - fail_fast_assert(sz[len] == 0); return{ sz, len }; + fail_fast_assert(sz[len] == 0); + return { sz, len }; } -template -basic_string_view ensure_z(T(&sz)[N]) { return ensure_z(&sz[0], N); } +template basic_string_view ensure_z(T(&sz)[N]) +{ + return ensure_z(&sz[0], N); +} -template -basic_string_view::type, dynamic_range> ensure_z(Cont& cont) +template +basic_string_view::type, dynamic_range> +ensure_z(Cont& cont) { return ensure_z(cont.data(), cont.length()); } @@ -136,48 +140,69 @@ basic_string_view::type, dy // // to_string() allow (explicit) conversions from string_view to string // -template +template std::basic_string::type> to_string(basic_string_view view) { - return{ view.data(), view.length() }; + return { view.data(), view.length() }; } -template -class basic_zstring_builder +template class basic_zstring_builder { -public: + public: using string_view_type = basic_string_view; using value_type = CharT; using pointer = CharT*; using size_type = typename string_view_type::size_type; using iterator = typename string_view_type::iterator; - basic_zstring_builder(CharT* data, size_type length) : sv_(data, length) {} + basic_zstring_builder(CharT* data, size_type length) : sv_(data, length) + { + } - template - basic_zstring_builder(CharT(&arr)[Size]) : sv_(arr) {} + template basic_zstring_builder(CharT(&arr)[Size]) : sv_(arr) + { + } - pointer data() const { return sv_.data(); } - string_view_type view() const { return sv_; } + pointer data() const + { + return sv_.data(); + } + string_view_type view() const + { + return sv_; + } - size_type length() const { return sv_.length(); } + size_type length() const + { + return sv_.length(); + } - pointer assume0() const { return data(); } - string_view_type ensure_z() const { return gsl::ensure_z(sv_); } + pointer assume0() const + { + return data(); + } + string_view_type ensure_z() const + { + return gsl::ensure_z(sv_); + } - iterator begin() const { return sv_.begin(); } - iterator end() const { return sv_.end(); } + iterator begin() const + { + return sv_.begin(); + } + iterator end() const + { + return sv_.end(); + } -private: + private: string_view_type sv_; }; -template -using zstring_builder = basic_zstring_builder; +template using zstring_builder = basic_zstring_builder; -template -using wzstring_builder = basic_zstring_builder; +template using wzstring_builder = basic_zstring_builder; } #endif // GSL_STRING_VIEW_H diff --git a/tests/array_view_tests.cpp b/tests/array_view_tests.cpp index 918df9e..75b1cc9 100644 --- a/tests/array_view_tests.cpp +++ b/tests/array_view_tests.cpp @@ -1,20 +1,20 @@ -/////////////////////////////////////////////////////////////////////////////// -// -// Copyright (c) 2015 Microsoft Corporation. All rights reserved. -// -// This code is licensed under the MIT License (MIT). -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// +/////////////////////////////////////////////////////////////////////////////// +// +// Copyright (c) 2015 Microsoft Corporation. All rights reserved. +// +// This code is licensed under the MIT License (MIT). +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// /////////////////////////////////////////////////////////////////////////////// -#include +#include #include #include #include @@ -29,1341 +29,1381 @@ using namespace std; using namespace gsl; -namespace +namespace { - void use(int&) {} - struct BaseClass {}; - struct DerivedClass : BaseClass {}; +void use(int&) +{ +} +struct BaseClass +{ +}; +struct DerivedClass : BaseClass +{ +}; } SUITE(array_view_tests) { - TEST(basics) - { - auto ptr = as_array_view(new int[10], 10); - fill(ptr.begin(), ptr.end(), 99); - for (int num : ptr) - { - CHECK(num == 99); - } + TEST(basics) + { + auto ptr = as_array_view(new int[10], 10); + fill(ptr.begin(), ptr.end(), 99); + for (int num : ptr) + { + CHECK(num == 99); + } - delete[] ptr.data(); + delete[] ptr.data(); - static_bounds bounds{ 3 }; - -#ifdef CONFIRM_COMPILATION_ERRORS - array_view av(nullptr, bounds); - av.extent(); - av.extent<2>(); - av[8][4][3]; -#endif - } - - TEST (array_view_convertible) - { -#ifdef CONFIRM_COMPILATION_ERRORS - array_view av1(nullptr, b1); -#endif - - auto f = [&]() { array_view av1(nullptr); }; - CHECK_THROW(f(), fail_fast); - - array_view av1(nullptr); + static_bounds bounds{ 3 }; #ifdef CONFIRM_COMPILATION_ERRORS - static_bounds b12(b11); - b12 = b11; - b11 = b12; - - array_view av1 = nullptr; - array_view av2(av1); - array_view av2(av1); + array_view av(nullptr, bounds); + av.extent(); + av.extent<2>(); + av[8][4][3]; #endif + } - array_view avd; + TEST(array_view_convertible) + { #ifdef CONFIRM_COMPILATION_ERRORS - array_view avb = avd; + array_view av1(nullptr, b1); #endif - array_view avcd = avd; - } - TEST(boundary_checks) - { - int arr[10][2]; - auto av = as_array_view(arr); + auto f = [&]() + { + array_view av1(nullptr); + }; + CHECK_THROW(f(), fail_fast); - fill(begin(av), end(av), 0); - - av[2][0] = 1; - av[1][1] = 3; - - // out of bounds - CHECK_THROW(av[1][3] = 3, fail_fast); - CHECK_THROW((av[{1, 3}] = 3), fail_fast); - - CHECK_THROW(av[10][2], fail_fast); - CHECK_THROW((av[{10,2}]), fail_fast); - } - - void overloaded_func(array_view exp, int expected_value) { - for (auto val : exp) - { - CHECK(val == expected_value); - } - } - - void overloaded_func(array_view exp, char expected_value) { - for (auto val : exp) - { - CHECK(val == expected_value); - } - } - - void fixed_func(array_view exp, int expected_value) { - for (auto val : exp) - { - CHECK(val == expected_value); - } - } - - TEST(array_view_parameter_test) - { - auto data = new int[4][3][5]; - - auto av = as_array_view(data, 4); - - CHECK(av.size() == 60); - - fill(av.begin(), av.end(), 34); - - int count = 0; - for_each(av.rbegin(), av.rend(), [&](int val) { count += val; }); - CHECK(count == 34 * 60); - overloaded_func(av, 34); - - overloaded_func(av.as_array_view(dim<>(4), dim<>(3), dim<>(5)), 34); - - //fixed_func(av, 34); - delete[] data; - } - - - TEST(md_access) - { - unsigned int width = 5, height = 20; - - unsigned int imgSize = width * height; - auto image_ptr = new int[imgSize][3]; - - // size check will be done - auto image_view = as_array_view(image_ptr, imgSize).as_array_view(dim<>(height), dim<>(width), dim<3>()); - - iota(image_view.begin(), image_view.end(), 1); - - int expected = 0; - for (unsigned int i = 0; i < height; i++) - { - for (unsigned int j = 0; j < width; j++) - { - CHECK(expected + 1 == image_view[i][j][0]); - CHECK(expected + 2 == image_view[i][j][1]); - CHECK(expected + 3 == image_view[i][j][2]); - - auto val = image_view[{i, j, 0}]; - CHECK(expected + 1 == val); - val = image_view[{i, j, 1}]; - CHECK(expected + 2 == val); - val = image_view[{i, j, 2}]; - CHECK(expected + 3 == val); - - expected += 3; - } - } - } - - TEST(array_view_factory_test) - { - { - int * arr = new int[150]; - - auto av = as_array_view(arr, dim<10>(), dim<>(3), dim<5>()); - - fill(av.begin(), av.end(), 24); - overloaded_func(av, 24); - - delete[] arr; - - - array stdarr{ 0 }; - auto av2 = as_array_view(stdarr); - overloaded_func(av2.as_array_view(dim<>(1), dim<3>(), dim<5>()), 0); - - - string str = "ttttttttttttttt"; // size = 15 - auto t = str.data(); - auto av3 = as_array_view(str); - overloaded_func(av3.as_array_view(dim<>(1), dim<3>(), dim<5>()), 't'); - } - - { - int a[3][4][5]; - auto av = as_array_view(a); - const int (*b)[4][5]; - b = a; - auto bv = as_array_view(b, 3); - - CHECK(av == bv); - - const std::array arr = {0.0, 0.0, 0.0}; - auto cv = as_array_view(arr); - - vector vec(3); - auto dv = as_array_view(vec); - -#ifdef CONFIRM_COMPILATION_ERRORS - auto dv2 = as_array_view(std::move(vec)); -#endif - } - } - - TEST (array_view_reshape_test) - { - int a[3][4][5]; - auto av = as_array_view(a); - auto av2 = av.as_array_view(dim<60>()); - auto av3 = av2.as_array_view(dim<3>(), dim<4>(), dim<5>()); - auto av4 = av3.as_array_view(dim<4>(), dim<>(3), dim<5>()); - auto av5 = av4.as_array_view(dim<3>(), dim<4>(), dim<5>()); - auto av6 = av5.as_array_view(dim<12>(), dim<>(5)); - - fill(av6.begin(), av6.end(), 1); - - auto av7 = av6.as_bytes(); - - auto av8 = av7.as_array_view(); - - CHECK(av8.size() == av6.size()); - for (size_t i = 0; i < av8.size(); i++) - { - CHECK(av8[i] == 1); - } + array_view av1(nullptr); #ifdef CONFIRM_COMPILATION_ERRORS - struct Foo {char c[11];}; - auto av9 = av7.as_array_view(); + static_bounds b12(b11); + b12 = b11; + b11 = b12; + + array_view av1 = nullptr; + array_view av2(av1); + array_view av2(av1); #endif - } + + array_view avd; +#ifdef CONFIRM_COMPILATION_ERRORS + array_view avb = avd; +#endif + array_view avcd = avd; + } + + TEST(boundary_checks) + { + int arr[10][2]; + auto av = as_array_view(arr); + + fill(begin(av), end(av), 0); + + av[2][0] = 1; + av[1][1] = 3; + + // out of bounds + CHECK_THROW(av[1][3] = 3, fail_fast); + CHECK_THROW((av[{ 1, 3 }] = 3), fail_fast); + + CHECK_THROW(av[10][2], fail_fast); + CHECK_THROW((av[{ 10, 2 }]), fail_fast); + } + + void overloaded_func(array_view exp, int expected_value) + { + for (auto val : exp) + { + CHECK(val == expected_value); + } + } + + void overloaded_func(array_view exp, char expected_value) + { + for (auto val : exp) + { + CHECK(val == expected_value); + } + } + + void fixed_func(array_view exp, int expected_value) + { + for (auto val : exp) + { + CHECK(val == expected_value); + } + } + + TEST(array_view_parameter_test) + { + auto data = new int[4][3][5]; + + auto av = as_array_view(data, 4); + + CHECK(av.size() == 60); + + fill(av.begin(), av.end(), 34); + + int count = 0; + for_each(av.rbegin(), av.rend(), [&](int val) + { + count += val; + }); + CHECK(count == 34 * 60); + overloaded_func(av, 34); + + overloaded_func(av.as_array_view(dim<>(4), dim<>(3), dim<>(5)), 34); + + // fixed_func(av, 34); + delete[] data; + } - TEST (array_view_section_test) - { - int a[30][4][5]; - - auto av = as_array_view(a); - auto sub = av.section({15, 0, 0}, gsl::index<3>{2, 2, 2}); - auto subsub = sub.section({1, 0, 0}, gsl::index<3>{1, 1, 1}); - } + TEST(md_access) + { + unsigned int width = 5, height = 20; - TEST(array_view_section) - { - std::vector data(5 * 10); - std::iota(begin(data), end(data), 0); - const array_view av = as_array_view(data).as_array_view(dim<5>(), dim<10>()); + unsigned int imgSize = width * height; + auto image_ptr = new int[imgSize][3]; - strided_array_view av_section_1 = av.section({ 1, 2 }, { 3, 4 }); - CHECK((av_section_1[{0, 0}] == 12)); - CHECK((av_section_1[{0, 1}] == 13)); - CHECK((av_section_1[{1, 0}] == 22)); - CHECK((av_section_1[{2, 3}] == 35)); + // size check will be done + auto image_view = + as_array_view(image_ptr, imgSize).as_array_view(dim<>(height), dim<>(width), dim<3>()); - strided_array_view av_section_2 = av_section_1.section({ 1, 2 }, { 2,2 }); - CHECK((av_section_2[{0, 0}] == 24)); - CHECK((av_section_2[{0, 1}] == 25)); - CHECK((av_section_2[{1, 0}] == 34)); - } - - TEST(strided_array_view_constructors) - { - // Check stride constructor - { - int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; - const int carr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + iota(image_view.begin(), image_view.end(), 1); - strided_array_view sav1{ arr, {{9}, {1}} }; // T -> T - CHECK(sav1.bounds().index_bounds() == index<1>{ 9 }); - CHECK(sav1.bounds().stride() == 1); - CHECK(sav1[0] == 1 && sav1[8] == 9); + int expected = 0; + for (unsigned int i = 0; i < height; i++) + { + for (unsigned int j = 0; j < width; j++) + { + CHECK(expected + 1 == image_view[i][j][0]); + CHECK(expected + 2 == image_view[i][j][1]); + CHECK(expected + 3 == image_view[i][j][2]); + + auto val = image_view[{ i, j, 0 }]; + CHECK(expected + 1 == val); + val = image_view[{ i, j, 1 }]; + CHECK(expected + 2 == val); + val = image_view[{ i, j, 2 }]; + CHECK(expected + 3 == val); + + expected += 3; + } + } + } + + TEST(array_view_factory_test) + { + { + int* arr = new int[150]; + + auto av = as_array_view(arr, dim<10>(), dim<>(3), dim<5>()); + + fill(av.begin(), av.end(), 24); + overloaded_func(av, 24); + + delete[] arr; - strided_array_view sav2{ carr, {{ 4 }, { 2 }} }; // const T -> const T - CHECK(sav2.bounds().index_bounds() == index<1>{ 4 }); - CHECK(sav2.bounds().strides() == index<1>{2}); - CHECK(sav2[0] == 1 && sav2[3] == 7); + array stdarr{ 0 }; + auto av2 = as_array_view(stdarr); + overloaded_func(av2.as_array_view(dim<>(1), dim<3>(), dim<5>()), 0); - strided_array_view sav3{ arr, {{ 2, 2 },{ 6, 2 }} }; // T -> const T - CHECK((sav3.bounds().index_bounds() == index<2>{ 2, 2 })); - CHECK((sav3.bounds().strides() == index<2>{ 6, 2 })); - CHECK((sav3[{0, 0}] == 1 && sav3[{0, 1}] == 3 && sav3[{1, 0}] == 7)); - } - // Check array_view constructor - { - int arr[] = { 1, 2 }; + string str = "ttttttttttttttt"; // size = 15 + auto t = str.data(); + auto av3 = as_array_view(str); + overloaded_func(av3.as_array_view(dim<>(1), dim<3>(), dim<5>()), 't'); + } - // From non-cv-qualified source - { - const array_view src{ arr }; + { + int a[3][4][5]; + auto av = as_array_view(a); + const int(*b)[4][5]; + b = a; + auto bv = as_array_view(b, 3); - strided_array_view sav{ src, {2, 1} }; - CHECK(sav.bounds().index_bounds() == index<1>{ 2 }); - CHECK(sav.bounds().strides() == index<1>{ 1 }); - CHECK(sav[1] == 2); + CHECK(av == bv); + + const std::array arr = { 0.0, 0.0, 0.0 }; + auto cv = as_array_view(arr); + + vector vec(3); + auto dv = as_array_view(vec); + +#ifdef CONFIRM_COMPILATION_ERRORS + auto dv2 = as_array_view(std::move(vec)); +#endif + } + } + + TEST(array_view_reshape_test) + { + int a[3][4][5]; + auto av = as_array_view(a); + auto av2 = av.as_array_view(dim<60>()); + auto av3 = av2.as_array_view(dim<3>(), dim<4>(), dim<5>()); + auto av4 = av3.as_array_view(dim<4>(), dim<>(3), dim<5>()); + auto av5 = av4.as_array_view(dim<3>(), dim<4>(), dim<5>()); + auto av6 = av5.as_array_view(dim<12>(), dim<>(5)); + + fill(av6.begin(), av6.end(), 1); + + auto av7 = av6.as_bytes(); + + auto av8 = av7.as_array_view(); + + CHECK(av8.size() == av6.size()); + for (size_t i = 0; i < av8.size(); i++) + { + CHECK(av8[i] == 1); + } + +#ifdef CONFIRM_COMPILATION_ERRORS + struct Foo + { + char c[11]; + }; + auto av9 = av7.as_array_view(); +#endif + } + + + TEST(array_view_section_test) + { + int a[30][4][5]; + + auto av = as_array_view(a); + auto sub = av.section({ 15, 0, 0 }, gsl::index<3>{ 2, 2, 2 }); + auto subsub = sub.section({ 1, 0, 0 }, gsl::index<3>{ 1, 1, 1 }); + } + + TEST(array_view_section) + { + std::vector data(5 * 10); + std::iota(begin(data), end(data), 0); + const array_view av = as_array_view(data).as_array_view(dim<5>(), dim<10>()); + + strided_array_view av_section_1 = av.section({ 1, 2 }, { 3, 4 }); + CHECK((av_section_1[{ 0, 0 }] == 12)); + CHECK((av_section_1[{ 0, 1 }] == 13)); + CHECK((av_section_1[{ 1, 0 }] == 22)); + CHECK((av_section_1[{ 2, 3 }] == 35)); + + strided_array_view av_section_2 = av_section_1.section({ 1, 2 }, { 2, 2 }); + CHECK((av_section_2[{ 0, 0 }] == 24)); + CHECK((av_section_2[{ 0, 1 }] == 25)); + CHECK((av_section_2[{ 1, 0 }] == 34)); + } + + TEST(strided_array_view_constructors) + { + // Check stride constructor + { + int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + const int carr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + + strided_array_view sav1{ arr, { { 9 }, { 1 } } }; // T -> T + CHECK(sav1.bounds().index_bounds() == index<1>{ 9 }); + CHECK(sav1.bounds().stride() == 1); + CHECK(sav1[0] == 1 && sav1[8] == 9); + + + strided_array_view sav2{ carr, { { 4 }, { 2 } } }; // const T -> const T + CHECK(sav2.bounds().index_bounds() == index<1>{ 4 }); + CHECK(sav2.bounds().strides() == index<1>{ 2 }); + CHECK(sav2[0] == 1 && sav2[3] == 7); + + strided_array_view sav3{ arr, { { 2, 2 }, { 6, 2 } } }; // T -> const T + CHECK((sav3.bounds().index_bounds() == index<2>{ 2, 2 })); + CHECK((sav3.bounds().strides() == index<2>{ 6, 2 })); + CHECK((sav3[{ 0, 0 }] == 1 && sav3[{ 0, 1 }] == 3 && sav3[{ 1, 0 }] == 7)); + } + + // Check array_view constructor + { + int arr[] = { 1, 2 }; + + // From non-cv-qualified source + { + const array_view src{ arr }; + + strided_array_view sav{ src, { 2, 1 } }; + CHECK(sav.bounds().index_bounds() == index<1>{ 2 }); + CHECK(sav.bounds().strides() == index<1>{ 1 }); + CHECK(sav[1] == 2); #if _MSC_VER > 1800 - strided_array_view sav_c{ {src}, {2, 1} }; -#else - strided_array_view sav_c{ array_view{src}, strided_bounds<1>{2, 1} }; -#endif - CHECK(sav_c.bounds().index_bounds() == index<1>{ 2 }); - CHECK(sav_c.bounds().strides() == index<1>{ 1 }); - CHECK(sav_c[1] == 2); - -#if _MSC_VER > 1800 - strided_array_view sav_v{ {src}, {2, 1} }; -#else - strided_array_view sav_v{ array_view{src}, strided_bounds<1>{2, 1} }; -#endif - CHECK(sav_v.bounds().index_bounds() == index<1>{ 2 }); - CHECK(sav_v.bounds().strides() == index<1>{ 1 }); - CHECK(sav_v[1] == 2); - -#if _MSC_VER > 1800 - strided_array_view sav_cv{ {src}, {2, 1} }; -#else - strided_array_view sav_cv{ array_view{src}, strided_bounds<1>{2, 1} }; -#endif - CHECK(sav_cv.bounds().index_bounds() == index<1>{ 2 }); - CHECK(sav_cv.bounds().strides() == index<1>{ 1 }); - CHECK(sav_cv[1] == 2); - } - - // From const-qualified source - { - const array_view src{ arr }; - - strided_array_view sav_c{ src, {2, 1} }; - CHECK(sav_c.bounds().index_bounds() == index<1>{ 2 }); - CHECK(sav_c.bounds().strides() == index<1>{ 1 }); - CHECK(sav_c[1] == 2); - -#if _MSC_VER > 1800 - strided_array_view sav_cv{ {src}, {2, 1} }; + strided_array_view sav_c{ { src }, { 2, 1 } }; #else - strided_array_view sav_cv{ array_view{src}, strided_bounds<1>{2, 1} }; -#endif - - CHECK(sav_cv.bounds().index_bounds() == index<1>{ 2 }); - CHECK(sav_cv.bounds().strides() == index<1>{ 1 }); - CHECK(sav_cv[1] == 2); - } - - // From volatile-qualified source - { - const array_view src{ arr }; - - strided_array_view sav_v{ src, {2, 1} }; - CHECK(sav_v.bounds().index_bounds() == index<1>{ 2 }); - CHECK(sav_v.bounds().strides() == index<1>{ 1 }); - CHECK(sav_v[1] == 2); + strided_array_view sav_c{ array_view{ src }, + strided_bounds<1>{ 2, 1 } }; +#endif + CHECK(sav_c.bounds().index_bounds() == index<1>{ 2 }); + CHECK(sav_c.bounds().strides() == index<1>{ 1 }); + CHECK(sav_c[1] == 2); #if _MSC_VER > 1800 - strided_array_view sav_cv{ {src}, {2, 1} }; + strided_array_view sav_v{ { src }, { 2, 1 } }; #else - strided_array_view sav_cv{ array_view{src}, strided_bounds<1>{2, 1} }; -#endif - CHECK(sav_cv.bounds().index_bounds() == index<1>{ 2 }); - CHECK(sav_cv.bounds().strides() == index<1>{ 1 }); - CHECK(sav_cv[1] == 2); - } - - // From cv-qualified source - { - const array_view src{ arr }; - - strided_array_view sav_cv{ src, {2, 1} }; - CHECK(sav_cv.bounds().index_bounds() == index<1>{ 2 }); - CHECK(sav_cv.bounds().strides() == index<1>{ 1 }); - CHECK(sav_cv[1] == 2); - } - } - - // Check const-casting constructor - { - int arr[2] = { 4, 5 }; - - const array_view av(arr, 2); - array_view av2{ av }; - CHECK(av2[1] == 5); - - static_assert(std::is_convertible, array_view>::value, "ctor is not implicit!"); - - const strided_array_view src{ arr, {2, 1} }; - strided_array_view sav{ src }; - CHECK(sav.bounds().index_bounds() == index<1>{ 2 }); - CHECK(sav.bounds().stride() == 1); - CHECK(sav[1] == 5); - - static_assert(std::is_convertible, strided_array_view>::value, "ctor is not implicit!"); - } - - // Check copy constructor - { - int arr1[2] = { 3, 4 }; - const strided_array_view src1{ arr1, {2, 1} }; - strided_array_view sav1{ src1 }; - - CHECK(sav1.bounds().index_bounds() == index<1>{ 2 }); - CHECK(sav1.bounds().stride() == 1); - CHECK(sav1[0] == 3); - - int arr2[6] = { 1, 2, 3, 4, 5, 6 }; - const strided_array_view src2{ arr2, {{ 3, 2 }, { 2, 1 }} }; - strided_array_view sav2{ src2 }; - CHECK((sav2.bounds().index_bounds() == index<2>{ 3, 2 })); - CHECK((sav2.bounds().strides() == index<2>{ 2, 1 })); - CHECK((sav2[{0, 0}] == 1 && sav2[{2, 0}] == 5)); - } - - // Check const-casting assignment operator - { - int arr1[2] = { 1, 2 }; - int arr2[6] = { 3, 4, 5, 6, 7, 8 }; - - const strided_array_view src{ arr1, {{2}, {1}} }; - strided_array_view sav{ arr2, {{3}, {2}} }; - strided_array_view& sav_ref = (sav = src); - CHECK(sav.bounds().index_bounds() == index<1>{ 2 }); - CHECK(sav.bounds().strides() == index<1>{ 1 }); - CHECK(sav[0] == 1); - CHECK(&sav_ref == &sav); - } - - // Check copy assignment operator - { - int arr1[2] = { 3, 4 }; - int arr1b[1] = { 0 }; - const strided_array_view src1{ arr1, {2, 1} }; - strided_array_view sav1{ arr1b, {1, 1} }; - strided_array_view& sav1_ref = (sav1 = src1); - CHECK(sav1.bounds().index_bounds() == index<1>{ 2 }); - CHECK(sav1.bounds().strides() == index<1>{ 1 }); - CHECK(sav1[0] == 3); - CHECK(&sav1_ref == &sav1); - - const int arr2[6] = { 1, 2, 3, 4, 5, 6 }; - const int arr2b[1] = { 0 }; - const strided_array_view src2{ arr2, {{ 3, 2 },{ 2, 1 }} }; - strided_array_view sav2{ arr2b, {{ 1, 1 },{ 1, 1 }} }; - strided_array_view& sav2_ref = (sav2 = src2); - CHECK((sav2.bounds().index_bounds() == index<2>{ 3, 2 })); - CHECK((sav2.bounds().strides() == index<2>{ 2, 1 })); - CHECK((sav2[{0, 0}] == 1 && sav2[{2, 0}] == 5)); - CHECK(&sav2_ref == &sav2); - } - } - - TEST(strided_array_view_slice) - { - std::vector data(5 * 10); - std::iota(begin(data), end(data), 0); - const array_view src = as_array_view(data).as_array_view(dim<5>(), dim<10>()); - - const strided_array_view sav{ src, {{5, 10}, {10, 1}} }; -#ifdef CONFIRM_COMPILATION_ERRORS - const strided_array_view csav{ {src},{ { 5, 10 },{ 10, 1 } } }; + strided_array_view sav_v{ array_view{ src }, + strided_bounds<1>{ 2, 1 } }; #endif - const strided_array_view csav{ array_view{ src }, { { 5, 10 },{ 10, 1 } } }; + CHECK(sav_v.bounds().index_bounds() == index<1>{ 2 }); + CHECK(sav_v.bounds().strides() == index<1>{ 1 }); + CHECK(sav_v[1] == 2); - strided_array_view sav_sl = sav[2]; - CHECK(sav_sl[0] == 20); - CHECK(sav_sl[9] == 29); - - strided_array_view csav_sl = sav[3]; - CHECK(csav_sl[0] == 30); - CHECK(csav_sl[9] == 39); - - CHECK(sav[4][0] == 40); - CHECK(sav[4][9] == 49); - } - - TEST(strided_array_view_column_major) - { - // strided_array_view may be used to accomodate more peculiar - // use cases, such as column-major multidimensional array - // (aka. "FORTRAN" layout). - - int cm_array[3 * 5] = { - 1, 4, 7, 10, 13, - 2, 5, 8, 11, 14, - 3, 6, 9, 12, 15 - }; - strided_array_view cm_sav{ cm_array, {{ 5, 3 },{ 1, 5 }} }; - - // Accessing elements - CHECK((cm_sav[{0, 0}] == 1)); - CHECK((cm_sav[{0, 1}] == 2)); - CHECK((cm_sav[{1, 0}] == 4)); - CHECK((cm_sav[{4, 2}] == 15)); - - // Slice - strided_array_view cm_sl = cm_sav[3]; - - CHECK(cm_sl[0] == 10); - CHECK(cm_sl[1] == 11); - CHECK(cm_sl[2] == 12); - - // Section - strided_array_view cm_sec = cm_sav.section( { 2, 1 }, { 3, 2 }); - - CHECK((cm_sec.bounds().index_bounds() == index<2>{3, 2})); - CHECK((cm_sec[{0, 0}] == 8)); - CHECK((cm_sec[{0, 1}] == 9)); - CHECK((cm_sec[{1, 0}] == 11)); - CHECK((cm_sec[{2, 1}] == 15)); - } - - TEST(strided_array_view_bounds) - { - int arr[] = { 0, 1, 2, 3 }; - array_view av(arr); - - { - // incorrect sections - - CHECK_THROW(av.section(0, 0)[0], fail_fast); - CHECK_THROW(av.section(1, 0)[0], fail_fast); - CHECK_THROW(av.section(1, 1)[1], fail_fast); - - CHECK_THROW(av.section(2, 5), fail_fast); - CHECK_THROW(av.section(5, 2), fail_fast); - CHECK_THROW(av.section(5, 0), fail_fast); - CHECK_THROW(av.section(0, 5), fail_fast); - CHECK_THROW(av.section(5, 5), fail_fast); - } - - { - // zero stride - strided_array_view sav{ av, {{4}, {}} }; - CHECK(sav[0] == 0); - CHECK(sav[3] == 0); - CHECK_THROW(sav[4], fail_fast); - } - - { - // zero extent - strided_array_view sav{ av,{ {},{1} } }; - CHECK_THROW(sav[0], fail_fast); - } - - { - // zero extent and stride - strided_array_view sav{ av,{ {},{} } }; - CHECK_THROW(sav[0], fail_fast); - } - - { - // strided array ctor with matching strided bounds - strided_array_view sav{ arr,{ 4, 1 } }; - CHECK(sav.bounds().index_bounds() == index<1>{ 4 }); - CHECK(sav[3] == 3); - CHECK_THROW(sav[4], fail_fast); - } - - { - // strided array ctor with smaller strided bounds - strided_array_view sav{ arr,{ 2, 1 } }; - CHECK(sav.bounds().index_bounds() == index<1>{ 2 }); - CHECK(sav[1] == 1); - CHECK_THROW(sav[2], fail_fast); - } - - { - // strided array ctor with fitting irregular bounds - strided_array_view sav{ arr,{ 2, 3 } }; - CHECK(sav.bounds().index_bounds() == index<1>{ 2 }); - CHECK(sav[0] == 0); - CHECK(sav[1] == 3); - CHECK_THROW(sav[2], fail_fast); - } - - { - // bounds cross data boundaries - from static arrays - CHECK_THROW((strided_array_view { arr, { 3, 2 } }), fail_fast); - CHECK_THROW((strided_array_view { arr, { 3, 3 } }), fail_fast); - CHECK_THROW((strided_array_view { arr, { 4, 5 } }), fail_fast); - CHECK_THROW((strided_array_view { arr, { 5, 1 } }), fail_fast); - CHECK_THROW((strided_array_view { arr, { 5, 5 } }), fail_fast); - } - - { - // bounds cross data boundaries - from array view - CHECK_THROW((strided_array_view { av, { 3, 2 } }), fail_fast); - CHECK_THROW((strided_array_view { av, { 3, 3 } }), fail_fast); - CHECK_THROW((strided_array_view { av, { 4, 5 } }), fail_fast); - CHECK_THROW((strided_array_view { av, { 5, 1 } }), fail_fast); - CHECK_THROW((strided_array_view { av, { 5, 5 } }), fail_fast); - } - - { - // bounds cross data boundaries - from dynamic arrays - CHECK_THROW((strided_array_view { av.data(), 4, { 3, 2 } }), fail_fast); - CHECK_THROW((strided_array_view { av.data(), 4, { 3, 3 } }), fail_fast); - CHECK_THROW((strided_array_view { av.data(), 4, { 4, 5 } }), fail_fast); - CHECK_THROW((strided_array_view { av.data(), 4, { 5, 1 } }), fail_fast); - CHECK_THROW((strided_array_view { av.data(), 4, { 5, 5 } }), fail_fast); - CHECK_THROW((strided_array_view { av.data(), 2, { 2, 2 } }), fail_fast); - } - -#ifdef CONFIRM_COMPILATION_ERRORS - { - strided_array_view sav0{ av.data(), { 3, 2 } }; - strided_array_view sav1{ arr, { 1 } }; - strided_array_view sav2{ arr, { 1,1,1 } }; - strided_array_view sav3{ av, { 1 } }; - strided_array_view sav4{ av, { 1,1,1 } }; - strided_array_view sav5{ av.as_array_view(dim<2>(), dim<2>()), { 1 } }; - strided_array_view sav6{ av.as_array_view(dim<2>(), dim<2>()), { 1,1,1 } }; - strided_array_view sav7{ av.as_array_view(dim<2>(), dim<2>()), { { 1,1 },{ 1,1 },{ 1,1 } } }; - } -#endif - - { - // stride initializer list size should match the rank of the array - CHECK_THROW((index<1>{ 0,1 }), fail_fast); - CHECK_THROW((strided_array_view{ arr, {1, {1,1}} }), fail_fast); -#ifdef _MSC_VER - CHECK_THROW((strided_array_view{ arr, {{1,1 }, {1,1}} }), fail_fast); -#endif - CHECK_THROW((strided_array_view{ av, {1, {1,1}} }), fail_fast); -#ifdef _MSC_VER - CHECK_THROW((strided_array_view{ av, {{1,1 }, {1,1}} }), fail_fast); -#endif - CHECK_THROW((strided_array_view{ av.as_array_view(dim<2>(), dim<2>()), {{1}, {1}} }), fail_fast); - CHECK_THROW((strided_array_view{ av.as_array_view(dim<2>(), dim<2>()), {{1}, {1,1,1}} }), fail_fast); -#ifdef _MSC_VER - CHECK_THROW((strided_array_view{ av.as_array_view(dim<2>(), dim<2>()), {{1,1,1}, {1}} }), fail_fast); -#endif - } - - } - - TEST(strided_array_view_type_conversion) - { - int arr[] = { 0, 1, 2, 3 }; - array_view av(arr); - - { - strided_array_view sav{ av.data(), av.size(), { av.size() / 2, 2 } }; -#ifdef CONFIRM_COMPILATION_ERRORS - strided_array_view lsav1 = sav.as_strided_array_view(); -#endif - } - { - strided_array_view sav{ av, { av.size() / 2, 2 } }; -#ifdef CONFIRM_COMPILATION_ERRORS - strided_array_view lsav1 = sav.as_strided_array_view(); -#endif - } - - array_view bytes = av.as_bytes(); - - // retype strided array with regular strides - from raw data - { - strided_bounds<2> bounds{ { 2, bytes.size() / 4 }, { bytes.size() / 2, 1 } }; - strided_array_view sav2{ bytes.data(), bytes.size(), bounds }; - strided_array_view sav3 = sav2.as_strided_array_view(); - CHECK(sav3[0][0] == 0); - CHECK(sav3[1][0] == 2); - CHECK_THROW(sav3[1][1], fail_fast); - CHECK_THROW(sav3[0][1], fail_fast); - } - - // retype strided array with regular strides - from array_view - { - strided_bounds<2> bounds{ { 2, bytes.size() / 4 }, { bytes.size() / 2, 1 } }; - array_view bytes2 = bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2)); - strided_array_view sav2{ bytes2, bounds }; - strided_array_view sav3 = sav2.as_strided_array_view(); - CHECK(sav3[0][0] == 0); - CHECK(sav3[1][0] == 2); - CHECK_THROW(sav3[1][1], fail_fast); - CHECK_THROW(sav3[0][1], fail_fast); - } - - // retype strided array with not enough elements - last dimension of the array is too small - { - strided_bounds<2> bounds{ { 4,2 },{ 4, 1 } }; - array_view bytes2 = bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2)); - strided_array_view sav2{ bytes2, bounds }; - CHECK_THROW(sav2.as_strided_array_view(), fail_fast); - } - - // retype strided array with not enough elements - strides are too small - { - strided_bounds<2> bounds{ { 4,2 },{ 2, 1 } }; - array_view bytes2 = bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2)); - strided_array_view sav2{ bytes2, bounds }; - CHECK_THROW(sav2.as_strided_array_view(), fail_fast); - } - - // retype strided array with not enough elements - last dimension does not divide by the new typesize - { - strided_bounds<2> bounds{ { 2,6 },{ 4, 1 } }; - array_view bytes2 = bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2)); - strided_array_view sav2{ bytes2, bounds }; - CHECK_THROW(sav2.as_strided_array_view(), fail_fast); - } - - // retype strided array with not enough elements - strides does not divide by the new typesize - { - strided_bounds<2> bounds{ { 2, 1 },{ 6, 1 } }; - array_view bytes2 = bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2)); - strided_array_view sav2{ bytes2, bounds }; - CHECK_THROW(sav2.as_strided_array_view(), fail_fast); - } - - // retype strided array with irregular strides - from raw data - { - strided_bounds<1> bounds{ bytes.size() / 2, 2 }; - strided_array_view sav2{ bytes.data(), bytes.size(), bounds }; - CHECK_THROW(sav2.as_strided_array_view(), fail_fast); - } - - // retype strided array with irregular strides - from array_view - { - strided_bounds<1> bounds{ bytes.size() / 2, 2 }; - strided_array_view sav2{ bytes, bounds }; - CHECK_THROW(sav2.as_strided_array_view(), fail_fast); - } - } - - TEST(empty_arrays) - { -#ifdef CONFIRM_COMPILATION_ERRORS - { - array_view empty; - strided_array_view empty2; - strided_array_view empty3{ nullptr,{ 0, 1 } }; - } -#endif - - { - array_view empty_av(nullptr); - - CHECK(empty_av.bounds().index_bounds() == index<1>{ 0 }); - CHECK_THROW(empty_av[0], fail_fast); - CHECK_THROW(empty_av.begin()[0], fail_fast); - CHECK_THROW(empty_av.cbegin()[0], fail_fast); - for (auto& v : empty_av) - { - CHECK(false); - } - } - - { - array_view empty_av = {}; - CHECK(empty_av.bounds().index_bounds() == index<1>{ 0 }); - CHECK_THROW(empty_av[0], fail_fast); - CHECK_THROW(empty_av.begin()[0], fail_fast); - CHECK_THROW(empty_av.cbegin()[0], fail_fast); - for (auto& v : empty_av) - { - CHECK(false); - } - } - - { - array_view empty_av(nullptr); - strided_array_view empty_sav{ empty_av, { 0, 1 } }; - - CHECK(empty_sav.bounds().index_bounds() == index<1>{ 0 }); - CHECK_THROW(empty_sav[0], fail_fast); - CHECK_THROW(empty_sav.begin()[0], fail_fast); - CHECK_THROW(empty_sav.cbegin()[0], fail_fast); - - for (auto& v : empty_sav) - { - CHECK(false); - } - } - - { - strided_array_view empty_sav{ nullptr, 0, { 0, 1 } }; - - CHECK(empty_sav.bounds().index_bounds() == index<1>{ 0 }); - CHECK_THROW(empty_sav[0], fail_fast); - CHECK_THROW(empty_sav.begin()[0], fail_fast); - CHECK_THROW(empty_sav.cbegin()[0], fail_fast); - - for (auto& v : empty_sav) - { - CHECK(false); - } - } - } - - TEST(index_constructor) - { - auto arr = new int[8]; - for (int i = 0; i < 4; ++i) - { - arr[2 * i] = 4 + i; - arr[2 * i + 1] = i; - } - - array_view av(arr, 8); - - size_t a[1] = { 0 }; - index<1> i = a; - - CHECK(av[i] == 4); - - auto av2 = av.as_array_view(dim<4>(), dim<>(2)); - size_t a2[2] = { 0, 1 }; - index<2> i2 = a2; - - CHECK(av2[i2] == 0); - CHECK(av2[0][i] == 4); - - delete[] arr; - } - - TEST(index_operations) - { - size_t a[3] = { 0, 1, 2 }; - size_t b[3] = { 3, 4, 5 }; - index<3> i = a; - index<3> j = b; - - CHECK(i[0] == 0); - CHECK(i[1] == 1); - CHECK(i[2] == 2); - - { - index<3> k = i + j; - - CHECK(i[0] == 0); - CHECK(i[1] == 1); - CHECK(i[2] == 2); - CHECK(k[0] == 3); - CHECK(k[1] == 5); - CHECK(k[2] == 7); - } - - { - index<3> k = i * 3; - - CHECK(i[0] == 0); - CHECK(i[1] == 1); - CHECK(i[2] == 2); - CHECK(k[0] == 0); - CHECK(k[1] == 3); - CHECK(k[2] == 6); - } - - { - index<2> k = index<2>::shift_left(i); - - CHECK(i[0] == 0); - CHECK(i[1] == 1); - CHECK(i[2] == 2); - CHECK(k[0] == 1); - CHECK(k[1] == 2); - } - - } - - void iterate_second_column(array_view av) - { - auto length = av.size() / 2; - - // view to the second column - auto section = av.section({ 0,1 }, { length,1 }); - - CHECK(section.size() == length); - for (unsigned int i = 0; i < section.size(); ++i) - { - CHECK(section[i][0] == av[i][1]); - } - - for (unsigned int i = 0; i < section.size(); ++i) - { - auto idx = index<2>{ i,0 }; // avoid braces inside the CHECK macro - CHECK(section[idx] == av[i][1]); - } - - CHECK(section.bounds().index_bounds()[0] == length); - CHECK(section.bounds().index_bounds()[1] == 1); - for (unsigned int i = 0; i < section.bounds().index_bounds()[0]; ++i) - { - for (unsigned int j = 0; j < section.bounds().index_bounds()[1]; ++j) - { - auto idx = index<2>{ i,j }; // avoid braces inside the CHECK macro - CHECK(section[idx] == av[i][1]); - } - } - - size_t idx = 0; - for (auto num : section) - { - CHECK(num == av[idx][1]); - idx++; - } - } - - TEST(array_view_section_iteration) - { - int arr[4][2] = { { 4,0 },{ 5,1 },{ 6,2 },{ 7,3 } }; - - // static bounds - { - array_view av = arr; - iterate_second_column(av); - } - // first bound is dynamic - { - array_view av = arr; - iterate_second_column(av); - } - // second bound is dynamic - { - array_view av = arr; - iterate_second_column(av); - } - // both bounds are dynamic - { - array_view av(arr, 4); - iterate_second_column(av); - } - } - - TEST(dynamic_array_view_section_iteration) - { - unsigned int height = 4, width = 2; - unsigned int size = height * width; - - auto arr = new int[size]; - for (int unsigned i = 0; i < size; ++i) - { - arr[i] = i; - } - - auto av = as_array_view(arr, size); - - // first bound is dynamic - { - array_view av2 = av.as_array_view(dim<>(height), dim<>(width)); - iterate_second_column(av2); - } - // second bound is dynamic - { - array_view av2 = av.as_array_view(dim<>(height), dim<>(width)); - iterate_second_column(av2); - } - // both bounds are dynamic - { - array_view av2 = av.as_array_view(dim<>(height), dim<>(width)); - iterate_second_column(av2); - } - - delete[] arr; - } - - void iterate_every_other_element(array_view av) - { - // pick every other element - - auto length = av.size() / 2; #if _MSC_VER > 1800 - auto bounds = strided_bounds<1>({ length }, { 2 }); + strided_array_view sav_cv{ { src }, { 2, 1 } }; #else - auto bounds = strided_bounds<1>(index<1>{ length }, index<1>{ 2 }); -#endif - strided_array_view strided(&av.data()[1], av.size() - 1, bounds); + strided_array_view sav_cv{ array_view{ src }, + strided_bounds<1>{ 2, 1 } }; +#endif + CHECK(sav_cv.bounds().index_bounds() == index<1>{ 2 }); + CHECK(sav_cv.bounds().strides() == index<1>{ 1 }); + CHECK(sav_cv[1] == 2); + } - CHECK(strided.size() == length); - CHECK(strided.bounds().index_bounds()[0] == length); - for (unsigned int i = 0; i < strided.size(); ++i) - { - CHECK(strided[i] == av[2 * i + 1]); - } + // From const-qualified source + { + const array_view src{ arr }; - int idx = 0; - for (auto num : strided) - { - CHECK(num == av[2 * idx + 1]); - idx++; - } - } + strided_array_view sav_c{ src, { 2, 1 } }; + CHECK(sav_c.bounds().index_bounds() == index<1>{ 2 }); + CHECK(sav_c.bounds().strides() == index<1>{ 1 }); + CHECK(sav_c[1] == 2); - TEST(strided_array_view_section_iteration) - { - int arr[8] = {4,0,5,1,6,2,7,3}; +#if _MSC_VER > 1800 + strided_array_view sav_cv{ { src }, { 2, 1 } }; +#else + strided_array_view sav_cv{ array_view{ src }, + strided_bounds<1>{ 2, 1 } }; +#endif - // static bounds - { - array_view av(arr, 8); - iterate_every_other_element(av); - } + CHECK(sav_cv.bounds().index_bounds() == index<1>{ 2 }); + CHECK(sav_cv.bounds().strides() == index<1>{ 1 }); + CHECK(sav_cv[1] == 2); + } - // dynamic bounds - { - array_view av(arr, 8); - iterate_every_other_element(av); - } - } + // From volatile-qualified source + { + const array_view src{ arr }; - TEST(dynamic_strided_array_view_section_iteration) - { - auto arr = new int[8]; - for (int i = 0; i < 4; ++i) - { - arr[2 * i] = 4 + i; - arr[2 * i + 1] = i; - } - - auto av = as_array_view(arr, 8); - iterate_every_other_element(av); + strided_array_view sav_v{ src, { 2, 1 } }; + CHECK(sav_v.bounds().index_bounds() == index<1>{ 2 }); + CHECK(sav_v.bounds().strides() == index<1>{ 1 }); + CHECK(sav_v[1] == 2); - delete[] arr; - } +#if _MSC_VER > 1800 + strided_array_view sav_cv{ { src }, { 2, 1 } }; +#else + strided_array_view sav_cv{ array_view{ src }, + strided_bounds<1>{ 2, 1 } }; +#endif + CHECK(sav_cv.bounds().index_bounds() == index<1>{ 2 }); + CHECK(sav_cv.bounds().strides() == index<1>{ 1 }); + CHECK(sav_cv[1] == 2); + } - void iterate_second_slice(array_view av) - { - int expected[6] = { 2,3,10,11,18,19 }; - auto section = av.section({ 0,1,0 }, { 3,1,2 }); + // From cv-qualified source + { + const array_view src{ arr }; - for (unsigned int i = 0; i < section.extent<0>(); ++i) - { - for (unsigned int j = 0; j < section.extent<1>(); ++j) - for (unsigned int k = 0; k < section.extent<2>(); ++k) - { - auto idx = index<3>{ i,j,k }; // avoid braces in the CHECK macro - CHECK(section[idx] == expected[2 * i + 2 * j + k]); - } - } + strided_array_view sav_cv{ src, { 2, 1 } }; + CHECK(sav_cv.bounds().index_bounds() == index<1>{ 2 }); + CHECK(sav_cv.bounds().strides() == index<1>{ 1 }); + CHECK(sav_cv[1] == 2); + } + } - for (unsigned int i = 0; i < section.extent<0>(); ++i) - { - for (unsigned int j = 0; j < section.extent<1>(); ++j) - for (unsigned int k = 0; k < section.extent<2>(); ++k) - CHECK(section[i][j][k] == expected[2 * i + 2 * j + k]); - } + // Check const-casting constructor + { + int arr[2] = { 4, 5 }; - int i = 0; - for (auto num : section) - { - CHECK(num == expected[i]); - i++; - } - } + const array_view av(arr, 2); + array_view av2{ av }; + CHECK(av2[1] == 5); - TEST(strided_array_view_section_iteration_3d) - { - int arr[3][4][2]; - for (int i = 0; i < 3; ++i) - { - for (int j = 0; j < 4; ++j) - for (unsigned int k = 0; k < 2; ++k) - arr[i][j][k] = 8 * i + 2 * j + k; - } + static_assert(std::is_convertible, array_view>::value, + "ctor is not implicit!"); - { - array_view av = arr; - iterate_second_slice(av); - } - } + const strided_array_view src{ arr, { 2, 1 } }; + strided_array_view sav{ src }; + CHECK(sav.bounds().index_bounds() == index<1>{ 2 }); + CHECK(sav.bounds().stride() == 1); + CHECK(sav[1] == 5); - TEST(dynamic_strided_array_view_section_iteration_3d) - { - unsigned int height = 12, width = 2; - unsigned int size = height * width; + static_assert(std::is_convertible, strided_array_view>::value, + "ctor is not implicit!"); + } - auto arr = new int[size]; - for (int unsigned i = 0; i < size; ++i) - { - arr[i] = i; - } + // Check copy constructor + { + int arr1[2] = { 3, 4 }; + const strided_array_view src1{ arr1, { 2, 1 } }; + strided_array_view sav1{ src1 }; - { - auto av = as_array_view(arr, 24).as_array_view(dim<3>(),dim<4>(),dim<2>()); - iterate_second_slice(av); - } + CHECK(sav1.bounds().index_bounds() == index<1>{ 2 }); + CHECK(sav1.bounds().stride() == 1); + CHECK(sav1[0] == 3); - { - auto av = as_array_view(arr, 24).as_array_view(dim<>(3), dim<4>(), dim<2>()); - iterate_second_slice(av); - } + int arr2[6] = { 1, 2, 3, 4, 5, 6 }; + const strided_array_view src2{ arr2, { { 3, 2 }, { 2, 1 } } }; + strided_array_view sav2{ src2 }; + CHECK((sav2.bounds().index_bounds() == index<2>{ 3, 2 })); + CHECK((sav2.bounds().strides() == index<2>{ 2, 1 })); + CHECK((sav2[{ 0, 0 }] == 1 && sav2[{ 2, 0 }] == 5)); + } - { - auto av = as_array_view(arr, 24).as_array_view(dim<3>(), dim<>(4), dim<2>()); - iterate_second_slice(av); - } + // Check const-casting assignment operator + { + int arr1[2] = { 1, 2 }; + int arr2[6] = { 3, 4, 5, 6, 7, 8 }; - { - auto av = as_array_view(arr, 24).as_array_view(dim<3>(), dim<4>(), dim<>(2)); - iterate_second_slice(av); - } - delete[] arr; - } + const strided_array_view src{ arr1, { { 2 }, { 1 } } }; + strided_array_view sav{ arr2, { { 3 }, { 2 } } }; + strided_array_view& sav_ref = (sav = src); + CHECK(sav.bounds().index_bounds() == index<1>{ 2 }); + CHECK(sav.bounds().strides() == index<1>{ 1 }); + CHECK(sav[0] == 1); + CHECK(&sav_ref == &sav); + } - TEST(strided_array_view_conversion) - { - // get an array_view of 'c' values from the list of X's + // Check copy assignment operator + { + int arr1[2] = { 3, 4 }; + int arr1b[1] = { 0 }; + const strided_array_view src1{ arr1, { 2, 1 } }; + strided_array_view sav1{ arr1b, { 1, 1 } }; + strided_array_view& sav1_ref = (sav1 = src1); + CHECK(sav1.bounds().index_bounds() == index<1>{ 2 }); + CHECK(sav1.bounds().strides() == index<1>{ 1 }); + CHECK(sav1[0] == 3); + CHECK(&sav1_ref == &sav1); - struct X { int a; int b; int c; }; + const int arr2[6] = { 1, 2, 3, 4, 5, 6 }; + const int arr2b[1] = { 0 }; + const strided_array_view src2{ arr2, { { 3, 2 }, { 2, 1 } } }; + strided_array_view sav2{ arr2b, { { 1, 1 }, { 1, 1 } } }; + strided_array_view& sav2_ref = (sav2 = src2); + CHECK((sav2.bounds().index_bounds() == index<2>{ 3, 2 })); + CHECK((sav2.bounds().strides() == index<2>{ 2, 1 })); + CHECK((sav2[{ 0, 0 }] == 1 && sav2[{ 2, 0 }] == 5)); + CHECK(&sav2_ref == &sav2); + } + } - X arr[4] = { { 0,1,2 },{ 3,4,5 },{ 6,7,8 },{ 9,10,11 } }; + TEST(strided_array_view_slice) + { + std::vector data(5 * 10); + std::iota(begin(data), end(data), 0); + const array_view src = as_array_view(data).as_array_view(dim<5>(), dim<10>()); - auto s = sizeof(int) / sizeof(byte); - auto d2 = 3 * s; - auto d1 = sizeof(int) * 12 / d2; + const strided_array_view sav{ src, { { 5, 10 }, { 10, 1 } } }; +#ifdef CONFIRM_COMPILATION_ERRORS + const strided_array_view csav{ { src }, { { 5, 10 }, { 10, 1 } } }; +#endif + const strided_array_view csav{ array_view{ src }, + { { 5, 10 }, { 10, 1 } } }; - // convert to 4x12 array of bytes - auto av = as_array_view(arr, 4).as_bytes().as_array_view(dim<>(d1), dim<>(d2)); + strided_array_view sav_sl = sav[2]; + CHECK(sav_sl[0] == 20); + CHECK(sav_sl[9] == 29); - CHECK(av.bounds().index_bounds()[0] == 4); - CHECK(av.bounds().index_bounds()[1] == 12); + strided_array_view csav_sl = sav[3]; + CHECK(csav_sl[0] == 30); + CHECK(csav_sl[9] == 39); - // get the last 4 columns - auto section = av.section({ 0, 2 * s }, { 4, s }); // { { arr[0].c[0], arr[0].c[1], arr[0].c[2], arr[0].c[3] } , { arr[1].c[0], ... } , ... } + CHECK(sav[4][0] == 40); + CHECK(sav[4][9] == 49); + } - // convert to array 4x1 array of integers - auto cs = section.as_strided_array_view(); // { { arr[0].c }, {arr[1].c } , ... } + TEST(strided_array_view_column_major) + { + // strided_array_view may be used to accomodate more peculiar + // use cases, such as column-major multidimensional array + // (aka. "FORTRAN" layout). - CHECK(cs.bounds().index_bounds()[0] == 4); - CHECK(cs.bounds().index_bounds()[1] == 1); + int cm_array[3 * 5] = { 1, 4, 7, 10, 13, 2, 5, 8, 11, 14, 3, 6, 9, 12, 15 }; + strided_array_view cm_sav{ cm_array, { { 5, 3 }, { 1, 5 } } }; - // transpose to 1x4 array - strided_bounds<2> reverse_bounds{ - { cs.bounds().index_bounds()[1] , cs.bounds().index_bounds()[0] }, - { cs.bounds().strides()[1], cs.bounds().strides()[0] } - }; + // Accessing elements + CHECK((cm_sav[{ 0, 0 }] == 1)); + CHECK((cm_sav[{ 0, 1 }] == 2)); + CHECK((cm_sav[{ 1, 0 }] == 4)); + CHECK((cm_sav[{ 4, 2 }] == 15)); - strided_array_view transposed{ cs.data(), cs.bounds().total_size(), reverse_bounds }; - - // slice to get a one-dimensional array of c's - strided_array_view result = transposed[0]; + // Slice + strided_array_view cm_sl = cm_sav[3]; - CHECK(result.bounds().index_bounds()[0] == 4); - CHECK_THROW(result.bounds().index_bounds()[1], fail_fast); + CHECK(cm_sl[0] == 10); + CHECK(cm_sl[1] == 11); + CHECK(cm_sl[2] == 12); - int i = 0; - for (auto& num : result) - { - CHECK(num == arr[i].c); - i++; - } + // Section + strided_array_view cm_sec = cm_sav.section({ 2, 1 }, { 3, 2 }); - } + CHECK((cm_sec.bounds().index_bounds() == index<2>{ 3, 2 })); + CHECK((cm_sec[{ 0, 0 }] == 8)); + CHECK((cm_sec[{ 0, 1 }] == 9)); + CHECK((cm_sec[{ 1, 0 }] == 11)); + CHECK((cm_sec[{ 2, 1 }] == 15)); + } - TEST(constructors) - { - array_view av(nullptr); - CHECK(av.length() == 0); + TEST(strided_array_view_bounds) + { + int arr[] = { 0, 1, 2, 3 }; + array_view av(arr); - array_view av2; - CHECK(av2.length() == 0); + { + // incorrect sections - array_view av3(nullptr, 0); - CHECK(av3.length() == 0); + CHECK_THROW(av.section(0, 0)[0], fail_fast); + CHECK_THROW(av.section(1, 0)[0], fail_fast); + CHECK_THROW(av.section(1, 1)[1], fail_fast); - // Constructing from a nullptr + length is specifically disallowed - auto f = [&]() {array_view av4(nullptr, 2);}; - CHECK_THROW(f(), fail_fast); + CHECK_THROW(av.section(2, 5), fail_fast); + CHECK_THROW(av.section(5, 2), fail_fast); + CHECK_THROW(av.section(5, 0), fail_fast); + CHECK_THROW(av.section(0, 5), fail_fast); + CHECK_THROW(av.section(5, 5), fail_fast); + } - int arr1[2][3]; - array_view av5(arr1); + { + // zero stride + strided_array_view sav{ av, { { 4 }, {} } }; + CHECK(sav[0] == 0); + CHECK(sav[3] == 0); + CHECK_THROW(sav[4], fail_fast); + } - array arr2; - array_view av6(arr2); + { + // zero extent + strided_array_view sav{ av, { {}, { 1 } } }; + CHECK_THROW(sav[0], fail_fast); + } - vector vec1(19); - array_view av7(vec1); - CHECK(av7.length() == 19); + { + // zero extent and stride + strided_array_view sav{ av, { {}, {} } }; + CHECK_THROW(sav[0], fail_fast); + } + + { + // strided array ctor with matching strided bounds + strided_array_view sav{ arr, { 4, 1 } }; + CHECK(sav.bounds().index_bounds() == index<1>{ 4 }); + CHECK(sav[3] == 3); + CHECK_THROW(sav[4], fail_fast); + } + + { + // strided array ctor with smaller strided bounds + strided_array_view sav{ arr, { 2, 1 } }; + CHECK(sav.bounds().index_bounds() == index<1>{ 2 }); + CHECK(sav[1] == 1); + CHECK_THROW(sav[2], fail_fast); + } + + { + // strided array ctor with fitting irregular bounds + strided_array_view sav{ arr, { 2, 3 } }; + CHECK(sav.bounds().index_bounds() == index<1>{ 2 }); + CHECK(sav[0] == 0); + CHECK(sav[1] == 3); + CHECK_THROW(sav[2], fail_fast); + } + + { + // bounds cross data boundaries - from static arrays + CHECK_THROW((strided_array_view{ arr, { 3, 2 } }), fail_fast); + CHECK_THROW((strided_array_view{ arr, { 3, 3 } }), fail_fast); + CHECK_THROW((strided_array_view{ arr, { 4, 5 } }), fail_fast); + CHECK_THROW((strided_array_view{ arr, { 5, 1 } }), fail_fast); + CHECK_THROW((strided_array_view{ arr, { 5, 5 } }), fail_fast); + } + + { + // bounds cross data boundaries - from array view + CHECK_THROW((strided_array_view{ av, { 3, 2 } }), fail_fast); + CHECK_THROW((strided_array_view{ av, { 3, 3 } }), fail_fast); + CHECK_THROW((strided_array_view{ av, { 4, 5 } }), fail_fast); + CHECK_THROW((strided_array_view{ av, { 5, 1 } }), fail_fast); + CHECK_THROW((strided_array_view{ av, { 5, 5 } }), fail_fast); + } + + { + // bounds cross data boundaries - from dynamic arrays + CHECK_THROW((strided_array_view{ av.data(), 4, { 3, 2 } }), fail_fast); + CHECK_THROW((strided_array_view{ av.data(), 4, { 3, 3 } }), fail_fast); + CHECK_THROW((strided_array_view{ av.data(), 4, { 4, 5 } }), fail_fast); + CHECK_THROW((strided_array_view{ av.data(), 4, { 5, 1 } }), fail_fast); + CHECK_THROW((strided_array_view{ av.data(), 4, { 5, 5 } }), fail_fast); + CHECK_THROW((strided_array_view{ av.data(), 2, { 2, 2 } }), fail_fast); + } + +#ifdef CONFIRM_COMPILATION_ERRORS + { + strided_array_view sav0{ av.data(), { 3, 2 } }; + strided_array_view sav1{ arr, { 1 } }; + strided_array_view sav2{ arr, { 1, 1, 1 } }; + strided_array_view sav3{ av, { 1 } }; + strided_array_view sav4{ av, { 1, 1, 1 } }; + strided_array_view sav5{ av.as_array_view(dim<2>(), dim<2>()), { 1 } }; + strided_array_view sav6{ av.as_array_view(dim<2>(), dim<2>()), { 1, 1, 1 } }; + strided_array_view sav7{ av.as_array_view(dim<2>(), dim<2>()), + { { 1, 1 }, { 1, 1 }, { 1, 1 } } }; + } +#endif + + { + // stride initializer list size should match the rank of the array + CHECK_THROW((index<1>{ 0, 1 }), fail_fast); + CHECK_THROW((strided_array_view{ arr, { 1, { 1, 1 } } }), fail_fast); +#ifdef _MSC_VER + CHECK_THROW((strided_array_view{ arr, { { 1, 1 }, { 1, 1 } } }), fail_fast); +#endif + CHECK_THROW((strided_array_view{ av, { 1, { 1, 1 } } }), fail_fast); +#ifdef _MSC_VER + CHECK_THROW((strided_array_view{ av, { { 1, 1 }, { 1, 1 } } }), fail_fast); +#endif + CHECK_THROW((strided_array_view{ av.as_array_view(dim<2>(), dim<2>()), { { 1 }, { 1 } } }), + fail_fast); + CHECK_THROW((strided_array_view{ av.as_array_view(dim<2>(), dim<2>()), + { { 1 }, { 1, 1, 1 } } }), + fail_fast); +#ifdef _MSC_VER + CHECK_THROW((strided_array_view{ av.as_array_view(dim<2>(), dim<2>()), + { { 1, 1, 1 }, { 1 } } }), + fail_fast); +#endif + } + } + + TEST(strided_array_view_type_conversion) + { + int arr[] = { 0, 1, 2, 3 }; + array_view av(arr); + + { + strided_array_view sav{ av.data(), av.size(), { av.size() / 2, 2 } }; +#ifdef CONFIRM_COMPILATION_ERRORS + strided_array_view lsav1 = sav.as_strided_array_view(); +#endif + } + { + strided_array_view sav{ av, { av.size() / 2, 2 } }; +#ifdef CONFIRM_COMPILATION_ERRORS + strided_array_view lsav1 = sav.as_strided_array_view(); +#endif + } + + array_view bytes = av.as_bytes(); + + // retype strided array with regular strides - from raw data + { + strided_bounds<2> bounds{ { 2, bytes.size() / 4 }, { bytes.size() / 2, 1 } }; + strided_array_view sav2{ bytes.data(), bytes.size(), bounds }; + strided_array_view sav3 = sav2.as_strided_array_view(); + CHECK(sav3[0][0] == 0); + CHECK(sav3[1][0] == 2); + CHECK_THROW(sav3[1][1], fail_fast); + CHECK_THROW(sav3[0][1], fail_fast); + } + + // retype strided array with regular strides - from array_view + { + strided_bounds<2> bounds{ { 2, bytes.size() / 4 }, { bytes.size() / 2, 1 } }; + array_view bytes2 = + bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2)); + strided_array_view sav2{ bytes2, bounds }; + strided_array_view sav3 = sav2.as_strided_array_view(); + CHECK(sav3[0][0] == 0); + CHECK(sav3[1][0] == 2); + CHECK_THROW(sav3[1][1], fail_fast); + CHECK_THROW(sav3[0][1], fail_fast); + } + + // retype strided array with not enough elements - last dimension of the array is too small + { + strided_bounds<2> bounds{ { 4, 2 }, { 4, 1 } }; + array_view bytes2 = + bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2)); + strided_array_view sav2{ bytes2, bounds }; + CHECK_THROW(sav2.as_strided_array_view(), fail_fast); + } + + // retype strided array with not enough elements - strides are too small + { + strided_bounds<2> bounds{ { 4, 2 }, { 2, 1 } }; + array_view bytes2 = + bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2)); + strided_array_view sav2{ bytes2, bounds }; + CHECK_THROW(sav2.as_strided_array_view(), fail_fast); + } + + // retype strided array with not enough elements - last dimension does not divide by the new + // typesize + { + strided_bounds<2> bounds{ { 2, 6 }, { 4, 1 } }; + array_view bytes2 = + bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2)); + strided_array_view sav2{ bytes2, bounds }; + CHECK_THROW(sav2.as_strided_array_view(), fail_fast); + } + + // retype strided array with not enough elements - strides does not divide by the new + // typesize + { + strided_bounds<2> bounds{ { 2, 1 }, { 6, 1 } }; + array_view bytes2 = + bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2)); + strided_array_view sav2{ bytes2, bounds }; + CHECK_THROW(sav2.as_strided_array_view(), fail_fast); + } + + // retype strided array with irregular strides - from raw data + { + strided_bounds<1> bounds{ bytes.size() / 2, 2 }; + strided_array_view sav2{ bytes.data(), bytes.size(), bounds }; + CHECK_THROW(sav2.as_strided_array_view(), fail_fast); + } + + // retype strided array with irregular strides - from array_view + { + strided_bounds<1> bounds{ bytes.size() / 2, 2 }; + strided_array_view sav2{ bytes, bounds }; + CHECK_THROW(sav2.as_strided_array_view(), fail_fast); + } + } + + TEST(empty_arrays) + { +#ifdef CONFIRM_COMPILATION_ERRORS + { + array_view empty; + strided_array_view empty2; + strided_array_view empty3{ nullptr, { 0, 1 } }; + } +#endif + + { + array_view empty_av(nullptr); + + CHECK(empty_av.bounds().index_bounds() == index<1>{ 0 }); + CHECK_THROW(empty_av[0], fail_fast); + CHECK_THROW(empty_av.begin()[0], fail_fast); + CHECK_THROW(empty_av.cbegin()[0], fail_fast); + for (auto& v : empty_av) + { + CHECK(false); + } + } + + { + array_view empty_av = {}; + CHECK(empty_av.bounds().index_bounds() == index<1>{ 0 }); + CHECK_THROW(empty_av[0], fail_fast); + CHECK_THROW(empty_av.begin()[0], fail_fast); + CHECK_THROW(empty_av.cbegin()[0], fail_fast); + for (auto& v : empty_av) + { + CHECK(false); + } + } + + { + array_view empty_av(nullptr); + strided_array_view empty_sav{ empty_av, { 0, 1 } }; + + CHECK(empty_sav.bounds().index_bounds() == index<1>{ 0 }); + CHECK_THROW(empty_sav[0], fail_fast); + CHECK_THROW(empty_sav.begin()[0], fail_fast); + CHECK_THROW(empty_sav.cbegin()[0], fail_fast); + + for (auto& v : empty_sav) + { + CHECK(false); + } + } + + { + strided_array_view empty_sav{ nullptr, 0, { 0, 1 } }; + + CHECK(empty_sav.bounds().index_bounds() == index<1>{ 0 }); + CHECK_THROW(empty_sav[0], fail_fast); + CHECK_THROW(empty_sav.begin()[0], fail_fast); + CHECK_THROW(empty_sav.cbegin()[0], fail_fast); + + for (auto& v : empty_sav) + { + CHECK(false); + } + } + } + + TEST(index_constructor) + { + auto arr = new int[8]; + for (int i = 0; i < 4; ++i) + { + arr[2 * i] = 4 + i; + arr[2 * i + 1] = i; + } + + array_view av(arr, 8); + + size_t a[1] = { 0 }; + index<1> i = a; + + CHECK(av[i] == 4); + + auto av2 = av.as_array_view(dim<4>(), dim<>(2)); + size_t a2[2] = { 0, 1 }; + index<2> i2 = a2; + + CHECK(av2[i2] == 0); + CHECK(av2[0][i] == 4); + + delete[] arr; + } + + TEST(index_operations) + { + size_t a[3] = { 0, 1, 2 }; + size_t b[3] = { 3, 4, 5 }; + index<3> i = a; + index<3> j = b; + + CHECK(i[0] == 0); + CHECK(i[1] == 1); + CHECK(i[2] == 2); + + { + index<3> k = i + j; + + CHECK(i[0] == 0); + CHECK(i[1] == 1); + CHECK(i[2] == 2); + CHECK(k[0] == 3); + CHECK(k[1] == 5); + CHECK(k[2] == 7); + } + + { + index<3> k = i * 3; + + CHECK(i[0] == 0); + CHECK(i[1] == 1); + CHECK(i[2] == 2); + CHECK(k[0] == 0); + CHECK(k[1] == 3); + CHECK(k[2] == 6); + } + + { + index<2> k = index<2>::shift_left(i); + + CHECK(i[0] == 0); + CHECK(i[1] == 1); + CHECK(i[2] == 2); + CHECK(k[0] == 1); + CHECK(k[1] == 2); + } + } + + void iterate_second_column(array_view av) + { + auto length = av.size() / 2; + + // view to the second column + auto section = av.section({ 0, 1 }, { length, 1 }); + + CHECK(section.size() == length); + for (unsigned int i = 0; i < section.size(); ++i) + { + CHECK(section[i][0] == av[i][1]); + } + + for (unsigned int i = 0; i < section.size(); ++i) + { + auto idx = index<2>{ i, 0 }; // avoid braces inside the CHECK macro + CHECK(section[idx] == av[i][1]); + } + + CHECK(section.bounds().index_bounds()[0] == length); + CHECK(section.bounds().index_bounds()[1] == 1); + for (unsigned int i = 0; i < section.bounds().index_bounds()[0]; ++i) + { + for (unsigned int j = 0; j < section.bounds().index_bounds()[1]; ++j) + { + auto idx = index<2>{ i, j }; // avoid braces inside the CHECK macro + CHECK(section[idx] == av[i][1]); + } + } + + size_t idx = 0; + for (auto num : section) + { + CHECK(num == av[idx][1]); + idx++; + } + } + + TEST(array_view_section_iteration) + { + int arr[4][2] = { { 4, 0 }, { 5, 1 }, { 6, 2 }, { 7, 3 } }; + + // static bounds + { + array_view av = arr; + iterate_second_column(av); + } + // first bound is dynamic + { + array_view av = arr; + iterate_second_column(av); + } + // second bound is dynamic + { + array_view av = arr; + iterate_second_column(av); + } + // both bounds are dynamic + { + array_view av(arr, 4); + iterate_second_column(av); + } + } + + TEST(dynamic_array_view_section_iteration) + { + unsigned int height = 4, width = 2; + unsigned int size = height * width; + + auto arr = new int[size]; + for (int unsigned i = 0; i < size; ++i) + { + arr[i] = i; + } + + auto av = as_array_view(arr, size); + + // first bound is dynamic + { + array_view av2 = av.as_array_view(dim<>(height), dim<>(width)); + iterate_second_column(av2); + } + // second bound is dynamic + { + array_view av2 = av.as_array_view(dim<>(height), dim<>(width)); + iterate_second_column(av2); + } + // both bounds are dynamic + { + array_view av2 = av.as_array_view(dim<>(height), dim<>(width)); + iterate_second_column(av2); + } + + delete[] arr; + } + + void iterate_every_other_element(array_view av) + { + // pick every other element + + auto length = av.size() / 2; +#if _MSC_VER > 1800 + auto bounds = strided_bounds<1>({ length }, { 2 }); +#else + auto bounds = strided_bounds<1>(index<1>{ length }, index<1>{ 2 }); +#endif + strided_array_view strided(&av.data()[1], av.size() - 1, bounds); + + CHECK(strided.size() == length); + CHECK(strided.bounds().index_bounds()[0] == length); + for (unsigned int i = 0; i < strided.size(); ++i) + { + CHECK(strided[i] == av[2 * i + 1]); + } + + int idx = 0; + for (auto num : strided) + { + CHECK(num == av[2 * idx + 1]); + idx++; + } + } + + TEST(strided_array_view_section_iteration) + { + int arr[8] = { 4, 0, 5, 1, 6, 2, 7, 3 }; + + // static bounds + { + array_view av(arr, 8); + iterate_every_other_element(av); + } + + // dynamic bounds + { + array_view av(arr, 8); + iterate_every_other_element(av); + } + } + + TEST(dynamic_strided_array_view_section_iteration) + { + auto arr = new int[8]; + for (int i = 0; i < 4; ++i) + { + arr[2 * i] = 4 + i; + arr[2 * i + 1] = i; + } + + auto av = as_array_view(arr, 8); + iterate_every_other_element(av); + + delete[] arr; + } + + void iterate_second_slice(array_view av) + { + int expected[6] = { 2, 3, 10, 11, 18, 19 }; + auto section = av.section({ 0, 1, 0 }, { 3, 1, 2 }); + + for (unsigned int i = 0; i < section.extent<0>(); ++i) + { + for (unsigned int j = 0; j < section.extent<1>(); ++j) + for (unsigned int k = 0; k < section.extent<2>(); ++k) + { + auto idx = index<3>{ i, j, k }; // avoid braces in the CHECK macro + CHECK(section[idx] == expected[2 * i + 2 * j + k]); + } + } + + for (unsigned int i = 0; i < section.extent<0>(); ++i) + { + for (unsigned int j = 0; j < section.extent<1>(); ++j) + for (unsigned int k = 0; k < section.extent<2>(); ++k) + CHECK(section[i][j][k] == expected[2 * i + 2 * j + k]); + } + + int i = 0; + for (auto num : section) + { + CHECK(num == expected[i]); + i++; + } + } + + TEST(strided_array_view_section_iteration_3d) + { + int arr[3][4][2]; + for (int i = 0; i < 3; ++i) + { + for (int j = 0; j < 4; ++j) + for (unsigned int k = 0; k < 2; ++k) + arr[i][j][k] = 8 * i + 2 * j + k; + } + + { + array_view av = arr; + iterate_second_slice(av); + } + } + + TEST(dynamic_strided_array_view_section_iteration_3d) + { + unsigned int height = 12, width = 2; + unsigned int size = height * width; + + auto arr = new int[size]; + for (int unsigned i = 0; i < size; ++i) + { + arr[i] = i; + } + + { + auto av = as_array_view(arr, 24).as_array_view(dim<3>(), dim<4>(), dim<2>()); + iterate_second_slice(av); + } + + { + auto av = as_array_view(arr, 24).as_array_view(dim<>(3), dim<4>(), dim<2>()); + iterate_second_slice(av); + } + + { + auto av = as_array_view(arr, 24).as_array_view(dim<3>(), dim<>(4), dim<2>()); + iterate_second_slice(av); + } + + { + auto av = as_array_view(arr, 24).as_array_view(dim<3>(), dim<4>(), dim<>(2)); + iterate_second_slice(av); + } + delete[] arr; + } + + TEST(strided_array_view_conversion) + { + // get an array_view of 'c' values from the list of X's + + struct X + { + int a; + int b; + int c; + }; + + X arr[4] = { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } }; + + auto s = sizeof(int) / sizeof(byte); + auto d2 = 3 * s; + auto d1 = sizeof(int) * 12 / d2; + + // convert to 4x12 array of bytes + auto av = as_array_view(arr, 4).as_bytes().as_array_view(dim<>(d1), dim<>(d2)); + + CHECK(av.bounds().index_bounds()[0] == 4); + CHECK(av.bounds().index_bounds()[1] == 12); + + // get the last 4 columns + auto section = av.section({ 0, 2 * s }, { 4, s }); // { { arr[0].c[0], arr[0].c[1], + // arr[0].c[2], arr[0].c[3] } , { + // arr[1].c[0], ... } , ... } + + // convert to array 4x1 array of integers + auto cs = section.as_strided_array_view(); // { { arr[0].c }, {arr[1].c } , ... } + + CHECK(cs.bounds().index_bounds()[0] == 4); + CHECK(cs.bounds().index_bounds()[1] == 1); + + // transpose to 1x4 array + strided_bounds<2> reverse_bounds{ { cs.bounds().index_bounds()[1], cs.bounds().index_bounds()[0] }, + { cs.bounds().strides()[1], cs.bounds().strides()[0] } }; + + strided_array_view transposed{ cs.data(), cs.bounds().total_size(), reverse_bounds }; + + // slice to get a one-dimensional array of c's + strided_array_view result = transposed[0]; + + CHECK(result.bounds().index_bounds()[0] == 4); + CHECK_THROW(result.bounds().index_bounds()[1], fail_fast); + + int i = 0; + for (auto& num : result) + { + CHECK(num == arr[i].c); + i++; + } + } + + TEST(constructors) + { + array_view av(nullptr); + CHECK(av.length() == 0); + + array_view av2; + CHECK(av2.length() == 0); + + array_view av3(nullptr, 0); + CHECK(av3.length() == 0); + + // Constructing from a nullptr + length is specifically disallowed + auto f = [&]() + { + array_view av4(nullptr, 2); + }; + CHECK_THROW(f(), fail_fast); + + int arr1[2][3]; + array_view av5(arr1); + + array arr2; + array_view av6(arr2); + + vector vec1(19); + array_view av7(vec1); + CHECK(av7.length() == 19); - array_view av8; - CHECK(av8.length() == 0); - array_view av9(arr2); - CHECK(av9.length() == 15); + array_view av8; + CHECK(av8.length() == 0); + array_view av9(arr2); + CHECK(av9.length() == 15); #ifdef CONFIRM_COMPILATION_ERRORS - array_view av10; - DerivedClass *p = nullptr; - array_view av11(p, 0); + array_view av10; + DerivedClass* p = nullptr; + array_view av11(p, 0); #endif - } + } - TEST(copyandassignment) - { - array_view av1; + TEST(copyandassignment) + { + array_view av1; - int arr[] = {3, 4, 5}; - av1 = arr; - array_view, dynamic_range> av2; - av2 = av1; - } + int arr[] = { 3, 4, 5 }; + av1 = arr; + array_view, dynamic_range> av2; + av2 = av1; + } - TEST(array_view_first) - { - int arr[5] = { 1, 2, 3, 4, 5 }; + TEST(array_view_first) + { + int arr[5] = { 1, 2, 3, 4, 5 }; - { - array_view av = arr; - CHECK((av.first<2>().bounds() == static_bounds())); - CHECK(av.first<2>().length() == 2); - CHECK(av.first(2).length() == 2); - } + { + array_view av = arr; + CHECK((av.first<2>().bounds() == static_bounds())); + CHECK(av.first<2>().length() == 2); + CHECK(av.first(2).length() == 2); + } - { - array_view av = arr; - CHECK((av.first<0>().bounds() == static_bounds())); - CHECK(av.first<0>().length() == 0); - CHECK(av.first(0).length() == 0); - } + { + array_view av = arr; + CHECK((av.first<0>().bounds() == static_bounds())); + CHECK(av.first<0>().length() == 0); + CHECK(av.first(0).length() == 0); + } - { - array_view av = arr; - CHECK((av.first<5>().bounds() == static_bounds())); - CHECK(av.first<5>().length() == 5); - CHECK(av.first(5).length() == 5); - } + { + array_view av = arr; + CHECK((av.first<5>().bounds() == static_bounds())); + CHECK(av.first<5>().length() == 5); + CHECK(av.first(5).length() == 5); + } - { - array_view av = arr; + { + array_view av = arr; #ifdef CONFIRM_COMPILATION_ERRORS - CHECK(av.first<6>().bounds() == static_bounds()); - CHECK(av.first<6>().length() == 6); + CHECK(av.first<6>().bounds() == static_bounds()); + CHECK(av.first<6>().length() == 6); #endif - CHECK_THROW(av.first(6).length(), fail_fast); - } + CHECK_THROW(av.first(6).length(), fail_fast); + } - { - array_view av; - CHECK((av.first<0>().bounds() == static_bounds())); - CHECK(av.first<0>().length() == 0); - CHECK(av.first(0).length() == 0); - } - } + { + array_view av; + CHECK((av.first<0>().bounds() == static_bounds())); + CHECK(av.first<0>().length() == 0); + CHECK(av.first(0).length() == 0); + } + } - TEST(array_view_last) - { - int arr[5] = { 1, 2, 3, 4, 5 }; + TEST(array_view_last) + { + int arr[5] = { 1, 2, 3, 4, 5 }; - { - array_view av = arr; - CHECK((av.last<2>().bounds() == static_bounds())); - CHECK(av.last<2>().length() == 2); - CHECK(av.last(2).length() == 2); - } + { + array_view av = arr; + CHECK((av.last<2>().bounds() == static_bounds())); + CHECK(av.last<2>().length() == 2); + CHECK(av.last(2).length() == 2); + } - { - array_view av = arr; - CHECK((av.last<0>().bounds() == static_bounds())); - CHECK(av.last<0>().length() == 0); - CHECK(av.last(0).length() == 0); - } + { + array_view av = arr; + CHECK((av.last<0>().bounds() == static_bounds())); + CHECK(av.last<0>().length() == 0); + CHECK(av.last(0).length() == 0); + } - { - array_view av = arr; - CHECK((av.last<5>().bounds() == static_bounds())); - CHECK(av.last<5>().length() == 5); - CHECK(av.last(5).length() == 5); - } + { + array_view av = arr; + CHECK((av.last<5>().bounds() == static_bounds())); + CHECK(av.last<5>().length() == 5); + CHECK(av.last(5).length() == 5); + } - - { - array_view av = arr; + + { + array_view av = arr; #ifdef CONFIRM_COMPILATION_ERRORS - CHECK((av.last<6>().bounds() == static_bounds())); - CHECK(av.last<6>().length() == 6); + CHECK((av.last<6>().bounds() == static_bounds())); + CHECK(av.last<6>().length() == 6); #endif - CHECK_THROW(av.last(6).length(), fail_fast); - } + CHECK_THROW(av.last(6).length(), fail_fast); + } - { - array_view av; - CHECK((av.last<0>().bounds() == static_bounds())); - CHECK(av.last<0>().length() == 0); - CHECK(av.last(0).length() == 0); - } - } + { + array_view av; + CHECK((av.last<0>().bounds() == static_bounds())); + CHECK(av.last<0>().length() == 0); + CHECK(av.last(0).length() == 0); + } + } - TEST(custmized_array_view_size) - { - double (*arr)[3][4] = new double[100][3][4]; - array_view, dynamic_range, 3, 4> av1(arr, (char)10); + TEST(custmized_array_view_size) + { + double(*arr)[3][4] = new double[100][3][4]; + array_view, dynamic_range, 3, 4> av1(arr, (char)10); - struct EffectiveStructure - { - double* v1; - char v2; - }; - CHECK(sizeof(av1) == sizeof(EffectiveStructure)); + struct EffectiveStructure + { + double* v1; + char v2; + }; + CHECK(sizeof(av1) == sizeof(EffectiveStructure)); - CHECK_THROW(av1[10][3][4], fail_fast); + CHECK_THROW(av1[10][3][4], fail_fast); - array_view av2 = av1.as_array_view(dim<>(5), dim<6>(), dim<4>()); - - } + array_view av2 = av1.as_array_view(dim<>(5), dim<6>(), dim<4>()); + } - TEST(array_view_sub) - { - int arr[5] = { 1, 2, 3, 4, 5 }; + TEST(array_view_sub) + { + int arr[5] = { 1, 2, 3, 4, 5 }; - { - array_view av = arr; - CHECK((av.sub<2,2>().bounds() == static_bounds())); - CHECK((av.sub<2,2>().length() == 2)); - CHECK(av.sub(2,2).length() == 2); - CHECK(av.sub(2,3).length() == 3); - } + { + array_view av = arr; + CHECK((av.sub<2, 2>().bounds() == static_bounds())); + CHECK((av.sub<2, 2>().length() == 2)); + CHECK(av.sub(2, 2).length() == 2); + CHECK(av.sub(2, 3).length() == 3); + } - { - array_view av = arr; - CHECK((av.sub<0,0>().bounds() == static_bounds())); - CHECK((av.sub<0,0>().length() == 0)); - CHECK(av.sub(0,0).length() == 0); - } + { + array_view av = arr; + CHECK((av.sub<0, 0>().bounds() == static_bounds())); + CHECK((av.sub<0, 0>().length() == 0)); + CHECK(av.sub(0, 0).length() == 0); + } - { - array_view av = arr; - CHECK((av.sub<0,5>().bounds() == static_bounds())); - CHECK((av.sub<0,5>().length() == 5)); - CHECK(av.sub(0,5).length() == 5); - CHECK_THROW(av.sub(0,6).length(), fail_fast); - CHECK_THROW(av.sub(1,5).length(), fail_fast); - } + { + array_view av = arr; + CHECK((av.sub<0, 5>().bounds() == static_bounds())); + CHECK((av.sub<0, 5>().length() == 5)); + CHECK(av.sub(0, 5).length() == 5); + CHECK_THROW(av.sub(0, 6).length(), fail_fast); + CHECK_THROW(av.sub(1, 5).length(), fail_fast); + } - { - array_view av = arr; - CHECK((av.sub<5,0>().bounds() == static_bounds())); + { + array_view av = arr; + CHECK((av.sub<5, 0>().bounds() == static_bounds())); CHECK((av.sub<5, 0>().length() == 0)); - CHECK(av.sub(5,0).length() == 0); - CHECK_THROW(av.sub(6,0).length(), fail_fast); - } + CHECK(av.sub(5, 0).length() == 0); + CHECK_THROW(av.sub(6, 0).length(), fail_fast); + } - { - array_view av; - CHECK((av.sub<0,0>().bounds() == static_bounds())); - CHECK((av.sub<0,0>().length() == 0)); - CHECK(av.sub(0,0).length() == 0); - CHECK_THROW((av.sub<1,0>().length()), fail_fast); - } + { + array_view av; + CHECK((av.sub<0, 0>().bounds() == static_bounds())); + CHECK((av.sub<0, 0>().length() == 0)); + CHECK(av.sub(0, 0).length() == 0); + CHECK_THROW((av.sub<1, 0>().length()), fail_fast); + } { array_view av; @@ -1380,11 +1420,11 @@ SUITE(array_view_tests) CHECK_THROW(av.sub(6).length(), fail_fast); auto av2 = av.sub(1); for (int i = 0; i < 4; ++i) - CHECK(av2[i] == i+2); + CHECK(av2[i] == i + 2); } - + { - array_view av = arr; + array_view av = arr; CHECK(av.sub(0).length() == 5); CHECK(av.sub(1).length() == 4); CHECK(av.sub(4).length() == 1); @@ -1392,288 +1432,295 @@ SUITE(array_view_tests) CHECK_THROW(av.sub(6).length(), fail_fast); auto av2 = av.sub(1); for (int i = 0; i < 4; ++i) - CHECK(av2[i] == i+2); + CHECK(av2[i] == i + 2); } } - void AssertNullEmptyProperties(array_view& av) - { - CHECK(av.length() == 0); - CHECK(av.data() == nullptr); - CHECK(!av); - } + void AssertNullEmptyProperties(array_view & av) + { + CHECK(av.length() == 0); + CHECK(av.data() == nullptr); + CHECK(!av); + } - template - void AssertContentsMatch(T a1, U a2) - { - CHECK(a1.length() == a2.length()); - for (size_t i = 0; i < a1.length(); ++i) - CHECK(a1[i] == a2[i]); - } + template void AssertContentsMatch(T a1, U a2) + { + CHECK(a1.length() == a2.length()); + for (size_t i = 0; i < a1.length(); ++i) + CHECK(a1[i] == a2[i]); + } - TEST(TestNullConstruction) - { - array_view av; - AssertNullEmptyProperties(av); + TEST(TestNullConstruction) + { + array_view av; + AssertNullEmptyProperties(av); - array_view av2(nullptr); - AssertNullEmptyProperties(av2); - } + array_view av2(nullptr); + AssertNullEmptyProperties(av2); + } - TEST(ArrayConstruction) - { - int a[] = { 1, 2, 3, 4 }; + TEST(ArrayConstruction) + { + int a[] = { 1, 2, 3, 4 }; - array_view av = { &a[1], 3 }; - CHECK(av.length() == 3); + array_view av = { &a[1], 3 }; + CHECK(av.length() == 3); - array_view av3 = { a, 2 }; - CHECK(av3.length() == 2); + array_view av3 = { a, 2 }; + CHECK(av3.length() == 2); - array_view av2 = a; - CHECK(av2.length() == 4); - } + array_view av2 = a; + CHECK(av2.length() == 4); + } - TEST(NonConstConstConversions) - { - int a[] = { 1, 2, 3, 4 }; + TEST(NonConstConstConversions) + { + int a[] = { 1, 2, 3, 4 }; #ifdef CONFIRM_COMPILATION_ERRORS - array_view cav = a; - array_view av = cav; + array_view cav = a; + array_view av = cav; #else - array_view av = a; - array_view cav = av; + array_view av = a; + array_view cav = av; #endif - AssertContentsMatch(av, cav); - } + AssertContentsMatch(av, cav); + } - TEST(FixedSizeConversions) - { - int arr[] = { 1, 2, 3, 4 }; - - // converting to an array_view from an equal size array is ok - array_view av4 = arr; - CHECK(av4.length() == 4); + TEST(FixedSizeConversions) + { + int arr[] = { 1, 2, 3, 4 }; - // converting to dynamic_range a_v is always ok - { - array_view av = av4; - } - { - array_view av = arr; - } + // converting to an array_view from an equal size array is ok + array_view av4 = arr; + CHECK(av4.length() == 4); - // initialization or assignment to static array_view that REDUCES size is NOT ok + // converting to dynamic_range a_v is always ok + { + array_view av = av4; + } + { + array_view av = arr; + } + +// initialization or assignment to static array_view that REDUCES size is NOT ok #ifdef CONFIRM_COMPILATION_ERRORS - { - array_view av2 = arr; - } - { - array_view av2 = av4; - } + { + array_view av2 = arr; + } + { + array_view av2 = av4; + } #endif - { - array_view av = arr; - array_view av2 = av; - } + { + array_view av = arr; + array_view av2 = av; + } #ifdef CONFIRM_COMPILATION_ERRORS - { - array_view av = arr; - array_view av2 = av.as_array_view(dim<2>(), dim<2>()); - } + { + array_view av = arr; + array_view av2 = av.as_array_view(dim<2>(), dim<2>()); + } #endif - { - array_view av = arr; - auto f = [&]() {array_view av2 = av.as_array_view(dim<>(2), dim<>(2));}; - CHECK_THROW(f(), fail_fast); - } + { + array_view av = arr; + auto f = [&]() + { + array_view av2 = av.as_array_view(dim<>(2), dim<>(2)); + }; + CHECK_THROW(f(), fail_fast); + } - // but doing so explicitly is ok - - // you can convert statically - { - array_view av2 = {arr, 2}; - } - { - array_view av2 = av4.first<1>(); - } - - // ...or dynamically - { - // NB: implicit conversion to array_view from array_view - array_view av2 = av4.first(1); - } + // but doing so explicitly is ok - // initialization or assignment to static array_view that requires size INCREASE is not ok. - int arr2[2] = { 1, 2 }; + // you can convert statically + { + array_view av2 = { arr, 2 }; + } + { + array_view av2 = av4.first<1>(); + } + + // ...or dynamically + { + // NB: implicit conversion to array_view from array_view + array_view av2 = av4.first(1); + } + + // initialization or assignment to static array_view that requires size INCREASE is not ok. + int arr2[2] = { 1, 2 }; #ifdef CONFIRM_COMPILATION_ERRORS - { - array_view av4 = arr2; - } - { - array_view av2 = arr2; - array_view av4 = av2; - } + { + array_view av4 = arr2; + } + { + array_view av2 = arr2; + array_view av4 = av2; + } #endif - { - auto f = [&]() {array_view av4 = {arr2, 2};}; - CHECK_THROW(f(), fail_fast); - } + { + auto f = [&]() + { + array_view av4 = { arr2, 2 }; + }; + CHECK_THROW(f(), fail_fast); + } - // this should fail - we are trying to assign a small dynamic a_v to a fixed_size larger one - array_view av = arr2; - auto f = [&](){ array_view av2 = av; }; - CHECK_THROW(f(), fail_fast); - } + // this should fail - we are trying to assign a small dynamic a_v to a fixed_size larger one + array_view av = arr2; + auto f = [&]() + { + array_view av2 = av; + }; + CHECK_THROW(f(), fail_fast); + } - TEST(AsWriteableBytes) - { - int a[] = { 1, 2, 3, 4 }; + TEST(AsWriteableBytes) + { + int a[] = { 1, 2, 3, 4 }; - { + { #ifdef CONFIRM_COMPILATION_ERRORS - // you should not be able to get writeable bytes for const objects - array_view av = a; - auto wav = av.as_writeable_bytes(); + // you should not be able to get writeable bytes for const objects + array_view av = a; + auto wav = av.as_writeable_bytes(); #endif - } + } - { - array_view av; - auto wav = av.as_writeable_bytes(); - CHECK(wav.length() == av.length()); - CHECK(wav.length() == 0); - CHECK(wav.bytes() == 0); - } + { + array_view av; + auto wav = av.as_writeable_bytes(); + CHECK(wav.length() == av.length()); + CHECK(wav.length() == 0); + CHECK(wav.bytes() == 0); + } - { - array_view av = a; - auto wav = av.as_writeable_bytes(); - CHECK(wav.data() == (byte*)&a[0]); - CHECK(wav.length() == sizeof(a)); - } + { + array_view av = a; + auto wav = av.as_writeable_bytes(); + CHECK(wav.data() == (byte*)&a[0]); + CHECK(wav.length() == sizeof(a)); + } + } - } + TEST(ArrayViewComparison) + { + { + int arr[10][2]; + auto av1 = as_array_view(arr); + array_view av2 = av1; - TEST(ArrayViewComparison) - { - { - int arr[10][2]; - auto av1 = as_array_view(arr); - array_view av2 = av1; + CHECK(av1 == av2); - CHECK(av1 == av2); + array_view, 20> av3 = av1.as_array_view(dim<>(20)); + CHECK(av3 == av2 && av3 == av1); + } - array_view, 20> av3 = av1.as_array_view(dim<>(20)); - CHECK(av3 == av2 && av3 == av1); - } + { + auto av1 = nullptr; + auto av2 = nullptr; + CHECK(av1 == av2); + CHECK(!(av1 != av2)); + CHECK(!(av1 < av2)); + CHECK(av1 <= av2); + CHECK(!(av1 > av2)); + CHECK(av1 >= av2); + CHECK(av2 == av1); + CHECK(!(av2 != av1)); + CHECK(!(av2 < av1)); + CHECK(av2 <= av1); + CHECK(!(av2 > av1)); + CHECK(av2 >= av1); + } - { - auto av1 = nullptr; - auto av2 = nullptr; - CHECK(av1 == av2); - CHECK(!(av1 != av2)); - CHECK(!(av1 < av2)); - CHECK(av1 <= av2); - CHECK(!(av1 > av2)); - CHECK(av1 >= av2); - CHECK(av2 == av1); - CHECK(!(av2 != av1)); - CHECK(!(av2 < av1)); - CHECK(av2 <= av1); - CHECK(!(av2 > av1)); - CHECK(av2 >= av1); - } + { + int arr[] = { 2, 1 }; // bigger - { - int arr[] = { 2, 1 }; // bigger + array_view av1 = nullptr; + array_view av2 = arr; - array_view av1 = nullptr; - array_view av2 = arr; + CHECK(av1 != av2); + CHECK(av2 != av1); + CHECK(!(av1 == av2)); + CHECK(!(av2 == av1)); + CHECK(av1 < av2); + CHECK(!(av2 < av1)); + CHECK(av1 <= av2); + CHECK(!(av2 <= av1)); + CHECK(av2 > av1); + CHECK(!(av1 > av2)); + CHECK(av2 >= av1); + CHECK(!(av1 >= av2)); + } - CHECK(av1 != av2); - CHECK(av2 != av1); - CHECK(!(av1 == av2)); - CHECK(!(av2 == av1)); - CHECK(av1 < av2); - CHECK(!(av2 < av1)); - CHECK(av1 <= av2); - CHECK(!(av2 <= av1)); - CHECK(av2 > av1); - CHECK(!(av1 > av2)); - CHECK(av2 >= av1); - CHECK(!(av1 >= av2)); - } + { + int arr1[] = { 1, 2 }; + int arr2[] = { 1, 2 }; + array_view av1 = arr1; + array_view av2 = arr2; - { - int arr1[] = { 1, 2 }; - int arr2[] = { 1, 2 }; - array_view av1 = arr1; - array_view av2 = arr2; + CHECK(av1 == av2); + CHECK(!(av1 != av2)); + CHECK(!(av1 < av2)); + CHECK(av1 <= av2); + CHECK(!(av1 > av2)); + CHECK(av1 >= av2); + CHECK(av2 == av1); + CHECK(!(av2 != av1)); + CHECK(!(av2 < av1)); + CHECK(av2 <= av1); + CHECK(!(av2 > av1)); + CHECK(av2 >= av1); + } - CHECK(av1 == av2); - CHECK(!(av1 != av2)); - CHECK(!(av1 < av2)); - CHECK(av1 <= av2); - CHECK(!(av1 > av2)); - CHECK(av1 >= av2); - CHECK(av2 == av1); - CHECK(!(av2 != av1)); - CHECK(!(av2 < av1)); - CHECK(av2 <= av1); - CHECK(!(av2 > av1)); - CHECK(av2 >= av1); - } + { + int arr[] = { 1, 2, 3 }; - { - int arr[] = { 1, 2, 3 }; + array_view av1 = { &arr[0], 2 }; // shorter + array_view av2 = arr; // longer - array_view av1 = { &arr[0], 2 }; // shorter - array_view av2 = arr; // longer + CHECK(av1 != av2); + CHECK(av2 != av1); + CHECK(!(av1 == av2)); + CHECK(!(av2 == av1)); + CHECK(av1 < av2); + CHECK(!(av2 < av1)); + CHECK(av1 <= av2); + CHECK(!(av2 <= av1)); + CHECK(av2 > av1); + CHECK(!(av1 > av2)); + CHECK(av2 >= av1); + CHECK(!(av1 >= av2)); + } - CHECK(av1 != av2); - CHECK(av2 != av1); - CHECK(!(av1 == av2)); - CHECK(!(av2 == av1)); - CHECK(av1 < av2); - CHECK(!(av2 < av1)); - CHECK(av1 <= av2); - CHECK(!(av2 <= av1)); - CHECK(av2 > av1); - CHECK(!(av1 > av2)); - CHECK(av2 >= av1); - CHECK(!(av1 >= av2)); - } + { + int arr1[] = { 1, 2 }; // smaller + int arr2[] = { 2, 1 }; // bigger - { - int arr1[] = { 1, 2 }; // smaller - int arr2[] = { 2, 1 }; // bigger + array_view av1 = arr1; + array_view av2 = arr2; - array_view av1 = arr1; - array_view av2 = arr2; - - CHECK(av1 != av2); - CHECK(av2 != av1); - CHECK(!(av1 == av2)); - CHECK(!(av2 == av1)); - CHECK(av1 < av2); - CHECK(!(av2 < av1)); - CHECK(av1 <= av2); - CHECK(!(av2 <= av1)); - CHECK(av2 > av1); - CHECK(!(av1 > av2)); - CHECK(av2 >= av1); - CHECK(!(av1 >= av2)); - } - } + CHECK(av1 != av2); + CHECK(av2 != av1); + CHECK(!(av1 == av2)); + CHECK(!(av2 == av1)); + CHECK(av1 < av2); + CHECK(!(av2 < av1)); + CHECK(av1 <= av2); + CHECK(!(av2 <= av1)); + CHECK(av2 > av1); + CHECK(!(av1 > av2)); + CHECK(av2 >= av1); + CHECK(!(av1 >= av2)); + } + } } -int main(int, const char *[]) +int main(int, const char* []) { - return UnitTest::RunAllTests(); + return UnitTest::RunAllTests(); } diff --git a/tests/assertion_tests.cpp b/tests/assertion_tests.cpp index acd381a..f5647d3 100644 --- a/tests/assertion_tests.cpp +++ b/tests/assertion_tests.cpp @@ -1,20 +1,20 @@ -/////////////////////////////////////////////////////////////////////////////// -// -// Copyright (c) 2015 Microsoft Corporation. All rights reserved. -// -// This code is licensed under the MIT License (MIT). -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// +/////////////////////////////////////////////////////////////////////////////// +// +// Copyright (c) 2015 Microsoft Corporation. All rights reserved. +// +// This code is licensed under the MIT License (MIT). +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// /////////////////////////////////////////////////////////////////////////////// -#include +#include #include using namespace gsl; @@ -34,7 +34,7 @@ SUITE(assertion_tests) } int g(int i) - { + { i++; Ensures(i > 0 && i < 10); return i; @@ -47,7 +47,7 @@ SUITE(assertion_tests) } } -int main(int, const char *[]) +int main(int, const char* []) { return UnitTest::RunAllTests(); } diff --git a/tests/at_tests.cpp b/tests/at_tests.cpp index d27dd9d..7027790 100644 --- a/tests/at_tests.cpp +++ b/tests/at_tests.cpp @@ -1,20 +1,20 @@ -/////////////////////////////////////////////////////////////////////////////// -// -// Copyright (c) 2015 Microsoft Corporation. All rights reserved. -// -// This code is licensed under the MIT License (MIT). -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// +/////////////////////////////////////////////////////////////////////////////// +// +// Copyright (c) 2015 Microsoft Corporation. All rights reserved. +// +// This code is licensed under the MIT License (MIT). +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// /////////////////////////////////////////////////////////////////////////////// -#include +#include #include #include @@ -28,17 +28,17 @@ SUITE(at_tests) int a[] = { 1, 2, 3, 4 }; for (int i = 0; i < 4; ++i) - CHECK(at(a, i) == i+1); + CHECK(at(a, i) == i + 1); CHECK_THROW(at(a, 4), fail_fast); } TEST(std_array) { - std::array a = { 1, 2, 3, 4 }; + std::array a = { 1, 2, 3, 4 }; for (int i = 0; i < 4; ++i) - CHECK(at(a, i) == i+1); + CHECK(at(a, i) == i + 1); CHECK_THROW(at(a, 4), fail_fast); } @@ -48,13 +48,13 @@ SUITE(at_tests) std::vector a = { 1, 2, 3, 4 }; for (int i = 0; i < 4; ++i) - CHECK(at(a, i) == i+1); + CHECK(at(a, i) == i + 1); CHECK_THROW(at(a, 4), fail_fast); } } -int main(int, const char *[]) +int main(int, const char* []) { return UnitTest::RunAllTests(); } diff --git a/tests/bounds_tests.cpp b/tests/bounds_tests.cpp index c3f549f..4e9dd77 100644 --- a/tests/bounds_tests.cpp +++ b/tests/bounds_tests.cpp @@ -1,99 +1,103 @@ -/////////////////////////////////////////////////////////////////////////////// -// -// Copyright (c) 2015 Microsoft Corporation. All rights reserved. -// -// This code is licensed under the MIT License (MIT). -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// +/////////////////////////////////////////////////////////////////////////////// +// +// Copyright (c) 2015 Microsoft Corporation. All rights reserved. +// +// This code is licensed under the MIT License (MIT). +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// /////////////////////////////////////////////////////////////////////////////// -#include +#include #include #include using namespace std; -using namespace gsl;; +using namespace gsl; +; -namespace +namespace { - void use(unsigned int&) {} +void use(unsigned int&) +{ +} } SUITE(bounds_test) { - TEST(basic_bounds) - { - for (auto point : static_bounds { 2 }) - { - for (unsigned int j = 0; j < decltype(point)::rank; j++) - { - use(j); - use(point[j]); - } - } - } - - TEST(bounds_basic) - { - static_bounds b; - auto a = b.slice(); - static_bounds x{ 4 }; - x.slice().slice(); - } - - TEST (arrayview_iterator) - { - static_bounds bounds{ 3 }; - - auto itr = bounds.begin(); - + TEST(basic_bounds) + { + for (auto point : static_bounds{ 2 }) + { + for (unsigned int j = 0; j < decltype(point)::rank; j++) + { + use(j); + use(point[j]); + } + } + } + + TEST(bounds_basic) + { + static_bounds b; + auto a = b.slice(); + static_bounds x{ 4 }; + x.slice().slice(); + } + + TEST(arrayview_iterator) + { + static_bounds bounds{ 3 }; + + auto itr = bounds.begin(); + #ifdef CONFIRM_COMPILATION_ERRORS - array_view< int, 4, dynamic_range, 2> av(nullptr, bounds); - - auto itr2 = av.cbegin(); - - for (auto & v : av) { - v = 4; - } - fill(av.begin(), av.end(), 0); -#endif - } - - TEST (bounds_convertible) - { - static_bounds b1; - static_bounds b2 = b1; - -#ifdef CONFIRM_COMPILATION_ERRORS - static_bounds b4 = b2; + array_view av(nullptr, bounds); + + auto itr2 = av.cbegin(); + + for (auto& v : av) + { + v = 4; + } + fill(av.begin(), av.end(), 0); #endif - - static_bounds b3 = b1; - static_bounds b4 = b3; + } - static_bounds b11; - - static_bounds b5; - static_bounds b6; - - b5 = static_bounds(); - CHECK_THROW(b6 = b5, fail_fast); - b5 = static_bounds(); - b6 = b5; + TEST(bounds_convertible) + { + static_bounds b1; + static_bounds b2 = b1; - CHECK(b5 == b6); - CHECK(b5.size() == b6.size()); - } +#ifdef CONFIRM_COMPILATION_ERRORS + static_bounds b4 = b2; +#endif + + static_bounds b3 = b1; + static_bounds b4 = b3; + + static_bounds b11; + + static_bounds b5; + static_bounds b6; + + b5 = static_bounds(); + CHECK_THROW(b6 = b5, fail_fast); + b5 = static_bounds(); + b6 = b5; + + CHECK(b5 == b6); + CHECK(b5.size() == b6.size()); + } } -int main(int, const char *[]) +int main(int, const char* []) { - return UnitTest::RunAllTests(); + return UnitTest::RunAllTests(); } diff --git a/tests/maybenull_tests.cpp b/tests/maybenull_tests.cpp index 74d449f..6f9ff6d 100644 --- a/tests/maybenull_tests.cpp +++ b/tests/maybenull_tests.cpp @@ -1,29 +1,39 @@ -/////////////////////////////////////////////////////////////////////////////// -// -// Copyright (c) 2015 Microsoft Corporation. All rights reserved. -// -// This code is licensed under the MIT License (MIT). -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// +/////////////////////////////////////////////////////////////////////////////// +// +// Copyright (c) 2015 Microsoft Corporation. All rights reserved. +// +// This code is licensed under the MIT License (MIT). +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// /////////////////////////////////////////////////////////////////////////////// -#include +#include #include #include #include using namespace gsl; -struct MyBase { bool foo() { return true; } }; -struct MyDerived : public MyBase {}; -struct Unrelated {}; +struct MyBase +{ + bool foo() + { + return true; + } +}; +struct MyDerived : public MyBase +{ +}; +struct Unrelated +{ +}; SUITE(MaybeNullTests) { @@ -31,46 +41,45 @@ SUITE(MaybeNullTests) { #ifdef CONFIRM_COMPILATION_ERRORS // Forbid non-nullptr assignable types - maybe_null_ret> f_ret(std::vector{1}); - maybe_null_ret> f_ret(std::vector{1}); + maybe_null_ret> f_ret(std::vector{ 1 }); + maybe_null_ret> f_ret(std::vector{ 1 }); maybe_null_ret z_ret(10); - maybe_null_dbg> y_dbg({1,2}); + maybe_null_dbg> y_dbg({ 1, 2 }); maybe_null_dbg z_dbg(10); - maybe_null_dbg> y_dbg({1,2}); + maybe_null_dbg> y_dbg({ 1, 2 }); #endif int n = 5; - maybe_null_dbg opt_n(&n); + maybe_null_dbg opt_n(&n); int result = 0; bool threw = false; CHECK_THROW(result = *opt_n, fail_fast); - maybe_null_ret> x_ret(std::make_shared(10)); // shared_ptr is nullptr assignable - maybe_null_dbg> x_dbg(std::make_shared(10)); // shared_ptr is nullptr assignable + maybe_null_ret> x_ret( + std::make_shared(10)); // shared_ptr is nullptr assignable + maybe_null_dbg> x_dbg( + std::make_shared(10)); // shared_ptr is nullptr assignable } TEST(TestMaybeNull2) { int n = 5; - maybe_null opt_n(&n); + maybe_null opt_n(&n); int result = 0; - if (opt_n.present()) - result = *opt_n; + if (opt_n.present()) result = *opt_n; } TEST(TestMaybeNull3) { int n = 5; - maybe_null opt_n(&n); + maybe_null opt_n(&n); int result = 0; - if (opt_n != nullptr) - result = *opt_n; + if (opt_n != nullptr) result = *opt_n; } - int test4_helper(maybe_null p) + int test4_helper(maybe_null p) { - if (p != nullptr) - return *p; + if (p != nullptr) return *p; return -1; } @@ -81,7 +90,7 @@ SUITE(MaybeNullTests) result = test4_helper(&n); } - int test5_helper(maybe_null_dbg p) + int test5_helper(maybe_null_dbg p) { return *p; } @@ -104,68 +113,64 @@ SUITE(MaybeNullTests) #endif int g_int; - void test7_helper(maybe_null *> outptr) + void test7_helper(maybe_null*> outptr) { g_int = 5; - if (outptr.present()) - *outptr = &g_int; + if (outptr.present()) *outptr = &g_int; } - void test7b_helper(maybe_null_dbg *> outptr) + void test7b_helper(maybe_null_dbg*> outptr) { g_int = 5; - if (outptr.present()) - *outptr = &g_int; + if (outptr.present()) *outptr = &g_int; } TEST(TestMaybeNull7a) { - maybe_null outval; + maybe_null outval; test7_helper(&outval); CHECK(outval.present() && *outval == 5); } TEST(TestMaybeNull7b) { - maybe_null_dbg outval; + maybe_null_dbg outval; test7b_helper(&outval); - CHECK_THROW((void)*outval, fail_fast); + CHECK_THROW((void)*outval, fail_fast); } - int test8_helper1(maybe_null_dbg opt) + int test8_helper1(maybe_null_dbg opt) { return *opt; } - int test8_helper2a(maybe_null_dbg opt) + int test8_helper2a(maybe_null_dbg opt) { - if (!opt.present()) - return 0; + if (!opt.present()) return 0; return test8_helper1(opt); } TEST(TestMaybeNull8a) { int n = 5; - maybe_null_dbg opt(&n); - CHECK_THROW(test8_helper2a(opt), fail_fast); + maybe_null_dbg opt(&n); + CHECK_THROW(test8_helper2a(opt), fail_fast); } #ifdef CONVERT_TO_PTR_TO_CONST - int test9_helper(maybe_null copt) + int test9_helper(maybe_null copt) { - if (copt.present()) - return *copt; + if (copt.present()) return *copt; return 0; } void TestMaybeNull9() { int n = 5; - maybe_null opt(&n); - CHECK_THROW(test9_helper(opt), fail_fast); + maybe_null opt(&n); + CHECK_THROW(test9_helper(opt), fail_fast); } #endif @@ -298,7 +303,7 @@ SUITE(MaybeNullTests) } } -int main(int, const char *[]) +int main(int, const char* []) { return UnitTest::RunAllTests(); } diff --git a/tests/notnull_tests.cpp b/tests/notnull_tests.cpp index a9624b8..6c50e62 100644 --- a/tests/notnull_tests.cpp +++ b/tests/notnull_tests.cpp @@ -1,35 +1,45 @@ -/////////////////////////////////////////////////////////////////////////////// -// -// Copyright (c) 2015 Microsoft Corporation. All rights reserved. -// -// This code is licensed under the MIT License (MIT). -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// +/////////////////////////////////////////////////////////////////////////////// +// +// Copyright (c) 2015 Microsoft Corporation. All rights reserved. +// +// This code is licensed under the MIT License (MIT). +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// /////////////////////////////////////////////////////////////////////////////// -#include +#include #include #include using namespace gsl; -struct MyBase {}; -struct MyDerived : public MyBase {}; -struct Unrelated {}; +struct MyBase +{ +}; +struct MyDerived : public MyBase +{ +}; +struct Unrelated +{ +}; // stand-in for a user-defined ref-counted class -template -struct RefCounted +template struct RefCounted { - RefCounted(T* p) : p_(p) {} - operator T*() { return p_; } + RefCounted(T* p) : p_(p) + { + } + operator T*() + { + return p_; + } T* p_; }; @@ -51,32 +61,33 @@ SUITE(NotNullTests) not_null p = up; // Forbid non-nullptr assignable types - not_null> f(std::vector{1}); + not_null> f(std::vector{ 1 }); not_null z(10); - not_null> y({1,2}); + not_null> y({ 1, 2 }); #endif - int i = 12; - auto rp = RefCounted(&i); - not_null p(rp); - CHECK(p.get() == &i); + int i = 12; + auto rp = RefCounted(&i); + not_null p(rp); + CHECK(p.get() == &i); - not_null> x(std::make_shared(10)); // shared_ptr is nullptr assignable + not_null> x( + std::make_shared(10)); // shared_ptr is nullptr assignable } TEST(TestNotNullCasting) { MyBase base; - MyDerived derived; - Unrelated unrelated; - not_null u = &unrelated; + MyDerived derived; + Unrelated unrelated; + not_null u = &unrelated; not_null p = &derived; not_null q = &base; - q = p; // allowed with heterogeneous copy ctor + q = p; // allowed with heterogeneous copy ctor CHECK(q == p); #ifdef CONFIRM_COMPILATION_ERRORS - q = u; // no viable conversion possible between MyBase* and Unrelated* - p = q; // not possible to implicitly convert MyBase* to MyDerived* + q = u; // no viable conversion possible between MyBase* and Unrelated* + p = q; // not possible to implicitly convert MyBase* to MyDerived* not_null r = p; not_null s = reinterpret_cast(p); @@ -88,7 +99,7 @@ SUITE(NotNullTests) TEST(TestNotNullAssignment) { int i = 12; - not_null p = &i; + not_null p = &i; CHECK(helper(p)); int* q = nullptr; @@ -96,7 +107,7 @@ SUITE(NotNullTests) } } -int main(int, const char *[]) +int main(int, const char* []) { return UnitTest::RunAllTests(); } diff --git a/tests/owner_tests.cpp b/tests/owner_tests.cpp index 47c223a..84aebf8 100644 --- a/tests/owner_tests.cpp +++ b/tests/owner_tests.cpp @@ -1,20 +1,20 @@ -/////////////////////////////////////////////////////////////////////////////// -// -// Copyright (c) 2015 Microsoft Corporation. All rights reserved. -// -// This code is licensed under the MIT License (MIT). -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// +/////////////////////////////////////////////////////////////////////////////// +// +// Copyright (c) 2015 Microsoft Corporation. All rights reserved. +// +// This code is licensed under the MIT License (MIT). +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// /////////////////////////////////////////////////////////////////////////////// -#include +#include #include #include @@ -37,7 +37,7 @@ SUITE(owner_tests) } } -int main(int, const char *[]) +int main(int, const char* []) { return UnitTest::RunAllTests(); } diff --git a/tests/string_view_tests.cpp b/tests/string_view_tests.cpp index 84b7f3f..0ec02be 100644 --- a/tests/string_view_tests.cpp +++ b/tests/string_view_tests.cpp @@ -1,20 +1,20 @@ -/////////////////////////////////////////////////////////////////////////////// -// -// Copyright (c) 2015 Microsoft Corporation. All rights reserved. -// -// This code is licensed under the MIT License (MIT). -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// +/////////////////////////////////////////////////////////////////////////////// +// +// Copyright (c) 2015 Microsoft Corporation. All rights reserved. +// +// This code is licensed under the MIT License (MIT). +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// /////////////////////////////////////////////////////////////////////////////// -#include +#include #include #include #include @@ -26,7 +26,7 @@ SUITE(string_view_tests) { TEST(TestLiteralConstruction) - { + { cwstring_view<> v = ensure_z(L"Hello"); CHECK(5 == v.length()); @@ -34,7 +34,7 @@ SUITE(string_view_tests) #ifdef CONFIRM_COMPILATION_ERRORS wstring_view<> v2 = ensure0(L"Hello"); #endif - } + } TEST(TestConstructFromStdString) { @@ -50,8 +50,8 @@ SUITE(string_view_tests) CHECK(v.length() == vec.size()); } - TEST(TestStackArrayConstruction) - { + TEST(TestStackArrayConstruction) + { wchar_t stack_string[] = L"Hello"; { @@ -77,13 +77,13 @@ SUITE(string_view_tests) CHECK(v.length() == 6); CHECK(v.used_length() == v.length()); } - } + } TEST(TestConversionToConst) { char stack_string[] = "Hello"; string_view<> v = ensure_z(stack_string); - cstring_view<> v2 = v; + cstring_view<> v2 = v; CHECK(v.length() == v2.length()); } @@ -98,7 +98,7 @@ SUITE(string_view_tests) } } -int main(int, const char *[]) +int main(int, const char* []) { return UnitTest::RunAllTests(); } diff --git a/tests/utils_tests.cpp b/tests/utils_tests.cpp index 3090c7d..0db3cd2 100644 --- a/tests/utils_tests.cpp +++ b/tests/utils_tests.cpp @@ -1,20 +1,20 @@ -/////////////////////////////////////////////////////////////////////////////// -// -// Copyright (c) 2015 Microsoft Corporation. All rights reserved. -// -// This code is licensed under the MIT License (MIT). -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// +/////////////////////////////////////////////////////////////////////////////// +// +// Copyright (c) 2015 Microsoft Corporation. All rights reserved. +// +// This code is licensed under the MIT License (MIT). +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// /////////////////////////////////////////////////////////////////////////////// -#include +#include #include #include @@ -31,7 +31,10 @@ SUITE(utils_tests) { int i = 0; { - auto _ = finally([&]() {f(i);}); + auto _ = finally([&]() + { + f(i); + }); CHECK(i == 0); } CHECK(i == 1); @@ -41,12 +44,15 @@ SUITE(utils_tests) { int i = 0; { - auto _1 = finally([&]() {f(i);}); + auto _1 = finally([&]() + { + f(i); + }); { auto _2 = std::move(_1); - CHECK(i == 0); + CHECK(i == 0); } - CHECK(i == 1); + CHECK(i == 1); } CHECK(i == 1); } @@ -62,7 +68,10 @@ SUITE(utils_tests) } int j = 0; - void g() { j += 1; }; + void g() + { + j += 1; + }; TEST(finally_function_ptr) { j = 0; @@ -90,12 +99,12 @@ SUITE(utils_tests) char c = narrow(n); CHECK(c == 120); - n = 300; + n = 300; CHECK_THROW(narrow(n), narrowing_error); } } -int main(int, const char *[]) +int main(int, const char* []) { return UnitTest::RunAllTests(); }