Page MenuHomec4science

iterator.hh
No OneTemporary

File Metadata

Created
Fri, Jun 7, 21:41

iterator.hh

/**
*
* @author Lucas Frérot <lucas.frerot@epfl.ch>
*
* @section LICENSE
*
* Copyright (©) 2016 EPFL (Ecole Polytechnique Fédérale de
* Lausanne) Laboratory (LSMS - Laboratoire de Simulation en Mécanique des
* Solides)
*
* Tamaas is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* Tamaas is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Tamaas. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* -------------------------------------------------------------------------- */
#ifndef __ITERATOR_HH__
#define __ITERATOR_HH__
/* -------------------------------------------------------------------------- */
#include "tamaas.hh"
#include <vector>
#include <utility>
#include <iterator>
#include <cstddef>
/* -------------------------------------------------------------------------- */
__BEGIN_TAMAAS__
namespace iterator_ {
template <typename T>
class iterator {
public:
typedef T value_type;
typedef std::ptrdiff_t difference_type;
typedef T * pointer;
typedef T & reference;
typedef std::random_access_iterator_tag iterator_category;
/* -------------------------------------------------------------------------- */
/* Constructors/Destructors */
/* -------------------------------------------------------------------------- */
/// constructor
iterator(pointer data,
std::size_t start,
ptrdiff_t offset):
data(data),
index(start),
step(offset),
access_offset(0) {}
/// copy constructor
iterator(const iterator & o):
data(o.data),
index(o.index),
step(o.step),
access_offset(o.access_offset) {}
/// destructor
~iterator() {}
/// copy operator
iterator & operator=(const iterator & o) {
data = o.data;
index = o.index;
step = o.step;
access_offset = o.access_offset;
return *this;
}
protected:
/// Compute access offset for data from index
inline void computeAccessOffset() {
access_offset = index;
}
public:
/// dereference iterator
inline reference operator*() {
return data[access_offset];
}
/// increment with given offset
inline iterator & operator+=(difference_type a) {
index += a*step;
computeAccessOffset();
return *this;
}
/// increment iterator
inline iterator & operator++() {
*this += 1;
return *this;
}
/// comparison
inline bool operator<(const iterator & a) const {
return index < a.index;
}
/// inequality
inline bool operator!=(const iterator & a) const {
return index != a.index;
}
/// equality
inline bool operator==(const iterator & a) const {
return index == a.index;
}
/// needed for OpenMP range calculations
inline difference_type operator-(const iterator & a) const {
return (index - a.index)/step;
}
protected:
T * data;
std::size_t index;
difference_type step;
difference_type access_offset;
};
template <typename T>
class iterator_view : iterator<T> {
public:
typedef T value_type;
typedef std::ptrdiff_t difference_type;
typedef T * pointer;
typedef T & reference;
/// Constructor
iterator_view(pointer data,
std::size_t start,
ptrdiff_t offset,
std::vector<UInt> strides,
std::vector<UInt> sizes):
iterator<T>(data, start, offset),
strides(strides),
n(sizes),
tuple(strides.size()) {}
iterator_view(const iterator_view & o):
iterator<T>(o),
strides(o.strides),
n(o.n),
tuple(o.tuple) {}
iterator_view & operator=(const iterator_view & o) {
iterator<T>::operator=(o);
strides = o.strides;
n = o.n;
tuple = o.tuple;
}
protected:
void computeAccessOffset() {
UInt index_copy = this->index;
this->tuple.back() = index_copy % this->n.back();
index_copy -= this->tuple.back();
index_copy /= this->n.back();
/// Computing tuple from index
for (UInt d = this->tuple.size()-2 ; d > 0 ; d--) {
this->tuple[d] = index_copy % this->n[d];
index_copy -= this->tuple[d];
index_copy /= this->n[d];
}
this->tuple[0] = index_copy;
this->access_offset = 0;
for (UInt i = 0 ; i < this->tuple.size() ; ++i)
this->access_offset += this->tuple[i] * this->strides[i];
}
std::vector<UInt> strides;
std::vector<UInt> n;
std::vector<UInt> tuple;
};
} // end namespace iterator_
__END_TAMAAS__
/* -------------------------------------------------------------------------- */
#endif // __ITERATOR_HH__

Event Timeline