Posts

Showing posts with the label hash

Perl: constants and compile-time vs runtime hash lookups?

Image
Clash Royale CLAN TAG #URR8PPP Perl: constants and compile-time vs runtime hash lookups? In Perl 5.26, constant-based hash lookups appear to be resolved at compile-time, not runtime. How can I enforce it to be resolved at runtime? Consider the following reduced testcase, boiled down from a hash-based state-machine I was trying to write, where the key is the state identifier and the value is the state function. use constant { STATE_1 => 1, STATE_2 => 2, }; my %fsm; %fsm = ( STATE_1, sub { $fsm{STATE_2}->(@_); return STATE_2; }, STATE_2, sub { return STATE_1; } ); my $state = STATE_1; $state = $fsm{$state}->(); Note that in STATE_1 , I'm trying to call the STATE_2 function. STATE_1 STATE_2 However, at runtime I get this: Can't use an undefined value as a subroutine reference at ./self-reference-hash.pl line 15. Which indicates that the $fsm{STATE_2}->(@_); line in STATE_1 is undefined. And indeed, at the time where...

Are C++ std::hash implementations always deterministic?

Image
Clash Royale CLAN TAG #URR8PPP Are C++ std::hash implementations always deterministic? I know that std::hash<T> are implementation dependent, but are they supposed to be deterministic? std::hash<T> I know if I ran the std::hash<T> function on a value in the same process multiple times, I would get the same output. However, if I restarted the process, would I get the same value? Is there a seed that is used for std::hash? Does it depend on the compiler version, or some other factor? std::hash<T> Is there a guarantee that with an input X I will always get output Y regardless of when the process running was started, the machine, or the compiler version? X Y By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.