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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//! Generate random tournaments.
//!
//! A tournament is a digraph in which an arc connects every unordered pair of
//! distinct vertices.
//!
//! # Examples
//!
//! ```
//! use graaf::{
//!     adjacency_list::Digraph,
//!     gen::RandomTournament,
//!     op::{
//!         Degree,
//!         Indegree,
//!         Order,
//!         Outdegree,
//!         Size,
//!         Vertices,
//!     },
//! };
//!
//! let tournament = Digraph::random_tournament(4, 0);
//!
//! assert_eq!(tournament.size(), 6);
//! assert_eq!(tournament.order(), 4);
//!
//! for s in tournament.vertices() {
//!     assert_eq!(tournament.degree(s), 3);
//!     assert!((0..=3).contains(&tournament.outdegree(s)));
//!     assert!((0..=3).contains(&tournament.indegree(s)));
//! }
//! ```

use {
    super::prng::Xoshiro256StarStar,
    crate::{
        gen::Empty,
        op::AddArc,
    },
};

/// Generate random tournaments.
///
/// # Implementing `RandomTournament`
///
/// Provide an implementation of `random_tournament` that generates a random
/// tournament of a given `order` OR implement `AddArc` and `Empty`.
///
/// ```
/// use {
///     graaf::{
///         gen::{
///             prng::Xoshiro256StarStar,
///             Empty,
///             RandomTournament,
///         },
///         op::{
///             AddArc,
///             Degree,
///             Indegree,
///             Order,
///             Outdegree,
///             Size,
///             Vertices,
///         },
///     },
///     std::collections::BTreeSet,
/// };
///
/// pub struct Tournament {
///     arcs: Vec<BTreeSet<usize>>,
/// }
///
/// impl RandomTournament for Tournament {
///     fn random_tournament(order: usize, seed: u64) -> Self {
///         let mut rng = Xoshiro256StarStar::new(seed);
///
///         let mut tournament = Self {
///             arcs: vec![BTreeSet::new(); order],
///         };
///
///         for u in 0..order {
///             for v in (u + 1)..order {
///                 if rng.next_bool() {
///                     tournament.arcs[u].insert(v);
///                 } else {
///                     tournament.arcs[v].insert(u);
///                 }
///             }
///         }
///
///         tournament
///     }
/// }
///
/// let tournament = Tournament::random_tournament(4, 0);
///
/// assert_eq!(tournament.arcs.len(), 4);
///
/// assert_eq!(
///     tournament.arcs.iter().map(|set| set.len()).sum::<usize>(),
///     6
/// );
///
/// for u in 0..tournament.arcs.len() {
///     assert!((0..3).contains(&tournament.arcs[u].len()));
/// }
/// ```
///
/// # Examples
///
/// ```
/// use graaf::{
///     adjacency_list::Digraph,
///     gen::RandomTournament,
///     op::{
///         Degree,
///         Indegree,
///         Order,
///         Outdegree,
///         Size,
///         Vertices,
///     },
/// };
///
/// let tournament = Digraph::random_tournament(4, 0);
///
/// assert_eq!(tournament.size(), 6);
/// assert_eq!(tournament.order(), 4);
///
/// for s in tournament.vertices() {
///     assert_eq!(tournament.degree(s), 3);
///     assert!((0..=3).contains(&tournament.outdegree(s)));
///     assert!((0..=3).contains(&tournament.indegree(s)));
/// }
/// ```
pub trait RandomTournament {
    /// Generates a random tournament.
    ///
    /// # Arguments
    ///
    /// * `order` - The number of vertices in the tournament.
    /// * `seed` - The seed for the random number generator.
    ///
    /// # Examples
    ///
    /// ```
    /// use graaf::{
    ///     adjacency_list::Digraph,
    ///     gen::RandomTournament,
    ///     op::{
    ///         Degree,
    ///         Indegree,
    ///         Order,
    ///         Outdegree,
    ///         Size,
    ///         Vertices,
    ///     },
    /// };
    ///
    /// let tournament = Digraph::random_tournament(4, 0);
    ///
    /// assert_eq!(tournament.size(), 6);
    /// assert_eq!(tournament.order(), 4);
    ///
    /// for s in tournament.vertices() {
    ///     assert_eq!(tournament.degree(s), 3);
    ///     assert!((0..=3).contains(&tournament.outdegree(s)));
    ///     assert!((0..=3).contains(&tournament.indegree(s)));
    /// }
    /// ```
    #[must_use]
    fn random_tournament(order: usize, seed: u64) -> Self;
}

impl<D> RandomTournament for D
where
    D: AddArc + Empty,
{
    fn random_tournament(order: usize, seed: u64) -> Self {
        let mut digraph = Self::empty(order);
        let mut rng = Xoshiro256StarStar::new(seed);

        for u in 0..order {
            for v in (u + 1)..order {
                if rng.next_bool() {
                    digraph.add_arc(u, v);
                } else {
                    digraph.add_arc(v, u);
                }
            }
        }

        digraph
    }
}