/* */ #ifndef D_SEQUENCE_H #define D_SEQUENCE_H #include namespace aria2 { template class Sequence { public: // Generates value in [first_, last_). last_ is not included. class Value { private: T first_; T last_; public: Value(const T& first, const T& last):first_(first), last_(last) {} T next() { return first_++; } bool hasNext() const { return first_ != last_; } }; typedef std::vector Values; private: Values values_; typename Values::iterator cur_; public: Sequence(const Values& values): values_(values), cur_(values_.begin()) {} Sequence():cur_(values_.end()) {} T next() { if(cur_ == values_.end()) { return T(); } T t = (*cur_).next(); if(!(*cur_).hasNext()) { ++cur_; } return t; } bool hasNext() { return cur_ != values_.end(); } std::vector flush() { std::vector r; while(hasNext()) { r.push_back(next()); } return r; } }; } // namespace aria2 #endif // D_SEQUENCE_H