Skip to content

Commit a360422

Browse files
committed
[pal] Remove LinkedList and HashTable
This commit replace pal's use of it's own custom linked lists and hash tables by Qt's QLinkedList and QHash classes. All use of char arrays have also been replaced by QStrings. Testing reveals no speed improvements by the changes, but the changes bring pal more into line with QGIS core and make the code much more readable.
1 parent 217e9e7 commit a360422

21 files changed

+283
-924
lines changed

src/core/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,6 @@ SET(QGIS_CORE_SRCS
249249
pal/priorityqueue.cpp
250250
pal/problem.cpp
251251
pal/util.cpp
252-
pal/linkedlist.hpp
253-
pal/hashtable.hpp
254252
pal/rtree.hpp
255253

256254
raster/qgscliptominmaxenhancement.cpp

src/core/pal/feature.cpp

Lines changed: 33 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,14 @@
3434
#include <iostream>
3535
#endif
3636

37-
#include <qglobal.h>
37+
#include <QLinkedList>
3838

3939
#include <cmath>
4040
#include <cstring>
4141
#include <cfloat>
4242

4343
#include "pal.h"
4444
#include "layer.h"
45-
#include "linkedlist.hpp"
4645
#include "feature.h"
4746
#include "geomfunction.h"
4847
#include "labelposition.h"
@@ -55,13 +54,14 @@
5554

5655
namespace pal
5756
{
58-
Feature::Feature( Layer* l, const char* geom_id, PalGeometry* userG, double lx, double ly )
57+
Feature::Feature( Layer* l, const QString &geom_id, PalGeometry* userG, double lx, double ly )
5958
: layer( l )
6059
, userGeom( userG )
6160
, label_x( lx )
6261
, label_y( ly )
6362
, distlabel( 0 )
6463
, labelInfo( NULL )
64+
, uid( geom_id )
6565
, fixedPos( false )
6666
, fixedPosX( 0.0 )
6767
, fixedPosY( 0.0 )
@@ -77,14 +77,11 @@ namespace pal
7777
, alwaysShow( false )
7878
{
7979
assert( finite( lx ) && finite( ly ) );
80-
81-
uid = new char[strlen( geom_id ) +1];
82-
strcpy( uid, geom_id );
8380
}
8481

8582
Feature::~Feature()
8683
{
87-
delete[] uid;
84+
8885
}
8986

9087
////////////
@@ -260,7 +257,7 @@ namespace pal
260257
}
261258

262259

263-
const char * FeaturePart::getUID()
260+
QString FeaturePart::getUID() const
264261
{
265262
return f->uid;
266263
}
@@ -573,11 +570,7 @@ namespace pal
573570
if ( flags == 0 )
574571
flags = FLAG_ON_LINE; // default flag
575572

576-
//LinkedList<PointSet*> *shapes_final;
577-
578-
//shapes_final = new LinkedList<PointSet*>(ptrPSetCompare);
579-
580-
LinkedList<LabelPosition*> *positions = new LinkedList<LabelPosition*> ( ptrLPosCompare );
573+
QLinkedList<LabelPosition*> positions;
581574

582575
int nbPoints;
583576
double *x;
@@ -691,16 +684,15 @@ namespace pal
691684
bool belowLine = ( !reversed && ( flags & FLAG_BELOW_LINE ) ) || ( reversed && ( flags & FLAG_ABOVE_LINE ) );
692685

693686
if ( aboveLine )
694-
positions->push_back( new LabelPosition( i, bx + cos( beta ) *distlabel, by + sin( beta ) *distlabel, xrm, yrm, alpha, cost, this, isRightToLeft ) ); // Line
687+
positions.append( new LabelPosition( i, bx + cos( beta ) *distlabel, by + sin( beta ) *distlabel, xrm, yrm, alpha, cost, this, isRightToLeft ) ); // Line
695688
if ( belowLine )
696-
positions->push_back( new LabelPosition( i, bx - cos( beta ) *( distlabel + yrm ), by - sin( beta ) *( distlabel + yrm ), xrm, yrm, alpha, cost, this, isRightToLeft ) ); // Line
689+
positions.append( new LabelPosition( i, bx - cos( beta ) *( distlabel + yrm ), by - sin( beta ) *( distlabel + yrm ), xrm, yrm, alpha, cost, this, isRightToLeft ) ); // Line
697690
if ( flags & FLAG_ON_LINE )
698-
positions->push_back( new LabelPosition( i, bx - yrm*cos( beta ) / 2, by - yrm*sin( beta ) / 2, xrm, yrm, alpha, cost, this, isRightToLeft ) ); // Line
691+
positions.append( new LabelPosition( i, bx - yrm*cos( beta ) / 2, by - yrm*sin( beta ) / 2, xrm, yrm, alpha, cost, this, isRightToLeft ) ); // Line
699692
}
700693
else if ( f->layer->arrangement == P_HORIZ )
701694
{
702-
positions->push_back( new LabelPosition( i, bx - xrm / 2, by - yrm / 2, xrm, yrm, 0, cost, this ) ); // Line
703-
//positions->push_back( new LabelPosition(i, bx -yrm/2, by - yrm*sin(beta)/2, xrm, yrm, alpha, cost, this, line)); // Line
695+
positions.append( new LabelPosition( i, bx - xrm / 2, by - yrm / 2, xrm, yrm, 0, cost, this ) ); // Line
704696
}
705697
else
706698
{
@@ -720,17 +712,15 @@ namespace pal
720712
delete[] d;
721713
delete[] ad;
722714

723-
int nbp = positions->size();
715+
int nbp = positions.size();
724716
*lPos = new LabelPosition *[nbp];
725717
i = 0;
726-
while ( positions->size() > 0 )
718+
while ( positions.size() > 0 )
727719
{
728-
( *lPos )[i] = positions->pop_front();
720+
( *lPos )[i] = positions.takeFirst();
729721
i++;
730722
}
731723

732-
delete positions;
733-
734724
return nbp;
735725
}
736726

