rust/hedgewars-server/src/core/indexslab.rs
author alfadur
Fri, 05 Jul 2019 23:40:03 +0300
changeset 15221 387345a14b3f
parent 15125 febccab419b1
child 15444 a158ff8f84ef
permissions -rw-r--r--
replace overflowing_sub with wrapping_sub
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15125
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
     1
use std::{
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
     2
    iter,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
     3
    mem::replace,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
     4
    ops::{Index, IndexMut},
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
     5
};
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
     6
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
     7
pub struct IndexSlab<T> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
     8
    data: Vec<Option<T>>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
     9
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    10
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    11
impl<T> IndexSlab<T> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    12
    pub fn new() -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    13
        Self { data: Vec::new() }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    14
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    15
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    16
    pub fn with_capacity(capacity: usize) -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    17
        Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    18
            data: Vec::with_capacity(capacity),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    19
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    20
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    21
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    22
    pub fn insert(&mut self, index: usize, value: T) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    23
        if index >= self.data.len() {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    24
            self.data.reserve(index - self.data.len() + 1);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    25
            self.data.extend((self.data.len()..index).map(|_| None));
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    26
            self.data.push(Some(value))
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    27
        } else {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    28
            self.data[index] = Some(value);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    29
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    30
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    31
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    32
    pub fn contains(&self, index: usize) -> bool {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    33
        self.data.get(index).and_then(|x| x.as_ref()).is_some()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    34
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    35
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    36
    pub fn remove(&mut self, index: usize) -> Option<T> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    37
        if let Some(x) = self.data.get_mut(index) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    38
            replace(x, None)
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    39
        } else {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    40
            None
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    41
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    42
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    43
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    44
    pub fn iter(&self) -> impl Iterator<Item = (usize, &T)> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    45
        self.data
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    46
            .iter()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    47
            .enumerate()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    48
            .filter_map(|(index, opt)| opt.as_ref().and_then(|x| Some((index, x))))
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    49
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    50
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    51
    pub fn iter_mut(&mut self) -> impl Iterator<Item = (usize, &mut T)> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    52
        self.data
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    53
            .iter_mut()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    54
            .enumerate()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    55
            .filter_map(|(index, opt)| opt.as_mut().and_then(|x| Some((index, x))))
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    56
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    57
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    58
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    59
impl<T> Index<usize> for IndexSlab<T> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    60
    type Output = T;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    61
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    62
    fn index(&self, index: usize) -> &Self::Output {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    63
        self.data[index].as_ref().unwrap()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    64
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    65
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    66
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    67
impl<T> IndexMut<usize> for IndexSlab<T> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    68
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    69
        self.data[index].as_mut().unwrap()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    70
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15079
diff changeset
    71
}