dimanche 6 décembre 2015

Creating sets of strings from list of lists

I have list of lists containing strings:

{ { "a", "b", "c" },
  { "1", "2" },
  { "x" } }

The number of inner lists may differ, as well as their length. What I want to get should join every possibility of every list with eachother so the result should be (for the exxample above):

{ { "a", "1", "x" },
  { "a", "2", "x" },
  { "b", "1", "x" },
  { "b", "2", "x" },
  { "c", "1", "x" },
  { "c", "2", "x" } }

I've modified a function I've found online so it looks like this:

static void permutation_states(QList<QStringList>& lists, QList<QStringList>& result, qint32 depth, QStringList& current) {
    if (depth == lists.length()) {
        result.append(current);
        return;
    }

    for (qint32 i = 0; i < lists.at(depth).length(); ++i) {
        QStringList new_current = current;
        new_current.append(lists.at(depth).at(i));
        permutation_states(lists, result, depth + 1, new_current);
    }
}

And it's called like that:

QList<QStringList> states;
for (const ConditionType& condition : conditions) {
    states.append((QStringList)FieldObject::condition_names(condition));
}
QList<QStringList> sorted_states;
QStringList var;
FieldObject::permutation_states(states, sorted_states, 0, var);

It's not working and yells

ASSERT failure in QList::at: "index out of range"

on lists.at(depth) of for-loop. I mean... I think that's there; I was trying to use qDebug() to find the exact place and reason but it's working everywhere but inside this function's body. I hope the error is somehow obvious.

The oryginal solution was written for Java and can be found here: http://ift.tt/1HP1jeq

Aucun commentaire:

Enregistrer un commentaire