Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Framework/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ foreach(t
AnalysisTask
AnalysisDataModel
ASoA
ASoAHelpers
BoostOptionsRetriever
CallbackRegistry
ChannelSpecHelpers
Expand Down
38 changes: 38 additions & 0 deletions Framework/Core/include/Framework/ASoAHelpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright CERN and copyright holders of ALICE O2. This software is
// distributed under the terms of the GNU General Public License v3 (GPL
// Version 3), copied verbatim in the file "COPYING".
//
// See http://alice-o2.web.cern.ch/license for full licensing information.
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

#ifndef O2_FRAMEWORK_ASOAHELPERS_H_
#define O2_FRAMEWORK_ASOAHELPERS_H_

#include "Framework/ASoA.h"

namespace o2::soa
{

/// @return a vector of pairs with all the possible
/// combinations of the rows of the table T.
/// FIXME: move to coroutines once we have C++20
template <typename T>
std::vector<std::pair<typename T::iterator, typename T::iterator>>
combinations(T const& table)
{
std::vector<std::pair<typename T::iterator, typename T::iterator>> result;
result.reserve((table.size() + 1) * table.size() / 2);
for (auto t0 = table.begin(); t0 + 1 != table.end(); ++t0) {
for (auto t1 = t0 + 1; t1 != table.end(); ++t1) {
result.push_back(std::make_pair(t0, t1));
}
}
return result;
};

} // namespace o2::soa

#endif // O2_FRAMEWORK_ASOAHELPERS_H_
68 changes: 68 additions & 0 deletions Framework/Core/test/test_ASoAHelpers.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright CERN and copyright holders of ALICE O2. This software is
// distributed under the terms of the GNU General Public License v3 (GPL
// Version 3), copied verbatim in the file "COPYING".
//
// See http://alice-o2.web.cern.ch/license for full licensing information.
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

#define BOOST_TEST_MODULE Test Framework ASoAHelpers
#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK

#include "Framework/ASoAHelpers.h"
#include "Framework/TableBuilder.h"
#include <boost/test/unit_test.hpp>

using namespace o2::framework;
using namespace o2::soa;

namespace test
{
DECLARE_SOA_COLUMN(X, x, int32_t, "x");
DECLARE_SOA_COLUMN(Y, y, int32_t, "y");
DECLARE_SOA_COLUMN(Z, z, int32_t, "z");
DECLARE_SOA_DYNAMIC_COLUMN(Sum, sum, [](int32_t x, int32_t y) { return x + y; });
} // namespace test

BOOST_AUTO_TEST_CASE(Combinations)
{
TableBuilder builderA;
auto rowWriterA = builderA.persist<int32_t, int32_t>({"x", "y"});
rowWriterA(0, 0, 0);
rowWriterA(0, 1, 0);
rowWriterA(0, 2, 0);
rowWriterA(0, 3, 0);
rowWriterA(0, 4, 0);
rowWriterA(0, 5, 0);
rowWriterA(0, 6, 0);
rowWriterA(0, 7, 0);
auto tableA = builderA.finalize();
BOOST_REQUIRE_EQUAL(tableA->num_rows(), 8);

using TestA = o2::soa::Table<o2::soa::Index<>, test::X, test::Y>;

TestA tests{tableA};

BOOST_REQUIRE_EQUAL(8, tests.size());
auto c = combinations(tests);
BOOST_CHECK_EQUAL(c.size(), 7 * (7 + 1) / 2);

int n = 0;
int i = 0;
int j = 1;
for (auto [t1, t2] : combinations(tests)) {
BOOST_CHECK_EQUAL(t1.x(), i);
BOOST_CHECK_EQUAL(t2.x(), j);
if (j == 7) {
++i;
j = i + 1;
continue;
}
n++;
j++;
}
//BOOST_CHECK_EQUAL(i, 28);
}