Commit 5e8c6cdb authored by Tom Perkins's avatar Tom Perkins
Browse files

Refactor some in-and-out functions in Kernel

re #15286
parent b32622af
......@@ -115,9 +115,8 @@ void findFacesAtIndex(const size_t linearIndex, IMDIterator *mdIterator,
const int &emptyLabelId,
const std::vector<size_t> &imageShape,
ClusterFaces &localClusterFaces) {
std::vector<size_t> indexes;
Mantid::Kernel::Utils::getIndicesFromLinearIndex(linearIndex, imageShape,
indexes);
auto indexes =
Mantid::Kernel::Utils::getIndicesFromLinearIndex(linearIndex, imageShape);
const auto neighbours = mdIterator->findNeighbourIndexesFaceTouching();
for (auto neighbourLinearIndex : neighbours) {
......@@ -128,9 +127,8 @@ void findFacesAtIndex(const size_t linearIndex, IMDIterator *mdIterator,
// We have an edge!
// In which dimension is the edge?
std::vector<size_t> neighbourIndexes;
Mantid::Kernel::Utils::getIndicesFromLinearIndex(
neighbourLinearIndex, imageShape, neighbourIndexes);
auto neighbourIndexes = Mantid::Kernel::Utils::getIndicesFromLinearIndex(
neighbourLinearIndex, imageShape);
for (size_t j = 0; j < imageShape.size(); ++j) {
if (indexes[j] != neighbourIndexes[j]) {
const bool maxEdge = neighbourLinearIndex > linearIndex;
......
......@@ -2339,7 +2339,6 @@ void FilterEventsByLogValuePreNexus::setProtonCharge(
*/
void FilterEventsByLogValuePreNexus::loadPixelMap(const std::string &filename) {
this->m_usingMappingFile = false;
this->m_pixelmap.clear();
// check that there is a mapping file
if (filename.empty()) {
......@@ -2355,7 +2354,7 @@ void FilterEventsByLogValuePreNexus::loadPixelMap(const std::string &filename) {
BinaryFile<PixelType> pixelmapFile(filename);
PixelType max_pid = static_cast<PixelType>(pixelmapFile.getNumElements());
// Load all the data
pixelmapFile.loadAllInto(this->m_pixelmap);
this->m_pixelmap = pixelmapFile.loadAllIntoVector();
// Check for funky file
if (std::find_if(m_pixelmap.begin(), m_pixelmap.end(),
......
......@@ -841,7 +841,6 @@ void LoadEventPreNexus::setProtonCharge(
*/
void LoadEventPreNexus::loadPixelMap(const std::string &filename) {
this->using_mapping_file = false;
this->pixelmap.clear();
// check that there is a mapping file
if (filename.empty()) {
......@@ -856,7 +855,7 @@ void LoadEventPreNexus::loadPixelMap(const std::string &filename) {
BinaryFile<PixelType> pixelmapFile(filename);
PixelType max_pid = static_cast<PixelType>(pixelmapFile.getNumElements());
// Load all the data
pixelmapFile.loadAllInto(this->pixelmap);
this->pixelmap = pixelmapFile.loadAllIntoVector();
// Check for funky file
if (std::find_if(pixelmap.begin(), pixelmap.end(),
......
......@@ -1248,7 +1248,6 @@ void LoadEventPreNexus2::setProtonCharge(
*/
void LoadEventPreNexus2::loadPixelMap(const std::string &filename) {
this->using_mapping_file = false;
this->pixelmap.clear();
// check that there is a mapping file
if (filename.empty()) {
......@@ -1263,7 +1262,7 @@ void LoadEventPreNexus2::loadPixelMap(const std::string &filename) {
BinaryFile<PixelType> pixelmapFile(filename);
PixelType max_pid = static_cast<PixelType>(pixelmapFile.getNumElements());
// Load all the data
pixelmapFile.loadAllInto(this->pixelmap);
this->pixelmap = pixelmapFile.loadAllIntoVector();
// Check for funky file
if (std::find_if(pixelmap.begin(), pixelmap.end(),
......
......@@ -177,9 +177,6 @@ void LoadPreNexusMonitors::exec() {
MatrixWorkspace_sptr localWorkspace = WorkspaceFactory::Instance().create(
"Workspace2D", nMonitors, numberTimeBins, tchannels);
// temp buffer for file reading
std::vector<uint32_t> buffer;
for (int i = 0; i < nMonitors; i++) {
// Now lets actually read the monitor files..
Poco::Path pMonitorFilename(dirPath, monitorFilenames[i]);
......@@ -188,7 +185,8 @@ void LoadPreNexusMonitors::exec() {
<< std::endl;
Kernel::BinaryFile<uint32_t> monitorFile(pMonitorFilename.toString());
monitorFile.loadAllInto(buffer);
// temp buffer for file reading
std::vector<uint32_t> buffer = monitorFile.loadAllIntoVector();
MantidVec intensity(buffer.begin(), buffer.end());
// Copy the same data into the error array
......
......@@ -331,12 +331,12 @@ void FakeMD::addFakeRegularData(const std::vector<double> &params,
gridSize *= indexMax[d];
}
// Create all the requested events
std::vector<size_t> indexes;
size_t cellCount(0);
for (size_t i = 0; i < num; ++i) {
coord_t centers[nd];
Kernel::Utils::getIndicesFromLinearIndex(cellCount, indexMax, indexes);
auto indexes =
Kernel::Utils::getIndicesFromLinearIndex(cellCount, indexMax);
++cellCount;
if (cellCount >= gridSize)
cellCount = 0;
......
......@@ -140,14 +140,13 @@ public:
* Loads the entire contents of the file into a std::vector.
* The file is closed once done.
* @param data :: The contents to load into the file
* @returns :: vector with contents of the file
*/
void loadAllInto(std::vector<T> &data) {
std::vector<T> loadAllIntoVector() {
if (!handle) {
throw std::runtime_error("BinaryFile: file is not open.");
}
// Clear the vector
data.clear();
std::vector<T> data;
// A buffer to load from
size_t buffer_size = getBufferSize(num_elements);
......@@ -169,6 +168,8 @@ public:
this->close();
// Free memory
delete[] buffer;
return data;
}
//-----------------------------------------------------------------------------
......
......@@ -254,21 +254,20 @@ inline void getIndicesFromLinearIndex(const size_t linear_index,
* @param num_bins :: a vector of [numDims] size, where numDims is the loop
*depth and each element equal to number of bins in the correspondent dimension
*
* @param[out] out_indices :: the vector, sized numDims, which will be
* @returns :: a vector, sized numDims, which will be
* filled with the index for each dimension, given the linear index
*/
inline void getIndicesFromLinearIndex(const size_t linear_index,
const std::vector<size_t> &num_bins,
std::vector<size_t> &out_indices) {
if (num_bins.empty()) {
out_indices.clear();
return;
} else {
inline std::vector<size_t>
getIndicesFromLinearIndex(const size_t linear_index,
const std::vector<size_t> &num_bins) {
std::vector<size_t> out_indices;
if (!num_bins.empty()) {
size_t nBins = num_bins.size();
out_indices.resize(nBins);
getIndicesFromLinearIndex(linear_index, &num_bins[0], nBins,
&out_indices[0]);
}
return out_indices;
}
/**
......
......@@ -94,7 +94,7 @@ public:
Poco::File(dummy_file).remove();
}
void testLoadAllInto() {
void testLoadAllIntoVector() {
MakeDummyFile(dummy_file, 20 * 8);
file.open(dummy_file);
......@@ -103,7 +103,7 @@ public:
TS_ASSERT_EQUALS(file.getNumElements(), num);
// Get it
std::vector<DasEvent> data;
TS_ASSERT_THROWS_NOTHING(file.loadAllInto(data));
TS_ASSERT_THROWS_NOTHING(data = file.loadAllIntoVector());
TS_ASSERT_EQUALS(data.size(), num);
// Check the first event
TS_ASSERT_EQUALS(data.at(0).tof, 0);
......@@ -187,7 +187,7 @@ public:
DasEvent *buffer = NULL;
TS_ASSERT_EQUALS(file2.getNumElements(), 0);
TS_ASSERT_THROWS(file2.loadAll(), std::runtime_error);
TS_ASSERT_THROWS(file2.loadAllInto(data), std::runtime_error);
TS_ASSERT_THROWS(data = file2.loadAllIntoVector(), std::runtime_error);
TS_ASSERT_THROWS(file2.loadBlock(buffer, 10), std::runtime_error);
}
};
......
......@@ -150,13 +150,12 @@ public:
numBins[1] = 20;
numBins[2] = 5;
std::vector<size_t> indexes;
size_t ic(0);
for (size_t k = 0; k < numBins[2]; k++) {
for (size_t j = 0; j < numBins[1]; j++) {
for (size_t i = 0; i < numBins[0]; i++) {
Utils::getIndicesFromLinearIndex(ic, numBins, indexes);
auto indexes = Utils::getIndicesFromLinearIndex(ic, numBins);
ic++;
TS_ASSERT_EQUALS(indexes[0], i);
......@@ -172,13 +171,12 @@ public:
numBins[1] = 1; // there can not be 0, shluld be at least 1
numBins[2] = 5;
std::vector<size_t> indexes;
size_t ic(0);
for (size_t k = 0; k < numBins[2]; k++) {
for (size_t j = 0; j < numBins[1]; j++) {
for (size_t i = 0; i < numBins[0]; i++) {
Utils::getIndicesFromLinearIndex(ic, numBins, indexes);
auto indexes = Utils::getIndicesFromLinearIndex(ic, numBins);
ic++;
TS_ASSERT_EQUALS(indexes[0], i);
......
......@@ -313,13 +313,13 @@ public:
TS_ASSERT_EQUALS(out->getNEvents(), 1000);
double expected_signal(2.);
std::vector<size_t> nBins(3), indexes(3);
std::vector<size_t> nBins(3);
nBins[0] = 40;
nBins[1] = 5;
nBins[2] = 20;
for (size_t i = 0; i < out->getNPoints(); i++) {
Utils::getIndicesFromLinearIndex(i, nBins, indexes);
auto indexes = Utils::getIndicesFromLinearIndex(i, nBins);
if (etta(int(indexes[0]), 4) && etta(int(indexes[2]), 2)) {
TS_ASSERT_DELTA(out->getSignalAt(i), expected_signal, 1e-5);
TS_ASSERT_DELTA(out->getNumEventsAt(i), expected_signal, 1e-5);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment