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
use crate::generic_containers::*;
use core::borrow::Borrow;
use core::ops::{Index, IndexMut};

/// Trait for a container indexed by a value that implements `Copy` and `Eq`.
pub trait CopyMap<K, V>: Container
where
    K: Copy + Eq,
{
    /// Get a reference to a value from this map.
    ///
    /// Takes a key by value
    /// and returns a reference to the corresponding data,
    /// or `None` if the key doesn't exist in the map.
    fn get(&self, key: K) -> Option<&V>;

    /// Get a mutable reference to a value from this map.
    ///
    /// Takes a key by value
    /// and returns a mutable reference to the corresponding data,
    /// or `None` if the key doesn't exist in the map.
    fn get_mut(&mut self, key: K) -> Option<&mut V>;

    /// Inserts a key-value pair into the map.
    ///
    /// If the map did not have this key present, None is returned.
    ///
    /// If the map did have this key present, the value is updated, and the old
    /// value is returned. Note that the key itself isn't necessarily updated.
    fn insert(&mut self, k: K, v: V) -> Option<V>;
}

/// Trait for a container indexed by a value that implements `Eq`.
pub trait Map<K, V>: Container
where
    K: Eq,
{
    /// Returns a reference to a value from this Map.
    ///
    /// Takes a key by reference
    /// and returns a reference to the corresponding data,
    /// or `None` if the key doesn't exist in the map.
    fn get<Q: ?Sized>(&self, key: &Q) -> Option<&V>
    where
        K: Borrow<Q>,
        Q: Eq;

    /// Returns a mutable reference to a value in this map.
    ///
    /// Takes a key by reference
    /// and returns a mutable reference to the corresponding data,
    /// or `None` if the key doesn't exist in the map.
    fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V>
    where
        K: Borrow<Q>,
        Q: Eq;

    /// Inserts a key-value pair into the map.
    ///
    /// If the map did not have this key present, None is returned.
    ///
    /// If the map did have this key present, the value is updated, and the old
    /// value is returned. Note that the key itself isn't necessarily updated.
    fn insert(&mut self, k: K, v: V) -> Option<V>;
}

/// Key-value map that can dynamically change size, indexed by a key that implements
/// `Copy`.
pub trait CopyDictionary<K, V>: CopyMap<K, V> + DynamicContainer
where
    K: Copy + Eq,
{
    /// Returns true if this container contains the key.
    fn contains(&self, key: K) -> bool {
        match self.get(key) {
            Some(_) => true,
            None => false,
        }
    }

    /// Removes an item from this container using the associated key,
    /// and returns it (if it existed).
    fn remove(&mut self, k: K) -> Option<V>;
}

/// Key-value map that can dynamically change size.
pub trait Dictionary<K, V>: Map<K, V> + DynamicContainer
where
    K: Eq,
{
    /// Returns true if this container contains the key.
    fn contains<Q: ?Sized>(&self, key: &Q) -> bool
    where
        K: Borrow<Q>,
        Q: Eq,
    {
        match self.get(key) {
            Some(_) => true,
            None => false,
        }
    }

    /// Removes an item from this container using the associated key,
    /// and returns it (if it existed).
    fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<V>
    where
        K: Borrow<Q>,
        Q: Eq;
}

/// Statically-sized array of values.
pub trait Array<V>:
    CopyMap<usize, V> + Index<usize, Output = V> + IndexMut<usize, Output = V>
{
}

/// Dynamically-sized array of values.
pub trait DynamicArray<V>:
    CopyMap<usize, V> + DynamicContainer + Index<usize, Output = V> + IndexMut<usize, Output = V>
{
}