|
| 1 | +// |
| 2 | +// This is a derivative work. originally part of the LLVM Project. |
| 3 | +// Licensed under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +// Copyright (c) 2025 Alan de Freitas (alandefreitas@gmail.com) |
| 8 | +// |
| 9 | +// Official repository: https://github.com/cppalliance/mrdocs |
| 10 | +// |
| 11 | + |
| 12 | +#ifndef MRDOCS_API_ADT_ARRAYVIEW_HPP |
| 13 | +#define MRDOCS_API_ADT_ARRAYVIEW_HPP |
| 14 | + |
| 15 | +#include <mrdocs/Platform.hpp> |
| 16 | +#include <algorithm> |
| 17 | +#include <cassert> |
| 18 | +#include <compare> |
| 19 | +#include <cstddef> |
| 20 | +#include <iterator> |
| 21 | +#include <memory> |
| 22 | +#include <type_traits> |
| 23 | + |
| 24 | +namespace mrdocs { |
| 25 | + |
| 26 | +/** A non-owning, read-only view over a contiguous array of T. |
| 27 | +
|
| 28 | + Similar to std::string_view but for arbitrary element type T. |
| 29 | +*/ |
| 30 | +template <class T> |
| 31 | +class ArrayView { |
| 32 | + static_assert(!std::is_void_v<T>, "ArrayView<void> is ill-formed"); |
| 33 | + |
| 34 | +public: |
| 35 | + // types |
| 36 | + using value_type = T; |
| 37 | + using size_type = std::size_t; |
| 38 | + using difference_type = std::ptrdiff_t; |
| 39 | + using pointer = const T*; |
| 40 | + using const_pointer = const T*; |
| 41 | + using reference = const T&; |
| 42 | + using const_reference = const T&; |
| 43 | + using iterator = const T*; |
| 44 | + using const_iterator = const T*; |
| 45 | + using reverse_iterator = std::reverse_iterator<const_iterator>; |
| 46 | + using const_reverse_iterator = std::reverse_iterator<const_iterator>; |
| 47 | + |
| 48 | + static constexpr size_type npos = static_cast<size_type>(-1); |
| 49 | + |
| 50 | + // ctors |
| 51 | + constexpr ArrayView() noexcept = default; |
| 52 | + |
| 53 | + constexpr ArrayView(const T* data, size_type count) noexcept |
| 54 | + : data_(data), size_(count) {} |
| 55 | + |
| 56 | + template <size_type N> |
| 57 | + constexpr ArrayView(const T (&arr)[N]) noexcept |
| 58 | + : data_(arr), size_(N) {} |
| 59 | + |
| 60 | + template <class It> |
| 61 | + requires (std::contiguous_iterator<It> && |
| 62 | + std::same_as<std::remove_cv_t<std::remove_reference_t<std::iter_value_t<It>>>, T>) |
| 63 | + constexpr ArrayView(It first, size_type count) noexcept |
| 64 | + : data_(std::to_address(first)), size_(count) {} |
| 65 | + |
| 66 | + // iterators |
| 67 | + constexpr const_iterator begin() const noexcept { return data_; } |
| 68 | + constexpr const_iterator end() const noexcept { return data_ + size_; } |
| 69 | + constexpr const_iterator cbegin() const noexcept { return begin(); } |
| 70 | + constexpr const_iterator cend() const noexcept { return end(); } |
| 71 | + constexpr const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); } |
| 72 | + constexpr const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); } |
| 73 | + |
| 74 | + // capacity |
| 75 | + constexpr size_type size() const noexcept { return size_; } |
| 76 | + constexpr size_type length() const noexcept { return size_; } |
| 77 | + [[nodiscard]] constexpr bool empty() const noexcept { return size_ == 0; } |
| 78 | + |
| 79 | + // element access |
| 80 | + constexpr const_reference operator[](size_type i) const noexcept { |
| 81 | + return data_[i]; |
| 82 | + } |
| 83 | + constexpr const_reference at(size_type i) const { |
| 84 | + assert(i < size_); |
| 85 | + return data_[i]; |
| 86 | + } |
| 87 | + constexpr const_reference front() const { |
| 88 | + assert(!empty()); |
| 89 | + return data_[0]; |
| 90 | + } |
| 91 | + constexpr const_reference back() const { |
| 92 | + assert(!empty()); |
| 93 | + return data_[size_ - 1]; |
| 94 | + } |
| 95 | + constexpr const_pointer data() const noexcept { return data_; } |
| 96 | + |
| 97 | + // modifiers (adjust the view; do not touch underlying data) |
| 98 | + constexpr void remove_prefix(size_type n) noexcept { |
| 99 | + assert(n <= size_); |
| 100 | + data_ += n; |
| 101 | + size_ -= n; |
| 102 | + } |
| 103 | + constexpr void remove_suffix(size_type n) noexcept { |
| 104 | + assert(n <= size_); |
| 105 | + size_ -= n; |
| 106 | + } |
| 107 | + |
| 108 | + // slicing |
| 109 | + constexpr ArrayView slice(size_type pos, size_type count = npos) const noexcept { |
| 110 | + assert(pos <= size_); |
| 111 | + const size_type rcount = (count == npos || pos + count > size_) ? (size_ - pos) : count; |
| 112 | + return ArrayView(data_ + pos, rcount); |
| 113 | + } |
| 114 | + constexpr ArrayView take_front(size_type n) const noexcept { |
| 115 | + return slice(0, n <= size_ ? n : size_); |
| 116 | + } |
| 117 | + constexpr ArrayView take_back(size_type n) const noexcept { |
| 118 | + n = (n <= size_) ? n : size_; |
| 119 | + return slice(size_ - n, n); |
| 120 | + } |
| 121 | + constexpr ArrayView drop_front(size_type n) const noexcept { |
| 122 | + return (n >= size_) ? ArrayView() : ArrayView(data_ + n, size_ - n); |
| 123 | + } |
| 124 | + constexpr ArrayView drop_back(size_type n) const noexcept { |
| 125 | + return (n >= size_) ? ArrayView() : ArrayView(data_, size_ - n); |
| 126 | + } |
| 127 | + |
| 128 | + // comparisons |
| 129 | + friend constexpr bool operator==(ArrayView a, ArrayView b) noexcept |
| 130 | + requires requires (const T& x, const T& y) { { x == y } -> std::convertible_to<bool>; } |
| 131 | + { |
| 132 | + return a.size_ == b.size_ && std::equal(a.begin(), a.end(), b.begin()); |
| 133 | + } |
| 134 | + |
| 135 | + friend constexpr auto operator<=>(ArrayView a, ArrayView b) noexcept |
| 136 | + requires requires (const T& x, const T& y) { x <=> y; } |
| 137 | + { |
| 138 | + return std::lexicographical_compare_three_way( |
| 139 | + a.begin(), a.end(), b.begin(), b.end(), std::compare_three_way{}); |
| 140 | + } |
| 141 | + |
| 142 | +private: |
| 143 | + const T* data_ = nullptr; |
| 144 | + size_type size_ = 0; |
| 145 | +}; |
| 146 | + |
| 147 | +// deduction guides |
| 148 | +template <class T> |
| 149 | +ArrayView(const T*, std::size_t) -> ArrayView<T>; |
| 150 | + |
| 151 | +template <class T, std::size_t N> |
| 152 | +ArrayView(const T (&)[N]) -> ArrayView<T>; |
| 153 | + |
| 154 | +// helpers |
| 155 | +template <class T> |
| 156 | +constexpr ArrayView<T> make_array_view(const T* data, std::size_t count) noexcept { |
| 157 | + return ArrayView<T>(data, count); |
| 158 | +} |
| 159 | + |
| 160 | +template <class T, std::size_t N> |
| 161 | +constexpr ArrayView<T> make_array_view(const T (&arr)[N]) noexcept { |
| 162 | + return ArrayView<T>(arr); |
| 163 | +} |
| 164 | + |
| 165 | +} // mrdocs |
| 166 | + |
| 167 | +#endif // MRDOCS_API_ADT_ARRAYVIEW_HPP |
0 commit comments