Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use map instead of hash
  • Loading branch information
nyalldawson committed Mar 25, 2022
1 parent 75e5fab commit 74db166
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 12 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Expand Up @@ -955,7 +955,7 @@ if (WITH_CORE AND WITH_BINDINGS AND NOT WITH_QT6)
include(SIPMacros)

set(SIP_INCLUDES ${PYQT_SIP_DIR} ${CMAKE_SOURCE_DIR}/python)
set(SIP_CONCAT_PARTS 16)
set(SIP_CONCAT_PARTS 17)

if (NOT BINDINGS_GLOBAL_INSTALL)
set(Python_SITEARCH ${QGIS_DATA_DIR}/python)
Expand Down
Expand Up @@ -30,7 +30,7 @@ Abstract base class for storage of elevation profiles.
Returns the unique string identifier for the results type.
%End

virtual QHash< double, double > distanceToHeightMap() const = 0;
virtual QMap< double, double > distanceToHeightMap() const = 0;
%Docstring
Returns the map of distance (chainage) to height.
%End
Expand Down
80 changes: 80 additions & 0 deletions python/core/conversions.sip
Expand Up @@ -1041,6 +1041,86 @@ template<double, TYPE>
%End
};

%MappedType QMap<double, double>
{
%TypeHeaderCode
#include <QMap>
%End

%ConvertFromTypeCode
// Create the dictionary.
PyObject *d = PyDict_New();

if (!d)
return NULL;

// Set the dictionary elements.
QMap<double, double>::iterator i;

for (i = sipCpp->begin(); i != sipCpp->end(); ++i)
{
PyObject *t1obj = PyFloat_FromDouble(i.key());
PyObject *t2obj = PyFloat_FromDouble(i.value());

if (t1obj == NULL || t2obj == NULL || PyDict_SetItem(d, t1obj, t2obj) < 0)
{
Py_DECREF(d);

if (t1obj)
Py_DECREF(t1obj);

if (t2obj)
Py_DECREF(t2obj);

return NULL;
}

Py_DECREF(t1obj);
Py_DECREF(t2obj);
}

return d;
%End

%ConvertToTypeCode
PyObject *t1obj, *t2obj;
Py_ssize_t i = 0;

// Check the type if that is all that is required.
if (sipIsErr == NULL)
{
if (!PyDict_Check(sipPy))
return 0;

while (PyDict_Next(sipPy, &i, &t1obj, &t2obj))
{
if (!PyFloat_Check(t1obj))
return 0;

if (!PyFloat_Check(t2obj))
return 0;
}

return 1;
}

QMap<double, double> *qm = new QMap<double, double>;

while (PyDict_Next(sipPy, &i, &t1obj, &t2obj))
{
int state;

double k = PyFloat_AsDouble(t1obj);
double v = PyFloat_AsDouble(t2obj);

qm->insert(k, v);
}

*sipCppPtr = qm;

return sipGetState(sipTransferObj);
%End
};

template<TYPE2>
%MappedType QMap<QString, QList<TYPE2> >
Expand Down
2 changes: 1 addition & 1 deletion src/core/elevation/qgsabstractprofilegenerator.h
Expand Up @@ -45,7 +45,7 @@ class CORE_EXPORT QgsAbstractProfileResults
/**
* Returns the map of distance (chainage) to height.
*/
virtual QHash< double, double > distanceToHeightMap() const = 0;
virtual QMap< double, double > distanceToHeightMap() const = 0;

/**
* Returns a list of sampled points, with their calculated elevation
Expand Down
4 changes: 2 additions & 2 deletions src/core/mesh/qgsmeshlayerprofilegenerator.cpp
Expand Up @@ -32,9 +32,9 @@ QString QgsMeshLayerProfileResults::type() const
return QStringLiteral( "mesh" );
}

QHash<double, double> QgsMeshLayerProfileResults::distanceToHeightMap() const
QMap<double, double> QgsMeshLayerProfileResults::distanceToHeightMap() const
{
QHash<double, double> res;
QMap<double, double> res;
for ( const Result &r : results )
{
res.insert( r.distance, r.height );
Expand Down
2 changes: 1 addition & 1 deletion src/core/mesh/qgsmeshlayerprofilegenerator.h
Expand Up @@ -58,7 +58,7 @@ class CORE_EXPORT QgsMeshLayerProfileResults : public QgsAbstractProfileResults
QList< Result > results;

QString type() const override;
QHash< double, double > distanceToHeightMap() const override;
QMap< double, double > distanceToHeightMap() const override;
QgsPointSequence sampledPoints() const override;
QVector< QgsGeometry > asGeometries() const override;
};
Expand Down
4 changes: 2 additions & 2 deletions src/core/raster/qgsrasterlayerprofilegenerator.cpp
Expand Up @@ -32,9 +32,9 @@ QString QgsRasterLayerProfileResults::type() const
return QStringLiteral( "raster" );
}

QHash<double, double> QgsRasterLayerProfileResults::distanceToHeightMap() const
QMap<double, double> QgsRasterLayerProfileResults::distanceToHeightMap() const
{
QHash<double, double> res;
QMap<double, double> res;
for ( const Result &r : results )
{
res.insert( r.distance, r.height );
Expand Down
2 changes: 1 addition & 1 deletion src/core/raster/qgsrasterlayerprofilegenerator.h
Expand Up @@ -55,7 +55,7 @@ class CORE_EXPORT QgsRasterLayerProfileResults : public QgsAbstractProfileResult
QList< Result > results;

QString type() const override;
QHash< double, double > distanceToHeightMap() const override;
QMap< double, double > distanceToHeightMap() const override;
QgsPointSequence sampledPoints() const override;
QVector< QgsGeometry > asGeometries() const override;
};
Expand Down
4 changes: 2 additions & 2 deletions src/core/vector/qgsvectorlayerprofilegenerator.cpp
Expand Up @@ -39,9 +39,9 @@ QString QgsVectorLayerProfileResults::type() const
return QStringLiteral( "vector" );
}

QHash<double, double> QgsVectorLayerProfileResults::distanceToHeightMap() const
QMap<double, double> QgsVectorLayerProfileResults::distanceToHeightMap() const
{
QHash<double, double> res;
QMap<double, double> res;
for ( const Result &r : results )
{
res.insert( r.distance, r.height );
Expand Down
2 changes: 1 addition & 1 deletion src/core/vector/qgsvectorlayerprofilegenerator.h
Expand Up @@ -62,7 +62,7 @@ class CORE_EXPORT QgsVectorLayerProfileResults : public QgsAbstractProfileResult
QVector< QgsGeometry > geometries;

QString type() const override;
QHash< double, double > distanceToHeightMap() const override;
QMap< double, double > distanceToHeightMap() const override;
QgsPointSequence sampledPoints() const override;
QVector< QgsGeometry > asGeometries() const override;
};
Expand Down

0 comments on commit 74db166

Please sign in to comment.