迭代工具.tee http://docs.python.org/2/library/itertools.html#itertools.tee非常适合 Python 中普遍存在的单遍迭代。例如发电机 http://wiki.python.org/moin/Generators是单通道,并且经常使用。
但如果你已经有了列表/双端队列你就不会使用迭代工具.tee对于它,因为它会涉及多余的重复 - 您可以一遍又一遍地迭代原始列表/双端队列。
例如,C++ 也有单遍范围的概念输入迭代器 http://www.sgi.com/tech/stl/InputIterator.html,但它们并不是那么普遍。这是典型 C++ 程序的另一组目标的结果 - 为用户提供尽可能大的保持最佳性能。如果你愿意的话,这是另一种心态。
为了说明这一点,让我们比较一下提升::转变 http://www.boost.org/doc/libs/1_53_0/libs/range/doc/html/range/reference/adaptors/reference/transformed.html and 迭代工具.imap http://docs.python.org/2/library/itertools.html#itertools.imap (or 生成器表达式 http://docs.python.org/2/reference/expressions.html#grammar-token-generator_expression):
它们都通过给定的“棱镜”提供输入序列的视图。迭代工具.imap返回单遍可迭代,而提升::转变返回与输入范围具有相同类别的范围视图 - 即,如果您通过随机访问范围 http://www.boost.org/doc/libs/1_53_0/libs/range/doc/html/range/concepts/random_access_range.html作为输入你会得到随机访问范围作为结果。
另一个事实是 C++ 使用值语义 http://en.wikipedia.org/wiki/Value_semantics默认情况下,Python 具有指针语义。这意味着如果在 C++ 中复制迭代器,并“碰撞”它几次 - 原始迭代器将不会改变(尽管如果它是单遍范围,它可能会失效,但这不是重点)。
但是,有时您确实希望累积单遍范围内的值并多次查看它们。在这种情况下,最常见的解决方案是手动将值显式累积到某个容器中。例如:
vector<int> cache(first,last);
然而,类似 Tee 的包装器在 C++ 中仍然是可能的,这里是概念验证。用法是:
auto forward_range = tee_range(first,last);
发球区范围 takes 单程范围作为参数并返回前进范围(这是多遍)(还有make_tee_iterator,它在迭代器级别工作)。因此,您可以复制该范围并对其进行多次迭代:
auto r = forward_range;
auto out = ostream_iterator<int>(cout," ");
copy(forward_range,out);
copy(forward_range,out);
copy(r,out);
也有改进迭代工具.tee- 内部只有一个deque用于缓存值。
现场演示 http://coliru.stacked-crooked.com/a/2689e33e4eb3ccce:
#include <boost/range/adaptor/transformed.hpp>
#include <boost/iterator/iterator_facade.hpp>
#include <boost/smart_ptr/make_shared.hpp>
#include <boost/range/iterator_range.hpp>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/container/vector.hpp>
#include <boost/container/deque.hpp>
#include <boost/range/algorithm.hpp>
#include <algorithm>
#include <iterator>
#include <cassert>
#include <limits>
template<typename InputIterator>
class tee_iterator : public boost::iterator_facade
<
tee_iterator<InputIterator>,
const typename std::iterator_traits<InputIterator>::value_type,
boost::forward_traversal_tag
>
{
typedef typename std::iterator_traits<InputIterator>::value_type Value;
typedef unsigned Index;
struct Data
{
boost::container::deque<Value> values;
boost::container::vector<tee_iterator*> iterators;
InputIterator current,end;
Index min_index, current_index;
Index poped_from_front;
//
Data(InputIterator first,InputIterator last)
: current(first), end(last), min_index(0), current_index(0), poped_from_front(0)
{}
~Data()
{
assert(iterators.empty());
}
};
boost::shared_ptr<Data> shared_data;
Index index;
static Index get_index(tee_iterator *p)
{
return p->index;
}
public:
tee_iterator()
: index(std::numeric_limits<Index>::max())
{}
tee_iterator(InputIterator first,InputIterator last)
: shared_data(boost::make_shared<Data>(first,last)), index(0)
{
shared_data->iterators.push_back(this);
}
tee_iterator(const tee_iterator &x)
: shared_data(x.shared_data), index(x.index)
{
if(shared_data)
shared_data->iterators.push_back(this);
}
friend void swap(tee_iterator &l,tee_iterator &r)
{
using std::swap;
*boost::find(l.shared_data->iterators,&l) = &r;
*boost::find(r.shared_data->iterators,&r) = &l;
swap(l.shared_data,r.shared_data);
swap(l.index,r.index);
}
tee_iterator &operator=(tee_iterator x)
{
swap(x,*this);
}
~tee_iterator()
{
if(shared_data)
{
erase_from_iterators();
if(!shared_data->iterators.empty())
{
using boost::adaptors::transformed;
shared_data->min_index = *boost::min_element(shared_data->iterators | transformed(&get_index));
Index to_pop = shared_data->min_index - shared_data->poped_from_front;
if(to_pop>0)
{
shared_data->values.erase(shared_data->values.begin(), shared_data->values.begin()+to_pop);
shared_data->poped_from_front += to_pop;
}
}
}
}
private:
friend class boost::iterator_core_access;
void erase_from_iterators()
{
shared_data->iterators.erase(boost::find(shared_data->iterators,this));
}
bool last_min_index() const
{
return boost::count
(
shared_data->iterators | boost::adaptors::transformed(&get_index),
shared_data->min_index
)==1;
}
Index obtained() const
{
return Index(shared_data->poped_from_front + shared_data->values.size());
}
void increment()
{
if((shared_data->min_index == index) && last_min_index())
{
shared_data->values.pop_front();
++shared_data->min_index;
++shared_data->poped_from_front;
}
++index;
if(obtained() <= index)
{
++shared_data->current;
if(shared_data->current != shared_data->end)
{
shared_data->values.push_back(*shared_data->current);
}
else
{
erase_from_iterators();
index=std::numeric_limits<Index>::max();
shared_data.reset();
}
}
}
bool equal(const tee_iterator& other) const
{
return (shared_data.get()==other.shared_data.get()) && (index == other.index);
}
const Value &dereference() const
{
if((index==0) && (obtained() <= index))
{
shared_data->values.push_back(*(shared_data->current));
}
assert( (index-shared_data->poped_from_front) < shared_data->values.size());
return shared_data->values[index-shared_data->poped_from_front];
}
};
template<typename InputIterator>
tee_iterator<InputIterator> make_tee_iterator(InputIterator first,InputIterator last)
{
return tee_iterator<InputIterator>(first,last);
}
template<typename InputIterator>
boost::iterator_range< tee_iterator<InputIterator> > tee_range(InputIterator first,InputIterator last)
{
return boost::iterator_range< tee_iterator<InputIterator> >
(
tee_iterator<InputIterator>(first,last),
tee_iterator<InputIterator>()
);
}
// _______________________________________________________ //
#include <iostream>
#include <ostream>
#include <sstream>
int main()
{
using namespace std;
stringstream ss;
ss << "1 2 3 4 5";
istream_iterator<int> first(ss /*cin*/ ),last;
typedef boost::iterator_range< tee_iterator< istream_iterator<int> > > Range; // C++98
Range r1 = tee_range(first,last);
Range r2 = r1, r3 = r1;
boost::copy(r1,ostream_iterator<int>(cout," "));
cout << endl;
boost::copy(r2,ostream_iterator<int>(cout," "));
cout << endl;
boost::copy(r2,ostream_iterator<int>(cout," "));
}
输出是:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
精神振奋 http://www.boost.org/doc/libs/1_53_0/libs/spirit/doc/html/index.html has 多遍迭代器 http://www.boost.org/doc/libs/1_53_0/libs/spirit/doc/html/spirit/support/multi_pass.html其目标相似。
multi_pass 迭代器会将任何输入迭代器转换为适合与 Spirit.Qi 一起使用的前向迭代器。 multi_pass 将在需要时缓冲数据,并在不再需要其内容时丢弃缓冲区。如果仅存在迭代器的一份副本或者无法发生回溯,则会发生这种情况。