[NFC][libc] Remove ArrayRef which is replaced with span

This commit is contained in:
Guillaume Chatelet 2022-08-22 10:52:14 +00:00
parent 541e24ef76
commit c860fd3f25
7 changed files with 13 additions and 421 deletions

View File

@ -1,153 +0,0 @@
//===-- Self contained ArrayRef type ----------------------------*- 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
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC_SUPPORT_CPP_ARRAYREF_H
#define LLVM_LIBC_SRC_SUPPORT_CPP_ARRAYREF_H
#include "array.h"
#include "type_traits.h" // RemoveCVType
#include <stddef.h> // For size_t.
namespace __llvm_libc {
namespace cpp {
// The implementations of ArrayRef and MutableArrayRef in this file are based
// on the implementations of the types with the same names in
// llvm/ADT/ArrayRef.h. The implementations in this file are of a limited
// functionality, but can be extended in an as needed basis.
namespace internal {
template <typename QualifiedT> class ArrayRefBase {
public:
using value_type = remove_cv_t<QualifiedT>;
using pointer = value_type *;
using const_pointer = const value_type *;
using reference = value_type &;
using const_reference = const value_type &;
using iterator = const_pointer;
using const_iterator = const_pointer;
using size_type = size_t;
using difference_type = ptrdiff_t;
ArrayRefBase() = default;
// Construct an ArrayRefBase from a single element.
explicit ArrayRefBase(QualifiedT &OneElt) : Data(&OneElt), Length(1) {}
// Construct an ArrayRefBase from a pointer and length.
ArrayRefBase(QualifiedT *Data, size_t Length) : Data(Data), Length(Length) {}
// Construct an ArrayRefBase from a range.
ArrayRefBase(QualifiedT *Begin, QualifiedT *End)
: Data(Begin), Length(End - Begin) {}
// Construct an ArrayRefBase from a C array.
template <size_t N>
constexpr ArrayRefBase(QualifiedT (&Arr)[N]) : Data(Arr), Length(N) {}
QualifiedT *data() const { return Data; }
size_t size() const { return Length; }
auto begin() const { return data(); }
auto end() const { return data() + size(); }
bool empty() const { return size() == 0; }
auto &operator[](size_t Index) const { return data()[Index]; }
// slice(n, m) - Chop off the first N elements of the array, and keep M
// elements in the array.
auto slice(size_t N, size_t M) const { return ArrayRefBase(data() + N, M); }
// slice(n) - Chop off the first N elements of the array.
auto slice(size_t N) const { return slice(N, size() - N); }
// Drop the first \p N elements of the array.
auto drop_front(size_t N = 1) const { return slice(N, size() - N); }
// Drop the last \p N elements of the array.
auto drop_back(size_t N = 1) const { return slice(0, size() - N); }
// Return a copy of *this with only the first \p N elements.
auto take_front(size_t N = 1) const {
if (N >= size())
return *this;
return drop_back(size() - N);
}
// Return a copy of *this with only the last \p N elements.
auto take_back(size_t N = 1) const {
if (N >= size())
return *this;
return drop_front(size() - N);
}
// equals - Check for element-wise equality.
bool equals(ArrayRefBase<QualifiedT> RHS) const {
if (Length != RHS.Length)
return false;
auto First1 = begin();
auto Last1 = end();
auto First2 = RHS.begin();
for (; First1 != Last1; ++First1, ++First2) {
if (!(*First1 == *First2)) {
return false;
}
}
return true;
}
private:
QualifiedT *Data = nullptr;
size_t Length = 0;
};
} // namespace internal
template <typename T> struct ArrayRef : public internal::ArrayRefBase<const T> {
private:
static_assert(is_same_v<T, remove_cv_t<T>>,
"ArrayRef must have a non-const, non-volatile value_type");
using Impl = internal::ArrayRefBase<const T>;
using Impl::Impl;
public:
// Construct an ArrayRef from void * pointer.
// |Length| is the byte length of the array pointed to by |Data|.
ArrayRef(const void *Data, size_t Length)
: Impl(reinterpret_cast<const T *>(Data), Length / sizeof(T)) {}
// From array.
template <size_t N> ArrayRef(const array<T, N> &Arr) : Impl(Arr.Data, N) {}
};
template <typename T>
struct MutableArrayRef : public internal::ArrayRefBase<T> {
private:
static_assert(
is_same_v<T, remove_cv_t<T>>,
"MutableArrayRef must have a non-const, non-volatile value_type");
using Impl = internal::ArrayRefBase<T>;
using Impl::Impl;
public:
// Construct an ArrayRef from void * pointer.
// |Length| is the byte length of the array pointed to by |Data|.
MutableArrayRef(void *Data, size_t Length)
: Impl(reinterpret_cast<T *>(Data), Length / sizeof(T)) {}
// From array.
template <size_t N> MutableArrayRef(array<T, N> &Arr) : Impl(Arr.Data, N) {}
operator ArrayRef<T>() const {
return ArrayRef<T>(this->data(), this->size());
}
};
} // namespace cpp
} // namespace __llvm_libc
#endif // LLVM_LIBC_SRC_SUPPORT_CPP_ARRAYREF_H

