#ifndef LLVM_ADT_ARRAYREF_H
#define LLVM_ADT_ARRAYREF_H
#include "llvm/ADT/None.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include <vector>
namespace llvm {
template<typename T>
class ArrayRef {
public:
typedef const T *iterator;
typedef const T *const_iterator;
typedef size_t size_type;
typedef std::reverse_iterator<iterator> reverse_iterator;
private:
const T *Data;
size_type Length;
struct TRefOrNothing {
const T *TPtr;
TRefOrNothing() : TPtr(nullptr) {}
TRefOrNothing(const T &TRef) : TPtr(&TRef) {}
};
public:
ArrayRef() : Data(nullptr), Length(0) {}
ArrayRef(NoneType) : Data(nullptr), Length(0) {}
ArrayRef(const T &OneElt)
: Data(&OneElt), Length(1) {}
ArrayRef(const T *data, size_t length)
: Data(data), Length(length) {}
ArrayRef(const T *begin, const T *end)
: Data(begin), Length(end - begin) {}
template<typename U>
ArrayRef(const SmallVectorTemplateCommon<T, U> &Vec)
: Data(Vec.data()), Length(Vec.size()) {
}
template<typename A>
ArrayRef(const std::vector<T, A> &Vec)
: Data(Vec.data()), Length(Vec.size()) {}
template <size_t N>
LLVM_CONSTEXPR ArrayRef(const T (&Arr)[N])
: Data(Arr), Length(N) {}
#if LLVM_HAS_INITIALIZER_LISTS
ArrayRef(const std::initializer_list<T> &Vec)
: Data(Vec.begin() == Vec.end() ? (T*)0 : Vec.begin()),
Length(Vec.size()) {}
#endif
iterator begin() const { return Data; }
iterator end() const { return Data + Length; }
reverse_iterator rbegin() const { return reverse_iterator(end()); }
reverse_iterator rend() const { return reverse_iterator(begin()); }
bool empty() const { return Length == 0; }
const T *data() const { return Data; }
size_t size() const { return Length; }
const T &front() const {
assert(!empty());
return Data[0];
}
const T &back() const {
assert(!empty());
return Data[Length-1];
}
template <typename Allocator> ArrayRef<T> copy(Allocator &A) {
T *Buff = A.template Allocate<T>(Length);
std::copy(begin(), end(), Buff);
return ArrayRef<T>(Buff, Length);
}
bool equals(ArrayRef RHS) const {
if (Length != RHS.Length)
return false;
for (auto L = begin(), LE = end(), R = RHS.begin(); L != LE; ++L, ++R)
if (!(*L == *R))
return false;
return true;
}
ArrayRef<T> slice(unsigned N) const {
assert(N <= size() && "Invalid specifier");
return ArrayRef<T>(data()+N, size()-N);
}
ArrayRef<T> slice(unsigned N, unsigned M) const {
assert(N+M <= size() && "Invalid specifier");
return ArrayRef<T>(data()+N, M);
}
ArrayRef<T> drop_back(unsigned N = 1) const {
assert(size() >= N && "Dropping more elements than exist");
return slice(0, size() - N);
}
const T &operator[](size_t Index) const {
assert(Index < Length && "Invalid index!");
return Data[Index];
}
std::vector<T> vec() const {
return std::vector<T>(Data, Data+Length);
}
operator std::vector<T>() const {
return std::vector<T>(Data, Data+Length);
}
bool equals(TRefOrNothing Arg0 = TRefOrNothing(),
TRefOrNothing Arg1 = TRefOrNothing(),
TRefOrNothing Arg2 = TRefOrNothing(),
TRefOrNothing Arg3 = TRefOrNothing(),
TRefOrNothing Arg4 = TRefOrNothing(),
TRefOrNothing Arg5 = TRefOrNothing(),
TRefOrNothing Arg6 = TRefOrNothing(),
TRefOrNothing Arg7 = TRefOrNothing(),
TRefOrNothing Arg8 = TRefOrNothing(),
TRefOrNothing Arg9 = TRefOrNothing(),
TRefOrNothing Arg10 = TRefOrNothing(),
TRefOrNothing Arg11 = TRefOrNothing(),
TRefOrNothing Arg12 = TRefOrNothing(),
TRefOrNothing Arg13 = TRefOrNothing(),
TRefOrNothing Arg14 = TRefOrNothing(),
TRefOrNothing Arg15 = TRefOrNothing()) {
TRefOrNothing Args[] = {Arg0, Arg1, Arg2, Arg3, Arg4, Arg5,
Arg6, Arg7, Arg8, Arg9, Arg10, Arg11,
Arg12, Arg13, Arg14, Arg15};
if (size() > array_lengthof(Args))
return false;
for (unsigned i = 0, e = size(); i != e; ++i)
if (Args[i].TPtr == nullptr || (*this)[i] != *Args[i].TPtr)
return false;
return size() == array_lengthof(Args) || Args[size()].TPtr == nullptr;
}
};
template<typename T>
class MutableArrayRef : public ArrayRef<T> {
public:
typedef T *iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
MutableArrayRef() : ArrayRef<T>() {}
MutableArrayRef(NoneType) : ArrayRef<T>() {}
MutableArrayRef(T &OneElt) : ArrayRef<T>(OneElt) {}
MutableArrayRef(T *data, size_t length)
: ArrayRef<T>(data, length) {}
MutableArrayRef(T *begin, T *end) : ArrayRef<T>(begin, end) {}
MutableArrayRef(SmallVectorImpl<T> &Vec)
: ArrayRef<T>(Vec) {}
MutableArrayRef(std::vector<T> &Vec)
: ArrayRef<T>(Vec) {}
template <size_t N>
LLVM_CONSTEXPR MutableArrayRef(T (&Arr)[N])
: ArrayRef<T>(Arr) {}
T *data() const { return const_cast<T*>(ArrayRef<T>::data()); }
iterator begin() const { return data(); }
iterator end() const { return data() + this->size(); }
reverse_iterator rbegin() const { return reverse_iterator(end()); }
reverse_iterator rend() const { return reverse_iterator(begin()); }
T &front() const {
assert(!this->empty());
return data()[0];
}
T &back() const {
assert(!this->empty());
return data()[this->size()-1];
}
MutableArrayRef<T> slice(unsigned N) const {
assert(N <= this->size() && "Invalid specifier");
return MutableArrayRef<T>(data()+N, this->size()-N);
}
MutableArrayRef<T> slice(unsigned N, unsigned M) const {
assert(N+M <= this->size() && "Invalid specifier");
return MutableArrayRef<T>(data()+N, M);
}
T &operator[](size_t Index) const {
assert(Index < this->size() && "Invalid index!");
return data()[Index];
}
};
template<typename T>
ArrayRef<T> makeArrayRef(const T &OneElt) {
return OneElt;
}
template<typename T>
ArrayRef<T> makeArrayRef(const T *data, size_t length) {
return ArrayRef<T>(data, length);
}
template<typename T>
ArrayRef<T> makeArrayRef(const T *begin, const T *end) {
return ArrayRef<T>(begin, end);
}
template <typename T>
ArrayRef<T> makeArrayRef(const SmallVectorImpl<T> &Vec) {
return Vec;
}
template <typename T, unsigned N>
ArrayRef<T> makeArrayRef(const SmallVector<T, N> &Vec) {
return Vec;
}
template<typename T>
ArrayRef<T> makeArrayRef(const std::vector<T> &Vec) {
return Vec;
}
template<typename T, size_t N>
ArrayRef<T> makeArrayRef(const T (&Arr)[N]) {
return ArrayRef<T>(Arr);
}
template<typename T>
inline bool operator==(ArrayRef<T> LHS, ArrayRef<T> RHS) {
return LHS.equals(RHS);
}
template<typename T>
inline bool operator!=(ArrayRef<T> LHS, ArrayRef<T> RHS) {
return !(LHS == RHS);
}
template <typename T> struct isPodLike;
template <typename T> struct isPodLike<ArrayRef<T> > {
static const bool value = true;
};
}
#endif