【问题标题】:A context-based partition_copy基于上下文的 partition_copy
【发布时间】:2015-05-09 16:49:30
【问题描述】:

我正在尝试为我的班级AlignedRead 实现一个通用过滤模型。这个想法是,在程序开始时,用户选项确定应该对每个AlignedRead 应用哪些过滤器系列。困难在于一些过滤器是“基于上下文的”,即它们以先前看到的元素为条件。例如,一个基于上下文的过滤器可能是读取是否重复,这需要最后一次看到的读取(必须对输入进行排序)。

这是我目前所拥有的:

#include <functional>
#include <vector>
#include <algorithm> // std::all_of, std::partition_copy
#include <iterator>  // std::cbegin etc

template <typename BidirectionalIterator>
class ReadFilter
{
public:
    using ContextFreeFilter  = std::function<bool(const AlignedRead&)>;
    using ContextBasedFilter = std::function<bool(const AlignedRead&, BidirectionalIterator,
                                                  BidirectionalIterator)>;

    void register_filter(ContextFreeFilter a_filter);
    void register_filter(ContextBasedFilter a_filter);
    template <typename OutputIterator1, typename OutputIterator2>
    void filter_reads(BidirectionalIterator first, BidirectionalIterator last,
                      OutputIterator1 good_reads, OutputIterator2 bad_reads) const;

private:
    std::vector<ContextFreeFilter> context_free_filters_;
    std::vector<ContextBasedFilter> context_based_filters_;

    bool filter_read(const AlignedRead& the_read, BidirectionalIterator first,
                     BidirectionalIterator previous) const;
};

template <typename BidirectionalIterator>
void ReadFilter<BidirectionalIterator>::register_filter(ContextFreeFilter a_filter)
{
    context_free_filters_.emplace_back(std::move(a_filter));
}

template <typename BidirectionalIterator>
void ReadFilter<BidirectionalIterator>::register_filter(ContextBasedFilter a_filter)
{
    context_based_filters_.emplace_back(std::move(a_filter));
}

template <typename BidirectionalIterator>
template <typename OutputIterator1, typename OutputIterator2>
void ReadFilter<BidirectionalIterator>::filter_reads(BidirectionalIterator first,
                                                     BidirectionalIterator last,
                                                     OutputIterator1 good_reads,
                                                     OutputIterator2 bad_reads) const
{
    BidirectionalIterator previous {first};
    std::partition_copy(first, last, good_reads, bad_reads,
                        [this, first, &previous] (const AlignedRead& the_read) {
                            return filter_read(the_read, first, (previous != first) ? previous++ :
                                               previous);
                        });
}

template <typename BidirectionalIterator>
bool ReadFilter<BidirectionalIterator>::filter_read(const AlignedRead& the_read,
                                                    BidirectionalIterator first,
                                                    BidirectionalIterator previous) const
{
    return std::all_of(std::cbegin(context_free_filters_), std::cend(context_free_filters_),
                       [&the_read] (const auto& filter) {
                           return filter(the_read);
    }) && std::all_of(std::cbegin(context_based_filters_), std::cend(context_based_filters_),
                      [&the_read, first, previous] (const auto& filter) {
                          return filter(the_read, first, previous);
    });
}

这很好用。但是,我想做两个我觉得很难的更改:

  1. 支持移动和复制(目前无法实现,因为移动会使之前看到的读取无效)。
  2. 通过基于上下文的过滤器过滤“良好读取”的范围。

注意解决 2. 自动解决 1. 我面临的主要困难是,在大多数情况下,std::back_insert_iterator 用于 OutputIterator1,而 doesn't seem to be possible 将其转换为其底层容器迭代器。

我能想到的唯一解决方案是让用户在“好读”的开头也提供一个正常的迭代器,但显然这有其自身的问题。我可以使用一些巧妙的技巧来解决这个问题吗?

编辑 我刚刚意识到,即使传入一个额外的迭代器的想法也行不通,因为如果底层容器调整大小,它可能会失效。

我还会考虑一种解决方案,它只跟踪上次看到的“好读”。所以基于上下文的过滤器变成std::function&lt;bool(const AlignedRead&amp;, const AlignedRead&amp;)&gt;的形式。

