Skip to content

Support std::multimap and std::unordered_multimap#15

Merged
jonatanklosko merged 4 commits intoelixir-nx:mainfrom
brodeuralexis:feature/multimap
Aug 19, 2025
Merged

Support std::multimap and std::unordered_multimap#15
jonatanklosko merged 4 commits intoelixir-nx:mainfrom
brodeuralexis:feature/multimap

Conversation

@brodeuralexis
Copy link
Copy Markdown
Contributor

This commit enables support for std::multimap and std::unordered_multimap using fine::decode and fine::encoder.

This can allow users to handle keyword lists more easily:

std::nullopt_t example(ErlNifEnv *, std::multimap<fine::Atom, fine::Term> opts) {
  // ...
  return std::nullopt;
}

Unlike std::map, std::multimap does not have an operator[], meaning that accessing keys that do not exist will not default initialize its value on access. User must use std::multimap::find and deal with the absence of key.

If accumulating values for the same key is required, std::multimap::range_equal, and std::multimap::lower_bound and std::multimap::upper_bound can be used in conjunction with <ranges> (C++20) or <algorithm> (C++17):

std::nullopt_t example(ErlNifEnv *, std::multimap<fine::Atom, fine::Term> opts) {
  std::vector<int64_t> opts_numbers;
  std::transform(
    opts.lower_bound(atoms::number),
    opts.upper_bound(atoms::number),
    std::back_inserter(numbers),
    [&](const auto term) {
      return fine::decode<int64_t>(env, term);
    });

  // ...
  return std::nullopt;
}

This commit enables support for `std::multimap` and `std::unordered_multimap`
using `fine::decode` and `fine::encoder`.
Copy link
Copy Markdown
Member

@jonatanklosko jonatanklosko left a comment

Choose a reason for hiding this comment

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

Tiny comments and looks good to me!

Comment thread c_include/fine.hpp Outdated
Comment thread test/test/finest_test.exs Outdated
Copy link
Copy Markdown
Member

@jonatanklosko jonatanklosko left a comment

Choose a reason for hiding this comment

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

Thank you!

@jonatanklosko jonatanklosko merged commit dd85a34 into elixir-nx:main Aug 19, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants