Skip to content

Commit

Permalink
refactor: Unify proxy iterator types
Browse files Browse the repository at this point in the history
We have quite a few container types whose iterators are container-index
pairs. These currently have different implementations which can be
fairly easily unified under a single template.
  • Loading branch information
stephenswat committed Oct 2, 2024
1 parent d468122 commit fd31c85
Show file tree
Hide file tree
Showing 12 changed files with 282 additions and 347 deletions.
28 changes: 19 additions & 9 deletions Core/include/Acts/EventData/SpacePointContainer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
#include "Acts/Definitions/Units.hpp"
#include "Acts/EventData/SpacePointData.hpp"
#include "Acts/EventData/SpacePointProxy.hpp"
#include "Acts/EventData/SpacePointProxyIterator.hpp"
#include "Acts/EventData/Utils.hpp"
#include "Acts/Utilities/HashedString.hpp"
#include "Acts/Utilities/Iterator.hpp"

#include <any>
#include <vector>
Expand Down Expand Up @@ -67,19 +67,22 @@ class SpacePointContainer {
public:
friend class Acts::SpacePointProxy<
Acts::SpacePointContainer<container_t, holder_t>>;
friend class Acts::SpacePointProxyIterator<
Acts::SpacePointContainer<container_t, holder_t>>;

public:
using iterator = Acts::SpacePointProxyIterator<
Acts::SpacePointContainer<container_t, holder_t>>;
using const_iterator = iterator;

using SpacePointProxyType =
Acts::SpacePointProxy<Acts::SpacePointContainer<container_t, holder_t>>;

using iterator =
ContainerIndexIterator<Acts::SpacePointContainer<container_t, holder_t>,
SpacePointProxyType&, false>;
using const_iterator =
ContainerIndexIterator<Acts::SpacePointContainer<container_t, holder_t>,
const SpacePointProxyType&, true>;

using ValueType = typename container_t::ValueType;
using ProxyType = SpacePointProxyType;
using value_type = ProxyType;
using size_type = std::size_t;

public:
// Constructors
Expand Down Expand Up @@ -118,16 +121,23 @@ class SpacePointContainer {

std::size_t size() const;

iterator begin() const;
iterator end() const;
iterator begin();
iterator end();
const_iterator cbegin() const;
const_iterator cend() const;
const_iterator begin() const;
const_iterator end() const;

ProxyType& at(const std::size_t n);
const ProxyType& at(const std::size_t n) const;
const ValueType& sp(const std::size_t n) const;

private:
void initialize();

const container_t& container() const;
const ProxyType& proxy(const std::size_t n) const;
std::vector<ProxyType>& proxies();
const std::vector<ProxyType>& proxies() const;

float x(const std::size_t n) const;
Expand Down
46 changes: 44 additions & 2 deletions Core/include/Acts/EventData/SpacePointContainer.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,59 @@ std::size_t SpacePointContainer<container_t, holder_t>::size() const {
}

template <typename container_t, template <typename> class holder_t>
typename SpacePointContainer<container_t, holder_t>::iterator
typename SpacePointContainer<container_t, holder_t>::const_iterator
SpacePointContainer<container_t, holder_t>::begin() {
return {*this, 0};
}

template <typename container_t, template <typename> class holder_t>
typename SpacePointContainer<container_t, holder_t>::const_iterator
SpacePointContainer<container_t, holder_t>::end() {
return {*this, size()};
}

template <typename container_t, template <typename> class holder_t>
typename SpacePointContainer<container_t, holder_t>::const_iterator
SpacePointContainer<container_t, holder_t>::begin() const {
return {*this, 0};
}

template <typename container_t, template <typename> class holder_t>
typename SpacePointContainer<container_t, holder_t>::iterator
typename SpacePointContainer<container_t, holder_t>::const_iterator
SpacePointContainer<container_t, holder_t>::end() const {
return {*this, size()};
}

template <typename container_t, template <typename> class holder_t>
typename SpacePointContainer<container_t, holder_t>::const_iterator
SpacePointContainer<container_t, holder_t>::cbegin() const {
return {*this, 0};
}

template <typename container_t, template <typename> class holder_t>
typename SpacePointContainer<container_t, holder_t>::const_iterator
SpacePointContainer<container_t, holder_t>::cend() const {
return {*this, size()};
}

template <typename container_t, template <typename> class holder_t>
const container_t& SpacePointContainer<container_t, holder_t>::container()
const {
return *m_container;
}

template <typename container_t, template <typename> class holder_t>
typename SpacePointContainer<container_t, holder_t>::ProxyType&
SpacePointContainer<container_t, holder_t>::at(const std::size_t n) {
return proxies().at(n);
}

template <typename container_t, template <typename> class holder_t>
const typename SpacePointContainer<container_t, holder_t>::ProxyType&
SpacePointContainer<container_t, holder_t>::at(const std::size_t n) const {
return proxies().at(n);
}

template <typename container_t, template <typename> class holder_t>
const typename SpacePointContainer<container_t, holder_t>::ValueType&
SpacePointContainer<container_t, holder_t>::sp(const std::size_t n) const {
Expand Down Expand Up @@ -173,6 +209,12 @@ SpacePointContainer<container_t, holder_t>::proxy(const std::size_t n) const {
return proxies()[n];
}

template <typename container_t, template <typename> class holder_t>
std::vector<typename SpacePointContainer<container_t, holder_t>::ProxyType>&
SpacePointContainer<container_t, holder_t>::proxies() {
return m_proxies;
}

template <typename container_t, template <typename> class holder_t>
const std::vector<
typename SpacePointContainer<container_t, holder_t>::ProxyType>&
Expand Down
58 changes: 0 additions & 58 deletions Core/include/Acts/EventData/SpacePointProxyIterator.hpp

This file was deleted.

100 changes: 0 additions & 100 deletions Core/include/Acts/EventData/SpacePointProxyIterator.ipp

This file was deleted.

47 changes: 31 additions & 16 deletions Core/include/Acts/EventData/TrackContainer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "Acts/EventData/Utils.hpp"
#include "Acts/Utilities/HashedString.hpp"
#include "Acts/Utilities/Holders.hpp"
#include "Acts/Utilities/Iterator.hpp"
#include "Acts/Utilities/TypeTraits.hpp"
#include "Acts/Utilities/UnitVectors.hpp"

#include <any>
Expand Down Expand Up @@ -78,6 +80,12 @@ class TrackContainer {
using ConstTrackStateProxy =
typename MultiTrajectory<traj_t>::ConstTrackStateProxy;

using size_type = IndexType;
using iterator =
Acts::ContainerIndexIterator<TrackContainer, TrackProxy, false>;
using const_iterator =
Acts::ContainerIndexIterator<TrackContainer, ConstTrackProxy, true>;

#ifndef DOXYGEN
friend TrackProxy;
friend ConstTrackProxy;
Expand Down Expand Up @@ -148,6 +156,21 @@ class TrackContainer {
return {*this, itrack};
}

/// Get a const track proxy for a track index
/// @param itrack the track index in the container
/// @return A const track proxy for the index
ConstTrackProxy at(IndexType itrack) const { return getTrack(itrack); }

/// Get a mutable track proxy for a track index
/// @note Only available if the track container is not read-only
/// @param itrack the track index in the container
/// @return A mutable track proxy for the index
TrackProxy at(IndexType itrack)
requires(!ReadOnly)
{
return {*this, itrack};
}

/// Add a track to the container. Note this only creates the logical track and
/// allocates memory. You can combine this with @c getTrack to obtain a track proxy
/// @note Only available if the track container is not read-only
Expand Down Expand Up @@ -184,36 +207,28 @@ class TrackContainer {
/// Get a mutable iterator to the first track in the container
/// @note Only available if the track container is not read-only
/// @return a mutable iterator to the first track
auto begin()
iterator begin()
requires(!ReadOnly)
{
return detail_tc::TrackProxyIterator<std::decay_t<decltype(*this)>,
TrackProxy, false>{*this, 0};
return iterator{*this, 0};
}

/// Get a past-the-end iterator for this container
/// @note Only available if the track container is not read-only
/// @return a past-the-end iterator
auto end()
iterator end()
requires(!ReadOnly)
{
return detail_tc::TrackProxyIterator<std::decay_t<decltype(*this)>,
TrackProxy, false>{*this, size()};
return iterator{*this, size()};
}

/// Get an const iterator to the first track in the container
/// @return a const iterator to the first track
auto begin() const {
return detail_tc::TrackProxyIterator<std::decay_t<decltype(*this)>,
ConstTrackProxy, true>{*this, 0};
}
const_iterator begin() const { return const_iterator{*this, 0}; }

/// Get a past-the-end iterator for this container
/// @return a past-the-end iterator
auto end() const {
return detail_tc::TrackProxyIterator<std::decay_t<decltype(*this)>,
ConstTrackProxy, true>{*this, size()};
}
const_iterator end() const { return const_iterator{*this, size()}; }

/// @}

Expand Down Expand Up @@ -411,8 +426,8 @@ class TrackContainer {
}
}

detail_tc::ConstIf<holder_t<track_container_t>, ReadOnly> m_container;
detail_tc::ConstIf<holder_t<traj_t>, ReadOnly> m_traj;
const_if_t<ReadOnly, holder_t<track_container_t>> m_container;
const_if_t<ReadOnly, holder_t<traj_t>> m_traj;
};

template <TrackContainerBackend track_container_t, typename traj_t>
Expand Down
Loading

0 comments on commit fd31c85

Please sign in to comment.