Skip to content

Instantly share code, notes, and snippets.

@target-san
target-san / hashable-hasher-separation.md
Last active October 26, 2021 08:02
Separate type hashing method from hash algorithm

Abstract

Current C++ standard dictates impementation of user-defined types hashing through specialization of std::hash type. This causes tight coupling between hashing method for conctete type and overall algorithm of turning type's significant bytes to hash value. One of consequences is constant reinvention of hash values combination in user code. Other consequence is that client code cannot use custom hashing algorithm without rewriting std::hash implementation for all components of user-defined type, down to primitive ones.

This document describes way to define and substitute hashing algorithm separate from hashing method for concrete type. Design is inspired by Rust's Hasher trait.

Execute cargo new --bin httpdl

Cargo.toml

[package]
name    = "httpdl"
version = "0.1.0"
authors = ["Igor Baidiuk <ibaidiuk@amcbridge.com>"]
[01:05:49] [main/DEBUG] [FML/]: Injecting tracing printstreams for STDOUT/STDERR.
[01:05:49] [main/INFO] [FML/]: Forge Mod Loader version 11.15.1.1722 for Minecraft 1.8.9 loading
[01:05:49] [main/INFO] [FML/]: Java is OpenJDK 64-Bit Server VM, version 1.8.0_91, running on Linux:amd64:3.16.0-38-generic, installed at /usr/lib/jvm/java-8-openjdk-amd64/jre
[01:05:49] [main/DEBUG] [FML/]: Java classpath at launch is /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/charsets.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/cldrdata.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/dnsns.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/icedtea-sound.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/jaccess.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/java-atk-wrapper.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/localedata.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/nashorn.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/sunec.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/sunjce_provider.jar:/usr/lib/
@target-san
target-san / adl-statics
Created April 29, 2015 08:43
ADL-based static init for arbitrary set of types
#include <iostream>
#include <typeinfo>
/** This sketch is derived from Cereal's polymorphic serialization logic
Type registration:
https://github.com/USCiLab/cereal/blob/master/include/cereal/types/polymorphic.hpp
Archive registration and binding details:
https://github.com/USCiLab/cereal/blob/master/include/cereal/details/polymorphic_impl.hpp
Archive registration macro:
https://github.com/USCiLab/cereal/blob/master/include/cereal/cereal.hpp