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
use crate::list::KeyNodeList;
use crate::map::Map;
use crate::node::Node;
use std::hash::Hash;

/// An owning iterator over the key-node paris of a [`KeyNodeList`].
pub struct IntoIter<K, N, M> {
  pub(crate) list: KeyNodeList<K, N, M>,
}

impl<K, N, M> Iterator for IntoIter<K, N, M>
where
  K: Hash + Eq + Clone,
  N: Node<Key = K>,
  M: Map<K, N>,
{
  type Item = (K, N);

  #[inline]
  fn next(&mut self) -> Option<Self::Item> {
    self.list.pop_front()
  }
}

/// An owning iterator over the keys of a [`KeyNodeList`].
pub struct IntoKeys<K, N, M> {
  pub(crate) iter: IntoIter<K, N, M>,
}

impl<K, N, M> Iterator for IntoKeys<K, N, M>
where
  K: Hash + Eq + Clone,
  N: Node<Key = K>,
  M: Map<K, N>,
{
  type Item = K;

  #[inline]
  fn next(&mut self) -> Option<Self::Item> {
    self.iter.next().map(|(k, _)| k)
  }
}

/// An owning iterator over the nodes of a [`KeyNodeList`].
pub struct IntoNodes<K, N, M> {
  pub(crate) iter: IntoIter<K, N, M>,
}

impl<K, N, M> Iterator for IntoNodes<K, N, M>
where
  K: Hash + Eq + Clone,
  N: Node<Key = K>,
  M: Map<K, N>,
{
  type Item = N;

  #[inline]
  fn next(&mut self) -> Option<Self::Item> {
    self.iter.next().map(|(_, n)| n)
  }
}

/// An iterator over the key-node pairs of a [`KeyNodeList`].
pub struct Iter<'a, K, N, M> {
  pub(crate) list: &'a KeyNodeList<K, N, M>,
  pub(crate) key: Option<&'a K>,
}

impl<'a, K, N, M> Iterator for Iter<'a, K, N, M>
where
  K: Hash + Eq,
  N: Node<Key = K>,
  M: Map<K, N>,
{
  type Item = (&'a K, &'a N);

  #[inline]
  fn next(&mut self) -> Option<Self::Item> {
    self.key.and_then(|k| {
      self.list.node(k).map(|n| {
        self.key = n.next();
        (k, n)
      })
    })
  }
}

/// An iterator over the keys of a [`KeyNodeList`].
pub struct Keys<'a, K, N, M> {
  pub(crate) iter: Iter<'a, K, N, M>,
}

impl<'a, K, N, M> Iterator for Keys<'a, K, N, M>
where
  K: Hash + Eq,
  N: Node<Key = K>,
  M: Map<K, N>,
{
  type Item = &'a K;

  #[inline]
  fn next(&mut self) -> Option<Self::Item> {
    self.iter.next().map(|(k, _)| k)
  }
}

/// An iterator over the nodes of a [`KeyNodeList`].
pub struct Nodes<'a, K, N, M> {
  pub(crate) iter: Iter<'a, K, N, M>,
}

impl<'a, K, N, M> Iterator for Nodes<'a, K, N, M>
where
  K: Hash + Eq,
  N: Node<Key = K>,
  M: Map<K, N>,
{
  type Item = &'a N;

  #[inline]
  fn next(&mut self) -> Option<Self::Item> {
    self.iter.next().map(|(_, n)| n)
  }
}