Page MenuHomec4science

iterator.hh
No OneTemporary

File Metadata

Created
Tue, May 14, 14:02

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 <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;
/* -------------------------------------------------------------------------- */
/* Constructors/Destructors */
/* -------------------------------------------------------------------------- */
/// constructor
iterator(pointer data,
std::size_t start,
ptrdiff_t offset,
std::vector<UInt> strides,
std::vector<UInt> sizes):
data(data),
index(start),
offset(offset),
strides(strides),
n(sizes) {}
/// copy constructor
iterator(const iterator & o):
data(o.data),
index(o.index),
offset(o.offset),
strides(o.strides),
n(o.n) {}
/// destructor
~iterator() {}
/// copy operator
iterator & operator=(const iterator & o) {
data = o.data;
index = o.index;
offset = o.offset;
strides = o.strides;
n = o.n;
}
/// dereference iterator
inline reference operator*() {
std::vector<UInt> tuple(strides.size());
UInt index_copy = index;
tuple.back() = index_copy % n.back();
index_copy -= tuple.back();
index_copy /= n.back();
/// Computing tuple from index
for (UInt d = tuple.size()-2 ; d > 0 ; d--) {
tuple[d] = index_copy % this->n[d];
index_copy -= tuple[d];
index_copy /= this->n[d];
}
tuple[0] = index_copy;
UInt access_offset = 0;
for (UInt i = 0 ; i < tuple.size() ; ++i)
access_offset += tuple[i] * strides[i];
return data[access_offset];
}
/// increment with given offset
inline iterator & operator+=(difference_type a) {
index += a*offset;
return *this;
}
/// increment iterator
iterator & operator++() {
*this += 1;
return *this;
}
/// comparison
inline bool operator<(const iterator & a) {
return index < a.index;
}
/// inequality
inline bool operator!=(const iterator & a) {
return index != a.index;
}
/// needed for OpenMP range calculations
inline Int operator-(const iterator & a) const {
return (index - a.index)/offset;
}
protected:
T * data;
std::size_t index;
ptrdiff_t offset;
std::vector<UInt> strides;
std::vector<UInt> n;
};
} // end namespace iterator
__END_TAMAAS__
/* -------------------------------------------------------------------------- */
#endif // __ITERATOR_HH__

Event Timeline