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
/*
 * Copyright 2017, 2024 Clint Byrum
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
use std::clone::Clone;
use std::cmp::Eq;
use std::collections::HashSet;
use std::hash::Hash;
use std::iter::Iterator;

/// A hash set that remembers the last key it returned with its iterator
/// it will wrap around and only return all of the keys once per iterator
///
/// Important: Prior to version 0.5 all iterators shared the count, so
/// if you only partilaly read one iterator, it would affect the next one.
/// As of 0.5 and forward, the position is remembered, but the count is
/// forgotten. This may break your app if you are depending on the old
/// behavior.
#[derive(Debug)]
pub struct WrappingHashSet<T>
where
    T: Eq + Hash,
{
    hashset: HashSet<T>,
    keys: Vec<T>,
    pos: usize,
}

pub struct Iter<'i, T: 'i>
where
    T: Eq + Hash,
{
    whs: &'i mut WrappingHashSet<T>,
    count: usize,
}

impl<'i, T> Iterator for Iter<'i, T>
where
    T: Eq + Hash + Clone,
{
    type Item = T;
    fn next(&mut self) -> Option<T> {
        // Wrap
        if self.whs.pos >= self.whs.hashset.len() {
            self.whs.pos = 0;
        }
        self.count += 1;
        if self.count > self.whs.hashset.len() {
            self.count = 0;
            return None;
        }
        self.whs.pos += 1;
        Some(self.whs.keys[self.whs.pos - 1].clone())
    }
}

impl<T> WrappingHashSet<T>
where
    T: Eq + Hash + Clone,
{
    pub fn new() -> WrappingHashSet<T> {
        WrappingHashSet {
            hashset: HashSet::new(),
            keys: Vec::new(),
            pos: 0,
        }
    }

    pub fn iter<'i>(&'i mut self) -> Iter<'i, T> {
        Iter {
            whs: self,
            count: 0,
        }
    }

    pub fn insert(&mut self, key: T) -> bool {
        if self.hashset.insert(key.clone()) {
            self.keys.push(key);
            return true;
        }
        return false;
    }

    pub fn remove<'b>(&mut self, key: &'b T) -> bool {
        if self.hashset.remove(key) {
            self.keys = Vec::new();
            for k in self.hashset.iter() {
                self.keys.push(k.clone())
            }
            return true;
        }
        return false;
    }
}

#[test]
fn test_wrapping_hashset() {
    let mut hs: WrappingHashSet<&str> = WrappingHashSet::new();
    let mut keys_as_found: Vec<&str> = Vec::new();
    hs.insert("foo");
    hs.insert("bar");
    hs.insert("baz");
    {
        for i in hs.iter() {
            keys_as_found.push(i);
        }
        let mut z = keys_as_found.clone();
        z.sort();
        // It returned all 3
        assert_eq!("bar", z[0]);
        assert_eq!("baz", z[1]);
        assert_eq!("foo", z[2]);
    }
    // Now test wrap
    {
        for i in hs.iter() {
            assert_eq!(keys_as_found[0], i, "First Iter returns first element");
            break;
        }
    }
    {
        for i in hs.iter() {
            assert_eq!(
                keys_as_found[1], i,
                "Second Iter returns second element first"
            );
            break;
        }
    }
    {
        for i in hs.iter() {
            assert_eq!(
                keys_as_found[2], i,
                "Third Iter returns third element first"
            );
            break;
        }
    }
    // Now it should wrap because we have a new iterator
    {
        for i in hs.iter() {
            assert_eq!(
                keys_as_found[0], i,
                "Fourth Iter returns first element first"
            );
            break;
        }
    }
    {
        let mut iter = hs.iter();
        assert_eq!(Some(keys_as_found[1]), iter.next());
        assert_eq!(Some(keys_as_found[2]), iter.next());
        assert_eq!(Some(keys_as_found[0]), iter.next());
        assert_eq!(None, iter.next(), "Should wrap only once");
    }
    {
        let mut iter = hs.iter();
        assert_eq!(Some(keys_as_found[1]), iter.next());
        assert_eq!(Some(keys_as_found[2]), iter.next());
        assert_eq!(Some(keys_as_found[0]), iter.next());
        assert_eq!(None, iter.next(), "Should repeat");
    }
    // Now use it partially
    {
        let mut iter = hs.iter();
        assert_eq!(Some(keys_as_found[1]), iter.next());
    }
    // Still picks up where it was
    {
        let mut iter = hs.iter();
        assert_eq!(Some(keys_as_found[2]), iter.next());
    }
    hs.remove(&keys_as_found[1]);
    {
        let mut j = 0;
        for i in hs.iter() {
            assert_ne!(keys_as_found[1], i, "Elements should not reappear");
            j = j + 1;
        }
        assert_eq!(2, j, "We should only iterate the leftover elements");
    }
}

#[test]
fn test_empty() {
    let mut hs: WrappingHashSet<&str> = WrappingHashSet::new();
    {
        let mut hsiter = hs.iter();
        assert_eq!(None, hsiter.next());
    }
    hs.insert("one");
    hs.insert("two");
    let mut hsiter = hs.iter();
    assert_eq!(Some("one"), hsiter.next());
    assert_eq!(Some("two"), hsiter.next());
    assert_eq!(None, hsiter.next());
}

#[test]
fn test_one_item() {
    let mut hs: WrappingHashSet<&str> = WrappingHashSet::new();
    hs.insert("onething");
    {
        let mut hsiter = hs.iter();
        assert_eq!(Some("onething"), hsiter.next());
        assert_eq!(None, hsiter.next());
    }
    {
        let mut _hsunused = hs.iter();
        // We never call this so pos should stay where it was
    }
    {
        let mut hsiter = hs.iter();
        assert_eq!(Some("onething"), hsiter.next());
        assert_eq!(None, hsiter.next());
    }
}