Skip to content

Commit 26e8a19

Browse files
committed
Fix bug where hash table perfect wouldn't reorder values
1 parent 0a3b4d3 commit 26e8a19

7 files changed

+15
-8
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class HashTablePerfectStructure_Int32_3_complex final {
2323
: key(key), value(value) {}
2424
};
2525
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))
26+
e(3, new Person(3, U"Bibi", nullptr)), e(1, new Person(1, U"Bob", new Person(4, U"Anna", nullptr))), e(2, new Person(2, U"Billy", nullptr))
2727
};
2828

2929
static constexpr uint64_t get_hash(const int32_t value) noexcept

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class HashTablePerfectStructure_Int32_3_simple final {
1616
: key(key), value(value) {}
1717
};
1818
inline static const std::array<e, 3> entries = {
19-
e(3, 1), e(1, 2), e(2, 3)
19+
e(3, 3), e(1, 1), e(2, 2)
2020
};
2121

2222
static constexpr uint64_t get_hash(const int32_t value) noexcept

Src/FastData.Generator.CSharp.Tests/Features/ObjectSupportTest-HashTablePerfectStructure_Int32_3_complex.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ private struct E
3636
}
3737
}
3838
private static readonly E[] _entries = {
39-
new E(3, new Person(1, "Bob", new Person(4, "Anna", null))), new E(1, new Person(2, "Billy", null)), new E(2, new Person(3, "Bibi", null))
39+
new E(3, new Person(3, "Bibi", null)), new E(1, new Person(1, "Bob", new Person(4, "Anna", null))), new E(2, new Person(2, "Billy", null))
4040
};
4141

4242

Src/FastData.Generator.CSharp.Tests/Features/ObjectSupportTest-HashTablePerfectStructure_Int32_3_simple.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ private struct E
2323
}
2424
}
2525
private static readonly E[] _entries = {
26-
new E(3, 1), new E(1, 2), new E(2, 3)
26+
new E(3, 3), new E(1, 1), new E(2, 2)
2727
};
2828

2929

Src/FastData.Generator.Rust.Tests/Features/ObjectSupportTest-HashTablePerfectStructure_Int32_3_complex.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl Person {
2929
impl HashTablePerfectStructure_Int32_3_complex {
3030

3131
const ENTRIES: [e; 3] = [
32-
e::new(3, &Person::new(1, "Bob", Some(&Person::new(4, "Anna", None)))), e::new(1, &Person::new(2, "Billy", None)), e::new(2, &Person::new(3, "Bibi", None))
32+
e::new(3, &Person::new(3, "Bibi", None)), e::new(1, &Person::new(1, "Bob", Some(&Person::new(4, "Anna", None)))), e::new(2, &Person::new(2, "Billy", None))
3333
];
3434

3535
#[inline(always)]

Src/FastData.Generator.Rust.Tests/Features/ObjectSupportTest-HashTablePerfectStructure_Int32_3_simple.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl e {
1919
impl HashTablePerfectStructure_Int32_3_simple {
2020

2121
const ENTRIES: [e; 3] = [
22-
e::new(3, 1), e::new(1, 2), e::new(2, 3)
22+
e::new(3, 3), e::new(1, 1), e::new(2, 2)
2323
];
2424

2525
#[inline(always)]

Src/FastData/Internal/Structures/HashTablePerfectStructure.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,18 @@ public HashTablePerfectContext<TKey, TValue> Create(TKey[] keys, TValue[]? value
1717

1818
ulong[] hashCodes = hashData.HashCodes;
1919
KeyValuePair<TKey, ulong>[] pairs = new KeyValuePair<TKey, ulong>[size];
20+
TValue[]? denseValues = values == null ? null : new TValue[size];
2021

2122
//We need to reorder the data to match hashes
2223
for (int i = 0; i < keys.Length; i++)
23-
pairs[hashCodes[i] % size] = new KeyValuePair<TKey, ulong>(keys[i], hashCodes[i]);
24+
{
25+
ulong index = hashCodes[i] % size;
26+
pairs[index] = new KeyValuePair<TKey, ulong>(keys[i], hashCodes[i]);
2427

25-
return new HashTablePerfectContext<TKey, TValue>(pairs, !keyType.IsIdentityHash(), values);
28+
if (denseValues != null)
29+
denseValues[index] = values![i];
30+
}
31+
32+
return new HashTablePerfectContext<TKey, TValue>(pairs, !keyType.IsIdentityHash(), denseValues);
2633
}
2734
}

0 commit comments

Comments
 (0)