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