C++ (Qt)QMap <QString, QMap <int, QMap <bool, Data*>>> byName;QMap <QString, Data*> anyType; Data* findObject(QString name, int type, bool visible){ auto byType = byName.find(name); //has name if (byType != byName.end()) { auto byVisibility = byType->find(type); //has type if (byVisibility != byType->end()) { auto object = byVisibility->find(visibility); //has visible if (object != byVisibility->end()) return *object; //no visible else return byVisibility[!visibility]; } //no type else { return anyType[name]; } } //no name return nullptr;}
C++ (Qt)void insert(QString name, int type, bool visible, Data *value){ anyType[name] = value; byName[name][type][visible] = value;}
C++ (Qt)struct Node { QString GetName( void ) const; int GetType( void ) const; bool GetVisibility( void ) const;}; typedef QPair<QString, int> TKey; // name + indextypedef QPair<int, int> TAux; // id (as bool) + visible (as bool)typedef QMap<TKey, Node *> TFindMap; TFindMap BuildFindMap( const QVector<Node *> & nodes ){ TFindMap fmap; for (int i = 0; i < nodes.size(); ++i) fmap[TKey(nodes[i]->GetName(), i)] = nodes[i]; return fmap;} Node * FindNode( const TFindMap & fmap, const QString & name, int type ){ TKey key(name, -1); TFindMap::const_iterator it = fmap.lowerBound(key); Node * bestNode = 0; TAux bestAux(-1, -1); while (it != fmap.end()) { if (it.key().first != name) break; Node * node = it.value(); TAux curAux((node->GetType() == type) ? 1 : 0, node->GetVisibility() ? 1 : 0); if (curAux > bestAux) { bestNode = node; bestAux = curAux; if (bestAux.first && bestAux.second) break; } ++it; } return bestNode;}
C++ (Qt)...
C++ (Qt)lowerBound(TKey(name, true, type)) // если найдено (name + type) - возвращаем, иначе lowerBound(TKey(name, false, type)) // если найдено (name + type) - возвращаем, иначе// лучший из 2