2019-12-13 13:22:54 +01:00
|
|
|
use std::cmp;
|
|
|
|
|
2019-08-16 16:35:19 +02:00
|
|
|
#[derive(Default, Clone)]
|
2019-06-24 17:29:14 +02:00
|
|
|
pub struct ReorderedAttrs {
|
|
|
|
reorders: Vec<Option<u16>>,
|
2019-12-13 13:22:54 +01:00
|
|
|
reverse: Vec<u16>,
|
2019-06-24 17:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ReorderedAttrs {
|
2019-07-01 14:34:06 +02:00
|
|
|
pub fn new() -> ReorderedAttrs {
|
2019-12-13 13:22:54 +01:00
|
|
|
ReorderedAttrs { reorders: Vec::new(), reverse: Vec::new() }
|
2019-07-01 14:34:06 +02:00
|
|
|
}
|
|
|
|
|
2019-06-24 17:29:14 +02:00
|
|
|
pub fn insert_attribute(&mut self, attribute: u16) {
|
2019-12-13 13:22:54 +01:00
|
|
|
let new_len = cmp::max(attribute as usize + 1, self.reorders.len());
|
|
|
|
self.reorders.resize(new_len, None);
|
|
|
|
self.reorders[attribute as usize] = Some(self.reverse.len() as u16);
|
|
|
|
self.reverse.push(attribute);
|
2019-06-24 17:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(&self, attribute: u16) -> Option<u16> {
|
2019-12-13 13:22:54 +01:00
|
|
|
match self.reorders.get(attribute as usize)? {
|
|
|
|
Some(attribute) => Some(*attribute),
|
|
|
|
None => None,
|
2019-06-24 17:29:14 +02:00
|
|
|
}
|
|
|
|
}
|
2019-12-13 13:22:54 +01:00
|
|
|
|
|
|
|
pub fn reverse(&self, attribute: u16) -> Option<u16> {
|
|
|
|
self.reverse.get(attribute as usize).copied()
|
|
|
|
}
|
2019-06-24 17:29:14 +02:00
|
|
|
}
|