Skip to content

Commit

Permalink
Fix #50
Browse files Browse the repository at this point in the history
Improve strink_to_fit to shrink the `IndexMap` too
  • Loading branch information
Gianmarco Garrisi committed Feb 4, 2024
1 parent 2bfbf45 commit d77b01c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 24 deletions.
12 changes: 6 additions & 6 deletions src/double_priority_queue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@ where
pub fn iter(&self) -> Iter<I, P> {
self.store.iter()
}

/// Shrinks the capacity of the internal data structures
/// that support this operation as much as possible.
pub fn shrink_to_fit(&mut self) {
self.store.shrink_to_fit();
}
}

impl<I, P, H> DoublePriorityQueue<I, P, H>
Expand Down Expand Up @@ -275,12 +281,6 @@ where
self.store.capacity()
}

/// Shrinks the capacity of the internal data structures
/// that support this operation as much as possible.
pub fn shrink_to_fit(&mut self) {
self.store.shrink_to_fit();
}

/// Removes the item with the lowest priority from
/// the priority queue and returns the pair (item, priority),
/// or None if the queue is empty.
Expand Down
12 changes: 6 additions & 6 deletions src/priority_queue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ where
pub fn iter(&self) -> Iter<I, P> {
self.store.iter()
}

/// Shrinks the capacity of the internal data structures
/// that support this operation as much as possible.
pub fn shrink_to_fit(&mut self) {
self.store.shrink_to_fit();
}
}

impl<I, P, H> PriorityQueue<I, P, H>
Expand Down Expand Up @@ -232,12 +238,6 @@ where
self.store.capacity()
}

/// Shrinks the capacity of the internal data structures
/// that support this operation as much as possible.
pub fn shrink_to_fit(&mut self) {
self.store.shrink_to_fit();
}

/// Removes the item with the greatest priority from
/// the priority queue and returns the pair (item, priority),
/// or None if the queue is empty.
Expand Down
17 changes: 9 additions & 8 deletions src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@ where
}
// reserve_exact -> IndexMap does not implement reserve_exact

/// Shrinks the capacity of the internal data structures
/// that support this operation as much as possible.
#[inline(always)]
pub fn shrink_to_fit(&mut self) {
self.heap.shrink_to_fit();
self.qp.shrink_to_fit();
self.map.shrink_to_fit();
}

/// Reserves capacity for at least `additional` more elements to be inserted
/// in the given `PriorityQueue`. The collection may reserve more space to avoid
/// frequent reallocations. After calling `reserve`, capacity will be
Expand Down Expand Up @@ -189,14 +198,6 @@ where
self.map.capacity()
}

/// Shrinks the capacity of the internal data structures
/// that support this operation as much as possible.
#[inline(always)]
pub fn shrink_to_fit(&mut self) {
self.heap.shrink_to_fit();
self.qp.shrink_to_fit();
}

/// Returns the number of elements in the priority queue.
#[inline(always)]
pub fn len(&self) -> usize {
Expand Down
3 changes: 1 addition & 2 deletions tests/double_priority_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ mod doublepq_tests {

let processor_priority = |pq: &DoublePriorityQueue<&str, i32>| {
*pq.iter()
.filter_map(|(i, p)| if *i == "Processor" { Some(p) } else { None })
.next()
.find_map(|(i, p)| if *i == "Processor" { Some(p) } else { None })
.unwrap()
};

Expand Down
3 changes: 1 addition & 2 deletions tests/priority_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ mod pqueue_tests {

let processor_priority = |pq: &PriorityQueue<&str, i32>| {
*pq.iter()
.filter_map(|(i, p)| if *i == "Processor" { Some(p) } else { None })
.next()
.find_map(|(i, p)| if *i == "Processor" { Some(p) } else { None })
.unwrap()
};

Expand Down

0 comments on commit d77b01c

Please sign in to comment.