mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Added .hash() functions for vectors (#2043)
* Added .hash() functions for vectors * Update test to a non-zero sized vector * Changed vector hash functions to hash the underlying bytes in a char slice, the same approch is used for arrays * Added test for hashed * Updated formatting to be consistant with C3 code style * Formatting, use "self" --------- Co-authored-by: Christoffer Lerno <christoffer@aegik.com>
This commit is contained in:
committed by
GitHub
parent
63e5aa58c5
commit
561a683230
@@ -431,22 +431,63 @@ macro @generic_hash(value)
|
|||||||
return h;
|
return h;
|
||||||
}
|
}
|
||||||
|
|
||||||
macro uint int.hash(int i) => @generic_hash(i);
|
|
||||||
macro uint uint.hash(uint i) => @generic_hash(i);
|
macro uint int128.hash(self) => @generic_hash(self);
|
||||||
macro uint short.hash(short s) => @generic_hash(s);
|
macro uint uint128.hash(self) => @generic_hash(self);
|
||||||
macro uint ushort.hash(ushort s) => @generic_hash(s);
|
macro uint long.hash(self) => @generic_hash(self);
|
||||||
macro uint char.hash(char c) => @generic_hash(c);
|
macro uint ulong.hash(self) => @generic_hash(self);
|
||||||
macro uint ichar.hash(ichar c) => @generic_hash(c);
|
macro uint int.hash(self) => @generic_hash(self);
|
||||||
macro uint long.hash(long i) => @generic_hash(i);
|
macro uint uint.hash(self) => @generic_hash(self);
|
||||||
macro uint ulong.hash(ulong i) => @generic_hash(i);
|
macro uint short.hash(self) => @generic_hash(self);
|
||||||
macro uint int128.hash(int128 i) => @generic_hash(i);
|
macro uint ushort.hash(self) => @generic_hash(self);
|
||||||
macro uint uint128.hash(uint128 i) => @generic_hash(i);
|
macro uint ichar.hash(self) => @generic_hash(self);
|
||||||
macro uint bool.hash(bool b) => @generic_hash(b);
|
macro uint char.hash(self) => @generic_hash(self);
|
||||||
|
macro uint bool.hash(self) => @generic_hash(self);
|
||||||
|
|
||||||
|
macro uint int128[*].hash(&self) => hash_array(self);
|
||||||
|
macro uint uint128[*].hash(&self) => hash_array(self);
|
||||||
|
macro uint long[*].hash(&self) => hash_array(self);
|
||||||
|
macro uint ulong[*].hash(&self) => hash_array(self);
|
||||||
|
macro uint int[*].hash(&self) => hash_array(self);
|
||||||
|
macro uint uint[*].hash(&self) => hash_array(self);
|
||||||
|
macro uint short[*].hash(&self) => hash_array(self);
|
||||||
|
macro uint ushort[*].hash(&self) => hash_array(self);
|
||||||
|
macro uint char[*].hash(&self) => hash_array(self);
|
||||||
|
macro uint ichar[*].hash(&self) => hash_array(self);
|
||||||
|
macro uint bool[*].hash(&self) => hash_array(self);
|
||||||
|
|
||||||
|
macro uint int128[<*>].hash(self) => hash_vec(self);
|
||||||
|
macro uint uint128[<*>].hash(self) => hash_vec(self);
|
||||||
|
macro uint long[<*>].hash(self) => hash_vec(self);
|
||||||
|
macro uint ulong[<*>].hash(self) => hash_vec(self);
|
||||||
|
macro uint int[<*>].hash(self) => hash_vec(self);
|
||||||
|
macro uint uint[<*>].hash(self) => hash_vec(self);
|
||||||
|
macro uint short[<*>].hash(self) => hash_vec(self);
|
||||||
|
macro uint ushort[<*>].hash(self) => hash_vec(self);
|
||||||
|
macro uint char[<*>].hash(self) => hash_vec(self);
|
||||||
|
macro uint ichar[<*>].hash(self) => hash_vec(self);
|
||||||
|
macro uint bool[<*>].hash(self) => hash_vec(self);
|
||||||
|
|
||||||
macro uint typeid.hash(typeid t) => @generic_hash(((ulong)(uptr)t));
|
macro uint typeid.hash(typeid t) => @generic_hash(((ulong)(uptr)t));
|
||||||
macro uint String.hash(String c) => (uint)fnv32a::hash(c);
|
macro uint String.hash(String c) => (uint)fnv32a::hash(c);
|
||||||
macro uint char[].hash(char[] c) => (uint)fnv32a::hash(c);
|
macro uint char[].hash(char[] c) => (uint)fnv32a::hash(c);
|
||||||
macro uint void*.hash(void* ptr) => @generic_hash(((ulong)(uptr)ptr));
|
macro uint void*.hash(void* ptr) => @generic_hash(((ulong)(uptr)ptr));
|
||||||
|
|
||||||
|
<*
|
||||||
|
@require @typekind(array_ptr) == POINTER &&& @typekind(*array_ptr) == ARRAY
|
||||||
|
*>
|
||||||
|
macro uint hash_array(array_ptr) @local
|
||||||
|
{
|
||||||
|
return (uint)fnv32a::hash(((char*)array_ptr)[:$sizeof(*array_ptr)]);
|
||||||
|
}
|
||||||
|
|
||||||
|
<*
|
||||||
|
@require @typekind(vec) == VECTOR
|
||||||
|
*>
|
||||||
|
macro uint hash_vec(vec) @local
|
||||||
|
{
|
||||||
|
return (uint)fnv32a::hash(((char*)&&vec)[:$sizeof(vec.len * $typeof(vec).inner.sizeof)]);
|
||||||
|
}
|
||||||
|
|
||||||
const MAX_FRAMEADDRESS = 128;
|
const MAX_FRAMEADDRESS = 128;
|
||||||
<*
|
<*
|
||||||
|
|||||||
@@ -63,17 +63,78 @@ fn void test_prefetch()
|
|||||||
fn void test_hash()
|
fn void test_hash()
|
||||||
{
|
{
|
||||||
(char){}.hash();
|
(char){}.hash();
|
||||||
|
(char[<100>]){}.hash();
|
||||||
|
(char[100]){}.hash();
|
||||||
(ichar){}.hash();
|
(ichar){}.hash();
|
||||||
|
(ichar[<100>]){}.hash();
|
||||||
|
(ichar[100]){}.hash();
|
||||||
(short){}.hash();
|
(short){}.hash();
|
||||||
|
(short[<100>]){}.hash();
|
||||||
|
(short[100]){}.hash();
|
||||||
(ushort){}.hash();
|
(ushort){}.hash();
|
||||||
|
(ushort[<100>]){}.hash();
|
||||||
|
(ushort[100]){}.hash();
|
||||||
(int){}.hash();
|
(int){}.hash();
|
||||||
|
(int[<100>]){}.hash();
|
||||||
|
(int[100]){}.hash();
|
||||||
(uint){}.hash();
|
(uint){}.hash();
|
||||||
|
(uint[<100>]){}.hash();
|
||||||
|
(uint[100]){}.hash();
|
||||||
(long){}.hash();
|
(long){}.hash();
|
||||||
|
(long[<100>]){}.hash();
|
||||||
|
(long[100]){}.hash();
|
||||||
(ulong){}.hash();
|
(ulong){}.hash();
|
||||||
|
(ulong[<100>]){}.hash();
|
||||||
|
(ulong[100]){}.hash();
|
||||||
(int128){}.hash();
|
(int128){}.hash();
|
||||||
|
(int128[<100>]){}.hash();
|
||||||
|
(int128[100]){}.hash();
|
||||||
(uint128){}.hash();
|
(uint128){}.hash();
|
||||||
|
(uint128[<100>]){}.hash();
|
||||||
|
(uint128[100]){}.hash();
|
||||||
|
(bool){}.hash();
|
||||||
|
(bool[<100>]){}.hash();
|
||||||
|
(bool[100]){}.hash();
|
||||||
String x = "abc";
|
String x = "abc";
|
||||||
char[] y = "abc";
|
char[] y = "abc";
|
||||||
assert(x.hash() == y.hash());
|
assert(x.hash() == y.hash());
|
||||||
assert(int.typeid.hash());
|
assert(int.typeid.hash());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn void test_hash_repeat()
|
||||||
|
{
|
||||||
|
assert((char){}.hash() == (char){}.hash());
|
||||||
|
assert((char[<100>]){}.hash() == (char[<100>]){}.hash());
|
||||||
|
assert((char[100]){}.hash() == (char[100]){}.hash());
|
||||||
|
assert((ichar){}.hash() == (ichar){}.hash());
|
||||||
|
assert((ichar[<100>]){}.hash() == (ichar[<100>]){}.hash());
|
||||||
|
assert((ichar[100]){}.hash() == (ichar[100]){}.hash());
|
||||||
|
assert((short){}.hash() == (short){}.hash());
|
||||||
|
assert((short[<100>]){}.hash() == (short[<100>]){}.hash());
|
||||||
|
assert((short[100]){}.hash() == (short[100]){}.hash());
|
||||||
|
assert((ushort){}.hash() == (ushort){}.hash());
|
||||||
|
assert((ushort[<100>]){}.hash() == (ushort[<100>]){}.hash());
|
||||||
|
assert((ushort[100]){}.hash() == (ushort[100]){}.hash());
|
||||||
|
assert((int){}.hash() == (int){}.hash());
|
||||||
|
assert((int[<100>]){}.hash() == (int[<100>]){}.hash());
|
||||||
|
assert((int[100]){}.hash() == (int[100]){}.hash());
|
||||||
|
assert((uint){}.hash() == (uint){}.hash());
|
||||||
|
assert((uint[<100>]){}.hash() == (uint[<100>]){}.hash());
|
||||||
|
assert((uint[100]){}.hash() == (uint[100]){}.hash());
|
||||||
|
assert((long){}.hash() == (long){}.hash());
|
||||||
|
assert((long[<100>]){}.hash() == (long[<100>]){}.hash());
|
||||||
|
assert((long[100]){}.hash() == (long[100]){}.hash());
|
||||||
|
assert((ulong){}.hash() == (ulong){}.hash());
|
||||||
|
assert((ulong[<100>]){}.hash() == (ulong[<100>]){}.hash());
|
||||||
|
assert((ulong[100]){}.hash() == (ulong[100]){}.hash());
|
||||||
|
assert((int128){}.hash() == (int128){}.hash());
|
||||||
|
assert((int128[<100>]){}.hash() == (int128[<100>]){}.hash());
|
||||||
|
assert((int128[100]){}.hash() == (int128[100]){}.hash());
|
||||||
|
assert((uint128){}.hash() == (uint128){}.hash());
|
||||||
|
assert((uint128[<100>]){}.hash() == (uint128[<100>]){}.hash());
|
||||||
|
assert((uint128[100]){}.hash() == (uint128[100]){}.hash());
|
||||||
|
assert((bool){}.hash() == (bool){}.hash());
|
||||||
|
assert((bool[<100>]){}.hash() == (bool[<100>]){}.hash());
|
||||||
|
assert((bool[100]){}.hash() == (bool[100]){}.hash());
|
||||||
|
assert(int.typeid.hash() == int.typeid.hash());
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user