Skip to content

Commit

Permalink
Support accessing FlatMap entries by positional indices (#25)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
  • Loading branch information
jviotti authored Jan 22, 2025
1 parent dbc1e2a commit 214d23b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/flat_map/include/sourcemeta/noa/flat_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ template <typename Key, typename Value, typename Hash> class FlatMap {
#endif
}

inline auto at(const size_type index) const noexcept -> const Entry & {
return this->data[index];
}

auto erase(const key_type &key, const hash_type key_hash) -> size_type {
const auto current_size{this->size()};

Expand Down
20 changes: 20 additions & 0 deletions test/flat_map/flat_map_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,23 @@ TEST(Noa_FlatMap, try_at) {
EXPECT_TRUE(*map.try_at("0", hash));
EXPECT_EQ(map.try_at("1", map.hash("1")), nullptr);
}

TEST(Noa_FlatMap, at_index) {
sourcemeta::noa::FlatMap<std::string, bool,
sourcemeta::noa::StandardHash<std::string>>
map;

const auto hash_1{map.assign("0", true)};
const auto hash_2{map.assign("1", false)};

const auto &entry_1{map.at(0)};
const auto &entry_2{map.at(1)};

EXPECT_EQ(entry_1.first, "0");
EXPECT_TRUE(entry_1.second);
EXPECT_EQ(entry_1.hash, hash_1);

EXPECT_EQ(entry_2.first, "1");
EXPECT_FALSE(entry_2.second);
EXPECT_EQ(entry_2.hash, hash_2);
}

4 comments on commit 214d23b

@github-actions
Copy link

Choose a reason for hiding this comment

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

Benchmark (macos/llvm)

Benchmark suite Current: 214d23b Previous: dbc1e2a Ratio
Regex_Lower_S_Or_Upper_S_Asterisk 1.6401514303339735 ns/iter 1.6425522750282888 ns/iter 1.00
Regex_Caret_Lower_S_Or_Upper_S_Asterisk_Dollar 1.5944826791695377 ns/iter 1.6502489281521493 ns/iter 0.97
Regex_Period_Asterisk 1.5968626160683688 ns/iter 1.662963521030707 ns/iter 0.96
Regex_Group_Period_Asterisk_Group 1.62399569454731 ns/iter 1.7105423620390872 ns/iter 0.95
Regex_Period_Plus 1.9352386635555838 ns/iter 1.9808410413865374 ns/iter 0.98
Regex_Period 1.9471920043530595 ns/iter 2.030329092767223 ns/iter 0.96
Regex_Caret_Period_Plus_Dollar 1.9271781290128638 ns/iter 2.0400920649249668 ns/iter 0.94
Regex_Caret_Group_Period_Plus_Group_Dollar 1.9170267888698804 ns/iter 1.9074630881973125 ns/iter 1.01
Regex_Caret_Period_Asterisk_Dollar 1.5969896400338144 ns/iter 1.5897658389232656 ns/iter 1.00
Regex_Caret_Group_Period_Asterisk_Group_Dollar 1.600677523665264 ns/iter 1.5931409976435178 ns/iter 1.00
Regex_Caret_X_Hyphen 6.749282019105777 ns/iter 6.630655011185851 ns/iter 1.02
Regex_Period_Md_Dollar 70.73669768358648 ns/iter 69.14498066745995 ns/iter 1.02
Regex_Caret_Slash_Period_Asterisk 5.084395979246659 ns/iter 5.1031690421033815 ns/iter 1.00
Regex_Caret_Period_Range_Dollar 2.228777172474936 ns/iter 2.2548313473197408 ns/iter 0.99
Regex_Nested_Backtrack 751.2890471935256 ns/iter 746.5492685191728 ns/iter 1.01

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

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

Benchmark (linux/llvm)

Benchmark suite Current: 214d23b Previous: dbc1e2a Ratio
Regex_Lower_S_Or_Upper_S_Asterisk 2.2050867231326547 ns/iter 2.258452925090921 ns/iter 0.98
Regex_Caret_Lower_S_Or_Upper_S_Asterisk_Dollar 2.2100882315668424 ns/iter 2.200908503246521 ns/iter 1.00
Regex_Period_Asterisk 2.1978781538054655 ns/iter 2.2008362533214343 ns/iter 1.00
Regex_Group_Period_Asterisk_Group 2.194200727383446 ns/iter 2.2095506856918306 ns/iter 0.99
Regex_Period_Plus 2.5552775977381 ns/iter 2.259900101922063 ns/iter 1.13
Regex_Period 2.492076400102254 ns/iter 2.244387121896975 ns/iter 1.11
Regex_Caret_Period_Plus_Dollar 2.2425677651340394 ns/iter 2.268741969448036 ns/iter 0.99
Regex_Caret_Group_Period_Plus_Group_Dollar 2.357148097311435 ns/iter 2.2476574615552387 ns/iter 1.05
Regex_Caret_Period_Asterisk_Dollar 2.486882812300636 ns/iter 2.4867601929389522 ns/iter 1.00
Regex_Caret_Group_Period_Asterisk_Group_Dollar 2.4877530515428172 ns/iter 2.333774677945727 ns/iter 1.07
Regex_Caret_X_Hyphen 13.053329939595303 ns/iter 13.056683305721776 ns/iter 1.00
Regex_Period_Md_Dollar 73.97677875287161 ns/iter 73.07237453548507 ns/iter 1.01
Regex_Caret_Slash_Period_Asterisk 7.160430719688381 ns/iter 5.908485871490405 ns/iter 1.21
Regex_Caret_Period_Range_Dollar 4.039346444819488 ns/iter 2.804084030645914 ns/iter 1.44
Regex_Nested_Backtrack 458.01741358677566 ns/iter 457.47339877025627 ns/iter 1.00

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

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

Benchmark (linux/gcc)

Benchmark suite Current: 214d23b Previous: dbc1e2a Ratio
Regex_Lower_S_Or_Upper_S_Asterisk 3.7418133562061575 ns/iter 3.736762465505039 ns/iter 1.00
Regex_Caret_Lower_S_Or_Upper_S_Asterisk_Dollar 3.7278165694701837 ns/iter 3.7340562042289385 ns/iter 1.00
Regex_Period_Asterisk 3.7297565229277674 ns/iter 3.744992826093141 ns/iter 1.00
Regex_Group_Period_Asterisk_Group 3.7288010954764115 ns/iter 3.7330429399917175 ns/iter 1.00
Regex_Period_Plus 3.7357140064808174 ns/iter 3.727543953006515 ns/iter 1.00
Regex_Period 3.7282925348027427 ns/iter 3.7269971728372786 ns/iter 1.00
Regex_Caret_Period_Plus_Dollar 3.7309961505397142 ns/iter 3.7359934596843387 ns/iter 1.00
Regex_Caret_Group_Period_Plus_Group_Dollar 3.7296894842484156 ns/iter 3.728383874301665 ns/iter 1.00
Regex_Caret_Period_Asterisk_Dollar 3.7324915127605673 ns/iter 3.7313267093809617 ns/iter 1.00
Regex_Caret_Group_Period_Asterisk_Group_Dollar 3.7279085104007583 ns/iter 3.7273399209081446 ns/iter 1.00
Regex_Caret_X_Hyphen 12.431306255993519 ns/iter 12.440941385608477 ns/iter 1.00
Regex_Period_Md_Dollar 89.63559153647003 ns/iter 91.7557622287665 ns/iter 0.98
Regex_Caret_Slash_Period_Asterisk 6.522908908999184 ns/iter 7.462789220130224 ns/iter 0.87
Regex_Caret_Period_Range_Dollar 4.357682754572321 ns/iter 4.365602744763098 ns/iter 1.00
Regex_Nested_Backtrack 836.166719026071 ns/iter 836.8215427948625 ns/iter 1.00

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

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

Benchmark (windows/msvc)

Benchmark suite Current: 214d23b Previous: dbc1e2a Ratio
Regex_Lower_S_Or_Upper_S_Asterisk 6.9072165178571 ns/iter 7.466733258927962 ns/iter 0.93
Regex_Caret_Lower_S_Or_Upper_S_Asterisk_Dollar 6.883229910714661 ns/iter 7.249348214285308 ns/iter 0.95
Regex_Period_Asterisk 7.090626116072078 ns/iter 7.256168271178035 ns/iter 0.98
Regex_Group_Period_Asterisk_Group 7.217571428571442 ns/iter 7.466171875000047 ns/iter 0.97
Regex_Period_Plus 7.219037946428435 ns/iter 8.044094866071255 ns/iter 0.90
Regex_Period 7.220366485623528 ns/iter 7.710590401785693 ns/iter 0.94
Regex_Caret_Period_Plus_Dollar 7.2370301339283065 ns/iter 7.755354910714551 ns/iter 0.93
Regex_Caret_Group_Period_Plus_Group_Dollar 7.178620950095945 ns/iter 7.719137276785811 ns/iter 0.93
Regex_Caret_Period_Asterisk_Dollar 6.850829433701918 ns/iter 7.522549107143054 ns/iter 0.91
Regex_Caret_Group_Period_Asterisk_Group_Dollar 6.961850000000221 ns/iter 7.4382977346498995 ns/iter 0.94
Regex_Caret_X_Hyphen 11.88890714285711 ns/iter 15.037165178572057 ns/iter 0.79
Regex_Period_Md_Dollar 137.56646037651123 ns/iter 153.95723214286505 ns/iter 0.89
Regex_Caret_Slash_Period_Asterisk 10.521857812499036 ns/iter 11.349646874999841 ns/iter 0.93
Regex_Caret_Period_Range_Dollar 7.558535714286155 ns/iter 8.072027642535264 ns/iter 0.94
Regex_Nested_Backtrack 598.9221428571057 ns/iter 651.8601785714395 ns/iter 0.92

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.