mardi 30 août 2016

How can I implement a custom C++ iterator that efficiently iterates over key-value pairs that are backed by two distinct arrays

I want to make use of libstdc++'s __gnu_parallel::multiway_merge to merge four large sequences of sorted key-value pairs at once (to save memory bandwidth). Each sequence of key-value pairs is represented by two distinct arrays, such that values[i] is the value associated with keys[i].

The implementation for multiway-merging a single array of keys (keys-only) or an array of std::pairs would be straightforward. However, I need to implement a custom iterator that I can pass to the multiway_merge, which holds one reference to my keys array and one to the corresponding values array.

So my approach looks something like the following:

    template<
        typename KeyT,
        typename ValueT
    >
    class ForwardIterator : public std::iterator<std::forward_iterator_tag, KeyT>
    {
        KeyT* k_itr;
        ValueT* v_itr;
        size_t offset;

        explicit ForwardIterator(KeyT* k_start, ValueT *v_start) : k_itr(k_start), v_itr(v_start), offset(0)
        {
        }
        ForwardIterator& operator++ () // Pre-increment
        {
            offset++;
            return *this;
        }
    }

However, the problems start as soon as I'm getting to the overloading of the dereferencing operator.

Help is really appreciated! Thanks!

Aucun commentaire:

Enregistrer un commentaire