Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct RadixHeapMap<K, V> {
initial: Bucket<K, V>,
}

impl<K: Radix + Ord + Copy, V> RadixHeapMap<K, V> {
impl<K: Radix + Ord + Clone, V> RadixHeapMap<K, V> {
/// Create an empty `RadixHeapMap`
pub fn new() -> RadixHeapMap<K, V> {
RadixHeapMap {
Expand Down Expand Up @@ -98,17 +98,18 @@ impl<K: Radix + Ord + Copy, V> RadixHeapMap<K, V> {
return;
};

let top = *repush
let top = repush
.iter()
.map(|(k, _)| k)
.max()
.expect("Expected non-empty bucket");

self.top = Some(top);
.expect("Expected non-empty bucket")
.clone();

repush
.drain(..)
.for_each(|(key, value)| buckets[key.radix_distance(&top) as usize].push((key, value)));

self.top = Some(top);
}

/// Pushes a new key value pair onto the heap.
Expand All @@ -118,9 +119,9 @@ impl<K: Radix + Ord + Copy, V> RadixHeapMap<K, V> {
/// Panics if the key is larger than the current top key.
#[inline]
pub fn push(&mut self, key: K, value: V) {
let bucket = if let Some(top) = self.top {
assert!(key <= top, "Key must be lower or equal to current top key");
&mut self.buckets[key.radix_distance(&top) as usize]
let bucket = if let Some(top) = &self.top {
assert!(key <= *top, "Key must be lower or equal to current top key");
&mut self.buckets[key.radix_distance(top) as usize]
} else {
&mut self.initial
};
Expand Down Expand Up @@ -165,7 +166,7 @@ impl<K: Radix + Ord + Copy, V> RadixHeapMap<K, V> {
/// The current top value. All keys pushed onto the heap must be smaller than this value.
#[inline]
pub fn top(&self) -> Option<K> {
self.top
self.top.clone()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could instead change this to return a reference to the top if you want to break API

}

/// Discards as much additional capacity as possible.
Expand Down