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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! `WeightedRandomList` enables you to to randomly select elements from
//! your collection while
//!
//! # Example select mirror
//!
//! In this example we would like to give more traffic/weight to the the system
//! "mirror-fast", less to the slow mirrors and the least to the main archive:
//!
//! ```rust
//! extern crate weighted_random_list;
//!
//! use weighted_random_list::WeightedRandomList;
//!
//! fn main() {
//!     let list = [
//!         (1, "https://source.example.net/archive"),
//!         (10, "https://mirror-slow0.example.net/archive"),
//!         (10, "https://mirror-slow1.example.net/archive"),
//!         (100, "https://mirror-fast.example.net/archive"),
//!     ];
//!
//!     let mirrors = list.iter()
//!             .map(|(weight, url)| (*weight, url.to_string()))
//!             .collect::<WeightedRandomList<String>>();
//!
//!
//!     let random_choice = mirrors.get_random();
//!     println!("Using {:?} this time", random_choice);
//! }
//! ```

use std::iter::FromIterator;

use rand::Rng;

/// Stores the data and keeps track of the chances.
/// It is built so that you can add and remove elements withouth the need of
/// recalculation.
pub struct WeightedRandomList<T> {
    sum_weights: usize,
    data: Vec<(usize, T)>,
}

impl<T> WeightedRandomList<T> {
    pub fn new() -> WeightedRandomList<T> {
        WeightedRandomList {
            sum_weights: 0,
            data: Vec::new(),
        }
    }

    /// Add an element to the collection with a given weight
    ///
    /// Panics if the sum of the weights exceed the holding datatype
    pub fn push(&mut self, weight: usize, new: T) {
        self.sum_weights = self.sum_weights.checked_add(weight)
                                .expect("WeightedRandomList.push() sum of weights exceided data type");
        self.data.push((weight , new))
    }

    /// Get a random value from the collection or `None` if it is empty.
    ///
    /// Accsess time is `O(n)`, however the linear storage compensates for
    /// this overhead.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use weighted_random_list::WeightedRandomList;
    /// let mut l = WeightedRandomList::new();
    /// assert_eq!(None, l.get_random());
    ///
    /// l.push(100, 42usize);
    /// assert_eq!(Some(&42), l.get_random());
    /// ```
    pub fn get_random(&self) -> Option<&T> {
        if self.data.is_empty() {
            return None;
        }
        let r = rand::thread_rng().gen_range(0, self.sum_weights);

        let mut local_sum = 0;
        for (weight, data) in &self.data {
            local_sum += weight;
            if local_sum >= r {
                return Some(data)
            }
        }
        None
    }
}

/// If the stored type `T` supports `PartialEq` and `Clone` we can manipulate
/// the contents of this container.
/// Therefore shifting the chances of one element to be selected.
///
/// # Example adjust weight after insertion
///
/// ```rust
/// # use weighted_random_list::WeightedRandomList;
/// let mut l = WeightedRandomList::new();
/// l.push(10, 1);
/// l.push(10, 2);
/// l.push(100, 3);
/// // total weights is now 120
///
/// {
///     let mut three = l.entry_first(&3);
///     three.set_weight(80);
/// }
/// // total weights is now 100
/// ```
impl<T> WeightedRandomList<T> where T: std::cmp::PartialEq + Clone {

    /// Find the first entry in the list matching `needle` and manipulate
    /// potential entries:
    ///
    /// ```rust
    /// # use weighted_random_list::WeightedRandomList;
    /// let mut l = WeightedRandomList::new();
    ///
    /// l.entry_first(&42).or_insert_with_weight(10);
    /// ```
    pub fn entry_first(&mut self, needle: &T) -> Entry<T> {
        for (index, (_weight, data)) in self.data.iter().enumerate() {
            if needle == data {
                return Entry::Occupied {
                    arena: self,
                    index,
                }
            }
        }

        Entry::Vacant {
            arena: self,
            needle: needle.clone(),
        }
    }
}

/// Update or insert one entry inside the the list.
pub enum Entry<'container, T> {
    Vacant {
        arena: &'container mut WeightedRandomList<T>,
        needle: T,
    },
    Occupied {
        arena: &'container mut WeightedRandomList<T>,
        index: usize,
    }
}

