Skip to content

Commit 622a676

Browse files
bors[bot]japaric
andauthored
Merge #291
291: add first/last API to IndexMap and IndexSet r=japaric a=japaric closes #256 Co-authored-by: Jorge Aparicio <[email protected]>
2 parents e52d483 + 7fd32f8 commit 622a676

File tree

3 files changed

+82
-3
lines changed

3 files changed

+82
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
88
## [Unreleased]
99

1010
### Added
11-
- Added support for AVR architecture.
1211

12+
* Added support for AVR architecture.
1313
* Add Entry Api to IndexMap
1414
* Implement IntoIterator trait for Indexmap
1515
* Implement FromIterator for String
16+
* Add first/last API to IndexMap and IndexSet
1617

1718
### Changed
1819

src/indexmap.rs

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,46 @@ where
604604
}
605605
}
606606

607+
/// Get the first key-value pair
608+
///
609+
/// Computes in **O(1)** time
610+
pub fn first(&self) -> Option<(&K, &V)> {
611+
self.core
612+
.entries
613+
.first()
614+
.map(|bucket| (&bucket.key, &bucket.value))
615+
}
616+
617+
/// Get the first key-value pair, with mutable access to the value
618+
///
619+
/// Computes in **O(1)** time
620+
pub fn first_mut(&mut self) -> Option<(&K, &mut V)> {
621+
self.core
622+
.entries
623+
.first_mut()
624+
.map(|bucket| (&bucket.key, &mut bucket.value))
625+
}
626+
627+
/// Get the last key-value pair
628+
///
629+
/// Computes in **O(1)** time
630+
pub fn last(&self) -> Option<(&K, &V)> {
631+
self.core
632+
.entries
633+
.last()
634+
.map(|bucket| (&bucket.key, &bucket.value))
635+
}
636+
637+
/// Get the last key-value pair, with mutable access to the value
638+
///
639+
/// Computes in **O(1)** time
640+
pub fn last_mut(&mut self) -> Option<(&K, &mut V)> {
641+
self.core
642+
.entries
643+
.last_mut()
644+
.map(|bucket| (&bucket.key, &mut bucket.value))
645+
}
646+
607647
/// Returns an entry for the corresponding key
608648
/// ```
609649
/// use heapless::FnvIndexMap;
@@ -1087,8 +1127,8 @@ where
10871127

10881128
#[cfg(test)]
10891129
mod tests {
1090-
use crate::indexmap::Entry;
1091-
use crate::FnvIndexMap;
1130+
use crate::{indexmap::Entry, FnvIndexMap};
1131+
10921132
use core::mem;
10931133

10941134
#[test]
@@ -1310,4 +1350,28 @@ mod tests {
13101350
}
13111351
assert!(src.is_empty());
13121352
}
1353+
1354+
#[test]
1355+
fn first_last() {
1356+
let mut map = FnvIndexMap::<_, _, 4>::new();
1357+
1358+
assert_eq!(None, map.first());
1359+
assert_eq!(None, map.last());
1360+
1361+
map.insert(0, 0).unwrap();
1362+
map.insert(2, 2).unwrap();
1363+
1364+
assert_eq!(Some((&0, &0)), map.first());
1365+
assert_eq!(Some((&2, &2)), map.last());
1366+
1367+
map.insert(1, 1).unwrap();
1368+
1369+
assert_eq!(Some((&1, &1)), map.last());
1370+
1371+
*map.first_mut().unwrap().1 += 1;
1372+
*map.last_mut().unwrap().1 += 1;
1373+
1374+
assert_eq!(Some((&0, &1)), map.first());
1375+
assert_eq!(Some((&1, &2)), map.last());
1376+
}
13131377
}

src/indexset.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,20 @@ where
128128
}
129129
}
130130

131+
/// Get the first value
132+
///
133+
/// Computes in **O(1)** time
134+
pub fn first(&self) -> Option<&T> {
135+
self.map.first().map(|(k, _v)| k)
136+
}
137+
138+
/// Get the last value
139+
///
140+
/// Computes in **O(1)** time
141+
pub fn last(&self) -> Option<&T> {
142+
self.map.last().map(|(k, _v)| k)
143+
}
144+
131145
/// Visits the values representing the difference, i.e. the values that are in `self` but not in
132146
/// `other`.
133147
///

0 commit comments

Comments
 (0)