Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include <vector>
#include <QList>
#include <QMap>
#include <QTextStream>
#include <QString>
#include <QStringList>
#include <QDir>
#include <QTime>
#include <QApplication>
#include <QDebug>
#include <QVector>

#include <qtconcurrentmap.h>

using namespace std;

using namespace QtConcurrent;

typedef QPair<int, QVector<int> > FileEntry;

QVector<int> mapping(FileEntry & entry, QVector< QVector<int> > mat)
{
    int index = entry.first;

    QVector<int> row1 = entry.second;

    QVector<int> row2 = mat.at(index);

    QVector<int> row;

    for(int i = 0; i != row1.size(); ++i)
    {
        row.append( row1.at(i) + row2.at(i));
    }



    return row;
}

void reduce(QVector< QVector<int> > & reduceResult, QVector<int> part )
{

    reduceResult.append(part);


}




int main()
{
    vector< vector<int> > vec;
    vector<int> temp;

    for(int i = 0; i != 9; ++i )
    {

        for(int i = 0; i != 9; ++i)
        {

            temp.push_back(i);
        }
        vec.push_back(temp);
    }

    vector< vector<int> >::iterator r;
    vector<int>::iterator c;


    QVector< QVector<int> > qmat;

    for (r = vec.begin(); r != vec.end(); r++)
    {
            QVector<int> qtemp;


            for (c = r->begin(); c != r->end(); c++)
            {
                qtemp.append(*c);


            }
            qmat.append(qtemp);



    }



    QList<FileEntry> fileEntries;
    fileEntries.reserve(9);
    int i = 0;


    QVectorIterator< QVector<int> > in(qmat);
    while (in.hasNext())
    {
        fileEntries << qMakePair(++i, in);
    }

    QVector< QVector <int> > final;

    final = mappedReduced(fileEntries, mapping, reduce, SequentialReduce);






}


I am trying to use the Qt framework, more specifically their MapReduce implementation; however, I don't really understand how to use it. I was trying to do matrix addition with it, but I think there's something wrong with the reduce function. I don't think it accepts anything other than lists, am I wrong? Can you tell me what the problem might be? I've been pulling my hair out for a while.

<pre lang="c++">#include <vector>
#include <iostream>
#include <QList>
#include <QMap>
#include <QTextStream>
#include <QString>
#include <QStringList>
#include <QDir>
#include <QTime>
#include <QApplication>
#include <QDebug>
#include <QVector>

#include <qtconcurrentmap.h>

using namespace std;

using namespace QtConcurrent;

//typedef QPair<int, QVector<int> > FileEntry;

void printQVec(QVector< QVector< int> > vec)
{
    for(int i = 0; i < vec.size(); ++i)
    {
        for(int j = 0; j < (vec.at(i)).size(); ++j)
        {
            cout << ((vec.at(i)).at(j)) << endl;
        }
    }
}

QVector<int> mapping(const QVector <int> vec)
{
    //int index = entry.first;

    //QVector<int> row1 = entry.second;

    //QVector<int> row2 = mat.at(index);

    QVector<int> row;

    for(int i = 0; i != vec.size(); ++i)
    {
        row.append( vec.at(i) + vec.at(i));

        cout << row.at(i) << endl;
    }



    return row;
}

void reduce(QVector< QVector<int> > & reduceResult, const QVector<int> part )
{

    //reduceResult.append(part);

    reduceResult += part;

    cout << "running reduce" << endl;


}




int main()
{
    vector< vector<int> > vec;
    vector<int> temp;

    for(int i = 0; i != 9; ++i )
    {

        for(int i = 0; i != 9; ++i)
        {

            temp.push_back(i);
        }
        vec.push_back(temp);
    }

    vector< vector<int> >::iterator r;
    vector<int>::iterator c;


    QVector< QVector<int> > qmat;

    for (r = vec.begin(); r != vec.end(); r++)
    {
            QVector<int> qtemp;


            for (c = r->begin(); c != r->end(); c++)
            {
                qtemp.append(*c);


            }
            qmat.append(qtemp);



    }

    printQVec(qmat);



    /*



    QList<FileEntry> fileEntries;
    fileEntries.reserve(9);
    int i = 0;


    QVectorIterator< QVector<int> > in(qmat);
    while (in.hasNext())
    {
        fileEntries << qMakePair(++i, in);
    }
    */

    QVector< QVector <int> > final;


    final = mappedReduced(qmat, mapping, reduce, SequentialReduce);










}


I tinkered the code a bit, but it still doesn't work. I can't produce a matrix at the end.
Posted
Updated 29-Mar-14 7:21am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900