Skip to content

Add hashing support compatible with std::hash #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions c_include/fine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,16 @@ inline ERL_NIF_TERM make_atom(ErlNifEnv *env, const char *msg) {
// A representation of an atom term.
class Atom {
public:
Atom(std::string name) : name(name), term(std::nullopt) {
Atom(std::string name) : name(std::move(name)), term(std::nullopt) {
if (!Atom::initialized) {
Atom::atoms.push_back(this);
}
}

std::string to_string() const { return this->name; }
public:
const std::string &to_string() const & noexcept { return this->name; }

std::string to_string() && noexcept { return this->name; }

bool operator==(const Atom &other) const { return this->name == other.name; }

Expand All @@ -90,6 +93,8 @@ class Atom {
}

friend struct Encoder<Atom>;
friend struct Decoder<Atom>;
friend struct ::std::hash<Atom>;

friend int __private__::load(ErlNifEnv *env, void **priv_data,
ERL_NIF_TERM load_info);
Expand Down Expand Up @@ -1158,4 +1163,18 @@ inline int load(ErlNifEnv *env, void **, ERL_NIF_TERM) {

} // namespace fine

namespace std {
template <> struct hash<::fine::Term> {
size_t operator()(const ::fine::Term &term) noexcept {
return enif_hash(ERL_NIF_INTERNAL_HASH, term, 0);
}
};

template <> struct hash<::fine::Atom> {
size_t operator()(const ::fine::Atom &atom) noexcept {
return std::hash<std::string_view>{}(atom.to_string());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To use *atom.term instead would require a rework of fine::Atom.
If receiving the atom from a NIF parameter, atom.term is std::nullopt. The only time when this is not the case is when atoms are globals and initialized during the load NIF callback.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think hashing the string is good!

}
};
} // namespace std

#endif
12 changes: 12 additions & 0 deletions test/c_src/finest.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <cstring>
#include <exception>
#include <functional>
#include <memory_resource>
#include <optional>
#include <stdexcept>
Expand Down Expand Up @@ -354,6 +355,17 @@ bool compare_ge(ErlNifEnv *, fine::Term lhs, fine::Term rhs) noexcept {
return lhs >= rhs;
}
FINE_NIF(compare_ge, 0);

std::uint64_t hash_term(ErlNifEnv *, fine::Term term) noexcept {
return std::invoke(std::hash<fine::Term>{}, term);
}
FINE_NIF(hash_term, 0);

std::uint64_t hash_atom(ErlNifEnv *, fine::Atom atom) noexcept {
return std::invoke(std::hash<fine::Atom>{}, atom);
}
FINE_NIF(hash_atom, 0);

} // namespace finest

FINE_INIT("Elixir.Finest.NIF");
3 changes: 3 additions & 0 deletions test/lib/finest/nif.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,8 @@ defmodule Finest.NIF do
def compare_gt(_lhs, _rhs), do: err!()
def compare_ge(_lhs, _rhs), do: err!()

def hash_term(_term), do: err!()
def hash_atom(_term), do: err!()

defp err!(), do: :erlang.nif_error(:not_loaded)
end
14 changes: 14 additions & 0 deletions test/test/finest_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -351,4 +351,18 @@ defmodule FinestTest do
assert NIF.compare_ge("fine", "fine")
end
end

describe "hash" do
test "term" do
for value <- [42, "fine", ["it", %{"should" => {"just", "work"}}], :atom] do
assert NIF.hash_term(value) == NIF.hash_term(value)
end
end

test "atom" do
for value <- [:ok, :error, :"with spaces", Enum, nil, true, false] do
assert NIF.hash_atom(value) == NIF.hash_atom(value)
end
end
end
end