proper enumeration of names and finding names in tabl e
This commit is contained in:
parent
f88c890210
commit
1badd4c698
|
@ -34,8 +34,8 @@ impl FName {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_names()
|
.as_names()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.iter()
|
.enumerate()
|
||||||
.position(|entry| entry.as_str() == name)
|
.find_map(|(i, entry)| (entry.as_str() == name).then_some(i))
|
||||||
.context("could not find fname.")? as u32;
|
.context("could not find fname.")? as u32;
|
||||||
|
|
||||||
entry.insert(name);
|
entry.insert(name);
|
||||||
|
|
|
@ -115,6 +115,10 @@ impl NameEntryArray {
|
||||||
NamesIterator::new(self)
|
NamesIterator::new(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn enumerate(&self) -> NamesEnumerator {
|
||||||
|
NamesEnumerator::new(self)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn len(&self) -> usize {
|
pub fn len(&self) -> usize {
|
||||||
self.num_elements as usize
|
self.num_elements as usize
|
||||||
}
|
}
|
||||||
|
@ -176,3 +180,32 @@ impl<'a> IntoIterator for &'a NameEntryArray {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct NamesEnumerator<'a> {
|
||||||
|
names: &'a NameEntryArray,
|
||||||
|
index: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> NamesEnumerator<'a> {
|
||||||
|
pub fn new(names: &'a NameEntryArray) -> Self {
|
||||||
|
Self { names, index: 0 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Iterator for NamesEnumerator<'a> {
|
||||||
|
type Item = (usize, &'a FNameEntry);
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
loop {
|
||||||
|
if !self.names.is_valid_index(self.index) {
|
||||||
|
break None;
|
||||||
|
}
|
||||||
|
let item = self.names.get_index(self.index);
|
||||||
|
self.index += 1;
|
||||||
|
// skip empty entries, we dont care about them
|
||||||
|
if let Some(item) = item {
|
||||||
|
break Some((self.index - 1, item));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue