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
/*
 * Copyright 2017 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.
///
#[derive(Debug)]
pub struct WrappingHashSet<T>
where
    T: Eq + Hash,
{
    hashset: HashSet<T>,
    keys: Vec<T>,
    pos: usize,
    count: usize,
}

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

impl<'i, T> Iterator for Iter<'i, T>
where
    T: Eq + Hash + Clone,
{
    type Item = T;
    fn next(&mut self) -> Option<T> {
        self.whs.pos += 1;
        self.whs.count += 1;
        if self.whs.count > self.whs.hashset.len() {
            self.whs.pos = 0;
            self.whs.count = 0;
            return None;
        }
        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,
            count: 0,
        }
    }

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

    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();

        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);
            break;
        }
    }
    {
        for i in hs.iter() {
            assert_eq!(keys_as_found[1], i);
            break;
        }
    }
    {
        for i in hs.iter() {
            assert_eq!(keys_as_found[2], i);
            break;
        }
    }
    {
        for i in hs.iter() {
            panic!("We should have gotten NONE, instead we got {:?}", i);
        }
    }
    {
        for i in hs.iter() {
            assert_eq!(keys_as_found[0], i);
            break;
        }
    }
    hs.remove(&keys_as_found[1]);
    {
        let mut j = 0;
        for i in hs.iter() {
            assert_ne!(keys_as_found[1], i);
            j = j + 1;
        }
        assert_eq!(1, j);
    }
}