Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find duplicates in vector using qt

Tags:

c++

csv

qt

I have a csv file whose contents look like the following:

Source  Target  LinkId  LinkName    Throughput
==================================================
1       12      1250     link1250           5
1       12      3250     link3250           14
1       14      1250     link1250           5
1       14      3250     link3250           14
1       18      1250     link1250           5
1       18      3250     link3250           14
2       12      2250     link2250           5
2       12      5250     link5250           14
2       14      2250     link2250           5
2       14      5250     link5250           14
2       18      2250     link2250           5
2       18      5250     link5250           14

and so on. The goal is to find the number of links that are multicast from each source node, i.e., for source 1, the targets are 12,14,18 for linkID 1250, for linkID 3250, for source 1, the targets are 12,14,18 and so on.

I have been using Qt and have read the csv into a vector of structs as follows:

struct edgeDetails_t{
    int source;
    int target;
    int linkID;
    QString linkName;
    int throughput;
};   
QVector<edgeDetails_t> multiCastLinks;

In order to achieve the goal as stated above, I have tried to use QHash

QHash<int, QList<int>> multiCastSenders;

with source as the key but I am not sure if this is the right way of doing this. Could someone please let me know any other way of doing this.

like image 792
smyslov Avatar asked Aug 08 '16 06:08

smyslov


2 Answers

This is also possible with QMultiMap which allows multiple keys, then you can use method keys() to get the keys and values(key) to get respective values.

To get rid of duplicate values you can use toSet() which returns only unique values of respective list.

QMultiMap<int, int> test;
test.insert(1, 12);
test.insert(1, 12);
test.insert(1, 12);
test.insert(1, 14);
test.insert(1, 18);
test.insert(1, 18);

test.insert(2, 12);
test.insert(2, 12);
test.insert(2, 12);
test.insert(2, 14);
test.insert(2, 18);
test.insert(2, 18);
qDebug() << "size:" << test.size();
QSet<int>::iterator it;
QSet<int> keys = test.keys().toSet();
qDebug() << "keys:" << keys;
for(it = keys.begin(); it != keys.end(); ++it) {
    qDebug() << "key:" << *it << "value:" << test.values(*it).toSet();
}

Output:

size: 12
keys: QSet(1, 2)
key: 1 value: QSet(12, 14, 18)
key: 2 value: QSet(12, 14, 18)

You can figure out the rest with your cool struct.

HTH

like image 124
nayana Avatar answered Oct 16 '22 06:10

nayana


struct edgeDetails_t{
  int linkID;
  QString linkName;
  int throughput;
}

struct sourceNode_t {
  int sourceNodeId; // For give source node
  QMap<int, int> targetNodeIdList; // Map link ID -> Target node ID
};

QMap<int, sourceNode_t> sourceNodeList; // All source nodes. Map node ID -> Node object
QMap<int, edgeDetails_t> edgeList; // All edges. Map edge ID -> Edge object

For give source node ID, you can easily find node object, from there you can iterate over all unique links and get target node ids.

like image 28
vcp Avatar answered Oct 16 '22 06:10

vcp