rust/hedgewars-server/src/core/indexslab.rs
author alfadur
Sat, 11 Apr 2020 23:20:09 +0300
changeset 15552 0031683bfa76
parent 15441 61a0bd0bb021
permissions -rw-r--r--
don't apply voted 🦔 to the in-game teams
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
     1
use std::{
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
     2
    iter,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
     3
    mem::replace,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
     4
    ops::{Index, IndexMut},
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
     5
};
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
     6
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
     7
pub struct IndexSlab<T> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
     8
    data: Vec<Option<T>>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
     9
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    10
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    11
impl<T> IndexSlab<T> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    12
    pub fn new() -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    13
        Self { data: Vec::new() }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    14
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    15
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    16
    pub fn with_capacity(capacity: usize) -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    17
        Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    18
            data: Vec::with_capacity(capacity),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    19
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    20
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    21
15441
61a0bd0bb021 refactor root handler
alfadur
parents: 15439
diff changeset
    22
    pub fn get(&self, index: usize) -> Option<&T> {
61a0bd0bb021 refactor root handler
alfadur
parents: 15439
diff changeset
    23
        self.data[index].as_ref()
61a0bd0bb021 refactor root handler
alfadur
parents: 15439
diff changeset
    24
    }
61a0bd0bb021 refactor root handler
alfadur
parents: 15439
diff changeset
    25
61a0bd0bb021 refactor root handler
alfadur
parents: 15439
diff changeset
    26
    pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
61a0bd0bb021 refactor root handler
alfadur
parents: 15439
diff changeset
    27
        self.data[index].as_mut()
61a0bd0bb021 refactor root handler
alfadur
parents: 15439
diff changeset
    28
    }
61a0bd0bb021 refactor root handler
alfadur
parents: 15439
diff changeset
    29
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    30
    pub fn insert(&mut self, index: usize, value: T) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    31
        if index >= self.data.len() {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    32
            self.data.reserve(index - self.data.len() + 1);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    33
            self.data.extend((self.data.len()..index).map(|_| None));
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    34
            self.data.push(Some(value))
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    35
        } else {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    36
            self.data[index] = Some(value);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    37
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    38
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    39
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    40
    pub fn contains(&self, index: usize) -> bool {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    41
        self.data.get(index).and_then(|x| x.as_ref()).is_some()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    42
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    43
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    44
    pub fn remove(&mut self, index: usize) -> Option<T> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    45
        if let Some(x) = self.data.get_mut(index) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    46
            replace(x, None)
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    47
        } else {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    48
            None
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    49
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    50
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    51
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15120
diff changeset
    52
    pub fn iter(&self) -> impl Iterator<Item = (usize, &T)> + Clone {
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    53
        self.data
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    54
            .iter()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    55
            .enumerate()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    56
            .filter_map(|(index, opt)| opt.as_ref().and_then(|x| Some((index, x))))
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    57
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    58
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    59
    pub fn iter_mut(&mut self) -> impl Iterator<Item = (usize, &mut T)> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    60
        self.data
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    61
            .iter_mut()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    62
            .enumerate()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    63
            .filter_map(|(index, opt)| opt.as_mut().and_then(|x| Some((index, x))))
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    64
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    65
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    66
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    67
impl<T> Index<usize> for IndexSlab<T> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    68
    type Output = T;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    69
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    70
    fn index(&self, index: usize) -> &Self::Output {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    71
        self.data[index].as_ref().unwrap()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    72
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    73
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    74
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    75
impl<T> IndexMut<usize> for IndexSlab<T> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    76
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    77
        self.data[index].as_mut().unwrap()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    78
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15074
diff changeset
    79
}