Hashtron inference: Difference between revisions

Added FreeBASIC
m (test demo)
(Added FreeBASIC)
Line 71:
 
== Example Code ==
 
=== {{header|Python}} ===
 
<syntaxhighlight lang="python" line>
 
def hash(n, s, max_val):
# Mixing stage, mix input with salt using subtraction
Line 114 ⟶ 111:
return out
 
print(inference(42,64,[[0,2]]))</syntaxhighlight>
</syntaxhighlight>
 
=== {{header|GoFreeBASIC}} ===
<syntaxhighlight lang="vbnet">Function hash(n As Ulongint, s As Ulongint, max As Ulongint) As Ulongint
Dim As Ulongint k = &hFFFFFFFF
' Mixing stage, mix input with salt using subtraction
Dim As Ulongint m = (n - s) And k
' Hashing stage, use xor shift with prime coefficients
m Xor= ((m Shl 2) And k)
m Xor= ((m Shl 3) And k)
m Xor= ((m Shr 5) And k)
m Xor= ((m Shr 7) And k)
m Xor= ((m Shl 11) And k)
m Xor= ((m Shl 13) And k)
m Xor= ((m Shr 17) And k)
m Xor= ((m Shl 19) And k)
' Mixing stage 2, mix input with salt using addition
m = (m + s) And k
' Modular stage using Lemire's fast alternative to modulo reduction
Return ((m * max) Shr 32) And k
End Function
 
Function inference(comando As Ulongint, bits As Integer, program() As Ulong) As Ulongint
Dim As Ulongint salida = 0
' Check if the program is empty
If Ubound(program) = -1 Then Return salida
' Iterate over the bits
For j As Integer = 0 To bits - 1
Dim As Ulongint entrada = comando Or (j Shl 16)
Dim As Ulongint ss = program(0)
Dim As Ulongint maxx = program(1)
entrada = hash(entrada, ss, maxx)
For i As Integer = 1 To Ubound(program)
Dim As Ulongint s = program(i)
Dim As Ulongint max = program(i + 1)
maxx -= max
entrada = hash(entrada, s, maxx)
Next i
entrada And= 1
If entrada <> 0 Then salida Or= (1 Shl j)
Next j
Return salida
End Function
 
Dim As Ulong program(1) = {0, 2}
Print "Test demo:"
Print inference(42, 64, program())
 
Sleep</syntaxhighlight>
 
=== {{header|Go}} ===
<syntaxhighlight lang="go" line>
package main
Line 169 ⟶ 212:
func main() {
println(Inference(42, 64, [][2]uint32{{0, 2}}))
}</syntaxhighlight>
}
</syntaxhighlight>
 
https://go.dev/play/p/AsmOzKWx7jB
 
=== {{header|PHP}} ===
 
<syntaxhighlight lang="php" line>
 
// define hashtron hash
$hash = function($n, $s, $max) {
Line 242 ⟶ 282:
 
return $out;
};</syntaxhighlight>
};
</syntaxhighlight>
 
=== {{header|Java}} ===
 
<syntaxhighlight lang="java" line>
 
public class Main {
public static long inference(long command, long bits, long[][] program) {
Line 308 ⟶ 345:
 
}
}</syntaxhighlight>
}
</syntaxhighlight>
 
==={{header|Wren}}===
2,169

edits