1

I have a Qmap, contains some Objects( it is not free), for the sake of simplicity, imagine I want to add each object's child into the Qmap, so I have to Iterate the Qmap and insert each object's child on that. How I can do it? However, the real code which is a little more complex is here:

// This parameterSearchList will be looped over to include the the parameters that are implicitly used in the function in the lists of function.
// This includes both parameter QProperties of random variables and decision variables and the parameters upstream.
// The type of the parameter is determined by down-casting a pointer of the types RRandomVariable, RDecisionVariable, RConstant and RResponse to the parameter, respectively.
// If the down-casting to -for example- RRandomVariable does not return a NULL pointer, then the type of this parameter is a random variable. Thus, the pointer is added to the list of random variables.
// Likewise, if the type of the parameter is determined to be RDecisionVarible, RConstant, or RResponse, again the pointer is added to the corresponding lists, which are theDecisionVariableList, theConstantList, and theResponseList.
QMap<QString,RParameter *> parameterSearchList = theExplicitParameterList;
int counter = 0;
QMap<QString, RParameter *>::iterator iter;
for (iter=parameterSearchList.begin(); iter != parameterSearchList.end(); ++iter) {
    if (counter++ % 100 == 0) {
        QCoreApplication::processEvents();
    }

    RParameter *parameterObject = iter.value();

    // adding a lock for the parameter if it has not been added before
    if (! theDependencyCalculationLockHash.contains(parameterObject))
        theDependencyCalculationLockHash.insert(parameterObject, new QReadWriteLock());

    // Down-casting a pointer with the type RRandomVariable to the parameter
    RRandomVariable *randomVariableObject = qobject_cast<RRandomVariable *>(parameterObject);
    // If the pointer is not NULL, then the type of the parameter is random variable. Thus, this parameter should be added to the list of random variables.
    if (randomVariableObject) {
        // If "theRandomVariableList" does not already contain this parameter ... 
        if (!theRandomVariableList.contains(randomVariableObject)) {
            // Adding the parameter to the "theRandomVariableList"
            theRandomVariableList.append(randomVariableObject);

            // Adding the parameter QProperties of the random variable to the parameterSearchList
            QList<RParameter *> tempParameterList = randomVariableObject->getParameterList();
            for (int j = 0; j < tempParameterList.count(); j++) {
                if (!parameterSearchList.contains(tempParameterList[j]->objectName())) {
                    parameterSearchList.insert(tempParameterList[j]->objectName(),tempParameterList[j]);
                }
            }
        }
        continue;
    }

    // Down-casting a pointer with the type RDecisionVariable to the parameter
    RDecisionVariable *decisionVariableObject = qobject_cast<RDecisionVariable *>(parameterObject);
    // If the pointer is not NULL, then the type of the parameter is decision variable. Thus, this parameter should be added to the list of decision variables.
    if (decisionVariableObject) {
        // If "theDecisionVariableList" does not already contain this parameter ... 
        if (!theDecisionVariableList.contains(decisionVariableObject)) {
            // Adding the parameter to the "theDecisionVariableList"
            theDecisionVariableList.append(decisionVariableObject);

            // Adding the parameter QProperties of the decision variable to the parameterSearchList
            QList<RParameter *> tempParameterList = decisionVariableObject->getParameterList();
            for (int j = 0; j < tempParameterList.count(); j++) {
                if (!parameterSearchList.contains(tempParameterList[j]->objectName())) {
                    parameterSearchList.insert(tempParameterList[j]->objectName(), tempParameterList[j]);
                }
            }
        }
        continue;
    }

    // Down-casting a pointer with the type RConstant to the parameter
    RConstant *constantObject = qobject_cast<RConstant *>(parameterObject);
    // If the pointer is not NULL, then the type of the parameter is constant. Thus, this parameter should be added to the list of constants.
    if (constantObject) {
        // If "theConstantList" does not already contain this parameter ... 
        if (!theConstantList.contains(constantObject)) {
            // Adding the parameter to the "theConstantList"
            theConstantList.append(constantObject);
        }
        continue;
    }
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
hosh0425
  • 98
  • 1
  • 10
  • Can you please simplify your question relatively to you code: Which `objects` would you add their children to which`QMap` in your code. – Tom Kim Jun 02 '19 at 20:25
  • There are some `objects` in `theExplicitParameterList`, I copy these to a new map, called `parameterSearchList`. Now I want to iterate it and add children of `objects` in `parameterSearchList` to `parameterSearchList`! As you know, the problem is that some children may be added before iterator, so the iterator cannot see them and I cannot find their children, (It is like a recursive iteration, before this I used `Qlist` and because newly children appended in `Qlist` I was able to iterate them and also find their children) – hosh0425 Jun 03 '19 at 07:12

1 Answers1

0

Is that what your looking for:

  • Dont Iterating the map directly, get and store in variable the list of keys QList<String> of your QMap and iterate this list like follows:
// Get the list of maps keys
QList<QString> keys = parameterSearchList.keys();

// iterate keys
for(QString p : keys){

    // Down-casting a pointer with the type RRandomVariable to the parameter
    RRandomVariable *randomVariableObject = qobject_cast<RRandomVariable *>(parameterSearchList[p]);

    // If the pointer is not NULL, then the type of the parameter is random variable. Thus, this parameter should be added to the list of random variables.
    if (randomVariableObject) {

        // Iterate ParameterList
        for(RParameter * param : randomVariableObject->getParameterList()) {

            // Check if not already exist in parameterSearchList
            // (optional if your sure that parameters are unique)
            if(!parameterSearchList.contains(param->objectName())){

                // add param to parameterSearchList
                parameterSearchList.insert(param->objectName(), param);

            }
        }

    }
}

Hope it helps you.

Tom Kim
  • 416
  • 2
  • 4
  • I want to use `QMap`, because of its `.contains()` lower time complexity in comparison to `QList` . However, I get a copy of my `QList` to a `QMap` then I iterate on `Qlist` but check the `.contains()` on `QMap` – hosh0425 Jun 04 '19 at 16:27
  • In my answer `contains()` is used only with `QMap`. So, sorry but i didn't understand what do you want to mean. – Tom Kim Jun 04 '19 at 18:05
  • You are right, I just want to do not use a temporary `QList` which seems it is not possible. – hosh0425 Jun 05 '19 at 14:32
  • I think that is the best way to do what you want, but i will try to think about another solution. – Tom Kim Jun 05 '19 at 16:48