WARNING! Access to this system is limited to authorised users only.
Unauthorised users may be subject to prosecution.
Unauthorised access to this system is a criminal offence under Australian law (Federal Crimes Act 1914 Part VIA)
It is a criminal offence to:
(1) Obtain access to data without authority. -Penalty 2 years imprisonment.
(2) Damage, delete, alter or insert data without authority. -Penalty 10 years imprisonment.
User activity is monitored and recorded. Anyone using this system expressly consents to such monitoring and recording.

To protect your data, the CISO officer has suggested users to enable 2FA as soon as possible.
Currently 2.6% of users enabled 2FA.

linked_hashset.rs 2.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
extern crate linked_hash_map;

use std::collections::hash_map::RandomState;
use std::hash::{BuildHasher, Hash};
use std::borrow::Borrow;

use self::linked_hash_map::Keys;
pub use self::linked_hash_map::LinkedHashMap;

pub struct LinkedHashSet<K, S = RandomState>(LinkedHashMap<K, (), S>);

impl<K: Hash + Eq> LinkedHashSet<K> {
    pub fn new() -> Self {
        LinkedHashSet(LinkedHashMap::new())
    }
16 17 18 19 20 21 22 23 24 25
    
    pub fn from_vec(from: Vec<K>) -> Self {
        let mut ret = LinkedHashSet::new();
        
        for ele in from {
            ret.insert(ele);
        }
        
        ret
    }
qinsoon's avatar
qinsoon committed
26

27 28 29 30 31 32 33 34 35 36
    pub fn to_vec(mut self) -> Vec<K> {
        let mut ret = vec![];

        while !self.is_empty() {
            ret.push(self.pop_front().unwrap());
        }

        ret
    }

qinsoon's avatar
qinsoon committed
37 38 39
    pub fn clear(&mut self) {
        self.0.clear();
    }
40 41 42
}

impl<K: Hash + Eq, S: BuildHasher> LinkedHashSet<K, S> {
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    pub fn len(&self) -> usize {
        self.0.len()
    }

    pub fn pop_front(&mut self) -> Option<K> {
        match self.0.pop_front() {
            Some((k, _)) => Some(k),
            None => None
        }
    }

    pub fn pop_back(&mut self) -> Option<K> {
        match self.0.pop_back() {
            Some((k, _)) => Some(k),
            None => None
        }
    }

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
    pub fn insert(&mut self, k: K) -> Option<()> {
        self.0.insert(k, ())
    }

    pub fn contains<Q: ?Sized>(&self, k: &Q) -> bool
        where K: Borrow<Q>,
              Q: Eq + Hash
    {
        self.0.contains_key(k)
    }

    pub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<()>
        where K: Borrow<Q>,
              Q: Eq + Hash
    {
        self.0.remove(k)
    }

    pub fn iter(&self) -> Keys<K, ()> {
        self.0.keys()
    }
    
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
    
    pub fn add_all(&mut self, mut other: Self) {
        while !other.is_empty() {
            let entry = other.pop_front().unwrap();
            self.insert(entry);
        }
    }
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

    pub fn add_from_vec(&mut self, mut vec: Vec<K>) {
        while !vec.is_empty() {
            self.insert(vec.pop().unwrap());
        }
    }

    pub fn equals(&self, other: &Self) -> bool {
        if self.len() != other.len() {
            return false;
        }

        for ele in self.iter() {
            if !other.contains(ele) {
                return false;
            }
        }

        true
    }
113 114 115 116 117 118 119 120 121 122 123 124 125 126
}

impl<K: Hash + Eq + Clone> Clone for LinkedHashSet<K> {
    fn clone(&self) -> Self {
        LinkedHashSet(self.0.clone())
    }
}

use std::fmt;
impl<A: fmt::Debug + Hash + Eq, S: BuildHasher> fmt::Debug for LinkedHashSet<A, S> {
    /// Returns a string that lists the key-value pairs in insertion order.
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_set().entries(self.iter()).finish()
    }
127
}