Using Qt to generate stringlist combinations



  • Hi

    How can I use Qt to write a function "nemely, generator" that returns combinations of argument sequences "string lists" e.g. combine(('A','B'),('1','2','3')) will return [('A','1'), ('A','2'), ('A','3'), ('B','1'), ('B','2'), ('B','3')] on demand by calling next() or something i.e. the function will not create the whole list of combinations in memory at once.
    The function is to be applicable to any number of sequences i.e. combine(Sequence1, Sequence2, Sequence3, ...)

    Can anybody help me?



  • Qt hasn't got functionality for this ( as far as I know), but you can easily write your own class.
    You can pass a number of desired sequence combination and then calculate string position.
    For next() function, you'll need a vector of integers ( if you have n-number of sequences) for number of string in list and another one for location in each list.
    "QString":http://qt-project.org/doc/qt-4.8/QString.html and "QStringList":http://qt-project.org/doc/qt-4.8/QStringList.html documentation.

    Hope it helped ;)
    Regards,
    Jake



  • I would write an iterator class for this, so you can re-use the code later on in generic algorithms. In this case, the iterator would work on n (>=1) containers, instead of one container.

    Your main issue, I think, is the return value for your iterator. That value somehow has to contain any number of values, and that is tricky, especialy if they can be of different types. If they are all the same type, you could create a single accessor method that takes an index for the container, and just returns the value, so that would simplify the issue a lot.

    Could you describe your constraints a bit better? The containers, what do they contain? Are they all of the same type? Are they random access, or do they have contraints like forward-iterators only?



  • Hi Jake, can you please put a sample code here, just the main idea.
    Thanks in advance

    [quote author="Jake007" date="1331986938"]Qt hasn't got functionality for this ( as far as I know), but you can easily write your own class.
    You can pass a number of desired sequence combination and then calculate string position.
    For next() function, you'll need a vector of integers ( if you have n-number of sequences) for number of string in list and another one for location in each list.
    "QString":http://qt-project.org/doc/qt-4.8/QString.html and "QStringList":http://qt-project.org/doc/qt-4.8/QStringList.html documentation.

    Hope it helped ;)
    Regards,
    Jake
    [/quote]



  • Hi Andre
    This sounds promising! for now my main issue is the recursion in itself!
    BTW the sequences are of QString type to make it easier.

    The containers, what do they contain?
    They are lists of QString type.

    Are they all of the same type?
    Yes, QString.

    Are they random access, or do they have contraints like forward-iterators only?
    Forward-iterators only

    Thanks for the reply!

    [quote author="Andre" date="1332008101"]I would write an iterator class for this, so you can re-use the code later on in generic algorithms. In this case, the iterator would work on n (>=1) containers, instead of one container.

    Your main issue, I think, is the return value for your iterator. That value somehow has to contain any number of values, and that is tricky, especialy if they can be of different types. If they are all the same type, you could create a single accessor method that takes an index for the container, and just returns the value, so that would simplify the issue a lot.

    Could you describe your constraints a bit better? The containers, what do they contain? Are they all of the same type? Are they random access, or do they have contraints like forward-iterators only?
    [/quote]



  • Brain to terminal ( not tested and this may be completely wrong ;) ):
    @// input is index of desired sequnce
    QString CNemelyGen::getSequnceNr(int sqNr)
    {
    // storage for indexes of strings
    QVector<int> tmpIndex;

    // size list -> class variable QVector, to store sizes of strings
    for(int i=0; i<sizeList.size(); i++)
    {
    // Calculates index of current argument sequnces
    tmpIndex.append(sqNr%sizeList[i]);
    sqNr%=sizeList[i];
    }

    // String that will be returned
    QString retSqString;

    // generates string based on index
    for(int i=0; i<stringList.size(); i++)
    {
    // Gets character in each string and append it at end of return string
    retSqString.append(stringList.at[i].at(tmpIndex[i]));
    }

    return retSqString;
    }@

    And as for next(), add tmpIndex to class instead of function. And only add one to last index ( make sure to check if last index isn't maxed out ( last possible index), if it is, reset it to zero, and ++ one before. And check again if it's the last one etc...)

    Hope that provided comments will be enough.

    Regards,
    Jake


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.