2
\$\begingroup\$

I wrote an iterator to access the pixel in an packed image. It works, but it is slow.

1.Are there any mistakes that can be avoided and might lead to better speed?

2.Are there any pitfalls that wait for me when using this code?

3.Is there a solution to the operator-> ?

4.Has any of the above an extra answer when using this iterator with cuda? (In an kernel and the intern iterator of GroupIterator is an pointer to device memory). Or is there an other approch when accesing pixel in cuda?

template<unsigned int N, class T>
class Pixel_values;
template<unsigned int N, class T>
class Pixel_reference;

template<unsigned int N, class T>
class Pixel_values{
private:
    T values[N];
public:
    using size_type = size_t;
    Pixel_values(){}
    Pixel_values(const Pixel_values<N,T>& other);
    Pixel_values(const Pixel_reference<N,T>& other);
    ~Pixel_values(){}

    Pixel_values& operator=(const Pixel_values<N,T>&);
    Pixel_values& operator=(const Pixel_reference<N,T>&);

    T operator[](size_type) const;
    T& operator[](size_type);
};

template<unsigned int N, class T>
class Pixel_reference{
private:
    T* values;
public:
    using size_type = size_t;
    Pixel_reference(){}
    Pixel_reference(T* values):values(values){}
    Pixel_reference(const Pixel_reference<N,T>& other):values(other.values){}
    ~Pixel_reference(){}

    Pixel_reference& operator=(const Pixel_values<N,T>&);
    Pixel_reference& operator=(const Pixel_reference<N,T>&);

    T operator[](size_type) const;
    T& operator[](size_type);
};


/**
 * @brief The GroupIterator class durchläuft einen anderen random_access_iterator in Sprüngen und fast die überSprungenen ETräge zu einem PixelTypen zusammen.
 */
template<unsigned int N, class T>
class GroupIterator{
public:
    using base_type = typename std::iterator_traits<T>::value_type;
    using size_type = typename std::iterator_traits<T>::difference_type;
    using difference_type = size_type;
    using value_type = Pixel_values<N,base_type>;
    using reference = Pixel_reference<N,base_type>;
    using pointer = Pixel_reference<N,base_type>*;
    using iterator_category = std::random_access_iterator_tag;

    GroupIterator();
    GroupIterator(T);
    GroupIterator(const GroupIterator&);
    ~GroupIterator();

    GroupIterator<N,T>& operator=(const GroupIterator<N,T>&);
    bool operator==(const GroupIterator<N,T>&) const;
    bool operator!=(const GroupIterator<N,T>&) const;
    bool operator<(const GroupIterator<N,T>&) const;
    bool operator>(const GroupIterator<N,T>&) const;
    bool operator<=(const GroupIterator<N,T>&) const;
    bool operator>=(const GroupIterator<N,T>&) const;

    GroupIterator<N,T>& operator++();
    GroupIterator<N,T>  operator++(int);
    GroupIterator<N,T>& operator--();
    GroupIterator<N,T>  operator--(int);
    GroupIterator<N,T>& operator+=(size_type);
    GroupIterator<N,T> operator+(size_type) const;
    friend GroupIterator<N,T> operator+(size_type, const GroupIterator<N,T>&);
    GroupIterator<N,T>& operator-=(size_type);
    GroupIterator<N,T> operator-(size_type) const;
    difference_type operator-(GroupIterator<N,T>) const;

    reference operator*() const;
    //pointer operator->() const;
    reference operator[](size_type) const;

private:
    T position;
};

/// Pixel_values

template<unsigned int N, class T>
Pixel_values<N,T>::Pixel_values(const Pixel_values<N,T>& other){
    for(T i = 0; i < N; ++i){
        values[i] = other[i];
    }
}

template<unsigned int N, class T>
Pixel_values<N,T>::Pixel_values(const Pixel_reference<N,T>& other){
    for(T i = 0; i < N; ++i){
        values[i] = other[i];
    }
}

template<unsigned int N, class T>
T& Pixel_values<N,T>::operator[](size_type index){
    return values[index];
}


template<unsigned int N, class T>
T Pixel_values<N,T>::operator[](size_type index) const{
    return values[index];
}

/// Pixel_reference
template<unsigned int N, class T>
T& Pixel_reference<N,T>::operator[](size_type index){
    return values[index];
}