【问题讨论】:

    标签: c++ stl iterator


    【解决方案1】:

    所以我通过创建一个新的反向插入器迭代器解决了这个问题,它可以访问底层容器的 beginend 方法,以及一点 TMP。

    这是新的迭代器:

    template <typename Container>
    class ContextBackInsertIterator :
    public std::iterator<std::output_iterator_tag, void, void, void, void>
    {
    protected:
        Container* container;
    
    public:
        using container_type = Container;
        explicit ContextBackInsertIterator (Container& x) : container(std::addressof(x)) {}
        ContextBackInsertIterator<Container>& operator= (const typename Container::value_type& value)
        { container->push_back(value); return *this; }
        ContextBackInsertIterator<Container>& operator= (typename Container::value_type&& value)
        { container->push_back(std::move(value)); return *this; }
        ContextBackInsertIterator<Container>& operator* ()
        { return *this; }
        ContextBackInsertIterator<Container>& operator++ ()
        { return *this; }
        ContextBackInsertIterator<Container> operator++ (int)
        { return *this; }
    
        typename container_type::const_iterator begin() const { return container->begin(); }
        typename container_type::const_iterator end() const { return container->end(); }
        typename container_type::const_iterator cbegin() const { return container->cbegin(); }
        typename container_type::const_iterator cend() const { return container->cend(); }
    };
    
    template <class Container>
    inline
    ContextBackInsertIterator<Container> ContextBackInserter(Container& x)
    {
        return ContextBackInsertIterator<Container>(x);
    }
    

    现在要注意的是,对于插入迭代器,++ 运算符什么也不做。这个想法是,每次我们看到“好读”时,我们都会在给定输入迭代器的副本上使用++。如果给定一个ContextBackInsertIterator,我们可以调用迭代器的beginend 方法来获取最后插入的元素,如果给定一个普通的迭代器,我们只是鹦鹉学舌返回迭代器。我通过检查迭代器的value_type 是否为void 来静态确定给定的迭代器是否为ContextBackInsertIterator——就像插入迭代器一样——但我承认可能有更好的方法。

    template <typename T>
    inline
    typename std::enable_if<std::is_void<typename T::value_type>::value,
        typename T::container_type::const_iterator>::type
    get_first(T first, T last)
    {
        return last.begin();
    }
    
    template <typename T>
    inline
    typename std::enable_if<!std::is_void<typename T::value_type>::value, T>::type
    get_first(T first, T last)
    {
        return first;
    }
    
    template <typename T>
    inline
    typename std::enable_if<std::is_void<typename T::value_type>::value,
        typename T::container_type::const_iterator>::type
    get_last(T first, T last)
    {
        return (last.begin() != last.end()) ? std::prev(last.end()) : last.begin();
    }
    
    template <typename T>
    inline
    typename std::enable_if<!std::is_void<typename T::value_type>::value, T>::type
    get_last(T first, T last)
    {
        return (first != last) ? std::prev(last) : last;
    }
    

    最后filter_reads函数变化如下:

    template <typename BidirectionalIterator>
    template <typename InputIterator, typename OutputIterator1, typename OutputIterator2>
    std::pair<OutputIterator1, OutputIterator2>
    ReadFilter<BidirectionalIterator>::filter_reads(InputIterator first, InputIterator last,
                                                     OutputIterator1 good_reads,
                                                     OutputIterator2 bad_reads) const
    {
        auto good_reads_last = good_reads;
        return std::partition_copy(first, last, good_reads, bad_reads,
            [this, good_reads, &good_reads_last] (const AlignedRead& the_read) {
            if (filter_read(the_read, get_first(good_reads, good_reads_last),
                            get_last(good_reads, good_reads_last))) {
                ++good_reads_last;
                return true;
            }
            return false;
        });
    }
    

    如果有人可以对此进行改进,我很乐意接受另一种解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-17
      • 2021-05-15
      • 2011-04-17
      • 2016-11-18
      • 1970-01-01
      • 1970-01-01
      • 2020-07-02
      相关资源
      最近更新 更多