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
use std::mem;

use stable_id_traits::{Maximum, Predecessor, Successor};

use crate::Eids;

impl<IndexT> Eids<IndexT>
where
    IndexT: Successor + Predecessor + Clone + Copy + Ord + Maximum,
{
    pub fn claim(&mut self) -> IndexT {
        assert!(
            self.next < IndexT::max_value(),
            "storing more items than you can address"
        );

        self.freed
            .iter()
            .next()
            .cloned()
            .map(|id| {
                // found an id in the free list, return it
                let is_removed = self.freed.remove(&id);
                debug_assert!(is_removed, "freeing something not in the database");
                id
            })
            .unwrap_or_else(|| {
                // otherwise increment the id and return it
                let next = self.next.next_value();
                mem::replace(&mut self.next, next)
            })
    }

    pub fn unclaim(&mut self, val: IndexT) {
        assert!(val < self.next, "not a valid entity");

        let is_double_inserted = self.freed.insert(val);
        debug_assert!(is_double_inserted, "double-freeing entity")
    }

    /**
        Pack up recycled ids from the freed list while you deal with the change through `f(old_id, new_id)`.

        ```
        use stable_id::Eids;

        let mut entities: Eids<u8> = Default::default();
        (0..255).for_each(|i| {
            assert_eq!(entities.claim(), i);
        });

        entities.unclaim(27);
        entities.unclaim(15);

        // free up 254 to 251, so that coalescing would be 15 & 27 to takeover 249, 250
        entities.unclaim(254);
        entities.unclaim(252);
        entities.unclaim(251);
        entities.unclaim(253);

        let mut records_old = Vec::new();
        let mut records_new = Vec::new();

        entities.coalesce(|old_id, new_id| {
            records_old.push(old_id);
            records_new.push(new_id);

            // update all data that reference the old_id and replace them with new_id
        });

        assert_eq!(records_old, [250,249]); // reclaiming from the last-issued
        assert_eq!(records_new, [27,15]); // note: larger ids come first
        ```
    */
    pub fn coalesce<F>(&mut self, mut f: F)
    where
        F: FnMut(IndexT, IndexT),
    {
        if self.freed.is_empty() {
            return;
        }

        while let Some(freed) = self.freed.pop_last() {
            let target = self.next.prev_value();
            self.next = target;

            if target != freed {
                f(target, freed);
            }
        }
    }
}

#[cfg(test)]
mod eid_tests {
    use super::Eids;

    #[test]
    fn claim_ids() {
        let mut entities: Eids<u8> = Default::default();
        for i in 0..100 {
            let id = entities.claim();
            assert_eq!(id, i);
        }

        fn is_multiple_of_3(i: &u8) -> bool {
            i % 3 == 0
        }

        (0..60u8)
            .filter(is_multiple_of_3)
            .for_each(|i| entities.unclaim(i));

        (0..60u8)
            .filter(is_multiple_of_3)
            .all(|i| entities.claim() == i);
    }

    #[test]
    #[should_panic]
    fn unclaim_invalid() {
        let mut entities: Eids<u8> = Default::default();
        entities.unclaim(123u8)
    }

    #[test]
    #[should_panic]
    fn double_free() {
        let mut entities: Eids<u8> = Default::default();
        let id = entities.claim();
        entities.unclaim(id);
        entities.unclaim(id);
    }

    #[test]
    #[should_panic]
    fn claim_over_max() {
        let mut entities: Eids<u8> = Default::default();
        (0..257).for_each(|_| {
            entities.claim();
        });
    }
}