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
//! Frontier for the Dijkstra algorithm.

use std::cmp::Reverse;
use std::collections::HashMap;
use std::hash::Hash;

use super::numtraits::IntoOrd;

/// Frontier for the Dijkstra algorithm.
/// Internally uses binary search to maintain priority queue and hashmap-based index.
pub(super) struct Frontier<T, W> {
    /// Stores items in cost-descending order,
    /// so the item with the lowest cost is placed at the end of the array.
    queue: Vec<(T, W)>,

    /// Links node tag `<T>` to the index in the `queue`.
    index: HashMap<T, usize>,
}

impl<T, W> Frontier<T, W>
where
    T: Clone + Ord + Eq + Hash,
    W: Clone + PartialOrd + IntoOrd,
{
    /// Create new empty instance.
    pub fn new() -> Frontier<T, W> {
        Frontier {
            queue: Vec::new(),
            index: HashMap::new(),
        }
    }

    /// Sorts by cost in descending order, then by tag.
    fn key_fn(item: &(T, W)) -> impl Ord {
        let (t, w) = item;
        (Reverse(w.clone().into_ord()), t.clone())
    }

    /// Find position in the queue where to insert a new item.
    fn insertion_pos(&self, item: &(T, W)) -> usize {
        match self.queue.binary_search_by_key(&Self::key_fn(item), Self::key_fn) {
            Ok(index) | Err(index) => index,
        }
    }

    /// Insert a node with associated cost into the priority queue.
    pub fn push(&mut self, tag: T, weight: W) {
        let item = (tag.clone(), weight);
        let index = self.insertion_pos(&item);
        self.queue.insert(index, item);
        self.index.iter_mut().for_each(|(_, old_idx)| {
            if *old_idx >= index {
                *old_idx += 1;
            }
        });
        self.index.insert(tag, index);
    }

    /// Extract node with the least cost from the queue.
    pub fn pop(&mut self) -> Option<(T, W)> {
        if let Some((tag, weight)) = self.queue.pop() {
            // Since pop() removed the item with the highest index,
            // no need to correct any other items in index.
            self.index.remove(&tag);
            Some((tag, weight))
        } else {
            None
        }
    }

    /// Insert a node if it is not in the queue yet,
    /// otherwise update associated cost if the new cost is less that existing.
    /// Returns `true` if the node was either inserted or updated,
    /// otherwise (node existed and current cost is less that the new one) `false`.
    pub fn try_insert_or_decrease_cost(&mut self, tag: &T, new_cost: W) -> bool {
        if let Some(&i) = self.index.get(tag) {
            if new_cost < self.queue[i].1 {
                // Remove old item
                self.queue.remove(i);
                self.index.remove(tag);
                self.index.iter_mut().for_each(|(_, old_idx)| {
                    if *old_idx >= i {
                        *old_idx -= 1;
                    }
                });

                // Re-insert with updated cost
                let new_item = (tag.clone(), new_cost);
                let new_index = self.insertion_pos(&new_item);
                self.queue.insert(new_index, new_item);
                self.index.iter_mut().for_each(|(_, old_idx)| {
                    if *old_idx >= new_index {
                        *old_idx += 1;
                    }
                });
                self.index.insert(tag.clone(), new_index);

                true
            } else {
                false
            }
        } else {
            self.push(tag.clone(), new_cost);
            true
        }
    }
}

#[test]
fn test_push_pop() {
    let mut f = Frontier::new();
    assert_eq!(f.pop(), None);

    f.push("N", 1.0);
    assert_eq!(f.pop(), Some(("N", 1.0)));
    assert_eq!(f.pop(), None);

    f.push("A", 1.0);
    f.push("B", 2.0);
    assert_eq!(f.pop(), Some(("A", 1.0)));
    assert_eq!(f.pop(), Some(("B", 2.0)));
    assert_eq!(f.pop(), None);

    f.push("X", 2.0);
    f.push("Y", 1.0);
    assert_eq!(f.pop(), Some(("Y", 1.0)));
    assert_eq!(f.pop(), Some(("X", 2.0)));
    assert_eq!(f.pop(), None);
}

#[test]
fn test_decrease_cost() {
    let mut f = Frontier::new();
    assert_eq!(f.pop(), None);

    assert_eq!(f.try_insert_or_decrease_cost(&"A", 1.0), true);
    assert_eq!(f.pop(), Some(("A", 1.0)));
    assert_eq!(f.pop(), None);

    f.push("B", 2.0);
    assert_eq!(f.try_insert_or_decrease_cost(&"A", 1.0), true);
    assert_eq!(f.pop(), Some(("A", 1.0)));
    assert_eq!(f.pop(), Some(("B", 2.0)));
    assert_eq!(f.pop(), None);

    assert_eq!(f.try_insert_or_decrease_cost(&"A", 1.0), true);
    assert_eq!(f.try_insert_or_decrease_cost(&"B", 2.0), true);
    assert_eq!(f.pop(), Some(("A", 1.0)));
    assert_eq!(f.pop(), Some(("B", 2.0)));
    assert_eq!(f.pop(), None);

    f.push("X", 1.0);
    assert_eq!(f.try_insert_or_decrease_cost(&"X", 2.0), false);
    assert_eq!(f.pop(), Some(("X", 1.0)));
    assert_eq!(f.pop(), None);

    f.push("Y", 2.0);
    assert_eq!(f.try_insert_or_decrease_cost(&"Y", 1.0), true);
    assert_eq!(f.pop(), Some(("Y", 1.0)));
    assert_eq!(f.pop(), None);

    f.push("X", 1.0);
    f.push("Y", 3.0);
    assert_eq!(f.try_insert_or_decrease_cost(&"Y", 2.0), true);
    assert_eq!(f.pop(), Some(("X", 1.0)));
    assert_eq!(f.pop(), Some(("Y", 2.0)));
    assert_eq!(f.pop(), None);

    f.push("X", 2.0);
    f.push("Y", 3.0);
    assert_eq!(f.try_insert_or_decrease_cost(&"Y", 1.0), true);
    assert_eq!(f.pop(), Some(("Y", 1.0)));
    assert_eq!(f.pop(), Some(("X", 2.0)));
    assert_eq!(f.pop(), None);
}