GQCP
Loading...
Searching...
No Matches
StepCollection.hpp
Go to the documentation of this file.
1// This file is part of GQCG-GQCP.
2//
3// Copyright (C) 2017-2020 the GQCG developers
4//
5// GQCG-GQCP is free software: you can redistribute it and/or modify
6// it under the terms of the GNU Lesser General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// GQCG-GQCP is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU Lesser General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public License
16// along with GQCG-GQCP. If not, see <http://www.gnu.org/licenses/>.
17
18#pragma once
19
20
22#include "Utilities/memory.hpp"
24
25#include <boost/format.hpp>
26
27#include <vector>
28
29
30namespace GQCP {
31
32
40template <typename _Environment>
41class StepCollection: public Step<_Environment> {
42public:
43 using Environment = _Environment;
44
45
46private:
47 std::vector<std::shared_ptr<Step<Environment>>> steps; // the consecutive steps that this collection consists of
48
49
50public:
51 /*
52 * PUBLIC OVERRIDDEN METHODS
53 */
54
58 std::string description() const override {
59
60 std::string description_string = (boost::format("An algorithmic step consisting of %s algorithmic steps:\n") % this->numberOfSteps()).str();
61
62 for (size_t i = 0; i < this->numberOfSteps(); i++) {
63 const auto& step = this->steps[i];
64 description_string += (boost::format("\t%s. %s\n") % std::to_string(i + 1) % step->description()).str();
65 }
66 return description_string;
67 }
68
69
75 void execute(Environment& environment) override {
76 for (const auto& step : this->steps) {
77 step->execute(environment);
78 }
79 }
80
81
82 /*
83 * PUBLIC METHODS
84 */
85
91 template <typename Z = Step<Environment>>
93 this->steps.push_back(std::make_shared<Z>(step));
94 return *this;
95 }
96
97
104 template <typename Z = Step<Environment>>
106
107 // Check if the index is out of bounds.
108 if (index > this->numberOfSteps()) {
109 throw std::invalid_argument("StepCollection::insert(const Z&, const size_t): Cannot insert at the given index.");
110 }
111
112
113 // Actually insert in the std::vector of steps.
114 if (index < this->numberOfSteps()) { // within the current bounds
115 const auto it = this->steps.begin();
116 this->steps.insert(it + index, std::make_shared<Z>(step)); // inserting at an given index goes through an iterator
117 }
118
119 else if (index == this->numberOfSteps()) { // at the end
120 this->add(step);
121 }
122 }
123
124
128 size_t numberOfSteps() const { return this->steps.size(); }
129
130
136 void remove(const size_t index) {
137 this->steps.erase(this->steps.begin() + index);
138 }
139
140
147 template <typename Z = Step<Environment>>
149 this->steps[index] = std::make_shared<Z>(step);
150 }
151};
152
153
154} // namespace GQCP
Definition: StepCollection.hpp:41
size_t numberOfSteps() const
Definition: StepCollection.hpp:128
enable_if_t< std::is_same< Environment, typename Z::Environment >::value, void > insert(const Z &step, const size_t index)
Definition: StepCollection.hpp:105
void execute(Environment &environment) override
Definition: StepCollection.hpp:75
std::string description() const override
Definition: StepCollection.hpp:58
enable_if_t< std::is_same< Environment, typename Z::Environment >::value, void > replace(const Z &step, const size_t index)
Definition: StepCollection.hpp:148
void remove(const size_t index)
Definition: StepCollection.hpp:136
_Environment Environment
Definition: StepCollection.hpp:43
enable_if_t< std::is_same< Environment, typename Z::Environment >::value, StepCollection< Environment > & > add(const Z &step)
Definition: StepCollection.hpp:92
Definition: Step.hpp:37
Definition: BaseOneElectronIntegralBuffer.hpp:25
typename std::enable_if< B, T >::type enable_if_t
Definition: type_traits.hpp:37