Skip to content

Commit 6291454

Browse files
committed
[libc] Create cpp::IntegerSequence analogous to std::integer_sequence
Reviewed By: sivachandra, lntue Differential Revision: https://reviews.llvm.org/D119511
1 parent f2a7f83 commit 6291454

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

libc/src/__support/CPP/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_header_library(
1010
Limits.h
1111
StringView.h
1212
TypeTraits.h
13+
Utility.h
1314
)
1415

1516
add_header_library(

libc/src/__support/CPP/Utility.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===-- Analogous to <utility> ----------------------------------*- 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+
#ifndef LLVM_LIBC_SRC_SUPPORT_CPP_UTILITY_H
10+
#define LLVM_LIBC_SRC_SUPPORT_CPP_UTILITY_H
11+
12+
#include "src/__support/CPP/TypeTraits.h"
13+
14+
namespace __llvm_libc::cpp {
15+
16+
template <typename T, T... Seq> struct IntegerSequence {
17+
static_assert(IsIntegral<T>::Value);
18+
template <T Next> using append = IntegerSequence<T, Seq..., Next>;
19+
};
20+
21+
namespace internal {
22+
23+
template <typename T, int N> struct MakeIntegerSequence {
24+
using type = typename MakeIntegerSequence<T, N - 1>::type::template append<N>;
25+
};
26+
27+
template <typename T> struct MakeIntegerSequence<T, -1> {
28+
using type = IntegerSequence<T>;
29+
};
30+
31+
} // namespace internal
32+
33+
template <typename T, int N>
34+
using MakeIntegerSequence =
35+
typename internal::MakeIntegerSequence<T, N - 1>::type;
36+
37+
} // namespace __llvm_libc::cpp
38+
39+
#endif // LLVM_LIBC_SRC_SUPPORT_CPP_UTILITY_H

libc/test/src/__support/CPP/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,13 @@ add_libc_unittest(
4949
DEPENDS
5050
libc.src.__support.CPP.vector
5151
)
52+
53+
add_libc_unittest(
54+
int_seq_test
55+
SUITE
56+
libc_cpp_utils_unittests
57+
SRCS
58+
integer_sequence_test.cpp
59+
DEPENDS
60+
libc.src.__support.CPP.standalone_cpp
61+
)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===-- Unittests for IntegerSequence -------------------------------------===//
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+
#include "src/__support/CPP/Utility.h"
10+
#include "utils/UnitTest/Test.h"
11+
12+
using namespace __llvm_libc::cpp;
13+
14+
TEST(LlvmLibcIntegerSequencetTest, Basic) {
15+
EXPECT_TRUE((IsSameV<IntegerSequence<int>, MakeIntegerSequence<int, 0>>));
16+
using ISeq = IntegerSequence<int, 0, 1, 2, 3>;
17+
EXPECT_TRUE((IsSameV<ISeq, MakeIntegerSequence<int, 4>>));
18+
using LSeq = IntegerSequence<long, 0, 1, 2, 3>;
19+
EXPECT_TRUE((IsSameV<LSeq, MakeIntegerSequence<long, 4>>));
20+
using ULLSeq = IntegerSequence<unsigned long long, 0ull, 1ull, 2ull, 3ull>;
21+
EXPECT_TRUE((IsSameV<ULLSeq, MakeIntegerSequence<unsigned long long, 4>>));
22+
}
23+
24+
template <typename T, T... Ts>
25+
bool checkArray(IntegerSequence<T, Ts...> seq) {
26+
T arr[sizeof...(Ts)]{Ts...};
27+
28+
for (T i = 0; i < static_cast<T>(sizeof...(Ts)); i++)
29+
if (arr[i] != i)
30+
return false;
31+
32+
return true;
33+
}
34+
35+
TEST(LlvmLibcIntegerSequencetTest, Many) {
36+
EXPECT_TRUE(checkArray(MakeIntegerSequence<int, 100>{}));
37+
}

0 commit comments

Comments
 (0)