Skip to content

Commit

Permalink
shuffle(begin, end), choice(begin, end)
Browse files Browse the repository at this point in the history
  • Loading branch information
ifsmirnov committed Mar 13, 2017
1 parent 1eea28b commit 11a3bbb
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 13 deletions.
11 changes: 5 additions & 6 deletions array.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "common.h"
#include "printers.h"
#include "random.h"
#include "sequence_ops.h"

#include <algorithm>
#include <initializer_list>
Expand Down Expand Up @@ -84,7 +85,7 @@ class GenericArray : public ReprProxy<GenericArray<T>>, public std::vector<T> {
GenericArray<T> subseq(
const std::initializer_list<Integer>& indices) const;

const T& choice() const;
T choice() const;
GenericArray<T> choice(size_t count) const;
GenericArray<T> choiceWithRepetition(size_t count) const;

Expand Down Expand Up @@ -191,9 +192,7 @@ GenericArray<T> GenericArray<T>::id(size_t size, T start) {

template<typename T>
GenericArray<T>& GenericArray<T>::shuffle() {
for (size_t i = 1; i < size(); ++i) {
std::swap(at(i), at(rnd.next(i + 1)));
}
jngen::shuffle(begin(), end());
return *this;
}

Expand Down Expand Up @@ -304,8 +303,8 @@ GenericArray<T> GenericArray<T>::subseq(
}

template<typename T>
const T& GenericArray<T>::choice() const {
return at(rnd.next(size()));
T GenericArray<T>::choice() const {
return jngen::choice(begin(), end());
}

template<typename T>
Expand Down
5 changes: 4 additions & 1 deletion build.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ def direct_deps(filename):
return res


unused_files = map(str.strip, open(".unused_files").readlines())
try:
unused_files = set(map(str.strip, open(".unused_files").readlines()))
except IOError:
unused_files = {}


deps = {}
Expand Down
37 changes: 31 additions & 6 deletions jngen.h
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,33 @@ auto operator<<(std::ostream& out, const T& t)
using namespace jngen::namespace_for_fake_operator_ltlt;


#include <algorithm>

namespace jngen {

// TODO: deprecate random_shuffle as done in testlib.h

template<typename Iterator>
void shuffle(Iterator begin, Iterator end) {
ensure(end > begin, "Cannot shuffle range of negative length");
size_t size = end - begin;
for (size_t i = 1; i < size; ++i) {
std::swap(*(begin + i), *(begin + rnd.next(i + 1)));
}
}

template<typename Iterator>
typename Iterator::value_type choice(Iterator begin, Iterator end) {
ensure(end > begin, "Cannot select from a range of negative length");
return *(begin + rnd.next(end - begin));
}

} // namespace jngen

using jngen::shuffle;
using jngen::choice;


#include <algorithm>
#include <initializer_list>
#include <numeric>
Expand Down Expand Up @@ -1100,7 +1127,7 @@ class GenericArray : public ReprProxy<GenericArray<T>>, public std::vector<T> {
GenericArray<T> subseq(
const std::initializer_list<Integer>& indices) const;

const T& choice() const;
T choice() const;
GenericArray<T> choice(size_t count) const;
GenericArray<T> choiceWithRepetition(size_t count) const;

Expand Down Expand Up @@ -1207,9 +1234,7 @@ GenericArray<T> GenericArray<T>::id(size_t size, T start) {

template<typename T>
GenericArray<T>& GenericArray<T>::shuffle() {
for (size_t i = 1; i < size(); ++i) {
std::swap(at(i), at(rnd.next(i + 1)));
}
jngen::shuffle(begin(), end());
return *this;
}

Expand Down Expand Up @@ -1320,8 +1345,8 @@ GenericArray<T> GenericArray<T>::subseq(
}

template<typename T>
const T& GenericArray<T>::choice() const {
return at(rnd.next(size()));
T GenericArray<T>::choice() const {
return jngen::choice(begin(), end());
}

template<typename T>
Expand Down
30 changes: 30 additions & 0 deletions sequence_ops.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include "common.h"
#include "random.h"

#include <algorithm>

namespace jngen {

// TODO: deprecate random_shuffle as done in testlib.h

template<typename Iterator>
void shuffle(Iterator begin, Iterator end) {
ensure(end > begin, "Cannot shuffle range of negative length");
size_t size = end - begin;
for (size_t i = 1; i < size; ++i) {
std::swap(*(begin + i), *(begin + rnd.next(i + 1)));
}
}

template<typename Iterator>
typename Iterator::value_type choice(Iterator begin, Iterator end) {
ensure(end > begin, "Cannot select from a range of negative length");
return *(begin + rnd.next(end - begin));
}

} // namespace jngen

using jngen::shuffle;
using jngen::choice;

0 comments on commit 11a3bbb

Please sign in to comment.