Skip to content

Commit

Permalink
#21 Optimize loops by exploiting invariant self.next <= N
Browse files Browse the repository at this point in the history
  • Loading branch information
WiebeCnossen committed Apr 18, 2023
1 parent 2c3864f commit 23d81bb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 36 deletions.
5 changes: 1 addition & 4 deletions src/debug.rs
Expand Up @@ -35,10 +35,7 @@ impl<K: Copy + PartialEq + Display, V: Clone + Copy + Display, const N: usize> D
{
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let mut parts = vec![];
for i in 0..N {
if self.next <= i {
break;
}
for i in 0..self.next {
if let Present((k, v)) = &self.pairs[i] {
parts.push(format!("{k}: {v}"));
}
Expand Down
10 changes: 2 additions & 8 deletions src/iterators.rs
Expand Up @@ -26,10 +26,7 @@ impl<'a, K: Clone, V: Clone, const N: usize> Iterator for Iter<'a, K, V, N> {
#[inline]
#[must_use]
fn next(&mut self) -> Option<Self::Item> {
while self.pos < N {
if self.next <= self.pos {
break;
}
while self.pos < self.next {
if let Present(p) = &self.pairs[self.pos] {
self.pos += 1;
return Some((&p.0, &p.1));
Expand All @@ -46,10 +43,7 @@ impl<'a, K: Clone, V: Clone, const N: usize> Iterator for IntoIter<'a, K, V, N>
#[inline]
#[must_use]
fn next(&mut self) -> Option<Self::Item> {
while self.pos < N {
if self.next <= self.pos {
break;
}
while self.pos < self.next {
if self.pairs[self.pos].is_some() {
let pair = self.pairs[self.pos].clone().unwrap();
self.pos += 1;
Expand Down
30 changes: 6 additions & 24 deletions src/map.rs
Expand Up @@ -65,10 +65,7 @@ impl<K: Copy + PartialEq, V: Clone + Copy, const N: usize> Map<K, V, N> {
return 0;
}
let mut busy = 0;
for i in 0..N {
if self.next <= i {
break;
}
for i in 0..self.next {
if self.pairs[i].is_some() {
busy += 1;
}
Expand All @@ -79,10 +76,7 @@ impl<K: Copy + PartialEq, V: Clone + Copy, const N: usize> Map<K, V, N> {
/// Does the map contain this key?
#[inline]
pub fn contains_key(&self, k: &K) -> bool {
for i in 0..N {
if self.next <= i {
break;
}
for i in 0..self.next {
if let Present((bk, _bv)) = &self.pairs[i] {
if bk == k {
return true;
Expand All @@ -95,10 +89,7 @@ impl<K: Copy + PartialEq, V: Clone + Copy, const N: usize> Map<K, V, N> {
/// Remove by key.
#[inline]
pub fn remove(&mut self, k: &K) {
for i in 0..N {
if self.next <= i {
break;
}
for i in 0..self.next {
if let Present((bk, _bv)) = &self.pairs[i] {
if bk == k {
self.pairs[i] = Absent;
Expand All @@ -116,10 +107,7 @@ impl<K: Copy + PartialEq, V: Clone + Copy, const N: usize> Map<K, V, N> {
#[inline]
pub fn insert(&mut self, k: K, v: V) {
self.remove(&k);
for i in 0..N {
if self.next <= i {
break;
}
for i in 0..self.next {
if !self.pairs[i].is_some() {
self.pairs[i] = Present((k, v));
return;
Expand All @@ -137,10 +125,7 @@ impl<K: Copy + PartialEq, V: Clone + Copy, const N: usize> Map<K, V, N> {
#[inline]
#[must_use]
pub fn get(&self, k: &K) -> Option<&V> {
for i in 0..N {
if self.next <= i {
break;
}
for i in 0..self.next {
if let Present(p) = &self.pairs[i] {
if p.0 == *k {
return Some(&p.1);
Expand All @@ -158,10 +143,7 @@ impl<K: Copy + PartialEq, V: Clone + Copy, const N: usize> Map<K, V, N> {
#[inline]
#[must_use]
pub fn get_mut(&mut self, k: &K) -> Option<&mut V> {
for i in 0..N {
if self.next <= i {
break;
}
for i in 0..self.next {
if let Present(p) = &mut self.pairs[i] {
if p.0 == *k {
return Some(&mut self.pairs[i].as_mut().unwrap().1);
Expand Down

0 comments on commit 23d81bb

Please sign in to comment.