1 | //===----------------------- adt.h - Handy ADTs -----------------*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, 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 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file is a part of the ORC runtime support library. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #ifndef ORC_RT_ADT_H |
14 | #define ORC_RT_ADT_H |
15 | |
16 | #include <cstring> |
17 | #include <limits> |
18 | #include <ostream> |
19 | #include <string> |
20 | |
21 | namespace __orc_rt { |
22 | |
23 | constexpr std::size_t dynamic_extent = std::numeric_limits<std::size_t>::max(); |
24 | |
25 | /// A substitute for std::span (and llvm::ArrayRef). |
26 | /// FIXME: Remove in favor of std::span once we can use c++20. |
27 | template <typename T, std::size_t Extent = dynamic_extent> class span { |
28 | public: |
29 | typedef T element_type; |
30 | typedef std::remove_cv<T> value_type; |
31 | typedef std::size_t size_type; |
32 | typedef std::ptrdiff_t difference_type; |
33 | typedef T *pointer; |
34 | typedef const T *const_pointer; |
35 | typedef T &reference; |
36 | typedef const T &const_reference; |
37 | |
38 | typedef pointer iterator; |
39 | |
40 | static constexpr std::size_t extent = Extent; |
41 | |
42 | constexpr span() noexcept = default; |
43 | constexpr span(T *first, size_type count) noexcept |
44 | : Data(first), Size(count) {} |
45 | |
46 | template <std::size_t N> |
47 | constexpr span(T (&arr)[N]) noexcept : Data(&arr[0]), Size(N) {} |
48 | |
49 | constexpr iterator begin() const noexcept { return Data; } |
50 | constexpr iterator end() const noexcept { return Data + Size; } |
51 | constexpr pointer data() const noexcept { return Data; } |
52 | constexpr reference operator[](size_type idx) const { return Data[idx]; } |
53 | constexpr size_type size() const noexcept { return Size; } |
54 | constexpr bool empty() const noexcept { return Size == 0; } |
55 | |
56 | private: |
57 | T *Data = nullptr; |
58 | size_type Size = 0; |
59 | }; |
60 | |
61 | } // end namespace __orc_rt |
62 | |
63 | #endif // ORC_RT_ADT_H |
64 | |