From 0bf36b679a9d408a1ff22dfbc08accaca20d0c1d Mon Sep 17 00:00:00 2001 From: q66 Date: Wed, 25 Jan 2017 23:46:48 +0100 Subject: [PATCH] iter extensions for std::array --- examples/range_pipe.cc | 2 +- ostd/array.hh | 136 ++++------------------------------------- 2 files changed, 12 insertions(+), 126 deletions(-) diff --git a/examples/range_pipe.cc b/examples/range_pipe.cc index 146cab8..648c544 100644 --- a/examples/range_pipe.cc +++ b/examples/range_pipe.cc @@ -68,7 +68,7 @@ int main() { writeln("several piped algorithms"); srand(time(0)); - Array arr; + std::array arr; generate(iter(arr), []() { return rand() % 128; }); auto r = iter(arr) diff --git a/ostd/array.hh b/ostd/array.hh index 077460c..334aa88 100644 --- a/ostd/array.hh +++ b/ostd/array.hh @@ -1,4 +1,4 @@ -/* Static array implementation for OctaSTD. +/* OctaSTD extensions for std::array. * * This file is part of OctaSTD. See COPYING.md for futher information. */ @@ -6,139 +6,25 @@ #ifndef OSTD_ARRAY_HH #define OSTD_ARRAY_HH -#include +#include -#include "ostd/algorithm.hh" #include "ostd/range.hh" -#include "ostd/string.hh" -#include "ostd/utility.hh" -#include "ostd/internal/tuple.hh" namespace ostd { -template -struct Array { - using Size = ostd::Size; - using Difference = Ptrdiff; - using Value = T; - using Reference = T &; - using ConstReference = T const &; - using Pointer = T *; - using ConstPointer = T const *; - using Range = PointerRange; - using ConstRange = PointerRange; - - T &operator[](Size i) noexcept { return p_buf[i]; } - T const &operator[](Size i) const noexcept { return p_buf[i]; } - - T *at(Size i) noexcept { - if (!in_range(i)) { - return nullptr; - } - return &p_buf[i]; - } - T const *at(Size i) const noexcept { - if (!in_range(i)) { - return nullptr; - } - return &p_buf[i]; - } - - T &front() noexcept { return p_buf[0]; } - T const &front() const noexcept { return p_buf[0]; } - - T &back() noexcept { return p_buf[(N > 0) ? (N - 1) : 0]; } - T const &back() const noexcept { return p_buf[(N > 0) ? (N - 1) : 0]; } - - Size size() const noexcept { return N; } - Size max_size() const noexcept { return Size(~0) / sizeof(T); } - - bool empty() const noexcept { return N == 0; } - - bool in_range(Size idx) noexcept { return idx < N; } - bool in_range(int idx) noexcept { return idx >= 0 && Size(idx) < N; } - bool in_range(ConstPointer ptr) noexcept { - return ptr >= &p_buf[0] && ptr < &p_buf[N]; - } - - Pointer data() noexcept { return p_buf; } - ConstPointer data() const noexcept { return p_buf; } - - Range iter() noexcept { - return Range(p_buf, p_buf + N); - } - ConstRange iter() const noexcept { - return ConstRange(p_buf, p_buf + N); - } - ConstRange citer() const noexcept { - return ConstRange(p_buf, p_buf + N); - } - - void swap(Array &v) noexcept( - noexcept(ostd::swap(declval(), declval())) - ) { - ostd::swap_ranges(iter(), v.iter()); - } - - T p_buf[(N > 0) ? N : 1]; -}; - -template -constexpr Size TupleSize> = N; - -template -struct TupleElementBase> { - using Type = T; -}; - -template -inline TupleElement> &get(Array &a) noexcept { - return a[I]; +template +inline PointerRange iter(std::array &v) { + return PointerRange{v.data(), N}; } -template -inline TupleElement> const &get(Array const &a) noexcept { - return a[I]; +template +inline PointerRange iter(std::array const &v) { + return PointerRange{v.data(), N}; } -template -inline TupleElement> &&get(Array &&a) noexcept { - return std::move(a.p_buf[I]); -} - -template -inline TupleElement> const &&get(Array const &&a) noexcept { - return std::move(a.p_buf[I]); -} - -template -inline bool operator==(Array const &x, Array const &y) { - return equal(x.iter(), y.iter()); -} - -template -inline bool operator!=(Array const &x, Array const &y) { - return !(x == y); -} - -template -inline bool operator<(Array const &x, Array const &y) { - return lexicographical_compare(x.iter(), y.iter()); -} - -template -inline bool operator>(Array const &x, Array const &y) { - return (y < x); -} - -template -inline bool operator<=(Array const &x, Array const &y) { - return !(y < x); -} - -template -inline bool operator>=(Array const &x, Array const &y) { - return !(x < y); +template +inline PointerRange citer(std::array const &v) { + return PointerRange{v.data(), N}; } } /* namespace ostd */