fn merge_molecules(&mut self, first: usize, second: usize) -> usize {
        // Do not try to merge a molecule with itself
        if first == second {
            return 0;
        }

        let (new_molid, old_molid) = if first < second {
            (first, second)
        } else {
            (second, first)
        };

        let mut new_mol = self.bondings[new_molid].clone();
        let old_mol = self.bondings[old_molid].clone();

        if new_mol.end() == old_mol.start() {
            // Just update the molecules ids
            for i in old_mol.indexes() {
                self.molecule_ids[i] = new_molid;
            }
        } else {
            // Move the particles close together
            let mut new_index = new_mol.end();
            for i in old_mol.indexes() {
                // Remove particles from the old position, and insert it to the
                // new one. The indexes are valid during the movement, because
                // we insert a new particle for each particle removed.
                let particle = self.particles.remove(i);
                self.particles.insert(new_index, particle);

                // Update molecule_ids
                let _ = self.molecule_ids.remove(i);
                self.molecule_ids.insert(new_index, new_molid);

                new_index += 1;
            }
        }

        let mut old_mol = self.bondings[old_molid].clone();
        let size = old_mol.size() as isize;

        // translate all indexes in the molecules between new_mol and old_mol
        for molecule in
            self.bondings.iter_mut().skip(new_molid + 1).take(old_molid - new_molid - 1)
        {
            molecule.translate_by(size);
        }

        // Update molid for all particles after the old molecule
        for molid in self.molecule_ids.iter_mut().skip(old_mol.end()) {
            *molid -= 1;
        }

        let delta = old_mol.start() - new_mol.end();
        old_mol.translate_by(-(delta as isize));

        new_mol.merge_with(old_mol);
        self.bondings[new_molid] = new_mol;
        let _ = self.bondings.remove(old_molid);

        debug_assert!(check_molid_sorted(&self.molecule_ids), "Unsorted molecule ids {:?}", self.molecule_ids);

        return delta;
    }
/// Merge the molecules at indexes `first` and `second` into one
/// molecule. The molecule are merged into the one with the lower molecule
/// index.
///
/// For example, if we have
/// ```text
///  0    1    2    3   # Molecules indexes
/// H-H  H-H  H-H  H-H
/// 0 1  2 3  4 5  6 7  # Particles indexes
/// ```
/// and call `merge_molecules(0, 3)` the result will be
/// ```text
/// 0 1 6 7  2 3  4 5  # Old indexes
/// H-H-H-H  H-H  H-H
/// 0 1 2 3  4 5  6 7  # New indexes
/// ```
///
/// This functions return the change in index of the first particle of the
/// moved molecule, i.e. in this example `4`.
Merge the molecules at indexes `first` and `second` into one
molecule. The molecule are merged into the one with the lower molecule
index.



This functions return the change in index of the first particle of the
moved molecule, i.e. in this example `4`.
Merge the molecules at indexes ` first ` and ` second ` into one molecule . The molecule are merged into the one with the lower molecule index . This functions return the change in index of the first particle of the moved molecule i . e . in this example ` 4 ` .
fn merge_molecules(&mut self, first: usize, second: usize) -> usize {
        // Do not try to merge a molecule with itself
        if first == second {
            return 0;
        }
        let (new_molid, old_molid) = if first < second {
            (first, second)
        } else {
            (second, first)
        };
        let mut new_mol = self.bondings[new_molid].clone();
        let old_mol = self.bondings[old_molid].clone();
        if new_mol.end() == old_mol.start() {
            // Just update the molecules ids
            for i in old_mol.indexes() {
                self.molecule_ids[i] = new_molid;
            }
        } else {
            // Move the particles close together
            let mut new_index = new_mol.end();
            for i in old_mol.indexes() {
                // Remove particles from the old position, and insert it to the
                // new one. The indexes are valid during the movement, because
                // we insert a new particle for each particle removed.
                let particle = self.particles.remove(i);
                self.particles.insert(new_index, particle);
                // Update molecule_ids
                let _ = self.molecule_ids.remove(i);
                self.molecule_ids.insert(new_index, new_molid);
                new_index += 1;
            }
        }
        let mut old_mol = self.bondings[old_molid].clone();
        let size = old_mol.size() as isize;
        // translate all indexes in the molecules between new_mol and old_mol
        for molecule in
            self.bondings.iter_mut().skip(new_molid + 1).take(old_molid - new_molid - 1)
        {
            molecule.translate_by(size);
        }
        // Update molid for all particles after the old molecule
        for molid in self.molecule_ids.iter_mut().skip(old_mol.end()) {
            *molid -= 1;
        }
        let delta = old_mol.start() - new_mol.end();
        old_mol.translate_by(-(delta as isize));
        new_mol.merge_with(old_mol);
        self.bondings[new_molid] = new_mol;
        let _ = self.bondings.remove(old_molid);
        debug_assert!(check_molid_sorted(&self.molecule_ids), "Unsorted molecule ids {:?}", self.molecule_ids);
        return delta;
    }
fn merge_molecules ( & mut self , first : usize , second : usize ) -> usize { // Do not try to merge a molecule with itself if first == second { return 0 ; } let ( new_molid , old_molid ) = if first < second { ( first , second ) } else { ( second , first ) } ; let mut new_mol = self . bondings [ new_molid ] . clone ( ) ; let old_mol = self . bondings [ old_molid ] . clone ( ) ; if new_mol . end ( ) == old_mol . start ( ) { // Just update the molecules ids for i in old_mol . indexes ( ) { self . molecule_ids [ i ] = new_molid ; } } else { // Move the particles close together let mut new_index = new_mol . end ( ) ; for i in old_mol . indexes ( ) { // Remove particles from the old position, and insert it to the // new one. The indexes are valid during the movement, because // we insert a new particle for each particle removed. let particle = self . particles . remove ( i ) ; self . particles . insert ( new_index , particle ) ; // Update molecule_ids let _ = self . molecule_ids . remove ( i ) ; self . molecule_ids . insert ( new_index , new_molid ) ; new_index += 1 ; } } let mut old_mol = self . bondings [ old_molid ] . clone ( ) ; let size = old_mol . size ( ) as isize ; // translate all indexes in the molecules between new_mol and old_mol for molecule in self . bondings . iter_mut ( ) . skip ( new_molid + 1 ) . take ( old_molid - new_molid - 1 ) { molecule . translate_by ( size ) ; } // Update molid for all particles after the old molecule for molid in self . molecule_ids . iter_mut ( ) . skip ( old_mol . end ( ) ) { * molid -= 1 ; } let delta = old_mol . start ( ) - new_mol . end ( ) ; old_mol . translate_by ( - ( delta as isize ) ) ; new_mol . merge_with ( old_mol ) ; self . bondings [ new_molid ] = new_mol ; let _ = self . bondings . remove ( old_molid ) ; debug_assert ! ( check_molid_sorted ( self molecule_ids ) " " self molecule_ids ) ; return delta ; }
Merge the molecules at indexes `first` and `second` into one
molecule.
Merge the molecules at indexes ` first ` and ` second ` into one molecule .
&mut self
first
usize
second
usize