21 #ifndef RANGEIMPLEMENTATIONS_HPP 22 #define RANGEIMPLEMENTATIONS_HPP 39 template<
typename NumberType>
40 inline void getRange (
const std::string& range, std::vector<NumberType>& x ) {
41 std::vector<std::string> aux;
42 boost::algorithm::split ( aux, range, boost::algorithm::is_any_of (
"," ) );
43 for ( uint i = 0; i < aux.size(); ++i ) {
44 std::string& s = aux[i];
45 if ( !
exists ( s,
":" ) ) {
46 x.push_back ( toNumber<NumberType> ( s ) );
49 std::vector<std::string> range_aux;
50 boost::algorithm::split ( range_aux, s, boost::algorithm::is_any_of (
":" ) );
52 && range_aux.size() <= 3,
"string range incorrectly defined!" );
54 if ( range_aux.size() == 3 ) jump = toNumber<NumberType> ( range_aux[1] );
55 NumberType start = toNumber<NumberType> ( range_aux[0] );
56 NumberType end = toNumber<NumberType> ( range_aux[range_aux.size() - 1] );
57 for ( NumberType k = start; k <= end; k += jump ) {
67 template<
class NumberType>
70 virtual void next (
void ) = 0;
71 virtual bool done (
void ) = 0;
72 virtual void start (
void ) = 0;
73 virtual NumberType
get ( void ) = 0;
77 template<
class NumberType = u
int>
81 std::vector<NumberType> range_;
90 NumberRange (
const std::vector<NumberType>& r ) : range_ ( r ), k_ ( 0 ) {};
99 getRange ( rg.
get<std::string> ( rangekey ), range_ );
116 return k_ >= range_.size();
119 inline NumberType
get ( void ) {
120 return range_.at ( k_ );
122 inline NumberType operator() (
void ) {
123 return range_.at ( k_ );
137 template <
typename NumberType>
144 OneRange (
const NumberType u = 0 ) : sid_ ( u ) , state_ ( false ) {};
159 inline NumberType
get ( void ) {
164 DISALLOW_COPY_AND_ASSIGN (
OneRange );
172 template<
typename NumberType = u
int >
191 inline NumberType
get ( void ) {
200 template<
typename NumberType>
213 #define IntRangeFactory RangeInitFactory<unsigned> 214 typedef boost::scoped_ptr < NumberRangeInterface<unsigned> >
IntRangePtr;
void next(void)
Empty implementation.
Implements a Range iterator that will never finish.
NumberRange(const std::string &range)
NumberRange(const RegistryPO &rg, const std::string &rangekey=HifstConstants::kRange)
Constructor.
T get(const std::string &key) const
Returns parsed value associated to key.
void start(void)
Empty implementation.
boost::scoped_ptr< NumberRangeInterface< unsigned > > IntRangePtr
const std::string kRangeInfinite
void start(void)
Empty implementation.
void next(void)
Increment index.
Interface for an arbitrary range of numbers.
NumberRangeInterface< NumberType > * RangeInitFactory(const RegistryPO &rg, const std::string &option=HifstConstants::kRangeInfinite)
void next(void)
Empty implementation.
Implements a Range iterator that only runs once. This is useful e.g. for fsttools that process a batc...
virtual ~NumberRangeInterface()
NumberRange(const std::vector< NumberType > &r)
Constructor.
bool exists(const std::string &source, const std::string &needle)
Convenience function to find out whether a needle exists in a text.
bool exists(const std::string &key) const
Determines whether a program option (key) has been defined by the user.
void start(void)
Empty implementation.
bool done(void)
return true.
#define USER_CHECK(exp, comment)
Tests whether exp is true. If not, comment is printed and program ends.
bool done(void)
Checks if reached the last element.
OneRange(const NumberType u=0)
bool done(void)
Always return false.
InfiniteRange(const NumberType u=1)
const std::string kRangeOne
void getRange(const std::string &range, std::vector< NumberType > &x)
Generates ranges from a compact string parameter such as 1,3:5,10.