Skip to content

Commit

Permalink
Improve the collection interface (#478)
Browse files Browse the repository at this point in the history
* Add empty method to collection interface

* Add operator== do collection iterators

* Fix clang-tidy warning
  • Loading branch information
tmadlener authored Sep 8, 2023
1 parent cda2833 commit 48a4bf6
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 6 deletions.
3 changes: 3 additions & 0 deletions include/podio/CollectionBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class CollectionBase {
/// number of elements in the collection
virtual size_t size() const = 0;

/// Is the collection empty
virtual bool empty() const = 0;

/// fully qualified type name
virtual const std::string_view getTypeName() const = 0;
/// fully qualified type name of elements - with namespace
Expand Down
5 changes: 5 additions & 0 deletions include/podio/UserDataCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ class UserDataCollection : public CollectionBase {
return _vec.size();
}

/// Is the collection empty
bool empty() const override {
return _vec.empty();
}

/// fully qualified type name
const std::string_view getTypeName() const override {
return typeName;
Expand Down
4 changes: 4 additions & 0 deletions python/templates/Collection.cc.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ std::size_t {{ collection_type }}::size() const {
return m_storage.entries.size();
}

bool {{ collection_type }}::empty() const {
return m_storage.entries.empty();
}

void {{ collection_type }}::setSubsetCollection(bool setSubset) {
if (m_isSubsetColl != setSubset && !m_storage.entries.empty()) {
throw std::logic_error("Cannot change the character of a collection that already contains elements");
Expand Down
3 changes: 3 additions & 0 deletions python/templates/Collection.h.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ public:
/// number of elements in the collection
std::size_t size() const final;

/// Is the collection empty
bool empty() const final;

/// fully qualified type name
const std::string_view getTypeName() const final { return typeName; }
/// fully qualified type name of elements - with namespace
Expand Down
4 changes: 4 additions & 0 deletions python/templates/macros/iterator.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public:
return m_index != x.m_index; // TODO: may not be complete
}

bool operator==(const {{ iterator_type }}& x) const {
return m_index == x.m_index; // TODO: may not be complete
}

{{ prefix }}{{ class.bare_type }} operator*();
{{ prefix }}{{ class.bare_type }}* operator->();
{{ iterator_type }}& operator++();
Expand Down
2 changes: 1 addition & 1 deletion tests/unittests/frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ void checkFrame(const podio::Frame& frame) {
REQUIRE(hits[1].energy() == 1);
REQUIRE(hits[1].cellID() == 0x123ULL);

REQUIRE(frame.get<ExampleClusterCollection>("emptyClusters").size() == 0);
REQUIRE(frame.get<ExampleClusterCollection>("emptyClusters").empty());

auto& clusters = frame.get<ExampleClusterCollection>("clusters");
REQUIRE(clusters.size() == 2);
Expand Down
15 changes: 10 additions & 5 deletions tests/unittests/unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ TEST_CASE("Assignment-operator ref count", "[basics][memory-management]") {
}

TEST_CASE("Clearing", "[UBSAN-FAIL][ASAN-FAIL][THREAD-FAIL][basics][memory-management]") {
bool success = true;
auto store = podio::EventStore();
auto& hits = store.create<ExampleHitCollection>("hits");
auto& clusters = store.create<ExampleClusterCollection>("clusters");
Expand All @@ -102,10 +101,7 @@ TEST_CASE("Clearing", "[UBSAN-FAIL][ASAN-FAIL][THREAD-FAIL][basics][memory-manag
oneRels.push_back(oneRel);
}
hits.clear();
if (hits.size() != 0) {
success = false;
}
REQUIRE(success);
REQUIRE(hits.empty());
}

TEST_CASE("Cloning", "[basics][memory-management]") {
Expand Down Expand Up @@ -440,6 +436,15 @@ TEST_CASE("NonPresentCollection", "[basics][event-store]") {
REQUIRE_THROWS_AS(store.get<ExampleHitCollection>("NonPresentCollection"), std::runtime_error);
}

TEST_CASE("Collection size and empty", "[basics][collections]") {
ExampleClusterCollection coll{};
REQUIRE(coll.empty());

coll.create();
coll.create();
REQUIRE(coll.size() == 2u);
}

TEST_CASE("const correct indexed access to const collections", "[const-correctness]") {
STATIC_REQUIRE(std::is_same_v<decltype(std::declval<const ExampleClusterCollection>()[0]),
ExampleCluster>); // const collections should only have indexed access to mutable
Expand Down

0 comments on commit 48a4bf6

Please sign in to comment.