Skip to content
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

Add permutations and permutations_sized adaptors #230

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fixed braced-initializer cursor initialization
  • Loading branch information
nwad123 committed Jan 27, 2025
commit 0b65e2452bd0cbef4a2cdcb2dfbd92b7b11a8fdc
4 changes: 2 additions & 2 deletions include/flux/adaptor/permutations.hpp
Original file line number Diff line number Diff line change
@@ -113,7 +113,7 @@
std::iota(indices.begin(), indices.end(), 0);

// return the cursor
return {.indices_ = indices, .index_ = 0};
return {.indices_ = indices, .permutation_index_ = 0};
}

static constexpr auto last(self_t& self) -> cursor_type
@@ -130,7 +130,7 @@
std::iota(indices.rbegin(), indices.rend(), 0);

// return the cursor
return {.indices_ = indices, .index_ = self.count_permutations()};
return {.indices_ = indices, .permutation_index_ = self.count_permutations()};
}

static constexpr auto is_last([[maybe_unused]] self_t& self, const cursor_type& cursor)
@@ -154,7 +154,7 @@
static constexpr auto read_at(self_t& self, const cursor_type& cursor) -> value_type
{
if (not self.is_cached()) {
self.cache_base();

Check warning on line 157 in include/flux/adaptor/permutations.hpp

Codecov / codecov/patch

include/flux/adaptor/permutations.hpp#L157

Added line #L157 was not covered by tests
}
return reindex_vec<inner_value_t>(self.cache_, cursor.indices_);
}
14 changes: 7 additions & 7 deletions include/flux/adaptor/permutations_sized.hpp
Original file line number Diff line number Diff line change
@@ -67,7 +67,7 @@
if (SubsequenceSize <= size) {
size_ = factorial(cache_.size()) / factorial(cache_.size() - SubsequenceSize);
} else {
size_ = 0;

Check warning on line 70 in include/flux/adaptor/permutations_sized.hpp

Codecov / codecov/patch

include/flux/adaptor/permutations_sized.hpp#L70

Added line #L70 was not covered by tests
}

state_ = state_t::Cached;
@@ -83,15 +83,15 @@
struct cursor_type {
std::vector<std::size_t> indices_;
std::vector<std::size_t> cycles_;
std::size_t index_;
std::size_t permutation_index_;

[[nodiscard]] constexpr auto operator<=>(const cursor_type& other) const
{
return index_ <=> other.index_;
return permutation_index_ <=> other.permutation_index_;
}
[[nodiscard]] constexpr bool operator==(const cursor_type& other) const
{
return index_ == other.index_;
return permutation_index_ == other.permutation_index_;
}
};

@@ -118,7 +118,7 @@
std::iota(cycles.rbegin(), cycles.rend(), base_length - SubsequenceSize);

// return the cursor
return {.indices_ = indices, .cycles_ = cycles, .index_ = 0};
return {.indices_ = indices, .cycles_ = cycles, .permutation_index_ = 0};
}

static constexpr auto last(self_t& self) -> cursor_type
@@ -138,20 +138,20 @@
std::vector<std::size_t> cycles(SubsequenceSize, 0);

// return the cursor
return {.indices_ = indices, .cycles_ = cycles, .index_ = self.count_permutations()};
return {.indices_ = indices, .cycles_ = cycles, .permutation_index_ = self.count_permutations()};
}

static constexpr auto is_last(self_t& self, const cursor_type& cursor) -> bool
{
return cursor.index_ >= self.count_permutations();
return cursor.permutation_index_ >= self.count_permutations();
}

static constexpr auto inc(self_t& self, cursor_type& cursor) -> void
{
const auto k = SubsequenceSize;
const auto n = self.cache_.size();

cursor.index_ += 1;
cursor.permutation_index_ += 1;

for (const auto i : flux::iota(std::size_t {0}, std::size_t {k}).reverse()) {
if (cursor.cycles_[i] == 0) {
@@ -170,7 +170,7 @@
static constexpr auto read_at(self_t& self, const cursor_type& cursor) -> value_type
{
if (not self.is_cached()) {
self.cache_base();

Check warning on line 173 in include/flux/adaptor/permutations_sized.hpp

Codecov / codecov/patch

include/flux/adaptor/permutations_sized.hpp#L173

Added line #L173 was not covered by tests
}
return reindex_vec<inner_value_t>(self.cache_, cursor.indices_, SubsequenceSize);
}
Loading