1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use crate::bits::BitSetViewExt;
use crate::join::{IntoJoin, Join};
use crate::svec::{Entry, SVector, Store, StoreMut};
use crate::traits::{IndexExcl, IndexLowerBound};
use std::mem;

/// Wrapper to allow immutable access to the elments of an SVector in join and merge oprations.
pub struct WrapRead<'a, S>
where
    S: Store,
{
    pub(crate) vec: &'a SVector<S>,
}

impl<'a, S> IndexExcl<usize> for WrapRead<'a, S>
where
    S: Store,
{
    type Item = &'a S::Item;

    fn index(&mut self, idx: usize) -> Self::Item {
        self.vec.store.get(idx)
    }
}

impl<'a, S> IndexLowerBound<usize> for WrapRead<'a, S>
where
    S: Store,
{
    fn lower_bound(&mut self, idx: usize) -> Option<usize> {
        self.vec.mask.lower_bound(idx)
    }
}

impl<'a, S> IntoJoin for WrapRead<'a, S>
where
    S: 'a + Store,
{
    type Store = Self;

    fn into_join(self) -> Join<Self::Store> {
        Join::from_parts(0..self.vec.capacity(), self)
    }
}

/// Wrapper to allow mutable access to the elments of an SVector in join and merge oprations.
pub struct WrapUpdate<'a, S>
where
    S: StoreMut,
{
    pub(crate) vec: &'a mut SVector<S>,
}

impl<'a, S> IndexExcl<usize> for WrapUpdate<'a, S>
where
    S: StoreMut,
{
    type Item = &'a mut S::Item;

    fn index(&mut self, idx: usize) -> Self::Item {
        unsafe { mem::transmute(self.vec.store.get_mut(idx)) } // GAT
    }
}

impl<'a, S> IndexLowerBound<usize> for WrapUpdate<'a, S>
where
    S: StoreMut,
{
    fn lower_bound(&mut self, idx: usize) -> Option<usize> {
        self.vec.mask.lower_bound(idx)
    }
}

impl<'a, S> IntoJoin for WrapUpdate<'a, S>
where
    S: StoreMut,
{
    type Store = Self;

    fn into_join(self) -> Join<Self::Store> {
        Join::from_parts(0..self.vec.capacity(), self)
    }
}

/// Wrapper to allow Entry based access to the elments of an SVector in join and merge oprations.
pub struct WrapWrite<'a, S>
where
    S: StoreMut,
{
    pub(crate) vec: &'a mut SVector<S>,
}

impl<'a, S> IndexExcl<usize> for WrapWrite<'a, S>
where
    S: StoreMut,
{
    type Item = Entry<'a, S>;

    fn index(&mut self, idx: usize) -> Self::Item {
        unsafe { mem::transmute(self.vec.get_entry(idx)) } // GAT
    }
}

impl<'a, S> IndexLowerBound<usize> for WrapWrite<'a, S>
where
    S: StoreMut,
{
    fn lower_bound(&mut self, idx: usize) -> Option<usize> {
        Some(idx)
    }
}

impl<'a, S> IntoJoin for WrapWrite<'a, S>
where
    S: 'a + StoreMut,
{
    type Store = Self;

    fn into_join(self) -> Join<Self::Store> {
        Join::from_parts(0..self.vec.capacity(), self)
    }
}