pub fn decrease_space_density(&mut self, id: ID) -> Result<Option<(Vec<ID>, ID)>> {
        if self.space_exists(id) {
            let neighbor = self.graph.neighbors(id).collect::<Vec<ID>>();
            let mut connected = neighbor
                .iter()
                .filter(|a| {
                    neighbor
                        .iter()
                        .any(|b| **a != *b && self.graph.edge_weight(**a, *b).is_some())
                }).cloned()
                .collect::<Vec<ID>>();
            if connected.len() != self.dimensions {
                Ok(None)
            } else {
                connected.push(id);
                let states = connected
                    .iter()
                    .map(|i| self.spaces[&i].state())
                    .cloned()
                    .collect::<Vec<S>>();
                let id = ID::new();
                self.graph.add_node(id);
                self.space_ids.insert(id);
                self.spaces
                    .insert(id, Space::new(id, State::merge(&states)));
                for i in &connected {
                    let outsiders = self
                        .graph
                        .neighbors(*i)
                        .filter(|n| !connected.contains(n))
                        .collect::<Vec<ID>>();
                    for n in outsiders {
                        self.graph.add_edge(id, n, ());
                    }
                }
                let space_ids = connected
                    .iter()
                    .map(|i| {
                        self.graph.remove_node(*i);
                        self.spaces.remove(i);
                        self.space_ids.remove(i);
                        *i
                    })
                    .collect::<Vec<ID>>();
                Ok(Some((space_ids, id)))
            }
        } else {
            Err(QDFError::SpaceDoesNotExists(id))
        }
    }
/// Decreases given space density (merge space children and rebind them properly to theirs
/// neighbors if space has 1 level of subdivision, otherwise perform this operation on its
/// subspaces), and returns process information (source space ids, merged space id) or throws
/// error if space does not exists.
///
/// # Arguments
/// * `id` - space id.
///
/// # Returns
/// `Ok` with `Some` tuple of vector of merged space ids and created space id, or `Ok` with
/// `None` if space cannot be merged or `Err` if given space does not exists.
///
/// # Examples
/// ```
/// use quantized_density_fields::QDF;
///
/// let (mut qdf, root) = QDF::new(2, 9);
/// let (_, subs, _) = qdf.increase_space_density(root).unwrap();
/// assert_eq!(subs.len(), 3);
/// let (_, root) = qdf.decrease_space_density(subs[0]).unwrap().unwrap();
/// assert_eq!(qdf.spaces().len(), 1);
/// assert_eq!(*qdf.spaces().nth(0).unwrap(), root);
/// ```
Decreases given space density (merge space children and rebind them properly to theirs
neighbors if space has 1 level of subdivision, otherwise perform this operation on its
subspaces), and returns process information (source space ids, merged space id) or throws
error if space does not exists.
Decreases given space density ( merge space children and rebind them properly to theirs neighbors if space has 1 level of subdivision otherwise perform this operation on its subspaces ) and returns process information ( source space ids merged space id ) or throws error if space does not exists .
pub fn decrease_space_density(&mut self, id: ID) -> Result<Option<(Vec<ID>, ID)>> {
        if self.space_exists(id) {
            let neighbor = self.graph.neighbors(id).collect::<Vec<ID>>();
            let mut connected = neighbor
                .iter()
                .filter(|a| {
                    neighbor
                        .iter()
                        .any(|b| **a != *b && self.graph.edge_weight(**a, *b).is_some())
                }).cloned()
                .collect::<Vec<ID>>();
            if connected.len() != self.dimensions {
                Ok(None)
            } else {
                connected.push(id);
                let states = connected
                    .iter()
                    .map(|i| self.spaces[&i].state())
                    .cloned()
                    .collect::<Vec<S>>();
                let id = ID::new();
                self.graph.add_node(id);
                self.space_ids.insert(id);
                self.spaces
                    .insert(id, Space::new(id, State::merge(&states)));
                for i in &connected {
                    let outsiders = self
                        .graph
                        .neighbors(*i)
                        .filter(|n| !connected.contains(n))
                        .collect::<Vec<ID>>();
                    for n in outsiders {
                        self.graph.add_edge(id, n, ());
                    }
                }
                let space_ids = connected
                    .iter()
                    .map(|i| {
                        self.graph.remove_node(*i);
                        self.spaces.remove(i);
                        self.space_ids.remove(i);
                        *i
                    })
                    .collect::<Vec<ID>>();
                Ok(Some((space_ids, id)))
            }
        } else {
            Err(QDFError::SpaceDoesNotExists(id))
        }
    }