impl<'container, T> Entry<'container, T> {
    /// Panics if `Entry` is `Vacant`
    /// ```rust
    /// # use weighted_random_list::WeightedRandomList;
    /// let mut l = WeightedRandomList::new();
    ///
    /// l.push(12, 42);
    /// let mut element = l.entry_first(&42).or_insert_with_weight(10);
    ///
    /// element.set_weight(100);
    /// ```
    pub fn set_weight(&mut self, new_weight: usize) {
        use Entry::*;
        match self {
            Vacant { .. } => { unimplemented!("set_weight on variant Vacant"); }
            Occupied { arena, index } => {
                let (weight, _data) = &mut arena.data
                    //[*index];
                    .get_mut(*index)
                    .expect("unable to directly access data from Entry view");
                arena.sum_weights -= *weight;
                arena.sum_weights += new_weight;
                *weight = new_weight;
            }
        }
    }

    /// Inserts if `Entry` is `Vacant`.
    /// Does nothing if found inside the list.

    /// ```rust
    /// # use weighted_random_list::WeightedRandomList;
    /// let mut l = WeightedRandomList::new();
    ///
    /// // add 42 with weight 10
    /// l.entry_first(&42).or_insert_with_weight(10);
    /// ```
    pub fn or_insert_with_weight(self, weight: usize) -> Self {
        use Entry::*;
        match self {
            Occupied { .. } => { self }
            Vacant { arena, needle } => {
                let index = arena.data.len();
                arena.push(weight, needle);

                Occupied {
                    arena,
                    index,
                }
            }
        }
    }

    /// Remove `Entry` if it exists.
    ///
    /// Errors if it does not exist.
    ///
    /// ```rust
    /// # use weighted_random_list::WeightedRandomList;
    /// let mut l = WeightedRandomList::new();
    /// l.push(12, 11);
    /// l.push(74, 22);
    /// l.push(25, 33);
    ///
    /// {
    ///     let second = l.entry_first(&22);
    ///     assert!(second.delete().is_ok(), "element was found and deleted");
    /// }
    ///
    /// {
    ///     let second = l.entry_first(&22);
    ///     assert!(second.delete().is_err(), "element alread gone");
    /// }
    /// ```
    pub fn delete(self) -> Result<(), ()> {
        use Entry::*;
        match self {
            Vacant { .. } => { Err(()) }
            Occupied { arena, index } => {
                let len = arena.data.len();
                if len != (index + 1) {
                    arena.data.swap(index, len - 1);
                }
                let (weight, _data) = arena.data.pop().expect("must be the last element");
                arena.sum_weights -= weight;
                Ok(())
            }
        }
    }
}

/// Enable `clollect`
impl<T> FromIterator<(usize, T)> for WeightedRandomList<T> {
    fn from_iter<I: IntoIterator<Item=(usize, T)>>(iter: I) -> Self {
        let mut c = WeightedRandomList::new();

        for (weight, data) in iter {
            c.push(weight, data);
        }

        c
    }
}
/* TODO implement in a way to be able to collect &str from static list
impl<'a, T: 'a> FromIterator<&'a(usize, &T)> for WeightedRandomList<T> {
    fn from_iter<I: IntoIterator<Item=&'a (usize, &T)>>(iter: I) -> Self {
        let mut c = WeightedRandomList::new();

        for (weight, data) in iter {
            c.push(*weight, *data);
        }

        c
    }
}
*/

#[cfg(test)]
mod tests {
    use crate::*;
    use std::collections::HashMap;

    #[test]
    fn simple_inserts() {
        let mut l = WeightedRandomList::new();
        assert_eq!(None, l.get_random());

        l.push(100, 42usize);
        assert_eq!(Some(&42), l.get_random());

        l.push(0, 23);
        for _ in 0..1000 {
            assert_eq!(Some(&42), l.get_random());
        }

        l.push(900, 12);
        let results = (0..1000).map(|_| l.get_random().unwrap().clone())
                .fold(HashMap::new(), |mut map, n| {
                    let count = map.entry(n).or_insert(0);
                    *count += 1;
                    map
                });
        assert!(results[&42] < results[&12]);
    }

