diff --git a/src/FlatLayerVisualisation.cpp b/src/FlatLayerVisualisation.cpp index 42febb2..d5b549a 100644 --- a/src/FlatLayerVisualisation.cpp +++ b/src/FlatLayerVisualisation.cpp @@ -2,6 +2,7 @@ #include #include "FlatLayerVisualisation.hpp" +#include "Range.hpp" using namespace std; using namespace fmri; @@ -39,7 +40,7 @@ FlatLayerVisualisation::FlatLayerVisualisation(const LayerData &layer, Ordering auto scalingMax = max(abs(*minElem), abs(*maxElem)); int v = 0; - for (int i = 0; i < limit; ++i) { + for (int i : Range(limit)) { setVertexPositions(i, vertexBuffer.get() + 12 * i); const int vertexBase = i * 4; diff --git a/src/MultiImageVisualisation.cpp b/src/MultiImageVisualisation.cpp index d1b3df6..26345d3 100644 --- a/src/MultiImageVisualisation.cpp +++ b/src/MultiImageVisualisation.cpp @@ -1,6 +1,7 @@ #include #include "MultiImageVisualisation.hpp" #include "glutils.hpp" +#include "Range.hpp" using namespace fmri; using namespace std; @@ -28,7 +29,7 @@ MultiImageVisualisation::MultiImageVisualisation(const fmri::LayerData &layer) auto dataPtr = layer.data(); - for (int i = 0; i < channels; ++i) { + for (auto i : Range(channels)) { textureReferences[i] = loadTexture(dataPtr, width, height); vertexBuffer[v++] = 0; vertexBuffer[v++] = 0 + 3 * r; diff --git a/src/Range.hpp b/src/Range.hpp index ffbd0ea..ff10f1c 100644 --- a/src/Range.hpp +++ b/src/Range.hpp @@ -2,6 +2,14 @@ namespace fmri { + + /** + * Iterable that produces a specific range of integers. + * + * Useful to make an automatically typed for loop over an unknown integer type. + * + * @tparam T The integer type to use. + */ template class Range { @@ -10,9 +18,20 @@ namespace fmri T end_; public: - explicit Range(const T &num) : start_(0), end_(num) {}; + /** + * Construct a range from 0 to num, non inclusive. + * + * @param num + */ + constexpr explicit Range(const T &num) : start_(0), end_(num) {}; - Range(const T &start, const T &end) : start_(start), end_(end) {}; + /** + * Construct a range from start to end, non inclusive. + * + * @param start + * @param end + */ + constexpr Range(const T &start, const T &end) : start_(start), end_(end) {}; class Iter { @@ -20,18 +39,28 @@ namespace fmri T cur_; public: - explicit Iter(const T &cur) : cur_(cur) + constexpr explicit Iter(const T &cur) : cur_(cur) {}; - bool operator!=(const Iter& o) { return o.cur_ != cur_; } - Iter&operator++() { ++cur_; return *this; } + typedef std::bidirectional_iterator_tag iterator_category; + typedef T value_type; + typedef typename std::make_signed::type difference_type; + typedef T& reference; + typedef T* pointer; - T &operator*() { return cur_; } + constexpr bool operator!=(const Iter& o) { return o.cur_ != cur_; } + constexpr Iter&operator++() { ++cur_; return *this; } + constexpr Iter&operator--() { --cur_; return *this; } + + constexpr const T &operator*() { return cur_; } }; typedef Iter const_iterator; + typedef std::reverse_iterator const_reverse_iterator; - const_iterator begin() const { return Iter(start_); } - const_iterator end() const { return Iter(end_); } + constexpr const_iterator begin() const { return Iter(start_); } + constexpr const_iterator end() const { return Iter(end_); } + constexpr const_reverse_iterator rbegin() const { return std::make_reverse_iterator(end()); } + constexpr const_reverse_iterator rend() const { return std::make_reverse_iterator(begin()); } }; -} \ No newline at end of file +} diff --git a/src/Simulator.cpp b/src/Simulator.cpp index f917398..4745826 100644 --- a/src/Simulator.cpp +++ b/src/Simulator.cpp @@ -9,6 +9,7 @@ #include #include "Simulator.hpp" +#include "Range.hpp" using namespace caffe; using namespace std; @@ -77,16 +78,17 @@ void Simulator::Impl::loadMeans(const string &means_file) CHECK_EQ(mean_blob.channels(), num_channels) << "Number of channels should match!" << endl; vector channels; - float* data = mean_blob.mutable_cpu_data(); - for (unsigned int i = 0; i < this->num_channels; ++i) { - channels.emplace_back(mean_blob.height(), mean_blob.width(), CV_32FC1, data); - data += mean_blob.height() * mean_blob.width(); - } + float *data = mean_blob.mutable_cpu_data(); + for (auto i : Range(num_channels)) { + (void)i;// Suppress unused warning + channels.emplace_back(mean_blob.height(), mean_blob.width(), CV_32FC1, data); + data += mean_blob.height() * mean_blob.width(); + } cv::Mat mean; merge(channels, mean); - this->means = cv::Mat(this->input_geometry, mean.type(), cv::mean(mean)); + this->means = cv::Mat(input_geometry, mean.type(), cv::mean(mean)); } vector Simulator::Impl::simulate(const string& image_file) @@ -107,7 +109,7 @@ vector Simulator::Impl::simulate(const string& image_file) const auto& names = net.layer_names(); const auto& results = net.top_vecs(); - for (unsigned int i = 0; i < names.size(); ++i) { + for (auto i : Range(names.size())) { CHECK_EQ(results[i].size(), 1) << "Multiple outputs per layer are not supported!" << endl; const auto blob = results[i][0]; @@ -126,7 +128,8 @@ vector Simulator::Impl::getWrappedInputLayer() const int height = input_geometry.height; DType* input_data = input_layer->mutable_cpu_data(); - for (unsigned int i = 0; i < num_channels; i++) { + for (auto i : Range(num_channels)) { + (void)i;// Suppress unused warning channels.emplace_back(height, width, CV_32FC1, input_data); input_data += width * height; } @@ -195,7 +198,9 @@ void Simulator::Impl::computeLayerInfo() const auto& names = net.layer_names(); const auto& layers = net.layers(); - for (auto i = 0u; i < names.size() && i < layers.size(); ++i) { + CHECK_EQ(names.size(), layers.size()) << "Size mismatch"; + + for (auto i : Range(names.size())) { auto& layer = layers[i]; layerInfo_.emplace(names[i], LayerInfo(names[i], layer->type(), layer->blobs())); } diff --git a/src/camera.cpp b/src/camera.cpp index 1776a0a..09bd9fc 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -43,7 +43,7 @@ static void move(unsigned char key) speed *= -1; } - for (unsigned int i = 0; i < 3; ++i) { + for (auto i = 0; i < 3; ++i) { camera.pos[i] += speed * dir[i]; } } diff --git a/src/main.cpp b/src/main.cpp index 38e6530..9d7981a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,6 +13,7 @@ #include "LayerVisualisation.hpp" #include "FlatLayerVisualisation.hpp" #include "MultiImageVisualisation.hpp" +#include "Range.hpp" using namespace std; using namespace fmri; @@ -75,7 +76,7 @@ static void render() glPushMatrix(); glTranslatef(5 * dataSet.size(), 0, 0); - for (unsigned int i = 0; i < dataSet.size(); ++i) { + for (auto i : Range(dataSet.size())) { glPushMatrix(); renderLayerName(dataSet[i]); rendererData.layerVisualisations[i]->render();