@@ -967,7 +957,7 @@ namespace pal
967957
return 0;
968958
}
969959

970-
LinkedList<LabelPosition*> *positions = new LinkedList<LabelPosition*> ( ptrLPosCompare );
960+
QLinkedList<LabelPosition*> positions;
971961
double delta = max( f->labelInfo->label_height, total_distance / 10.0 );
972962

973963
unsigned long flags = f->layer->getArrangementFlags();
@@ -1017,25 +1007,24 @@ namespace pal
10171007
double angle_avg = atan2( sin_avg / f->labelInfo->char_num, cos_avg / f->labelInfo->char_num );
10181008
// displacement
10191009
if ( flags & FLAG_ABOVE_LINE )
1020-
positions->push_back( _createCurvedCandidate( slp, angle_avg, f->distlabel ) );
1010+
positions.append( _createCurvedCandidate( slp, angle_avg, f->distlabel ) );
10211011
if ( flags & FLAG_ON_LINE )
1022-
positions->push_back( _createCurvedCandidate( slp, angle_avg, -f->labelInfo->label_height / 2 ) );
1012+
positions.append( _createCurvedCandidate( slp, angle_avg, -f->labelInfo->label_height / 2 ) );
10231013
if ( flags & FLAG_BELOW_LINE )
1024-
positions->push_back( _createCurvedCandidate( slp, angle_avg, -f->labelInfo->label_height - f->distlabel ) );
1014+
positions.append( _createCurvedCandidate( slp, angle_avg, -f->labelInfo->label_height - f->distlabel ) );
10251015

10261016
// delete original candidate
10271017
delete slp;
10281018
}
10291019
}
10301020

10311021

1032-
int nbp = positions->size();
1022+
int nbp = positions.size();
10331023
( *lPos ) = new LabelPosition*[nbp];
10341024
for ( int i = 0; i < nbp; i++ )
10351025
{
1036-
( *lPos )[i] = positions->pop_front();
1026+
( *lPos )[i] = positions.takeFirst();
10371027
}
1038-
delete positions;
10391028
delete[] path_distances;
10401029

10411030
return nbp;
@@ -1081,28 +1070,20 @@ namespace pal
10811070

10821071
//print();
10831072

1084-
//LinkedList<PointSet*> *shapes_toCut;
1085-
LinkedList<PointSet*> *shapes_toProcess;
1086-
LinkedList<PointSet*> *shapes_final;
1087-
1088-
//shapes_toCut = new LinkedList<PointSet*>(ptrPSetCompare);
1089-
shapes_toProcess = new LinkedList<PointSet*> ( ptrPSetCompare );
1090-
shapes_final = new LinkedList<PointSet*> ( ptrPSetCompare );
1073+
QLinkedList<PointSet*> shapes_toProcess;
1074+
QLinkedList<PointSet*> shapes_final;
10911075

10921076
mapShape->parent = NULL;
10931077

1094-
shapes_toProcess->push_back( mapShape );
1078+
shapes_toProcess.append( mapShape );
10951079

10961080
splitPolygons( shapes_toProcess, shapes_final, xrm, yrm, f->uid );
10971081

1098-
1099-
delete shapes_toProcess;
1100-
11011082
int nbp;
11021083

1103-
if ( shapes_final->size() > 0 )
1084+
if ( shapes_final.size() > 0 )
11041085
{
1105-
LinkedList<LabelPosition*> *positions = new LinkedList<LabelPosition*> ( ptrLPosCompare );
1086+
QLinkedList<LabelPosition*> positions;
11061087

11071088
int id = 0; // ids for candidates
11081089
double dlx, dly; // delta from label center and bottom-left corner
@@ -1114,13 +1095,13 @@ namespace pal
11141095
double beta;
11151096
double diago = sqrt( xrm * xrm / 4.0 + yrm * yrm / 4 );
11161097
double rx, ry;
1117-
CHullBox **boxes = new CHullBox*[shapes_final->size()];
1098+
CHullBox **boxes = new CHullBox*[shapes_final.size()];
11181099
j = 0;
11191100

11201101
// Compute bounding box foreach finalShape
1121-
while ( shapes_final->size() > 0 )
1102+
while ( shapes_final.size() > 0 )
11221103
{
1123-
PointSet *shape = shapes_final->pop_front();
1104+
PointSet *shape = shapes_final.takeFirst();
11241105
boxes[j] = shape->compute_chull_bbox();
11251106

11261107
if ( shape->parent )
@@ -1248,13 +1229,13 @@ namespace pal
12481229
if ( isPointInPolygon( mapShape->nbPoints, mapShape->x, mapShape->y, rx, ry ) )
12491230
{
12501231
// cost is set to minimal value, evaluated later
1251-
positions->push_back( new LabelPosition( id++, rx - dlx, ry - dly, xrm, yrm, alpha, 0.0001, this ) ); // Polygon
1232+
positions.append( new LabelPosition( id++, rx - dlx, ry - dly, xrm, yrm, alpha, 0.0001, this ) ); // Polygon
12521233
}
12531234
}
12541235
}
12551236
} // forall box
12561237

1257-
nbp = positions->size();
1238+
nbp = positions.size();
12581239
if ( nbp == 0 )
12591240
{
12601241
dx /= 2;
@@ -1264,12 +1245,12 @@ namespace pal
12641245
}
12651246
while ( nbp == 0 && num_try < max_try );
12661247

1267-
nbp = positions->size();
1248+
nbp = positions.size();
12681249

12691250
( *lPos ) = new LabelPosition*[nbp];
12701251
for ( i = 0; i < nbp; i++ )
12711252
{
1272-
( *lPos )[i] = positions->pop_front();
1253+
( *lPos )[i] = positions.takeFirst();
12731254
}
12741255

12751256
for ( bbid = 0; bbid < j; bbid++ )
@@ -1278,21 +1259,19 @@ namespace pal
12781259
}
12791260

12801261
delete[] boxes;
1281-
delete positions;
12821262
}
12831263
else
12841264
{
12851265
nbp = 0;
12861266
}
12871267

1288-
delete shapes_final;
1289-
12901268
#ifdef _DEBUG_FULL_
12911269
std::cout << "NbLabelPosition: " << nbp << std::endl;
12921270
#endif
12931271
return nbp;
12941272
}
12951273

1274+
#if 0
12961275
void FeaturePart::print()
12971276
{
12981277
int i, j;
@@ -1315,6 +1294,7 @@ namespace pal
13151294

13161295
std::cout << std::endl;
13171296
}
1297+
#endif
13181298

13191299
int FeaturePart::setPosition( double scale, LabelPosition ***lPos,
13201300
double bbox_min[2], double bbox_max[2],

src/core/pal/feature.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <iostream>
3434
#include <fstream>
3535
#include <cmath>
36+
#include <QString>
3637

3738
#include <geos_c.h>
3839

@@ -79,7 +80,7 @@ namespace pal
7980
friend class FeaturePart;
8081

8182
public:
82-
Feature( Layer* l, const char* geom_id, PalGeometry* userG, double lx, double ly );
83+
Feature( Layer* l, const QString& geom_id, PalGeometry* userG, double lx, double ly );
8384
~Feature();
8485

8586
void setLabelInfo( LabelInfo* info ) { labelInfo = info; }
@@ -103,7 +104,7 @@ namespace pal
103104
double distlabel;
104105
LabelInfo* labelInfo; // optional
105106

106-
char *uid;
107+
QString uid;
107108

108109
bool fixedPos; //true in case of fixed position (only 1 candidate position with cost 0)
109110
double fixedPosX;
@@ -231,15 +232,16 @@ namespace pal
231232
* \brief get the unique id of the feature
232233
* \return the feature unique identifier
233234
*/
234-
const char *getUID();
235+
QString getUID() const;
235236

236237

238+
#if 0
237239
/**
238240
* \brief Print feature information
239241
* Print feature unique id, geometry type, points, and holes on screen
240242
*/
241243
void print();
242-
244+
#endif
243245

244246
PalGeometry* getUserGeometry() { return f->userGeom; }
245247

0 commit comments

Comments
 (0)