    #[test]
    fn modify_weight() {
        let mut l = WeightedRandomList::new();
        l.push(10, 1);
        l.push(10, 2);
        l.push(100, 3);
        assert_eq!(120, l.sum_weights);

        {
            let mut three = l.entry_first(&3);
            three.set_weight(80);
        }
        assert_eq!(100, l.sum_weights);

        let results = (0..1000).map(|_| l.get_random().unwrap().clone())
                .fold(HashMap::new(), |mut map, n| {
                    let count = map.entry(n).or_insert(0);
                    *count += 1;
                    map
                });
        assert!(results[&1] + results[&2] < results[&3]);
    }

    #[test]
    fn or_insert() {
        let mut l = WeightedRandomList::new();

        l.entry_first(&1).or_insert_with_weight(10);

        {
            assert_eq!(10, l.sum_weights);
            let mut two = l.entry_first(&2).or_insert_with_weight(1);
            two.set_weight(10);
            assert_eq!(20, l.sum_weights);
        }

        l.entry_first(&3).or_insert_with_weight(80);

        let results = (0..1000).map(|_| l.get_random().unwrap().clone())
                .fold(HashMap::new(), |mut map, n| {
                    let count = map.entry(n).or_insert(0);
                    *count += 1;
                    map
                });
        assert!(results[&1] + results[&2] < results[&3]);
    }

    #[test]
    fn delete_last() {
        let mut l = WeightedRandomList::new();
        l.push(100, 3);
        assert_eq!(1, l.data.len());
        assert_eq!(100, l.sum_weights);

        {
            let three = l.entry_first(&3);
            assert!(three.delete().is_ok());
        }
        assert_eq!(0, l.sum_weights);
        assert_eq!(0, l.data.len());
    }
    #[test]
    fn delete_mid() {
        let mut l = WeightedRandomList::new();
        assert_eq!(0, l.data.len());
        l.push(10, 1);
        assert_eq!(1, l.data.len());
        l.push(10, 2);
        assert_eq!(2, l.data.len());
        l.push(100, 3);
        assert_eq!(3, l.data.len());
        assert_eq!(120, l.sum_weights);

        {
            let two = l.entry_first(&2);
            assert!(two.delete().is_ok());
        }
        assert_eq!(110, l.sum_weights); assert_eq!(2, l.data.len());
    }
    #[test]
    fn delete_end() {
        let mut l = WeightedRandomList::new();
        assert_eq!(0, l.data.len());
        l.push(10, 1);
        assert_eq!(1, l.data.len());
        l.push(10, 2);
        assert_eq!(2, l.data.len());
        l.push(100, 3);
        assert_eq!(3, l.data.len());
        assert_eq!(120, l.sum_weights);

        {
            let three = l.entry_first(&3);
            assert!(three.delete().is_ok());
        }
        assert_eq!(20, l.sum_weights);
        assert_eq!(2, l.data.len());
    }
    #[test]
    fn delete_not_found() {
        let mut l = WeightedRandomList::new();
        assert_eq!(0, l.data.len());
        l.push(10, 1);
        assert_eq!(1, l.data.len());
        l.push(10, 2);
        assert_eq!(2, l.data.len());
        l.push(100, 3);
        assert_eq!(3, l.data.len());
        assert_eq!(120, l.sum_weights);

        {
            let absent = l.entry_first(&22);
            assert!(absent.delete().is_err());
        }
        assert_eq!(120, l.sum_weights);
        assert_eq!(3, l.data.len());
    }

    #[test]
    fn collect() {
        // make sure this compiles
        let list = [
            (1, "https://source.example.net/archive"),
            (10, "https://mirror-slow0.example.net/archive"),
            (10, "https://mirror-slow1.example.net/archive"),
            (100, "https://mirror-fast.example.net/archive"),
        ];

        let collected = list.iter()
                .map(|(weight, url)| (*weight, url.to_string()))
                .collect::<WeightedRandomList<String>>();

        assert_eq!(list.len(), collected.data.len());
        for ((w, url), (wc, urlc)) in list.iter().zip(collected.data.iter()) {
            assert_eq!(w, wc);
            assert_eq!(url, urlc);
        }
        assert_eq!(list.iter().map(|(w,_)| w).sum::<usize>(), collected.sum_weights);
    }
}