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 <string> 19 20 namespace __orc_rt { 21 22 constexpr std::size_t dynamic_extent = std::numeric_limits<std::size_t>::max(); 23 24 /// A substitute for std::span (and llvm::ArrayRef). 25 /// FIXME: Remove in favor of std::span once we can use c++20. 26 template <typename T, std::size_t Extent = dynamic_extent> class span { 27 public: 28 typedef T element_type; 29 typedef std::remove_cv<T> value_type; 30 typedef std::size_t size_type; 31 typedef std::ptrdiff_t difference_type; 32 typedef T *pointer; 33 typedef const T *const_pointer; 34 typedef T &reference; 35 typedef const T &const_reference; 36 37 typedef pointer iterator; 38 39 static constexpr std::size_t extent = Extent; 40 41 constexpr span() noexcept = default; 42 constexpr span(T *first, size_type count) noexcept 43 : Data(first), Size(count) {} 44 45 template <std::size_t N> 46 constexpr span(T (&arr)[N]) noexcept : Data(&arr[0]), Size(N) {} 47 48 constexpr iterator begin() const noexcept { return Data; } 49 constexpr iterator end() const noexcept { return Data + Size; } 50 constexpr pointer data() const noexcept { return Data; } 51 constexpr reference operator[](size_type idx) const { return Data[idx]; } 52 constexpr size_type size() const noexcept { return Size; } 53 constexpr bool empty() const noexcept { return Size == 0; } 54 55 private: 56 T *Data = nullptr; 57 size_type Size = 0; 58 }; 59 60 /// A substitue for std::string_view (and llvm::StringRef). 61 /// FIXME: Remove in favor of std::string_view once we have c++17. 62 class string_view { 63 public: 64 typedef char value_type; 65 typedef char *pointer; 66 typedef const char *const_pointer; 67 typedef char &reference; 68 typedef const char &const_reference; 69 typedef std::size_t size_type; 70 typedef std::ptrdiff_t difference_type; 71 72 typedef const_pointer const_iterator; 73 typedef const_iterator iterator; 74 75 constexpr string_view() noexcept = default; 76 constexpr string_view(const char *S, size_type Count) 77 : Data(S), Size(Count) {} 78 string_view(const char *S) : Data(S), Size(strlen(S)) {} 79 80 constexpr const_iterator begin() const noexcept { return Data; } 81 constexpr const_iterator end() const noexcept { return Data + Size; } 82 constexpr const_pointer data() const noexcept { return Data; } 83 constexpr const_reference operator[](size_type idx) { return Data[idx]; } 84 constexpr size_type size() const noexcept { return Size; } 85 constexpr bool empty() const noexcept { return Size == 0; } 86 87 friend bool operator==(const string_view &LHS, const string_view &RHS) { 88 if (LHS.Size != RHS.Size) 89 return false; 90 if (LHS.Data == RHS.Data) 91 return true; 92 for (size_t I = 0; I != LHS.Size; ++I) 93 if (LHS.Data[I] != RHS.Data[I]) 94 return false; 95 return true; 96 } 97 98 friend bool operator!=(const string_view &LHS, const string_view &RHS) { 99 return !(LHS == RHS); 100 } 101 102 private: 103 const char *Data = nullptr; 104 size_type Size = 0; 105 }; 106 107 inline std::string to_string(string_view SV) { 108 return std::string(SV.data(), SV.size()); 109 } 110 111 } // end namespace __orc_rt 112 113 #endif // ORC_RT_ADT_H 114