Debian Codegraph @

avogadro/core/layer.cpp

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 #include "layer.h"
7 #include <cassert>
8
9 namespace Avogadro::Core {
10
11 void Layer::addAtom(size_t layer)
12 {
13 addAtom(layer, m_atomAndLayers.size());
14 }
15
16 void Layer::addAtom(size_t layer, Index atom)
17 {
18 assert(layer <= m_maxLayer);
19 if (atom == m_atomAndLayers.size()) {
20 m_atomAndLayers.push_back(layer);
21 } else if (atom > m_atomAndLayers.size()) {
22 m_atomAndLayers.resize(layer + 1, MaxIndex);
23 m_atomAndLayers[atom] = layer;
24 } else {
25 m_atomAndLayers[atom] = layer;
26 }
27 }
28
29 void Layer::addAtomToActiveLayer(Index atom)
30 {
31 addAtom(m_activeLayer, atom);
32 }
33
34 void Layer::setActiveLayer(size_t layer)
35 {
36 assert(layer <= m_maxLayer + 1);
37 m_activeLayer = layer;
38 }
39
40 void Layer::removeAtom(Index atom)
41 {
42 m_atomAndLayers.swapAndPop(atom);
43 }
44
45 void Layer::addLayer()
46 {
47 ++m_maxLayer;
48 }
49
50 void Layer::addLayer(size_t layer)
51 {
52 assert(layer <= m_maxLayer + 1);
53 for (auto& atomLayer : m_atomAndLayers) {
54 if (atomLayer >= layer) {
55 ++atomLayer;
56 }
57 }
58 ++m_maxLayer;
59 }
60
61 size_t Layer::getLayerID(Index atom) const
62 {
63 if (atom >= m_atomAndLayers.size()) {
64 return MaxIndex;
65 } else {
66 return m_atomAndLayers[atom];
67 }
68 }
69
70 void Layer::clear()
71 {
72 m_atomAndLayers.clear();
73 m_activeLayer = m_maxLayer = 0;
74 }
75
76 void Layer::resize(size_t count)
77 {
78 if (count == m_atomAndLayers.size())
79 return;
80 if (count < m_atomAndLayers.size()) {
81 m_atomAndLayers.resize(count);
82 } else {
83 // Default new atoms to the active layer
84 m_atomAndLayers.resize(count, m_activeLayer);
85 }
86 }
87
88 size_t Layer::activeLayer() const
89 {
90 return m_activeLayer;
91 }
92
93 size_t Layer::maxLayer() const
94 {
95 return m_maxLayer;
96 }
97
98 size_t Layer::atomCount() const
99 {
100 return m_atomAndLayers.size();
101 }
102
103 void Layer::removeLayer(size_t layer)
104 {
105 assert(layer <= m_maxLayer);
106 if (m_maxLayer >= 1) {
107 for (auto it = m_atomAndLayers.begin(); it != m_atomAndLayers.end();) {
108 if (*it == layer) {
109 it = m_atomAndLayers.erase(it);
110 } else {
111 if (*it > layer) {
112 --(*it);
113 }
114 ++it;
115 }
116 }
117 --m_maxLayer;
118 }
119 }
120
121 void Layer::swapLayer(Index a, Index b)
122 {
123 // Allow Argument Dependent Lookup for swap
124 using std::swap;
125
126 swap(m_atomAndLayers[a], m_atomAndLayers[b]);
127 }
128
129 size_t Layer::layerCount() const
130 {
131 return m_maxLayer + 1;
132 }
133
134 } // namespace Avogadro::Core