This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/packages/CLPBN/horus/Indexer.h

344 lines
5.7 KiB
C
Raw Normal View History

2013-02-07 17:50:02 +00:00
#ifndef YAP_PACKAGES_CLPBN_HORUS_INDEXER_H_
#define YAP_PACKAGES_CLPBN_HORUS_INDEXER_H_
2012-05-23 14:56:01 +01:00
2013-02-07 20:09:10 +00:00
#include <vector>
2012-05-23 14:56:01 +01:00
#include <algorithm>
2012-05-28 17:57:45 +01:00
#include <numeric>
2012-05-23 14:56:01 +01:00
#include "Util.h"
namespace Horus {
2013-02-07 23:53:13 +00:00
class Indexer {
2012-05-23 14:56:01 +01:00
public:
Indexer (const Ranges& ranges, bool calcOffsets = true);
2012-05-23 14:56:01 +01:00
void increment();
2012-05-23 14:56:01 +01:00
void incrementDimension (size_t dim);
2012-05-23 14:56:01 +01:00
void incrementExceptDimension (size_t dim);
2012-05-23 14:56:01 +01:00
Indexer& operator++();
2012-05-23 14:56:01 +01:00
operator size_t() const;
2012-05-23 14:56:01 +01:00
unsigned operator[] (size_t dim) const;
2012-05-23 14:56:01 +01:00
bool valid() const;
2012-05-23 14:56:01 +01:00
void reset();
2012-05-23 14:56:01 +01:00
void resetDimension (size_t dim);
2012-05-23 14:56:01 +01:00
size_t size() const;
2012-05-23 14:56:01 +01:00
private:
void calculateOffsets();
2012-05-23 14:56:01 +01:00
friend std::ostream& operator<< (std::ostream&, const Indexer&);
2013-02-07 13:37:15 +00:00
size_t index_;
Ranges indices_;
const Ranges& ranges_;
size_t size_;
std::vector<size_t> offsets_;
2012-12-27 22:25:45 +00:00
DISALLOW_COPY_AND_ASSIGN (Indexer);
2012-05-23 14:56:01 +01:00
};
inline
Indexer::Indexer (const Ranges& ranges, bool calcOffsets)
: index_(0), indices_(ranges.size(), 0), ranges_(ranges),
size_(Util::sizeExpected (ranges))
{
if (calcOffsets) {
calculateOffsets();
}
}
inline void
Indexer::increment()
{
for (size_t i = ranges_.size(); i-- > 0; ) {
indices_[i] ++;
if (indices_[i] != ranges_[i]) {
break;
} else {
indices_[i] = 0;
}
}
index_ ++;
}
inline void
Indexer::incrementDimension (size_t dim)
{
assert (dim < ranges_.size());
assert (ranges_.size() == offsets_.size());
assert (indices_[dim] < ranges_[dim]);
indices_[dim] ++;
index_ += offsets_[dim];
}
inline void
Indexer::incrementExceptDimension (size_t dim)
{
assert (ranges_.size() == offsets_.size());
for (size_t i = ranges_.size(); i-- > 0; ) {
if (i != dim) {
indices_[i] ++;
index_ += offsets_[i];
if (indices_[i] != ranges_[i]) {
return;
} else {
indices_[i] = 0;
index_ -= offsets_[i] * ranges_[i];
}
}
}
index_ = size_;
}
inline Indexer&
Indexer::operator++()
{
increment();
return *this;
}
inline
Indexer::operator size_t() const
{
return index_;
}
inline unsigned
Indexer::operator[] (size_t dim) const
{
assert (valid());
assert (dim < ranges_.size());
return indices_[dim];
}
inline bool
Indexer::valid() const
{
return index_ < size_;
}
inline void
Indexer::reset()
{
index_ = 0;
2013-02-13 15:25:55 +00:00
std::fill (indices_.begin(), indices_.end(), 0);
}
inline void
Indexer::resetDimension (size_t dim)
{
indices_[dim] = 0;
index_ -= offsets_[dim] * ranges_[dim];
}
inline size_t
Indexer::size() const
{
return size_ ;
}
inline void
Indexer::calculateOffsets()
{
size_t prod = 1;
offsets_.resize (ranges_.size());
for (size_t i = ranges_.size(); i-- > 0; ) {
offsets_[i] = prod;
prod *= ranges_[i];
}
}
class MapIndexer {
2012-05-23 14:56:01 +01:00
public:
2013-02-07 13:37:15 +00:00
MapIndexer (const Ranges& ranges, const std::vector<bool>& mask);
2012-05-23 14:56:01 +01:00
MapIndexer (const Ranges& ranges, size_t dim);
2012-12-20 23:19:10 +00:00
template <typename T>
MapIndexer (
2013-02-07 13:37:15 +00:00
const std::vector<T>& allArgs,
2013-02-13 15:25:55 +00:00
const Ranges& allRanges,
2013-02-07 13:37:15 +00:00
const std::vector<T>& wantedArgs,
2013-02-13 15:25:55 +00:00
const Ranges& wantedRanges);
MapIndexer& operator++();
2012-05-23 14:56:01 +01:00
operator size_t() const;
2012-05-23 14:56:01 +01:00
unsigned operator[] (size_t dim) const;
2012-05-23 14:56:01 +01:00
bool valid() const;
2012-05-23 14:56:01 +01:00
void reset();
2012-05-23 14:56:01 +01:00
private:
friend std::ostream& operator<< (std::ostream&, const MapIndexer&);
2013-02-07 13:37:15 +00:00
size_t index_;
Ranges indices_;
const Ranges& ranges_;
bool valid_;
std::vector<size_t> offsets_;
2012-12-27 22:25:45 +00:00
DISALLOW_COPY_AND_ASSIGN (MapIndexer);
2012-05-23 14:56:01 +01:00
};
2012-05-25 20:15:05 +01:00
inline
MapIndexer::MapIndexer (
const Ranges& ranges,
const std::vector<bool>& mask)
: index_(0), indices_(ranges.size(), 0), ranges_(ranges),
valid_(true)
{
size_t prod = 1;
offsets_.resize (ranges.size(), 0);
for (size_t i = ranges.size(); i-- > 0; ) {
if (mask[i]) {
offsets_[i] = prod;
prod *= ranges[i];
}
}
assert (ranges.size() == mask.size());
}
inline
MapIndexer::MapIndexer (const Ranges& ranges, size_t dim)
: index_(0), indices_(ranges.size(), 0), ranges_(ranges),
valid_(true)
{
size_t prod = 1;
offsets_.resize (ranges.size(), 0);
for (size_t i = ranges.size(); i-- > 0; ) {
if (i != dim) {
offsets_[i] = prod;
prod *= ranges[i];
}
}
}
template <typename T> inline
MapIndexer::MapIndexer (
2013-02-07 13:37:15 +00:00
const std::vector<T>& allArgs,
2013-02-13 15:25:55 +00:00
const Ranges& allRanges,
2013-02-07 13:37:15 +00:00
const std::vector<T>& wantedArgs,
2013-02-13 15:25:55 +00:00
const Ranges& wantedRanges)
: index_(0), indices_(allArgs.size(), 0), ranges_(allRanges),
valid_(true)
{
size_t prod = 1;
2013-02-07 13:37:15 +00:00
std::vector<size_t> offsets (wantedRanges.size());
for (size_t i = wantedRanges.size(); i-- > 0; ) {
offsets[i] = prod;
prod *= wantedRanges[i];
}
offsets_.reserve (allArgs.size());
for (size_t i = 0; i < allArgs.size(); i++) {
size_t idx = Util::indexOf (wantedArgs, allArgs[i]);
offsets_.push_back (idx != wantedArgs.size() ? offsets[idx] : 0);
}
}
inline MapIndexer&
MapIndexer::operator++()
{
assert (valid_);
for (size_t i = ranges_.size(); i-- > 0; ) {
indices_[i] ++;
index_ += offsets_[i];
if (indices_[i] != ranges_[i]) {
return *this;
} else {
indices_[i] = 0;
index_ -= offsets_[i] * ranges_[i];
}
}
valid_ = false;
return *this;
}
inline
MapIndexer::operator size_t() const
{
assert (valid());
return index_;
}
inline unsigned
MapIndexer::operator[] (size_t dim) const
{
assert (valid());
assert (dim < ranges_.size());
return indices_[dim];
}
inline bool
MapIndexer::valid() const
{
return valid_;
}
inline void
MapIndexer::reset()
{
index_ = 0;
2013-02-13 15:25:55 +00:00
std::fill (indices_.begin(), indices_.end(), 0);
}
} // namespace Horus
2013-02-07 23:53:13 +00:00
2013-02-08 00:20:01 +00:00
#endif // YAP_PACKAGES_CLPBN_HORUS_INDEXER_H_
2012-05-23 14:56:01 +01:00