void parseHits(QXmlStreamReader & in, Task & task, const QString subtask) { QMultiMap<int, Task::Hit> sortedHits; while(!in.atEnd()) { QXmlStreamReader::TokenType token = in.readNext(); if (token == QXmlStreamReader::StartElement) { QString elementName = in.name().toString(); if (0) { } else if (elementName.compare("h", Qt::CaseInsensitive) == 0){ Task::Hit hit; QXmlStreamAttributes attrs = in.attributes(); hit.timestamp = QDateTime::fromString(attrs.value("t").toString(), DATETIMEFORMAT); hit.duration = attrs.value("d").toString().toUInt(); sortedHits.insertMulti(hit.timestamp.toTime_t(), hit); } } else if (token == QXmlStreamReader::EndElement && in.name().toString().compare("hits", Qt::CaseInsensitive) == 0) { break; } } // remove duplicates QMultiMap<int, Task::Hit>::iterator itr = sortedHits.begin(); Task::Hit prev; while(itr != sortedHits.end()) { if (itr.value() == prev) itr = sortedHits.erase(itr); else { prev = itr.value(); itr++; } } task.hits[subtask] = sortedHits.values(); }
int QgsSnapper::snapMapPoint( const QgsPoint& mapCoordPoint, QList<QgsSnappingResult>& snappingResult, const QList<QgsPoint>& excludePoints ) { snappingResult.clear(); QMultiMap<double, QgsSnappingResult> snappingResultList;//all snapping results QMultiMap<double, QgsSnappingResult> currentResultList; //snapping results of examined layer //start point in (output) map coordinates QgsPoint layerCoordPoint; //start point in layer coordinates QgsSnappingResult newResult; QList<QgsSnapper::SnapLayer>::iterator snapLayerIt; for ( snapLayerIt = mSnapLayers.begin(); snapLayerIt != mSnapLayers.end(); ++snapLayerIt ) { if ( !snapLayerIt->mLayer->hasGeometryType() ) continue; currentResultList.clear(); //transform point from map coordinates to layer coordinates layerCoordPoint = mMapSettings.mapToLayerCoordinates( snapLayerIt->mLayer, mapCoordPoint ); double tolerance = QgsTolerance::toleranceInMapUnits( snapLayerIt->mTolerance, snapLayerIt->mLayer, mMapSettings, snapLayerIt->mUnitType ); if ( snapLayerIt->mLayer->snapWithContext( layerCoordPoint, tolerance, currentResultList, snapLayerIt->mSnapTo ) != 0 ) { //error } //transform each result from layer crs to map crs (including distance) QMultiMap<double, QgsSnappingResult>::iterator currentResultIt; for ( currentResultIt = currentResultList.begin(); currentResultIt != currentResultList.end(); ++currentResultIt ) { //for each snapping result: transform start point, snap point and other points into map coordinates to find out distance //store results in snapping result list newResult = currentResultIt.value(); newResult.snappedVertex = mMapSettings.layerToMapCoordinates( snapLayerIt->mLayer, currentResultIt.value().snappedVertex ); newResult.beforeVertex = mMapSettings.layerToMapCoordinates( snapLayerIt->mLayer, currentResultIt.value().beforeVertex ); newResult.afterVertex = mMapSettings.layerToMapCoordinates( snapLayerIt->mLayer, currentResultIt.value().afterVertex ); snappingResultList.insert( sqrt( newResult.snappedVertex.sqrDist( mapCoordPoint ) ), newResult ); } } //excluded specific points from result cleanResultList( snappingResultList, excludePoints ); //evaluate results according to snap mode QMultiMap<double, QgsSnappingResult>::iterator evalIt = snappingResultList.begin(); if ( evalIt == snappingResultList.end() ) { return 0; } //Gives a priority to vertex snapping over segment snapping QgsSnappingResult returnResult = evalIt.value(); for ( evalIt = snappingResultList.begin(); evalIt != snappingResultList.end(); ++evalIt ) { if ( evalIt.value().snappedVertexNr != -1 ) { returnResult = evalIt.value(); snappingResultList.erase( evalIt ); break; } } //We return the preferred result snappingResult.push_back( returnResult ); if ( mSnapMode == QgsSnapper::SnapWithOneResult ) { //return only a single result, nothing more to do } else if ( mSnapMode == QgsSnapper::SnapWithResultsForSamePosition ) { //take all snapping results within a certain tolerance because rounding differences may occur double tolerance = 0.000001; for ( evalIt = snappingResultList.begin(); evalIt != snappingResultList.end(); ++evalIt ) { if ( returnResult.snappedVertex.sqrDist( evalIt.value().snappedVertex ) < tolerance*tolerance ) { snappingResult.push_back( evalIt.value() ); } } } else //take all results { for ( evalIt = snappingResultList.begin(); evalIt != snappingResultList.end(); ++evalIt ) { snappingResult.push_back( evalIt.value() ); } } return 0; }
QByteArray huffman(QByteArray data, MainWindow *mainWindow) { // count int count[tableSize]; qFill(&count[0], &count[tableSize - 1], 0); for (int i = 0; i < data.size(); ++i) ++count[static_cast<quint8>(data[i])]; QMultiMap<int, QList<QPair<quint8, QBitArray> > > p; // <count, <symbol, code> > for (int i = 0; i < tableSize; ++i) { if (count[i] == 0) continue; QList<QPair<quint8, QBitArray> > list; list.append(qMakePair(static_cast<quint8>(i), QBitArray())); p.insert(count[i], list); } // caculate codes from bottom to top while (p.size() > 1) { const int count0 = p.begin().key(); QList<QPair<quint8, QBitArray> > list0 = p.begin().value(); p.erase(p.begin()); const int count1 = p.begin().key(); QList<QPair<quint8, QBitArray> > list1 = p.begin().value(); p.erase(p.begin()); for (QList<QPair<quint8, QBitArray> >::Iterator iter = list0.begin(); iter != list0.end(); ++iter) { iter->second.resize(iter->second.size() + 1); iter->second.setBit(iter->second.size() - 1, false); } for (QList<QPair<quint8, QBitArray> >::Iterator iter = list1.begin(); iter != list1.end(); ++iter) { iter->second.resize(iter->second.size() + 1); iter->second.setBit(iter->second.size() - 1, true); } p.insert(count0 + count1, list0 + list1); } // extract codes QHash<quint8, QBitArray> codes; for (QList<QPair<quint8, QBitArray> >::ConstIterator iter = p.begin().value().constBegin(); iter != p.begin().value().constEnd(); ++iter) { QBitArray code; code.resize(iter->second.size()); for (int j = 0; j < code.size(); ++j) if (iter->second[code.size() - j - 1]) code.setBit(j); codes[iter->first] = code; } // encode QBitArray bits; for (int i = 0; i < data.size(); ++i) { mainWindow->setProgress(qRound(static_cast<qreal>(100 * i) / data.size())); const QBitArray &code = codes[static_cast<quint8>(data[i])]; const int oldSize = bits.size(); bits.resize(oldSize + code.size()); for (int i = 0; i < code.size(); ++i) if (code[i]) bits.setBit(oldSize + i); } QByteArray result; { QDataStream stream(&result, QIODevice::WriteOnly); stream << codes << static_cast<qint32>(data.size()) << bits; } return result; }