Cambridge SMT System
range.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use these files except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 // Copyright 2012 - Gonzalo Iglesias, AdriĆ  de Gispert, William Byrne
14 
21 #ifndef RANGEIMPLEMENTATIONS_HPP
22 #define RANGEIMPLEMENTATIONS_HPP
23 
24 namespace HifstConstants {
25 const std::string kRange = "range";
26 const std::string kRangeOne = "one";
27 const std::string kRangeInfinite = "infinite";
28 }
29 
30 namespace ucam {
31 namespace util {
32 
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 ) );
47  continue;
48  }
49  std::vector<std::string> range_aux;
50  boost::algorithm::split ( range_aux, s, boost::algorithm::is_any_of ( ":" ) );
51  USER_CHECK ( range_aux.size()
52  && range_aux.size() <= 3, "string range incorrectly defined!" );
53  NumberType jump = 1;
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 ) {
58  x.push_back ( k );
59  }
60  }
61 };
62 
67 template<class NumberType>
69  public:
70  virtual void next ( void ) = 0;
71  virtual bool done ( void ) = 0;
72  virtual void start ( void ) = 0;
73  virtual NumberType get ( void ) = 0;
74  virtual ~NumberRangeInterface() {};
75 };
76 
77 template<class NumberType = uint>
78 class NumberRange : public NumberRangeInterface<NumberType> {
79  private:
81  std::vector<NumberType> range_;
83  uint k_;
84  public:
90  NumberRange ( const std::vector<NumberType>& r ) : range_ ( r ), k_ ( 0 ) {};
91 
97  NumberRange ( const RegistryPO& rg ,
98  const std::string& rangekey = HifstConstants::kRange) : k_ ( 0 ) {
99  getRange ( rg.get<std::string> ( rangekey ), range_ );
100  };
101 
102  NumberRange ( const std::string& range) : k_ ( 0 ) {
103  getRange ( range , range_ );
104  };
105 
107  inline void start ( void ) {
108  k_ = 0;
109  };
111  inline void next ( void ) {
112  if ( !done() ) ++k_;
113  };
115  inline bool done ( void ) {
116  return k_ >= range_.size();
117  };
119  inline NumberType get ( void ) {
120  return range_.at ( k_ );
121  };
122  inline NumberType operator() ( void ) {
123  return range_.at ( k_ );
124  };
125 
126  private:
127 
128  DISALLOW_COPY_AND_ASSIGN ( NumberRange );
129 
130 };
131 
137 template <typename NumberType>
138 class OneRange : public NumberRangeInterface<NumberType> {
139  private:
141  NumberType sid_;
142  bool state_;
143  public:
144  OneRange ( const NumberType u = 0 ) : sid_ ( u ) , state_ ( false ) {};
146  inline void start ( void ) {
147  state_ = false;
148  };
150  inline void next ( void ) {
151  state_ = true;
152  };
153 
155  inline bool done ( void ) {
156  return state_;
157  };
159  inline NumberType get ( void ) {
160  return sid_;
161  };
162 
163  private:
164  DISALLOW_COPY_AND_ASSIGN ( OneRange );
165 
166 };
167 
172 template<typename NumberType = uint >
173 class InfiniteRange : public NumberRangeInterface<NumberType> {
174  private:
176  NumberType sid_;
177  public:
178  InfiniteRange ( const NumberType u = 1 ) : sid_ ( u ) {};
180  inline void start ( void ) {};
182  inline void next ( void ) {
183  ++sid_;
184  };
185 
187  inline bool done ( void ) {
188  return false;
189  };
191  inline NumberType get ( void ) {
192  return sid_;
193  };
194 
195  private:
196  DISALLOW_COPY_AND_ASSIGN ( InfiniteRange );
197 
198 };
199 
200 template<typename NumberType>
202  const std::string& option = HifstConstants::kRangeInfinite ) {
203  if ( !rg.exists ( HifstConstants::kRange ) )
204  if ( option == HifstConstants::kRangeInfinite )
205  return new InfiniteRange<NumberType>();
206  //this range will never break
207  else if ( option == HifstConstants::kRangeOne )
208  return new OneRange<NumberType>();
209  //Just run once if range not specified
210  return new NumberRange<NumberType> ( rg );
211 };
212 
213 #define IntRangeFactory RangeInitFactory<unsigned>
214 typedef boost::scoped_ptr < NumberRangeInterface<unsigned> > IntRangePtr;
215 
216 }
217 } // end namespaces
218 
219 #endif
220 
void next(void)
Empty implementation.
Definition: range.hpp:150
Implements a Range iterator that will never finish.
Definition: range.hpp:173
NumberRange(const std::string &range)
Definition: range.hpp:102
NumberRange(const RegistryPO &rg, const std::string &rangekey=HifstConstants::kRange)
Constructor.
Definition: range.hpp:97
const std::string kRange
Definition: range.hpp:25
T get(const std::string &key) const
Returns parsed value associated to key.
Definition: registrypo.hpp:194
void start(void)
Empty implementation.
Definition: range.hpp:146
boost::scoped_ptr< NumberRangeInterface< unsigned > > IntRangePtr
Definition: range.hpp:214
const std::string kRangeInfinite
Definition: range.hpp:27
void start(void)
Empty implementation.
Definition: range.hpp:107
void next(void)
Increment index.
Definition: range.hpp:111
Interface for an arbitrary range of numbers.
Definition: range.hpp:68
NumberRangeInterface< NumberType > * RangeInitFactory(const RegistryPO &rg, const std::string &option=HifstConstants::kRangeInfinite)
Definition: range.hpp:201
void next(void)
Empty implementation.
Definition: range.hpp:182
Implements a Range iterator that only runs once. This is useful e.g. for fsttools that process a batc...
Definition: range.hpp:138
NumberRange(const std::vector< NumberType > &r)
Constructor.
Definition: range.hpp:90
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.
Definition: registrypo.hpp:235
void start(void)
Empty implementation.
Definition: range.hpp:180
bool done(void)
return true.
Definition: range.hpp:155
#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.
Definition: range.hpp:115
OneRange(const NumberType u=0)
Definition: range.hpp:144
bool done(void)
Always return false.
Definition: range.hpp:187
InfiniteRange(const NumberType u=1)
Definition: range.hpp:178
const std::string kRangeOne
Definition: range.hpp:26
void getRange(const std::string &range, std::vector< NumberType > &x)
Generates ranges from a compact string parameter such as 1,3:5,10.
Definition: range.hpp:40
Definition: bleu.hpp:14