Skip to content
Snippets Groups Projects
Commit befd1997 authored by Philip Trettner's avatar Philip Trettner
Browse files

added random to ranges

parent f587df38
Branches
No related tags found
No related merge requests found
......@@ -448,6 +448,23 @@ iterator smart_collection<mesh_ptr, tag, iterator>::end() const
return {{this->mesh, typename primitive<tag>::index(primitive<tag>::all_size(*this->mesh))}};
}
template <class mesh_ptr, class tag, class iterator>
template <class Generator>
typename primitive<tag>::handle smart_collection<mesh_ptr, tag, iterator>::random(Generator &g) const
{
auto s = primitive<tag>::all_size(*this->mesh);
assert(s > 0 && "Cannot chose from empty mesh");
std::uniform_int_distribution<> uniform(0, s - 1);
typename primitive<tag>::handle h = {this->mesh, typename primitive<tag>::index(uniform(g))};
if (iterator::is_valid_iterator)
while (h.is_removed())
h = {this->mesh, typename primitive<tag>::index(uniform(g))};
return h;
}
template <class iterator>
void vertex_collection<iterator>::remove(vertex_handle v) const
{
......
......@@ -16,6 +16,9 @@ struct valid_primitive_iterator
{
using handle_t = typename primitive<tag>::handle;
static const bool is_all_iterator = false;
static const bool is_valid_iterator = true;
valid_primitive_iterator() = default;
valid_primitive_iterator(handle_t handle) : handle(handle) { this->handle.idx = handle.mesh->next_valid_idx_from(handle.idx); }
......@@ -53,6 +56,9 @@ struct all_primitive_iterator
{
using handle_t = typename primitive<tag>::handle;
static const bool is_all_iterator = true;
static const bool is_valid_iterator = false;
all_primitive_iterator() = default;
all_primitive_iterator(handle_t handle) : handle(handle) {}
......
......@@ -175,6 +175,12 @@ struct smart_collection : smart_range<smart_collection<mesh_ptr, tag, iterator>,
iterator begin() const;
iterator end() const;
/// returns a handle chosen uniformly at random
/// NOTE: when only valid handles are allowed, this will use rejection sampling
/// and thus get very slow if a lot of data is invalid
template <class Generator>
handle random(Generator& g) const;
protected:
/// Backreference to mesh
mesh_ptr mesh;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment