Skip to content

Commit 4b29641

Browse files
committed
Improve CPlusPlus code generation
1 parent 77cf110 commit 4b29641

File tree

98 files changed

+567
-819
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+567
-819
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
## Description
88

9-
FastData is a code generator that analyzes your data and creates high-performance, read-only lookup data structures for
10-
static data. It can output the data structures in many different languages (C#, C++, Rust, etc.), ready for inclusion in your project with zero dependencies.
9+
FastData is a code generator that analyzes your data and creates high-performance, read-only data structures with key/value and membership queries on
10+
static data. It supports many different languages (C#, C++, Rust, etc.), ready for inclusion in your project with zero dependencies.
1111

1212
## Download
1313

Src/FastData.Generator.CPlusPlus.Tests/Features/ObjectSupportTest-ArrayStructure_Int32_3.verified.txt

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,38 @@
99
struct Person {
1010
int32_t age;
1111
std::u32string_view name;
12-
const Person* other;
12+
const Person* other;
1313

1414
constexpr Person(const int32_t age, const std::u32string_view name, const Person* other) noexcept : age(age), name(name), other(other) { }
1515
};
16-
class ArrayStructure_Int32_3 final
17-
{
18-
static constexpr std::array<int32_t, 3> keys = {
16+
class ArrayStructure_Int32_3 final {
17+
inline static const std::array<Person*, 3> values = {
18+
new Person(1, U"Bob", new Person(4, U"Anna", nullptr)), new Person(2, U"Billy", nullptr), new Person(3, U"Bibi", nullptr)
19+
};
20+
static constexpr std::array<int32_t, 3> keys = {
1921
1, 2, 3
20-
};
22+
};
2123

2224
public:
2325
[[nodiscard]]
24-
static constexpr bool contains(const int32_t key) noexcept
25-
{
26+
static constexpr bool contains(const int32_t key) noexcept {
2627
if (key < 1 || key > 3)
2728
return false;
2829

2930
for (size_t i = 0; i < 3; i++)
3031
{
3132
if (keys[i] == key)
32-
return true;
33+
return true;
3334
}
3435
return false;
3536
}
36-
inline static const std::array<Person*, 3> values = {
37-
new Person(1, U"Bob", new Person(4, U"Anna", nullptr)), new Person(2, U"Billy", nullptr), new Person(3, U"Bibi", nullptr)
38-
};
39-
4037
[[nodiscard]]
41-
static constexpr bool try_lookup(const int32_t key, const Person*& value) noexcept
42-
{
38+
static constexpr bool try_lookup(const int32_t key, const Person*& value) noexcept {
4339
if (key < 1 || key > 3)
4440
return false;
4541

46-
for (size_t i = 0; i < 3; i++)
47-
{
48-
if (keys[i] == key)
49-
{
42+
for (size_t i = 0; i < 3; i++) {
43+
if (keys[i] == key) {
5044
value = values[i];
5145
return true;
5246
}

Src/FastData.Generator.CPlusPlus.Tests/Features/ObjectSupportTest-BinarySearchStructure_Int32_3.verified.txt

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,63 +9,59 @@
99
struct Person {
1010
int32_t age;
1111
std::u32string_view name;
12-
const Person* other;
12+
const Person* other;
1313

1414
constexpr Person(const int32_t age, const std::u32string_view name, const Person* other) noexcept : age(age), name(name), other(other) { }
1515
};
16-
class BinarySearchStructure_Int32_3 final
17-
{
16+
class BinarySearchStructure_Int32_3 final {
17+
inline static const std::array<Person*, 3> values = {
18+
new Person(1, U"Bob", new Person(4, U"Anna", nullptr)), new Person(2, U"Billy", nullptr), new Person(3, U"Bibi", nullptr)
19+
};
1820
static constexpr std::array<int32_t, 3> keys = {
1921
1, 2, 3
2022
};
2123

2224
public:
2325
[[nodiscard]]
24-
static constexpr bool contains(const int32_t key) noexcept
25-
{
26+
static constexpr bool contains(const int32_t key) noexcept {
2627
if (key < 1 || key > 3)
2728
return false;
2829

29-
size_t lo = 0;
30-
size_t hi = 2;
31-
while (lo <= hi)
32-
{
33-
const size_t mid = lo + ((hi - lo) >> 1);
30+
int32_t lo = 0;
31+
int32_t hi = 2;
32+
while (lo <= hi) {
33+
const int32_t mid = lo + ((hi - lo) >> 1);
34+
const int32_t mid_key = keys[mid];
3435

35-
if (keys[mid] == key)
36+
if (mid_key == key)
3637
return true;
3738

38-
if (keys[mid] < key)
39+
if (mid_key < key)
3940
lo = mid + 1;
4041
else
4142
hi = mid - 1;
4243
}
4344

4445
return false;
4546
}
46-
inline static const std::array<Person*, 3> values = {
47-
new Person(1, U"Bob", new Person(4, U"Anna", nullptr)), new Person(2, U"Billy", nullptr), new Person(3, U"Bibi", nullptr)
48-
};
49-
5047
[[nodiscard]]
51-
static constexpr bool try_lookup(const int32_t key, const Person*& value) noexcept
52-
{
48+
static constexpr bool try_lookup(const int32_t key, const Person*& value) noexcept {
5349
if (key < 1 || key > 3)
5450
return false;
5551

56-
size_t lo = 0;
57-
size_t hi = 2;
58-
while (lo <= hi)
59-
{
60-
const size_t mid = lo + ((hi - lo) >> 1);
52+
int32_t lo = 0;
53+
int32_t hi = 2;
54+
while (lo <= hi) {
55+
const int32_t mid = lo + ((hi - lo) >> 1);
56+
const int32_t mid_key = keys[mid];
6157

62-
if (keys[mid] == key)
58+
if (mid_key == key)
6359
{
6460
value = values[mid];
6561
return true;
6662
}
6763

68-
if (keys[mid] < key)
64+
if (mid_key < key)
6965
lo = mid + 1;
7066
else
7167
hi = mid - 1;

Src/FastData.Generator.CPlusPlus.Tests/Features/ObjectSupportTest-ConditionalStructure_Int32_3.verified.txt

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,35 @@
99
struct Person {
1010
int32_t age;
1111
std::u32string_view name;
12-
const Person* other;
12+
const Person* other;
1313

1414
constexpr Person(const int32_t age, const std::u32string_view name, const Person* other) noexcept : age(age), name(name), other(other) { }
1515
};
16-
class ConditionalStructure_Int32_3 final
17-
{
16+
class ConditionalStructure_Int32_3 final {
17+
inline static const std::array<Person*, 3> values = {
18+
new Person(1, U"Bob", new Person(4, U"Anna", nullptr)), new Person(2, U"Billy", nullptr), new Person(3, U"Bibi", nullptr)
19+
};
1820
public:
1921
[[nodiscard]]
20-
static constexpr bool contains(const int32_t key) noexcept
21-
{
22-
22+
static constexpr bool contains(const int32_t key) noexcept {
2323

2424
if (key == 1 || key == 2 || key == 3)
2525
return true;
2626

2727
return false;
2828
}
29-
inline static const std::array<Person*, 3> values = {
30-
new Person(1, U"Bob", new Person(4, U"Anna", nullptr)), new Person(2, U"Billy", nullptr), new Person(3, U"Bibi", nullptr)
31-
};
32-
3329
[[nodiscard]]
34-
static constexpr bool try_lookup(const int32_t key, const Person*& value) noexcept
35-
{
30+
static constexpr bool try_lookup(const int32_t key, const Person*& value) noexcept {
3631

37-
if (key == 1)
38-
{
32+
if (key == 1) {
3933
value = values[0];
4034
return true;
4135
}
42-
if (key == 2)
43-
{
36+
if (key == 2) {
4437
value = values[1];
4538
return true;
4639
}
47-
if (key == 3)
48-
{
40+
if (key == 3) {
4941
value = values[2];
5042
return true;
5143
}

Src/FastData.Generator.CPlusPlus.Tests/Features/ObjectSupportTest-HashTablePerfectStructure_Int32_3.verified.txt

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,22 @@
99
struct Person {
1010
int32_t age;
1111
std::u32string_view name;
12-
const Person* other;
12+
const Person* other;
1313

1414
constexpr Person(const int32_t age, const std::u32string_view name, const Person* other) noexcept : age(age), name(name), other(other) { }
1515
};
16-
class HashTablePerfectStructure_Int32_3 final
17-
{
18-
struct e
19-
{
20-
int32_t key;
21-
22-
const Person* value;
16+
class HashTablePerfectStructure_Int32_3 final {
17+
struct e {
18+
int32_t key;
19+
20+
const Person* value;
2321

24-
constexpr e(const int32_t key, const Person* value) noexcept
25-
: key(key), value(value) {}
26-
};
27-
inline static const std::array<e, 3> entries = {
28-
e(3, new Person(1, U"Bob", new Person(4, U"Anna", nullptr))), e(1, new Person(2, U"Billy", nullptr)), e(2, new Person(3, U"Bibi", nullptr))
29-
};
22+
constexpr e(const int32_t key, const Person* value) noexcept
23+
: key(key), value(value) {}
24+
};
25+
inline static const std::array<e, 3> entries = {
26+
e(3, new Person(1, U"Bob", new Person(4, U"Anna", nullptr))), e(1, new Person(2, U"Billy", nullptr)), e(2, new Person(3, U"Bibi", nullptr))
27+
};
3028

3129
static constexpr uint64_t get_hash(const int32_t value) noexcept
3230
{
@@ -35,8 +33,7 @@ inline static const std::array<e, 3> entries = {
3533

3634
public:
3735
[[nodiscard]]
38-
static constexpr bool contains(const int32_t key) noexcept
39-
{
36+
static constexpr bool contains(const int32_t key) noexcept {
4037
if (key < 1 || key > 3)
4138
return false;
4239

@@ -47,17 +44,15 @@ public:
4744
return key == entry.key;
4845
}
4946
[[nodiscard]]
50-
static constexpr bool try_lookup(const int32_t key, const Person*& value) noexcept
51-
{
47+
static constexpr bool try_lookup(const int32_t key, const Person*& value) noexcept {
5248
if (key < 1 || key > 3)
5349
return false;
5450

5551
const uint64_t hash = get_hash(key);
5652
const size_t index = hash % 3;
5753
const auto& entry = entries[index];
5854

59-
if (key == entry.key)
60-
{
55+
if (key == entry.key) {
6156
value = entry.value;
6257
return true;
6358
}

Src/FastData.Generator.CPlusPlus.Tests/Features/ObjectSupportTest-HashTableStructure_Int32_3.verified.txt

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@
99
struct Person {
1010
int32_t age;
1111
std::u32string_view name;
12-
const Person* other;
12+
const Person* other;
1313

1414
constexpr Person(const int32_t age, const std::u32string_view name, const Person* other) noexcept : age(age), name(name), other(other) { }
1515
};
16-
class HashTableStructure_Int32_3 final
17-
{
18-
struct e
19-
{
16+
class HashTableStructure_Int32_3 final {
17+
struct e {
2018
int32_t key;
2119
int8_t next;
2220

@@ -40,17 +38,15 @@ class HashTableStructure_Int32_3 final
4038

4139
public:
4240
[[nodiscard]]
43-
static constexpr bool contains(const int32_t key) noexcept
44-
{
41+
static constexpr bool contains(const int32_t key) noexcept {
4542
if (key < 1 || key > 3)
4643
return false;
4744

4845
const uint64_t hash = get_hash(key);
4946
const size_t index = hash % 3;
5047
int8_t i = static_cast<int8_t>(buckets[index] - 1);
5148

52-
while (i >= 0)
53-
{
49+
while (i >= 0) {
5450
const auto& entry = entries[i];
5551

5652
if (entry.key == key)
@@ -62,21 +58,18 @@ public:
6258
return false;
6359
}
6460
[[nodiscard]]
65-
static constexpr bool try_lookup(const int32_t key, const Person*& value) noexcept
66-
{
61+
static constexpr bool try_lookup(const int32_t key, const Person*& value) noexcept {
6762
if (key < 1 || key > 3)
6863
return false;
6964

7065
const uint64_t hash = get_hash(key);
7166
const size_t index = hash % 3;
7267
int8_t i = static_cast<int8_t>(buckets[index] - 1);
7368

74-
while (i >= 0)
75-
{
69+
while (i >= 0) {
7670
const auto& entry = entries[i];
7771

78-
if (entry.key == key)
79-
{
72+
if (entry.key == key) {
8073
value = entry.value;
8174
return true;
8275
}

Src/FastData.Generator.CPlusPlus.Tests/Features/ObjectSupportTest-KeyLengthStructure_String_3.verified.txt

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
struct Person {
1010
int32_t age;
1111
std::string_view name;
12-
const Person* other;
12+
const Person* other;
1313

1414
constexpr Person(const int32_t age, const std::string_view name, const Person* other) noexcept : age(age), name(name), other(other) { }
1515
};
16-
class KeyLengthStructure_String_3 final
17-
{
16+
class KeyLengthStructure_String_3 final {
1817
static constexpr std::array<int32_t, 3> offsets = {
1918
0, 1, 2
2019
};
20+
2121
inline static const std::array<Person*, 3> values = {
2222
new Person(1, "Bob", new Person(4, "Anna", nullptr)), new Person(2, "Billy", nullptr), new Person(3, "Bibi", nullptr)
2323
};
@@ -27,29 +27,26 @@ class KeyLengthStructure_String_3 final
2727

2828
public:
2929
[[nodiscard]]
30-
static constexpr bool contains(const std::string_view key) noexcept
31-
{
30+
static constexpr bool contains(const std::string_view key) noexcept {
3231
if (const size_t len = key.length(); len < 1u || len > 3u)
3332
return false;
3433

3534
return key == keys[key.length() - 1];
3635
}
37-
[[nodiscard]]
38-
static constexpr bool try_lookup(const std::string_view key, const Person*& value) noexcept
39-
{
36+
[[nodiscard]]
37+
static constexpr bool try_lookup(const std::string_view key, const Person*& value) noexcept {
4038
if (const size_t len = key.length(); len < 1u || len > 3u)
4139
return false;
4240

43-
size_t idx = key.length() - 1;
44-
if (key == keys[idx])
45-
{
46-
value = values[offsets[idx]];
47-
return true;
48-
}
41+
size_t idx = key.length() - 1;
42+
if (key == keys[idx]) {
43+
value = values[offsets[idx]];
44+
return true;
45+
}
4946

50-
value = nullptr;
51-
return false;
52-
}
47+
value = nullptr;
48+
return false;
49+
}
5350

5451
static constexpr size_t item_count = 3;
5552
static constexpr size_t min_key_length = 1;

0 commit comments

Comments
 (0)