Skip to content

Commit

Permalink
add range insert. Fixes #31
Browse files Browse the repository at this point in the history
  • Loading branch information
rekola committed Feb 21, 2024
1 parent ab21684 commit 651ab72
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
10 changes: 9 additions & 1 deletion include/radix_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,14 @@ namespace radix_cpp {
return emplace(keyval).first;
}

template<class InputIt>
void insert(InputIt first, InputIt last) {
while (first != last) {
insert(*first);
first++;
}
}

template <typename Q = mapped_type>
typename std::enable_if<!std::is_void<Q>::value, Q&>::type operator[](const key_type& key) noexcept {
auto it = find(key);
Expand Down Expand Up @@ -806,7 +814,7 @@ namespace radix_cpp {
}
}
iterator end() noexcept {
// iterator is by default and end iterator (the depth is zero)
// iterator is by default and end iterator (ptr is nil)
return iterator(this);
}

Expand Down
19 changes: 19 additions & 0 deletions tests/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,3 +498,22 @@ TEST_CASE( "signed integers in set", "[signed_integer_set]") {
REQUIRE(*it++ == 10);
REQUIRE(it == S.end());
}

TEST_CASE( "insert range", "[insert_range]") {
std::vector<double> V;
for (size_t i = 0; i < 1000; i++) {
V.push_back(0.1 * static_cast<double>(i));
}
radix_cpp::set<double> S;
S.insert(V.begin(), V.end());
REQUIRE(S.size() == 1000);
auto it = S.begin();
REQUIRE(*it++ == 0.0);
REQUIRE(*it++ == 0.1);
REQUIRE(*it++ == 0.2);
it = S.find(0.1 * static_cast<double>(998));
REQUIRE(it != S.end());
REQUIRE(*it++ == 0.1 * static_cast<double>(998));
REQUIRE(*it++ == 0.1 * static_cast<double>(999));
REQUIRE(it == S.end());
}

0 comments on commit 651ab72

Please sign in to comment.