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 /// A substitue for std::string_view (and llvm::StringRef). 62 /// FIXME: Remove in favor of std::string_view once we have c++17. 63 class string_view { 64 public: 65 typedef char value_type; 66 typedef char *pointer; 67 typedef const char *const_pointer; 68 typedef char &reference; 69 typedef const char &const_reference; 70 typedef std::size_t size_type; 71 typedef std::ptrdiff_t difference_type; 72 73 typedef const_pointer const_iterator; 74 typedef const_iterator iterator; 75 76 constexpr string_view() noexcept = default; 77 constexpr string_view(const char *S, size_type Count) 78 : Data(S), Size(Count) {} 79 string_view(const char *S) : Data(S), Size(strlen(S)) {} 80 string_view(const std::string &S) : Data(S.data()), Size(S.size()) {} 81 82 constexpr const_iterator begin() const noexcept { return Data; } 83 constexpr const_iterator end() const noexcept { return Data + Size; } 84 constexpr const_pointer data() const noexcept { return Data; } 85 constexpr const_reference operator[](size_type idx) { return Data[idx]; } 86 constexpr size_type size() const noexcept { return Size; } 87 constexpr bool empty() const noexcept { return Size == 0; } 88 89 friend bool operator==(const string_view &LHS, const string_view &RHS) { 90 if (LHS.Size != RHS.Size) 91 return false; 92 if (LHS.Data == RHS.Data) 93 return true; 94 for (size_t I = 0; I != LHS.Size; ++I) 95 if (LHS.Data[I] != RHS.Data[I]) 96 return false; 97 return true; 98 } 99 100 friend bool operator!=(const string_view &LHS, const string_view &RHS) { 101 return !(LHS == RHS); 102 } 103 104 private: 105 const char *Data = nullptr; 106 size_type Size = 0; 107 }; 108 109 inline std::ostream &operator<<(std::ostream &OS, string_view S) { 110 return OS.write(S.data(), S.size()); 111 } 112 113 } // end namespace __orc_rt 114 115 namespace std { 116 // Make string_view hashable. 117 // FIXME: This can be removed (along with the string_view class) when we move 118 // to C++17. 119 template <> struct hash<__orc_rt::string_view> { 120 size_t operator()(const __orc_rt::string_view &S) const { 121 std::string Tmp(S.data(), S.size()); 122 return hash<std::string>()(Tmp); 123 } 124 }; 125 126 } // namespace std 127 128 #endif // ORC_RT_ADT_H 129