Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Algol68
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C with Coccinelle
C++ with Coccinelle
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Mojo
Nim
Numba
Nix
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
PTX
Python
Racket
Raku
Ruby
Rust
Sail
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
Triton
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
sway
zig source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
zig 0.10.0
zig 0.11.0
zig 0.12.0
zig 0.12.1
zig 0.13.0
zig 0.14.0
zig 0.14.1
zig 0.15.1
zig 0.2.0
zig 0.3.0
zig 0.4.0
zig 0.5.0
zig 0.6.0
zig 0.7.0
zig 0.7.1
zig 0.8.0
zig 0.9.0
zig trunk
Options
Source code
const std = @import("std"); fn Character( comptime character_count: comptime_int, ) type { return std.math.IntFittingRange( 0, character_count + 1, // +1 for whitespace ); } fn maxIndex( comptime word_length: comptime_int, comptime character_count: comptime_int, ) comptime_int { const a: comptime_int = std.math.powi(usize, character_count + 1, word_length) catch unreachable; return a - 1; } fn Index( comptime word_length: comptime_int, comptime character_count: comptime_int, ) type { return std.math.IntFittingRange(0, maxIndex(word_length, character_count)); } fn hash( comptime word_length: comptime_int, comptime character_count: comptime_int, word: [word_length]Character(character_count), ) Index(word_length, character_count) { const adjusted_character_count = character_count + 1; // 1 for whitespace var count: [adjusted_character_count]std.math.IntFittingRange(0, character_count) = .{0} ** adjusted_character_count; for (word) |char| count[char] += 1; const Result = Index(word_length, character_count); var result: Result = 0; var base: Result = 1; for (count[1..], 1..) |this_char_count, character| { const first_term = character * base; const pow = std.math.powi(Result, adjusted_character_count, this_char_count) catch unreachable; base *= pow; const multiplier = @divExact(pow - 1, character_count); const to_add = first_term * multiplier; result += @intCast(to_add); } return result; } const test_namespace = struct { const test_character_count = 26; const test_word_length = 7; fn testHash(word: []const u8) void { var word_adjusted: [test_word_length]Character(test_character_count) = undefined; for (word, word_adjusted[0..word.len]) |src, *dst| { dst.* = @intCast(src - 'A' + 1); } @memset(word_adjusted[word.len..], 0); const index = hash(test_word_length, test_character_count, word_adjusted); std.debug.print("{}\n", .{index}); } }; pub fn main() void { const testHash = test_namespace.testHash; testHash(""); testHash("A"); testHash("AABC"); testHash("BCAA"); testHash("ZZZZZZZ"); }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Compiler Explorer Shop
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
Statistics
Changelog
Version tree