17#ifndef __algo_random_loop_h__
18#define __algo_random_loop_h__
24#include <unordered_set>
31 template <
class ImageType,
class RandomEngine>
35 RandomEngine& random_engine,
36 const size_t&
axis = 0,
37 const size_t& number_iterations = std::numeric_limits<ssize_t>::max()):
39 engine (random_engine),
41 idx(image.size(
axis)),
42 max_cnt (number_iterations),
50 std::iota (std::begin(idx), std::end(idx), 0);
51 std::shuffle(std::begin(idx), std::end(idx), engine);
59 image.index(ax) = *it;
66 if (cnt > max_cnt or it == stop) {
72 return status && image.index(ax) >= 0 && image.index(ax) < image.size(ax);
88 template <
class ImageType>
92 const size_t&
axis = 0,
93 const size_t& number_iterations = std::numeric_limits<ssize_t>::max(),
94 const bool repeat =
false,
95 const ssize_t& min_index = 0,
96 const ssize_t& max_index = std::numeric_limits<ssize_t>::max()):
104 if (max_index < image.size(ax))
105 range = max_index - min_idx + 1;
107 range = image.size(ax) - min_idx;
108 if (number_iterations < range)
109 max_cnt = number_iterations;
113 assert (max_cnt >= 1);
121 while (cnt < max_cnt){
122 index = rand() % range + min_idx;
123 if (!idx_done.count(index)){
124 idx_done.insert(index);
129 image.index(ax) = index;
130 assert(idx_done.size() < range);
134 index = rand() % range + min_idx;
136 image.index(ax) = index;
149 operator bool()
const
151 return status && image.index(ax) >= 0 && image.index(ax) < image.size(ax);
164 std::unordered_set<ssize_t> idx_done;
167 template <
class ImageType,
class IterType>
173 const size_t&
axis = 0,
174 const size_t& number_iterations = std::numeric_limits<ssize_t>::max()):
179 max_cnt (number_iterations),
188 image.index(ax) = *start;
195 if (cnt > max_cnt or start == stop) {
199 operator bool()
const
201 return status && image.index(ax) >= 0 && image.index(ax) < image.size(ax);
Iterator_loop(ImageType &in, IterType first, IterType last, const size_t &axis=0, const size_t &number_iterations=std::numeric_limits< ssize_t >::max())
Random_loop(ImageType &in, RandomEngine &random_engine, const size_t &axis=0, const size_t &number_iterations=std::numeric_limits< ssize_t >::max())
void set_next_index_no_repeat()
Random_sparse_loop(ImageType &in, const size_t &axis=0, const size_t &number_iterations=std::numeric_limits< ssize_t >::max(), const bool repeat=false, const ssize_t &min_index=0, const ssize_t &max_index=std::numeric_limits< ssize_t >::max())
void set_next_index_with_repeat()