-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathshuffle.h
74 lines (56 loc) · 1.91 KB
/
shuffle.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#ifndef __ASHUFFLE_SHUFFLE_H__
#define __ASHUFFLE_SHUFFLE_H__
#include <deque>
#include <random>
#include <string>
#include <string_view>
#include <vector>
namespace ashuffle {
class ShuffleChain;
class ShuffleItem {
public:
template <typename T>
ShuffleItem(T v) : ShuffleItem(std::vector<std::string>{v}){};
ShuffleItem(std::vector<std::string> uris) : _uris(uris){};
private:
std::vector<std::string> _uris;
friend class ShuffleChain;
};
class ShuffleChain {
public:
// By default, create a new shuffle chain with a window-size of 1.
ShuffleChain() : ShuffleChain(1){};
// Create a new ShuffleChain with the given window length.
explicit ShuffleChain(size_t window) : _max_window(window) {
std::random_device rd;
_rng.seed(rd());
}
// Create a new ShuffleChain with the given window length
// and using the given RandomNumberEngine
ShuffleChain(size_t window, std::mt19937 rng)
: _max_window(window), _rng(rng) {}
// Clear this shuffle chain, removing anypreviously added songs.
void Clear();
// Add a string to the pool of songs that can be picked out of this
// chain.
void Add(ShuffleItem i);
// Return the total number of Items (groups) in this chain.
size_t Len() const;
// Return the total number of URIs in this chain, in all items.
size_t LenURIs() const;
// Pick a group of songs out of this chain.
const std::vector<std::string>& Pick();
// Items returns a vector of all items in this chain. This operation is
// extremely heavyweight, since it copies most of the storage used by
// the chain. Use with caution.
std::vector<std::vector<std::string>> Items();
private:
void FillWindow();
size_t _max_window;
std::vector<ShuffleItem> _items;
std::deque<size_t> _window;
std::deque<size_t> _pool;
std::mt19937 _rng;
};
} // namespace ashuffle
#endif