Skip to content

Commit

Permalink
Implement pop_back for kv (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanganto committed Jan 1, 2022
1 parent 1314562 commit 6acb07c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
14 changes: 12 additions & 2 deletions sewup/src/kv/bucket.rs
Expand Up @@ -261,8 +261,18 @@ impl<'a, K: Key + PartialEq, V: Clone + Value> Bucket<K, V> {
}

/// Pop the last item
pub fn pop_back(&self) -> Option<Item<K, V>> {
None
pub fn pop_back(&mut self) -> Option<Item<K, V>> {
let mut prev_pair: Option<(K, V)> = None;
let mut iter = self.iter();
while let Some(pair) = iter.next() {
prev_pair = Some(pair);
}
if let Some((key, value)) = prev_pair {
self.remove(key.clone());
Some((key, value))
} else {
None
}
}

/// Pop the first item
Expand Down
18 changes: 18 additions & 0 deletions sewup/src/kv/tests.rs
Expand Up @@ -38,3 +38,21 @@ fn test_pop_for_bucket() {
assert_eq!(bucket.get(2).unwrap(), None);
assert_eq!(bucket.get(3).unwrap(), Some(3));
}

#[cfg(feature = "default")]
#[test]
fn test_pop_back_for_bucket() {
let mut bucket = Bucket {
name: "test_bucket".into(),
raw_bucket: (vec![], vec![]),
phantom_k: PhantomData::<usize>,
phantom_v: PhantomData::<usize>,
};
bucket.set(1, 1);
bucket.set(2, 2);
assert_eq!(bucket.get(1).unwrap(), Some(1));
let last = bucket.pop_back();
assert_eq!(last, Some((2, 2)));
assert_eq!(bucket.get(1).unwrap(), Some(1));
assert_eq!(bucket.get(2).unwrap(), None);
}

0 comments on commit 6acb07c

Please sign in to comment.