View File

@ -20,12 +20,6 @@ add_header_library(
.uint
)
add_header_library(
array_ref
HDRS
ArrayRef.h
)
add_header_library(
bit
HDRS

View File

@ -334,7 +334,7 @@ add_entrypoint_object(
pthread_getname_np.h
DEPENDS
libc.include.pthread
libc.src.__support.CPP.array_ref
libc.src.__support.CPP.span
libc.src.__support.CPP.stringstream
libc.src.__support.threads.thread
)

View File

@ -31,16 +31,6 @@ add_libc_unittest(
libc.src.__support.CPP.uint
)
add_libc_unittest(
arrayref_test
SUITE
libc_cpp_utils_unittests
SRCS
arrayref_test.cpp
DEPENDS
libc.src.__support.CPP.array_ref
)
add_libc_unittest(
vector_test
SUITE

View File

@ -1,239 +0,0 @@
//===-- Unittests for ArrayRef --------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "src/__support/CPP/ArrayRef.h"
#include "utils/UnitTest/Test.h"
namespace __llvm_libc {
namespace cpp {
// The following tests run on both 'ArrayRef' and 'MutableArrayRef'.
using Types = testing::TypeList<ArrayRef<int>, MutableArrayRef<int>>;
TYPED_TEST(LlvmLibcArrayRefTest, ConstructFromElement, Types) {
using value_type = typename ParamType::value_type;
using const_pointer = typename ParamType::const_pointer;
value_type element = 5;
ParamType arrayref(element);
EXPECT_FALSE(arrayref.empty());
EXPECT_EQ(arrayref.size(), size_t(1));
EXPECT_EQ(arrayref[0], 5);
EXPECT_EQ((const_pointer)arrayref.data(), (const_pointer)&element);
}
TYPED_TEST(LlvmLibcArrayRefTest, ConstructFromPointerAndSize, Types) {
using value_type = typename ParamType::value_type;
using const_pointer = typename ParamType::const_pointer;
value_type values[] = {1, 2};
ParamType arrayref(values, 2);
EXPECT_FALSE(arrayref.empty());
EXPECT_EQ(arrayref.size(), size_t(2));
EXPECT_EQ(arrayref[0], 1);
EXPECT_EQ(arrayref[1], 2);
EXPECT_EQ((const_pointer)arrayref.data(), (const_pointer)values);
}
TYPED_TEST(LlvmLibcArrayRefTest, ConstructFromIterator, Types) {
using value_type = typename ParamType::value_type;
using const_pointer = typename ParamType::const_pointer;
value_type values[] = {1, 2};
ParamType arrayref(&values[0], &values[2]);
EXPECT_FALSE(arrayref.empty());
EXPECT_EQ(arrayref.size(), size_t(2));
EXPECT_EQ(arrayref[0], 1);
EXPECT_EQ(arrayref[1], 2);
EXPECT_EQ((const_pointer)arrayref.data(), (const_pointer)&values[0]);
}
TYPED_TEST(LlvmLibcArrayRefTest, ConstructFromCArray, Types) {
using value_type = typename ParamType::value_type;
using const_pointer = typename ParamType::const_pointer;
value_type values[] = {1, 2};
ParamType arrayref(values);
EXPECT_FALSE(arrayref.empty());
EXPECT_EQ(arrayref.size(), size_t(2));
EXPECT_EQ(arrayref[0], 1);
EXPECT_EQ(arrayref[1], 2);
EXPECT_EQ((const_pointer)arrayref.data(), (const_pointer)values);
}
TYPED_TEST(LlvmLibcArrayRefTest, ConstructFromLibcArray, Types) {
using value_type = typename ParamType::value_type;
using const_pointer = typename ParamType::const_pointer;
array<value_type, 2> values = {1, 2};
ParamType arrayref(values);
EXPECT_FALSE(arrayref.empty());
EXPECT_EQ(arrayref.size(), size_t(2));
EXPECT_EQ(arrayref[0], 1);
EXPECT_EQ(arrayref[1], 2);
EXPECT_EQ((const_pointer)arrayref.data(), (const_pointer)values.data());
}
TYPED_TEST(LlvmLibcArrayRefTest, Equals, Types) {
using value_type = typename ParamType::value_type;
value_type values[] = {1, 2, 3};
ParamType initial(values);
EXPECT_TRUE(initial.equals(initial));
ParamType shallow_copy(values);
EXPECT_TRUE(initial.equals(shallow_copy));
value_type same_values[] = {1, 2, 3};
EXPECT_TRUE(initial.equals(same_values));
value_type different_values[] = {1, 2, 4};
EXPECT_FALSE(initial.equals(different_values));
}
TYPED_TEST(LlvmLibcArrayRefTest, SliceUnary, Types) {
using value_type = typename ParamType::value_type;
value_type values[] = {1, 2, 3};
ParamType arrayref(values);
{
value_type values[] = {1, 2, 3};
EXPECT_TRUE(arrayref.slice(0).equals(values));
}
{
value_type values[] = {2, 3};
EXPECT_TRUE(arrayref.slice(1).equals(values));
}
{
value_type values[] = {3};
EXPECT_TRUE(arrayref.slice(2).equals(values));
}
{ EXPECT_TRUE(arrayref.slice(3).empty()); }
}
TYPED_TEST(LlvmLibcArrayRefTest, SliceBinary, Types) {
using value_type = typename ParamType::value_type;
value_type values[] = {1, 2, 3};
ParamType arrayref(values);
{
EXPECT_TRUE(arrayref.slice(0, 0).empty());
EXPECT_TRUE(arrayref.slice(1, 0).empty());
EXPECT_TRUE(arrayref.slice(2, 0).empty());
EXPECT_TRUE(arrayref.slice(3, 0).empty());
}
{
value_type values[] = {1};
EXPECT_TRUE(arrayref.slice(0, 1).equals(values));
}
{
value_type values[] = {2};
EXPECT_TRUE(arrayref.slice(1, 1).equals(values));
}
{
value_type values[] = {3};
EXPECT_TRUE(arrayref.slice(2, 1).equals(values));
}
{
value_type values[] = {1, 2};
EXPECT_TRUE(arrayref.slice(0, 2).equals(values));
}
{
value_type values[] = {2, 3};
EXPECT_TRUE(arrayref.slice(1, 2).equals(values));
}
{
value_type values[] = {1, 2, 3};
EXPECT_TRUE(arrayref.slice(0, 3).equals(values));
}
}
TYPED_TEST(LlvmLibcArrayRefTest, DropFront, Types) {
using value_type = typename ParamType::value_type;
value_type values[] = {1, 2, 3};
ParamType arrayref(values);
{
value_type values[] = {1, 2, 3};
EXPECT_TRUE(arrayref.drop_front(0).equals(values));
}
{
value_type values[] = {2, 3};
EXPECT_TRUE(arrayref.drop_front(1).equals(values));
}
{
value_type values[] = {3};
EXPECT_TRUE(arrayref.drop_front(2).equals(values));
}
{ EXPECT_TRUE(arrayref.drop_front(3).empty()); }
}
TYPED_TEST(LlvmLibcArrayRefTest, DropBack, Types) {
using value_type = typename ParamType::value_type;
value_type values[] = {1, 2, 3};
ParamType arrayref(values);
{
value_type values[] = {1, 2, 3};
EXPECT_TRUE(arrayref.drop_back(0).equals(values));
}
{
value_type values[] = {1, 2};
EXPECT_TRUE(arrayref.drop_back(1).equals(values));
}
{
value_type values[] = {1};
EXPECT_TRUE(arrayref.drop_back(2).equals(values));
}
{ EXPECT_TRUE(arrayref.drop_back(3).empty()); }
}
TYPED_TEST(LlvmLibcArrayRefTest, TakeFront, Types) {
using value_type = typename ParamType::value_type;
value_type values[] = {1, 2, 3};
ParamType arrayref(values);
{ EXPECT_TRUE(arrayref.take_front(0).empty()); }
{
value_type values[] = {1};
EXPECT_TRUE(arrayref.take_front(1).equals(values));
}
{
value_type values[] = {1, 2};
EXPECT_TRUE(arrayref.take_front(2).equals(values));
}
{
value_type values[] = {1, 2, 3};
EXPECT_TRUE(arrayref.take_front(3).equals(values));
}
}
TYPED_TEST(LlvmLibcArrayRefTest, TakeBack, Types) {
using value_type = typename ParamType::value_type;
value_type values[] = {1, 2, 3};
ParamType arrayref(values);
{ EXPECT_TRUE(arrayref.take_back(0).empty()); }
{
value_type values[] = {3};
EXPECT_TRUE(arrayref.take_back(1).equals(values));
}
{
value_type values[] = {2, 3};
EXPECT_TRUE(arrayref.take_back(2).equals(values));
}
{
value_type values[] = {1, 2, 3};
EXPECT_TRUE(arrayref.take_back(3).equals(values));
}
}
TEST(LlvmLibcArrayRefTest, ConstructFromVoidPtr) {
unsigned data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
void *ptr = data;
const void *const_ptr = data;
ArrayRef<unsigned> ref(const_ptr, sizeof(data));
MutableArrayRef<unsigned> mutable_ref(ptr, sizeof(data));
ASSERT_EQ(ref.size(), sizeof(data) / sizeof(unsigned));
ASSERT_EQ(mutable_ref.size(), sizeof(data) / sizeof(unsigned));
unsigned val = 123;
for (size_t i = 0; i < sizeof(data) / sizeof(unsigned); ++i)
mutable_ref[i] = val;
for (size_t i = 0; i < sizeof(data) / sizeof(unsigned); ++i)
ASSERT_EQ(ref[i], val);
}
} // namespace cpp
} // namespace __llvm_libc

View File

@ -15,7 +15,7 @@ add_libc_unittest(
DEPENDS
libc.src.string.memory_utils.memory_utils
libc.src.__support.CPP.array
libc.src.__support.CPP.array_ref
libc.src.__support.CPP.span
)
if(NOT LLVM_LIBC_FULL_BUILD)
@ -30,6 +30,6 @@ add_libc_unittest(
DEPENDS
libc.src.string.memory_utils.memory_utils
libc.src.__support.CPP.array
libc.src.__support.CPP.array_ref
libc.src.__support.CPP.span
)
endif()

View File

@ -41,16 +41,6 @@ cc_library(
deps = [":libc_root"],
)
cc_library(
name = "__support_cpp_array_ref",
hdrs = ["src/__support/CPP/ArrayRef.h"],
deps = [
":__support_cpp_array",
":__support_cpp_type_traits",
":libc_root",
],
)
cc_library(
name = "__support_cpp_bit",
hdrs = ["src/__support/CPP/bit.h"],
@ -81,6 +71,16 @@ cc_library(
deps = [":libc_root"],
)
cc_library(
name = "__support_cpp_span",
hdrs = ["src/__support/CPP/span.h"],
deps = [
"__support_cpp_array",
"__support_cpp_type_traits",
":libc_root",
],
)
cc_library(
name = "__support_cpp_string_view",
hdrs = ["src/__support/CPP/StringView.h"],