22 #include <tensor/indices.h>
27 : base_(0), limit_(0), factor_(1)
42 Range::set_factor(index factor)
48 Range::set_limit(index limit)
69 FullRange::FullRange()
70 : Range(), counter_(0), counter_end_(0)
77 if (counter_ >= counter_end_) {
80 index output = counter_;
81 counter_ += get_factor();
87 FullRange::set_factor(index factor)
89 index old = get_factor();
91 counter_end_ = get_limit() * factor;
92 Range::set_factor(factor);
96 FullRange::set_limit(index limit)
98 counter_end_ = limit * get_factor();
99 Range::set_limit(limit);
103 FullRange::size()
const
117 SingleRange::SingleRange(index ndx)
118 : Range(), ndx_(ndx), counter_(ndx)
132 SingleRange::set_factor(index factor)
134 index old = get_factor();
135 ndx_ = (ndx_ / old) * factor;
136 Range::set_factor(factor);
140 SingleRange::set_limit(index limit)
142 assert(limit > ndx_);
143 Range::set_limit(limit);
147 SingleRange::size()
const
162 StepRange::StepRange(index start, index end, index step) :
163 Range(), start_(start), end_(end), step_(step), ndx_(0)
180 StepRange::set_factor(index factor)
182 index old = get_factor();
183 end_ = (end_ / old) * factor;
184 step_ = (step_ / old) * factor;
185 ndx_ = start_ = (start_ / old) * factor;
186 Range::set_factor(factor);
190 StepRange::set_limit(index limit)
192 assert(limit > end_);
193 Range::set_limit(limit);
197 StepRange::size()
const
199 return (end_ - start_) / step_ + 1;
211 IndexRange::IndexRange(
const Indices &i)
212 : Range(), indices_(i), counter_(0)
219 if (counter_ >= indices_.size())
221 return indices_[counter_++];
225 IndexRange::set_factor(index factor)
227 index old = get_factor();
228 for (Indices::iterator it = indices_.begin(); it != indices_.end(); it++) {
229 *it = (*it / old) * factor;
232 Range::set_factor(factor);
236 IndexRange::set_limit(index limit)
239 for (Indices::const_iterator it = indices_.begin(); it != indices_.end(); it++) {
240 assert(*it <= limit);
243 Range::set_limit(limit);
247 IndexRange::size()
const
249 return indices_.size();
261 ProductRange::ProductRange(Range *r1, Range *r2) : r1_(r1), r2_(r2)
263 r2_->set_factor(r1_->get_factor() * r1_->get_limit());
266 ProductRange::~ProductRange()
279 index n = r1_->pop();
282 out = base_ = r2_->pop();
288 ProductRange::reset()
296 ProductRange::set_factor(index factor)
298 r1_->set_factor(factor);
299 r2_->set_factor(factor * r1_->get_limit());
303 ProductRange::set_limit(index limit)
309 ProductRange::size()
const
311 return r1_->size() * r2_->size();
314 Range *product(Range *r1, Range *r2)
316 return new ProductRange(r1, r2);