Remove all explicitly typed for loops.

This commit is contained in:
2018-01-22 15:55:32 +01:00
parent ef70f913bf
commit cf5dd28fef
6 changed files with 59 additions and 22 deletions

View File

@@ -2,6 +2,7 @@
#include <GL/gl.h> #include <GL/gl.h>
#include "FlatLayerVisualisation.hpp" #include "FlatLayerVisualisation.hpp"
#include "Range.hpp"
using namespace std; using namespace std;
using namespace fmri; using namespace fmri;
@@ -39,7 +40,7 @@ FlatLayerVisualisation::FlatLayerVisualisation(const LayerData &layer, Ordering
auto scalingMax = max(abs(*minElem), abs(*maxElem)); auto scalingMax = max(abs(*minElem), abs(*maxElem));
int v = 0; int v = 0;
for (int i = 0; i < limit; ++i) { for (int i : Range(limit)) {
setVertexPositions(i, vertexBuffer.get() + 12 * i); setVertexPositions(i, vertexBuffer.get() + 12 * i);
const int vertexBase = i * 4; const int vertexBase = i * 4;

View File

@@ -1,6 +1,7 @@
#include <glog/logging.h> #include <glog/logging.h>
#include "MultiImageVisualisation.hpp" #include "MultiImageVisualisation.hpp"
#include "glutils.hpp" #include "glutils.hpp"
#include "Range.hpp"
using namespace fmri; using namespace fmri;
using namespace std; using namespace std;
@@ -28,7 +29,7 @@ MultiImageVisualisation::MultiImageVisualisation(const fmri::LayerData &layer)
auto dataPtr = layer.data(); auto dataPtr = layer.data();
for (int i = 0; i < channels; ++i) { for (auto i : Range(channels)) {
textureReferences[i] = loadTexture(dataPtr, width, height); textureReferences[i] = loadTexture(dataPtr, width, height);
vertexBuffer[v++] = 0; vertexBuffer[v++] = 0;
vertexBuffer[v++] = 0 + 3 * r; vertexBuffer[v++] = 0 + 3 * r;

View File

@@ -2,6 +2,14 @@
namespace fmri 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 T> template<class T>
class Range class Range
{ {
@@ -10,9 +18,20 @@ namespace fmri
T end_; T end_;
public: 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 class Iter
{ {
@@ -20,18 +39,28 @@ namespace fmri
T cur_; T cur_;
public: 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_; } typedef std::bidirectional_iterator_tag iterator_category;
Iter&operator++() { ++cur_; return *this; } typedef T value_type;
typedef typename std::make_signed<T>::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 Iter const_iterator;
typedef std::reverse_iterator<Iter> const_reverse_iterator;
const_iterator begin() const { return Iter(start_); } constexpr const_iterator begin() const { return Iter(start_); }
const_iterator end() const { return Iter(end_); } 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()); }
}; };
} }

View File

@@ -9,6 +9,7 @@
#include <opencv2/imgproc/imgproc.hpp> #include <opencv2/imgproc/imgproc.hpp>
#include "Simulator.hpp" #include "Simulator.hpp"
#include "Range.hpp"
using namespace caffe; using namespace caffe;
using namespace std; 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; CHECK_EQ(mean_blob.channels(), num_channels) << "Number of channels should match!" << endl;
vector<cv::Mat> channels; vector<cv::Mat> channels;
float* data = mean_blob.mutable_cpu_data(); float *data = mean_blob.mutable_cpu_data();
for (unsigned int i = 0; i < this->num_channels; ++i) { for (auto i : Range(num_channels)) {
channels.emplace_back(mean_blob.height(), mean_blob.width(), CV_32FC1, data); (void)i;// Suppress unused warning
data += mean_blob.height() * mean_blob.width(); channels.emplace_back(mean_blob.height(), mean_blob.width(), CV_32FC1, data);
} data += mean_blob.height() * mean_blob.width();
}
cv::Mat mean; cv::Mat mean;
merge(channels, 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<LayerData> Simulator::Impl::simulate(const string& image_file) vector<LayerData> Simulator::Impl::simulate(const string& image_file)
@@ -107,7 +109,7 @@ vector<LayerData> Simulator::Impl::simulate(const string& image_file)
const auto& names = net.layer_names(); const auto& names = net.layer_names();
const auto& results = net.top_vecs(); 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; CHECK_EQ(results[i].size(), 1) << "Multiple outputs per layer are not supported!" << endl;
const auto blob = results[i][0]; const auto blob = results[i][0];
@@ -126,7 +128,8 @@ vector<cv::Mat> Simulator::Impl::getWrappedInputLayer()
const int height = input_geometry.height; const int height = input_geometry.height;
DType* input_data = input_layer->mutable_cpu_data(); 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); channels.emplace_back(height, width, CV_32FC1, input_data);
input_data += width * height; input_data += width * height;
} }
@@ -195,7 +198,9 @@ void Simulator::Impl::computeLayerInfo()
const auto& names = net.layer_names(); const auto& names = net.layer_names();
const auto& layers = net.layers(); 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]; auto& layer = layers[i];
layerInfo_.emplace(names[i], LayerInfo(names[i], layer->type(), layer->blobs())); layerInfo_.emplace(names[i], LayerInfo(names[i], layer->type(), layer->blobs()));
} }

View File

@@ -43,7 +43,7 @@ static void move(unsigned char key)
speed *= -1; speed *= -1;
} }
for (unsigned int i = 0; i < 3; ++i) { for (auto i = 0; i < 3; ++i) {
camera.pos[i] += speed * dir[i]; camera.pos[i] += speed * dir[i];
} }
} }

View File

@@ -13,6 +13,7 @@
#include "LayerVisualisation.hpp" #include "LayerVisualisation.hpp"
#include "FlatLayerVisualisation.hpp" #include "FlatLayerVisualisation.hpp"
#include "MultiImageVisualisation.hpp" #include "MultiImageVisualisation.hpp"
#include "Range.hpp"
using namespace std; using namespace std;
using namespace fmri; using namespace fmri;
@@ -75,7 +76,7 @@ static void render()
glPushMatrix(); glPushMatrix();
glTranslatef(5 * dataSet.size(), 0, 0); glTranslatef(5 * dataSet.size(), 0, 0);
for (unsigned int i = 0; i < dataSet.size(); ++i) { for (auto i : Range(dataSet.size())) {
glPushMatrix(); glPushMatrix();
renderLayerName(dataSet[i]); renderLayerName(dataSet[i]);
rendererData.layerVisualisations[i]->render(); rendererData.layerVisualisations[i]->render();