pub fn decrease_space_density ( & mut self , id : ID ) -> Result < Option < ( Vec < ID > , ID ) > > { if self . space_exists ( id ) { let neighbor = self . graph . neighbors ( id ) . collect :: < Vec < ID > > ( ) ; let mut connected = neighbor . iter ( ) . filter ( | a | { neighbor . iter ( ) . any ( | b | * * a != * b && self . graph . edge_weight ( * * a , * b ) . is_some ( ) ) } ) . cloned ( ) . collect :: < Vec < ID > > ( ) ; if connected . len ( ) != self . dimensions { Ok ( None ) } else { connected . push ( id ) ; let states = connected . iter ( ) . map ( | i | self . spaces [ & i ] . state ( ) ) . cloned ( ) . collect :: < Vec < S > > ( ) ; let id = ID :: new ( ) ; self . graph . add_node ( id ) ; self . space_ids . insert ( id ) ; self . spaces . insert ( id , Space :: new ( id , State :: merge ( & states ) ) ) ; for i in & connected { let outsiders = self . graph . neighbors ( * i ) . filter ( | n | ! connected . contains ( n ) ) . collect :: < Vec < ID > > ( ) ; for n in outsiders { self . graph . add_edge ( id , n , ( ) ) ; } } let space_ids = connected . iter ( ) . map ( | i | { self . graph . remove_node ( * i ) ; self . spaces . remove ( i ) ; self . space_ids . remove ( i ) ; * i } ) . collect :: < Vec < ID > > ( ) ; Ok ( Some ( ( space_ids , id ) ) ) } } else { Err ( QDFError :: SpaceDoesNotExists ( id ) ) } }
Decreases given space density (merge space children and rebind them properly to theirs
neighbors if space has 1 level of subdivision, otherwise perform this operation on its
subspaces), and returns process information (source space ids, merged space id) or throws
error if space does not exists.
Decreases given space density ( merge space children and rebind them properly to theirs neighbors if space has 1 level of subdivision otherwise perform this operation on its subspaces ) and returns process information ( source space ids merged space id ) or throws error if space does not exists .
&mut self
id
ID
`Ok` with `Some` tuple of vector of merged space ids and created space id, or `Ok` with
`None` if space cannot be merged or `Err` if given space does not exists.
` Ok ` with ` Some ` tuple of vector of merged space ids and created space id or ` Ok ` with ` None ` if space cannot be merged or ` Err ` if given space does not exists .
&mut self
id
ID
examples
```
use quantized_density_fields::QDF.

let (mut qdf, root) = QDF::new(2, 9);
let (_, subs, _) = qdf.increase_space_density(root).unwrap();
assert_eq!(subs.len(), 3);
let (_, root) = qdf.decrease_space_density(subs[0]).unwrap().unwrap();
assert_eq!(qdf.spaces().len(), 1);
assert_eq!(*qdf.spaces().nth(0).unwrap(), root);
```
` ` ` use quantized_density_fields :: QDF . let ( mut qdf root ) = QDF :: new ( 2 9 ) ; let ( _ subs _ ) = qdf . increase_space_density ( root ) . unwrap () ; assert_eq! ( subs . len () 3 ) ; let ( _ root ) = qdf . decrease_space_density ( subs [ 0 ] ) . unwrap () . unwrap () ; assert_eq! ( qdf . spaces () . len () 1 ) ; assert_eq! ( * qdf . spaces () . nth ( 0 ) . unwrap () root ) ; ` ` `