mirror of
https://github.com/anoma/juvix.git
synced 2024-12-13 11:16:48 +03:00
55374ec96a
* Depends on PR #1909 * Closes #1750 * Adds recursion unrolling tests on JuvixCore * Adds a version of the mid-square hash example without the recursion manually unrolled For now, the recursion is always unrolled to a fixed depth (140). In the future, we want to add a global option to override this depth, as well as a mechanism to specify it on a per-function basis. In a more distant future, we might want to try deriving the unrolling depth heuristically for each function.
25 lines
723 B
Plaintext
25 lines
723 B
Plaintext
-- This file implements the mid-square hashing function in JuvixCore. See:
|
|
-- https://research.cs.vt.edu/AVresearch/hashing/midsquare.php
|
|
--
|
|
-- The implementation is for hashing natural numbers with maximum 16 bits into 6
|
|
-- bits.
|
|
--
|
|
|
|
-- `pow N` is 2 ^ N
|
|
def pow : Int -> Int := \(x : Int) if x = 0 then 1 else 2 * pow (x - 1);
|
|
|
|
-- `hash' N` hashes a number with max N bits (i.e. smaller than 2^N) into 6 bits
|
|
-- (i.e. smaller than 64) using the mid-square algorithm.
|
|
def hash' : Int -> Int -> Int := \(n : Int) \(x : Int)
|
|
if n <= 3 then
|
|
x * x
|
|
else if x < pow (n - 1) then
|
|
hash' (n - 1) x
|
|
else
|
|
((x * x) / pow (n - 3)) % pow 6;
|
|
|
|
def hash : Int -> Int := hash' 16;
|
|
|
|
hash 1367
|
|
-- result: 3
|