Skip to content

Commit

Permalink
refs #9155. Refactor current working.
Browse files Browse the repository at this point in the history
Avoid all vector resizing possible. Pre-cache as much as possible.
  • Loading branch information
OwenArnold committed Mar 11, 2014
1 parent 8a1fb0f commit 8da661a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 53 deletions.
6 changes: 1 addition & 5 deletions Code/Mantid/Framework/Kernel/inc/MantidKernel/Utils.h
Expand Up @@ -4,7 +4,6 @@
#include "MantidKernel/DllConfig.h"
#include <cmath>
#include <vector>
#include <iostream>

namespace Mantid
{
Expand Down Expand Up @@ -268,14 +267,11 @@ namespace Utils
* @param num_bins
* @return True if the are neighbours, otherwise false.
*/
inline bool isNeighbourOfSubjectt(const size_t ndims, const size_t neighbour_linear_index, const size_t* subject_indices, const size_t * num_bins, const size_t * index_max)
inline bool isNeighbourOfSubject(const size_t ndims, const size_t neighbour_linear_index, const size_t* subject_indices, const size_t * num_bins, const size_t * index_max)
{
size_t neighbour_indices[ndims];
Utils::NestedForLoop::GetIndicesFromLinearIndex(ndims, neighbour_linear_index, num_bins, index_max, neighbour_indices);

std::cout << "Subject Indexes " << subject_indices[0] << "\t" << subject_indices[1] << std::endl;
std::cout << "Neighbour Indexes " << neighbour_indices[0] << "\t" << neighbour_indices[1] << std::endl;

for(size_t ind = 0; ind < ndims; ++ind)
{
auto diff = std::abs(static_cast<long>(subject_indices[ind]) -static_cast<long>(neighbour_indices[ind]));
Expand Down
76 changes: 28 additions & 48 deletions Code/Mantid/Framework/MDEvents/src/MDHistoWorkspaceIterator.cpp
Expand Up @@ -12,7 +12,18 @@ namespace Mantid
{
namespace MDEvents
{

namespace
{
size_t integerPower(const size_t base, const size_t pow)
{
size_t result = 1;
for(size_t i = 0; i < pow; ++i)
{
result *= base;
}
return result;
}
}

//----------------------------------------------------------------------------------------------
/** Constructor
Expand Down Expand Up @@ -138,13 +149,7 @@ namespace MDEvents
next();
}

const size_t neighbourSpan = 3;
size_t totalNeighbours = neighbourSpan;
for (size_t i = 1; i < m_nd; i++)
{
totalNeighbours = totalNeighbours * neighbourSpan;
}
auto temp = std::vector<long>(totalNeighbours, 0);
auto temp = std::vector<long>(integerPower(3, m_nd), 0);
m_permutations.swap(temp);
}

Expand Down Expand Up @@ -374,29 +379,6 @@ namespace MDEvents
return m_pos;
}

bool isNeighbourOfSubject(const size_t ndims, const size_t neighbour_linear_index, const size_t* subject_indices, const size_t * num_bins, const size_t * index_max)
{
size_t neighbour_indices[ndims];
Utils::NestedForLoop::GetIndicesFromLinearIndex(ndims, neighbour_linear_index, num_bins, index_max, neighbour_indices);


auto a = subject_indices[0];
auto b = subject_indices[1];

auto n1 = neighbour_indices[0];
auto n2 = neighbour_indices[1];

for(size_t ind = 0; ind < ndims; ++ind)
{
auto diff = std::abs(static_cast<long>(subject_indices[ind]) -static_cast<long>(neighbour_indices[ind]));
if (diff > 1)
{
return false;
}
}
return true;
}

/**
* Gets indexes of bins/pixels/boxes neighbouring the present iterator location.
* The number of neighbour indexes returned will depend upon the dimensionality of the workspace as well as the presence
Expand All @@ -410,36 +392,34 @@ namespace MDEvents
m_index);

long offset = 1;
const int base = 3;

std::vector<long> permutations(3);
permutations[0] = 0;
permutations[1] = 1;
permutations[2] = -1;
m_permutations[0] = 0;
m_permutations[1] = 1;
m_permutations[2] = -1;

size_t nPermutations = permutations.size();
size_t nPermutations = 3;
for(size_t j = 1; j < m_nd; ++j)
{
offset = offset * m_ws->getDimension(j-1)->getNBins();
for(size_t k = 0; k <nPermutations; ++k)
size_t counter = nPermutations;
for(size_t k = 0; k <nPermutations; k+=1, counter+=2)
{
long newVariant = permutations[k] + offset;
permutations.push_back(newVariant);
permutations.push_back(-1*newVariant);
long newVariant = m_permutations[k] + offset;
m_permutations[counter] = newVariant;
m_permutations[counter+1] = (-1*newVariant);
}
nPermutations = permutations.size();
nPermutations *= 3;
}

std::vector<size_t> neighbourIndexes;
for(size_t i = 0; i < permutations.size(); ++i)
std::vector<size_t> neighbourIndexes; // Accumulate neighbour indexes.
for(size_t i = 0; i < nPermutations; ++i)
{
if (permutations[i] == 0)
if (m_permutations[i] == 0)
{
continue;
}

size_t neighbour_index = m_pos + permutations[i];
if( isNeighbourOfSubject(m_nd, neighbour_index, m_index, m_indexMaker, m_indexMax ) )
size_t neighbour_index = m_pos + m_permutations[i];
if( Utils::isNeighbourOfSubject(m_nd, neighbour_index, m_index, m_indexMaker, m_indexMax ) )
{
neighbourIndexes.push_back(neighbour_index);
}
Expand Down

0 comments on commit 8da661a

Please sign in to comment.