use std::slice;
use std::mem;
use super::{Attribute, AttributeId, AttributeValue};
pub struct Attributes(Vec<Attribute>);
impl Attributes {
#[inline]
pub fn new() -> Attributes {
Attributes(Vec::new())
}
#[inline]
pub fn get(&self, id: AttributeId) -> Option<&Attribute> {
for v in &self.0 {
if v.id == id {
return Some(v);
}
}
None
}
#[inline]
pub fn get_mut(&mut self, id: AttributeId) -> Option<&mut Attribute> {
for v in &mut self.0 {
if v.id == id {
return Some(v);
}
}
None
}
#[inline]
pub fn get_value(&self, id: AttributeId) -> Option<&AttributeValue> {
for v in &self.0 {
if v.id == id {
return Some(&v.value);
}
}
None
}
#[inline]
pub fn get_value_mut(&mut self, id: AttributeId) -> Option<&mut AttributeValue> {
for v in &mut self.0 {
if v.id == id {
return Some(&mut v.value);
}
}
None
}
#[inline]
pub fn get_value_or<'a>(&'a self, id: AttributeId, def_value: &'a AttributeValue)
-> &AttributeValue {
match self.get(id) {
Some(a) => &a.value,
None => def_value,
}
}
pub fn insert(&mut self, attr: Attribute) {
if self.0.capacity() == 0 {
self.0.reserve(16);
}
let idx = self.0.iter().position(|x| x.id == attr.id);
match idx {
Some(i) => { mem::replace(&mut self.0[i], attr); }
None => self.0.push(attr),
}
}
pub fn remove(&mut self, id: AttributeId) {
let idx = self.0.iter().position(|x| x.id == id);
if let Some(i) = idx {
self.0.remove(i);
}
}
#[inline]
pub fn contains(&self, id: AttributeId) -> bool {
self.0.iter().any(|a| a.id == id)
}
#[inline]
pub fn len(&self) -> usize {
self.0.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[inline]
pub fn iter(&self) -> slice::Iter<Attribute> {
self.0.iter()
}
#[inline]
pub fn iter_mut(&mut self) -> slice::IterMut<Attribute> {
self.0.iter_mut()
}
#[inline]
pub fn retain<F>(&mut self, f: F)
where F: FnMut(&Attribute) -> bool
{
self.0.retain(f)
}
}