template<unsigned int N, class T>
T Pixel_reference<N,T>::operator[](size_type index) const{
    return values[index];
}
/// GroupIterator
template<unsigned int N, class T>
GroupIterator<N,T>::GroupIterator(){}
template<unsigned int N, class T>
GroupIterator<N,T>::GroupIterator(T position):position(position){}
template<unsigned int N, class T>
GroupIterator<N,T>::GroupIterator(const GroupIterator<N,T>& other):position(other.position){}
template<unsigned int N, class T>
GroupIterator<N,T>::~GroupIterator(){}

template<unsigned int N, class T>
GroupIterator<N,T>& GroupIterator<N,T>::operator=(const GroupIterator<N,T>& other){ this->position = other.position;}
template<unsigned int N, class T>
bool GroupIterator<N,T>::operator==(const GroupIterator<N,T>& other) const{return this->position == other.position;}
template<unsigned int N, class T>
bool GroupIterator<N,T>::operator!=(const GroupIterator<N,T>& other) const{return this->position != other.position;}
template<unsigned int N, class T>
bool GroupIterator<N,T>::operator< (const GroupIterator<N,T>& other) const{return this->position <  other.position;}
template<unsigned int N, class T>
bool GroupIterator<N,T>::operator> (const GroupIterator<N,T>& other) const{return this->position >  other.position;}
template<unsigned int N, class T>
bool GroupIterator<N,T>::operator<=(const GroupIterator<N,T>& other) const{return this->position <= other.position;}
template<unsigned int N, class T>
bool GroupIterator<N,T>::operator>=(const GroupIterator<N,T>& other) const{return this->position >= other.position;}

template<unsigned int N, class T>
GroupIterator<N,T>& GroupIterator<N,T>::operator++()   {
    this->position += N;
    return *this;
}
template<unsigned int N, class T>
GroupIterator<N,T> GroupIterator<N,T>::operator++(int){
    GroupIterator<N,T>&result(this->position);
    this->position += N;
    return result;
}
template<unsigned int N, class T>
GroupIterator<N,T>& GroupIterator<N,T>::operator--()   {
    this->position -= N;
    return *this;
}
template<unsigned int N, class T>
GroupIterator<N,T> GroupIterator<N,T>::operator--(int){
    GroupIterator<N,T>&result(this->position);
    this->position -= N;
    return result;
}

template<unsigned int N, class T>
GroupIterator<N,T>& GroupIterator<N,T>::operator+=(size_type step){
    this->position += step*N;
    return *this;
}
template<unsigned int N, class T>
GroupIterator<N,T> GroupIterator<N,T>::operator+(size_type step) const{
    return GroupIterator(this->position+step*N);
}
template<unsigned int N, class T>
GroupIterator<N,T> operator+(typename GroupIterator<N,T>::size_type step, const GroupIterator<N,T>& g_itr){
    return GroupIterator<N,T>(g_itr.position+step*N);
}
template<unsigned int N, class T>
GroupIterator<N,T>&  GroupIterator<N,T>::operator-=(size_type step){
    this->position -= step*N;
    return *this;
}
template<unsigned int N, class T>
GroupIterator<N,T>  GroupIterator<N,T>::operator-(size_type step) const{
    return GroupIterator(this->position-step*N);
}
template<unsigned int N, class T>
typename GroupIterator<N,T>::difference_type GroupIterator<N,T>::operator-(GroupIterator<N,T> other) const{
    return (this->position-other.position)/N; // NOTE: Division in operator- könnte kostenstelle sein
}

template<unsigned int N, class T>
typename GroupIterator<N,T>::reference GroupIterator<N,T>::operator*() const{
    return reference(&(*this->position));
}

template<unsigned int N, class T>
Pixel_reference<N,T> foo(T* position){
    return Pixel_reference<N,T>(position);
}

//template<unsigned int N, class T>
//typename GroupIterator<N,T>::poTer GroupIterator<N,T>::operator->() const{
//    return &foo<N,T>(this->position);//&Pixel_reference<N,T>(this->position);
//}
// error: taking address of temporary

template<unsigned int N, class T>
typename GroupIterator<N,T>::reference GroupIterator<N,T>::operator[](size_type index) const{
    return reference(&(this->position[index*N]));
}
\$\endgroup\$
  • 2
    \$\begingroup\$ I don't understand what you are trying to accomplish with this code. Could you explain a bit further what this code does? \$\endgroup\$ – 200_success Nov 30 '18 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.