Debian Codegraph @

avogadro/core/atomtyper-inline.h

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #ifndef AVOGADRO_CORE_ATOMTYPER_INLINE_H
7 #define AVOGADRO_CORE_ATOMTYPER_INLINE_H
8
9 #include "atomtyper.h"
10
11 #include "atom.h"
12 #include "molecule.h"
13
14 namespace Avogadro::Core {
15
16 template <typename OutputType>
17 AtomTyper<OutputType>::AtomTyper(const Molecule* mol) : m_molecule(mol)
18 {
19 }
20
21 template <typename OutputType>
22 AtomTyper<OutputType>::~AtomTyper()
23 {
24 }
25
26 template <typename OutputType>
27 void AtomTyper<OutputType>::setMolecule(const Molecule* mol)
28 {
29 if (m_molecule != mol) {
30 m_molecule = mol;
31 reset();
32 }
33 }
34
35 template <typename OutputType>
36 void AtomTyper<OutputType>::run()
37 {
38 initialize();
39 Index numAtoms = m_molecule ? m_molecule->atomCount() : 0;
40 for (Index atomId = 0; atomId < numAtoms; ++atomId) {
41 Atom atom = m_molecule->atom(atomId);
42 m_types.push_back(type(atom));
43 }
44 }
45
46 template <typename OutputType>
47 OutputType AtomTyper<OutputType>::atomType(const Atom& atom)
48 {
49 OutputType result{};
50 if (atom.isValid() && atom.molecule() == m_molecule) {
51
52 // Return the calculated value if we've already run the typer.
53 if (atom.index() < m_types.size()) {
54 result = m_types[atom.index()];
55 } else {
56 initialize();
57 result = type(atom);
58 }
59 }
60 return result;
61 }
62
63 template <typename OutputType>
64 Array<OutputType> AtomTyper<OutputType>::types() const
65 {
66 return m_types;
67 }
68
69 template <typename OutputType>
70 void AtomTyper<OutputType>::reset()
71 {
72 m_types.clear();
73 }
74
75 template <typename OutputType>
76 void AtomTyper<OutputType>::initialize()
77 {
78 m_types.reserve(m_molecule ? m_molecule->atomCount() : 0);
79 }
80
81 } // namespace Avogadro::Core
82
83 #endif // AVOGADRO_CORE_ATOMTYPER_INLINE_H