Skip to content

Commit 60171c0

Browse files
author
Ryan Haining
committed
Modifies combinations_with_replacement(c, 0) to produce {{}}
Issue #101
1 parent 9b2cee4 commit 60171c0

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

combinations_with_replacement.hpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ class iter::impl::CombinatorWithReplacement {
6060
? 0
6161
: COMPLETE} {}
6262

63+
static Iterator zero_length_end(ContainerT& container) {
64+
Iterator it{container, 0};
65+
it.steps_ = 0;
66+
return it;
67+
}
68+
6369
CombIteratorDeref<ContainerT>& operator*() {
6470
return indices_;
6571
}
@@ -69,15 +75,19 @@ class iter::impl::CombinatorWithReplacement {
6975
}
7076

7177
Iterator& operator++() {
78+
if (indices_.get().empty()) {
79+
// zero-length case.
80+
++steps_;
81+
return *this;
82+
}
7283
for (auto iter = indices_.get().rbegin(); iter != indices_.get().rend();
7384
++iter) {
7485
++(*iter);
7586
if (!(*iter != get_end(*container_p_))) {
7687
if ((iter + 1) != indices_.get().rend()) {
77-
for (auto down = iter; ; --down) {
88+
for (auto down = iter;; --down) {
7889
(*down) = dumb_next(*(iter + 1));
79-
if (down == indices_.get().rbegin())
80-
break;
90+
if (down == indices_.get().rbegin()) break;
8191
}
8292
} else {
8393
steps_ = COMPLETE;
@@ -117,6 +127,9 @@ class iter::impl::CombinatorWithReplacement {
117127
}
118128

119129
Iterator<Container> end() {
130+
if (length_ == 0) {
131+
return Iterator<Container>::zero_length_end(container_);
132+
}
120133
return {container_, 0};
121134
}
122135

@@ -125,6 +138,9 @@ class iter::impl::CombinatorWithReplacement {
125138
}
126139

127140
Iterator<AsConst<Container>> end() const {
141+
if (length_ == 0) {
142+
return Iterator<AsConst<Container>>::zero_length_end(container_);
143+
}
128144
return {std::as_const(container_), 0};
129145
}
130146
};

test/test_combinations_with_replacement.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include <combinations_with_replacement.hpp>
2-
32
#include <iterator>
43
#include <set>
54
#include <string>
@@ -96,8 +95,23 @@ TEST_CASE("combinations_with_replacement: big size is no problem",
9695
TEST_CASE("combinations_with_replacement: 0 size is empty",
9796
"[combinations_with_replacement]") {
9897
std::string s{"A"};
99-
auto cwr = combinations_with_replacement(s, 0);
100-
REQUIRE(std::begin(cwr) == std::end(cwr));
98+
CharCombSet sc;
99+
for (auto v : combinations_with_replacement(s, 0)) {
100+
sc.emplace_back(std::begin(v), std::end(v));
101+
}
102+
CharCombSet ans = {{}};
103+
REQUIRE(ans == sc);
104+
}
105+
106+
TEST_CASE("combinations_with_replacement: 0 size is empty with empty container",
107+
"[combinations_with_replacement]") {
108+
std::string s{};
109+
CharCombSet sc;
110+
for (auto v : combinations_with_replacement(s, 0)) {
111+
sc.emplace_back(std::begin(v), std::end(v));
112+
}
113+
CharCombSet ans = {{}};
114+
REQUIRE(ans == sc);
101115
}
102116

103117
TEST_CASE("combinations_with_replacement: operator->",

0 commit comments

Comments
 (0)