File: adt.h

package info (click to toggle)
llvm-toolchain-15 1%3A15.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,554,644 kB
  • sloc: cpp: 5,922,452; ansic: 1,012,136; asm: 674,362; python: 191,568; objc: 73,855; f90: 42,327; lisp: 31,913; pascal: 11,973; javascript: 10,144; sh: 9,421; perl: 7,447; ml: 5,527; awk: 3,523; makefile: 2,520; xml: 885; cs: 573; fortran: 567
file content (128 lines) | stat: -rw-r--r-- 4,050 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//===----------------------- adt.h - Handy ADTs -----------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file is a part of the ORC runtime support library.
//
//===----------------------------------------------------------------------===//

#ifndef ORC_RT_ADT_H
#define ORC_RT_ADT_H

#include <cstring>
#include <limits>
#include <ostream>
#include <string>

namespace __orc_rt {

constexpr std::size_t dynamic_extent = std::numeric_limits<std::size_t>::max();

/// A substitute for std::span (and llvm::ArrayRef).
/// FIXME: Remove in favor of std::span once we can use c++20.
template <typename T, std::size_t Extent = dynamic_extent> class span {
public:
  typedef T element_type;
  typedef std::remove_cv<T> value_type;
  typedef std::size_t size_type;
  typedef std::ptrdiff_t difference_type;
  typedef T *pointer;
  typedef const T *const_pointer;
  typedef T &reference;
  typedef const T &const_reference;

  typedef pointer iterator;

  static constexpr std::size_t extent = Extent;

  constexpr span() noexcept = default;
  constexpr span(T *first, size_type count) noexcept
      : Data(first), Size(count) {}

  template <std::size_t N>
  constexpr span(T (&arr)[N]) noexcept : Data(&arr[0]), Size(N) {}

  constexpr iterator begin() const noexcept { return Data; }
  constexpr iterator end() const noexcept { return Data + Size; }
  constexpr pointer data() const noexcept { return Data; }
  constexpr reference operator[](size_type idx) const { return Data[idx]; }
  constexpr size_type size() const noexcept { return Size; }
  constexpr bool empty() const noexcept { return Size == 0; }

private:
  T *Data = nullptr;
  size_type Size = 0;
};

/// A substitue for std::string_view (and llvm::StringRef).
/// FIXME: Remove in favor of std::string_view once we have c++17.
class string_view {
public:
  typedef char value_type;
  typedef char *pointer;
  typedef const char *const_pointer;
  typedef char &reference;
  typedef const char &const_reference;
  typedef std::size_t size_type;
  typedef std::ptrdiff_t difference_type;

  typedef const_pointer const_iterator;
  typedef const_iterator iterator;

  constexpr string_view() noexcept = default;
  constexpr string_view(const char *S, size_type Count)
      : Data(S), Size(Count) {}
  string_view(const char *S) : Data(S), Size(strlen(S)) {}
  string_view(const std::string &S) : Data(S.data()), Size(S.size()) {}

  constexpr const_iterator begin() const noexcept { return Data; }
  constexpr const_iterator end() const noexcept { return Data + Size; }
  constexpr const_pointer data() const noexcept { return Data; }
  constexpr const_reference operator[](size_type idx) { return Data[idx]; }
  constexpr size_type size() const noexcept { return Size; }
  constexpr bool empty() const noexcept { return Size == 0; }

  friend bool operator==(const string_view &LHS, const string_view &RHS) {
    if (LHS.Size != RHS.Size)
      return false;
    if (LHS.Data == RHS.Data)
      return true;
    for (size_t I = 0; I != LHS.Size; ++I)
      if (LHS.Data[I] != RHS.Data[I])
        return false;
    return true;
  }

  friend bool operator!=(const string_view &LHS, const string_view &RHS) {
    return !(LHS == RHS);
  }

private:
  const char *Data = nullptr;
  size_type Size = 0;
};

inline std::ostream &operator<<(std::ostream &OS, string_view S) {
  return OS.write(S.data(), S.size());
}

} // end namespace __orc_rt

namespace std {
// Make string_view hashable.
// FIXME: This can be removed (along with the string_view class) when we move
// to C++17.
template <> struct hash<__orc_rt::string_view> {
  size_t operator()(const __orc_rt::string_view &S) const {
    std::string Tmp(S.data(), S.size());
    return hash<std::string>()(Tmp);
  }
};

} // namespace std

#endif // ORC_RT_ADT_H