From 656fc21e284442740e71eb17221c2125d0c1faec Mon Sep 17 00:00:00 2001 From: ConvolutedDog <102723346+ConvolutedDog@users.noreply.github.com> Date: Tue, 3 Sep 2024 11:14:17 +0800 Subject: [PATCH] Revert "use interface add hash function" This reverts commit 1fb9a8793a36608578340f1481c6c11763753cd5. --- builtin_packages/.prajna | 1 - builtin_packages/float.prajna | 1 - builtin_packages/hash.prajna | 54 ------------------- builtin_packages/int.prajna | 5 +- builtin_packages/primitive_type.prajna | 2 + .../special_statement_test.prajna | 30 +++++++---- 6 files changed, 25 insertions(+), 68 deletions(-) delete mode 100644 builtin_packages/hash.prajna diff --git a/builtin_packages/.prajna b/builtin_packages/.prajna index a9597dc0..242de179 100644 --- a/builtin_packages/.prajna +++ b/builtin_packages/.prajna @@ -41,7 +41,6 @@ use string::String; use serializable::Serializable; use serialize; -use hash::Hashable; func ExitWithMessage(msg: String) { msg.PrintLine(); diff --git a/builtin_packages/float.prajna b/builtin_packages/float.prajna index 8e986d51..1f4870d9 100644 --- a/builtin_packages/float.prajna +++ b/builtin_packages/float.prajna @@ -190,4 +190,3 @@ implement FloatScientificPrinter { return str_re; } } - diff --git a/builtin_packages/hash.prajna b/builtin_packages/hash.prajna deleted file mode 100644 index 77837ffa..00000000 --- a/builtin_packages/hash.prajna +++ /dev/null @@ -1,54 +0,0 @@ -interface Hashable{ - func Hash()->i64; -} - -implement Hashable for char{ - func Hash()->i64{ - var hash: i64 = 5381; - var int_bits = cast(this); - hash = ((hash * 16) + hash) + int_bits; - return hash; - } -} - -implement Hashable for bool{ - func Hash()->i64{ - var hash: i64 = 5381; - var int_bits = cast(this); - hash = ((hash * 16) + hash) + int_bits; - return hash; - } -} - -template -implement Hashable for Int{ - func Hash()->i64{ - var hash: i64 = 5381; - var int_bits = cast, i64>(this); - hash = ((hash * 32) + hash) + int_bits; - return hash; - } -} - -template -implement Hashable for Float{ - func Hash()->i64{ - var hash: i64 = 5381; - var int_bits = cast, i64>(this); - hash = ((hash * 32) + hash) + int_bits; - return hash; - } -} - -implement Hashable for String{ - func Hash()->i64{ - var hash: i64 = 5381; - for x in 0 to this.Length(){ - var int_bits = cast(this.__get_linear_index__(x)); - hash = ((hash * 16) + hash) + int_bits; - } - return hash; - } -} - - diff --git a/builtin_packages/int.prajna b/builtin_packages/int.prajna index 70fe641f..5456db97 100644 --- a/builtin_packages/int.prajna +++ b/builtin_packages/int.prajna @@ -1,3 +1,5 @@ + + template implement Uint { @static @@ -25,6 +27,3 @@ implement Int { return !Int::Max(); } } - - - diff --git a/builtin_packages/primitive_type.prajna b/builtin_packages/primitive_type.prajna index 81193661..df7cd45d 100644 --- a/builtin_packages/primitive_type.prajna +++ b/builtin_packages/primitive_type.prajna @@ -292,3 +292,5 @@ use NegativeAndPositiveTemplate2; // use ComparableTemplate2; use LogicalableTemplate2; + + diff --git a/tests/prajna_sources/special_statement_test.prajna b/tests/prajna_sources/special_statement_test.prajna index a2df4407..ac3c76ca 100644 --- a/tests/prajna_sources/special_statement_test.prajna +++ b/tests/prajna_sources/special_statement_test.prajna @@ -1,14 +1,26 @@ -@test -func Test1() { - 3i64.Hash().ToString().PrintLine(); - 2i32.Hash().ToString().PrintLine(); +template +struct Hash{} + +template +implement Hash { + func __call__(v: T)->i64 { + return 1; + } +} + +implement Hash { + func __call__(v: i64)->i64 { + return v; + } +} - 4.0f64.Hash().ToString().PrintLine(); - 3.1f32.Hash().ToString().PrintLine(); - 'b'.Hash().ToString().PrintLine(); - "hello".Hash().ToString().PrintLine(); - true.Hash().ToString().PrintLine(); +@test +func Test1() { + var tmp: Hash; + tmp(3).PrintLine(); + var tmp2: Hash; + tmp2(3.0f32).PrintLine(); }