Skip to content

Commit 194e25d

Browse files
committed
Add position and rposition methods directly on IndexVec for inverse index lookup.
1 parent 49bdeb7 commit 194e25d

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

Diff for: src/librustc_data_structures/indexed_vec.rs

+10
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,16 @@ impl<I: Idx, T> IndexVec<I, T> {
554554
self.raw.pop()
555555
}
556556

557+
/// Searches for an element from the start of this vector, returning its index.
558+
pub fn position(&self, predicate: impl FnMut(&T) -> bool) -> Option<I> {
559+
self.iter().position(predicate).map(I::new)
560+
}
561+
562+
/// Searches for an element from the end of this vector, returning its index.
563+
pub fn rposition(&self, predicate: impl FnMut(&T) -> bool) -> Option<I> {
564+
self.iter().rposition(predicate).map(I::new)
565+
}
566+
557567
#[inline]
558568
pub fn len(&self) -> usize {
559569
self.raw.len()

0 commit comments

Comments
 (0)