@@ -1275,6 +1275,7 @@ distance_taxicab(PG_FUNCTION_ARGS)
1275
1275
if (DIM (a ) < DIM (b ))
1276
1276
{
1277
1277
NDBOX * tmp = b ;
1278
+
1278
1279
b = a ;
1279
1280
a = tmp ;
1280
1281
swapped = true;
@@ -1283,11 +1284,13 @@ distance_taxicab(PG_FUNCTION_ARGS)
1283
1284
distance = 0.0 ;
1284
1285
/* compute within the dimensions of (b) */
1285
1286
for (i = 0 ; i < DIM (b ); i ++ )
1286
- distance += fabs (distance_1D (LL_COORD (a ,i ), UR_COORD (a ,i ), LL_COORD (b ,i ), UR_COORD (b ,i )));
1287
+ distance += fabs (distance_1D (LL_COORD (a , i ), UR_COORD (a , i ),
1288
+ LL_COORD (b , i ), UR_COORD (b , i )));
1287
1289
1288
1290
/* compute distance to zero for those dimensions in (a) absent in (b) */
1289
1291
for (i = DIM (b ); i < DIM (a ); i ++ )
1290
- distance += fabs (distance_1D (LL_COORD (a ,i ), UR_COORD (a ,i ), 0.0 , 0.0 ));
1292
+ distance += fabs (distance_1D (LL_COORD (a , i ), UR_COORD (a , i ),
1293
+ 0.0 , 0.0 ));
1291
1294
1292
1295
if (swapped )
1293
1296
{
@@ -1309,13 +1312,15 @@ distance_chebyshev(PG_FUNCTION_ARGS)
1309
1312
NDBOX * a = PG_GETARG_NDBOX (0 ),
1310
1313
* b = PG_GETARG_NDBOX (1 );
1311
1314
bool swapped = false;
1312
- double d , distance ;
1315
+ double d ,
1316
+ distance ;
1313
1317
int i ;
1314
1318
1315
1319
/* swap the box pointers if needed */
1316
1320
if (DIM (a ) < DIM (b ))
1317
1321
{
1318
1322
NDBOX * tmp = b ;
1323
+
1319
1324
b = a ;
1320
1325
a = tmp ;
1321
1326
swapped = true;
@@ -1325,15 +1330,16 @@ distance_chebyshev(PG_FUNCTION_ARGS)
1325
1330
/* compute within the dimensions of (b) */
1326
1331
for (i = 0 ; i < DIM (b ); i ++ )
1327
1332
{
1328
- d = fabs (distance_1D (LL_COORD (a ,i ), UR_COORD (a ,i ), LL_COORD (b ,i ), UR_COORD (b ,i )));
1333
+ d = fabs (distance_1D (LL_COORD (a , i ), UR_COORD (a , i ),
1334
+ LL_COORD (b , i ), UR_COORD (b , i )));
1329
1335
if (d > distance )
1330
1336
distance = d ;
1331
1337
}
1332
1338
1333
1339
/* compute distance to zero for those dimensions in (a) absent in (b) */
1334
1340
for (i = DIM (b ); i < DIM (a ); i ++ )
1335
1341
{
1336
- d = fabs (distance_1D (LL_COORD (a ,i ), UR_COORD (a ,i ), 0.0 , 0.0 ));
1342
+ d = fabs (distance_1D (LL_COORD (a , i ), UR_COORD (a , i ), 0.0 , 0.0 ));
1337
1343
if (d > distance )
1338
1344
distance = d ;
1339
1345
}
@@ -1357,44 +1363,41 @@ g_cube_distance(PG_FUNCTION_ARGS)
1357
1363
{
1358
1364
GISTENTRY * entry = (GISTENTRY * ) PG_GETARG_POINTER (0 );
1359
1365
StrategyNumber strategy = (StrategyNumber ) PG_GETARG_UINT16 (2 );
1360
- NDBOX * cube = DatumGetNDBOX (entry -> key );
1361
- double retval ;
1366
+ NDBOX * cube = DatumGetNDBOX (entry -> key );
1367
+ double retval ;
1362
1368
1363
1369
if (strategy == CubeKNNDistanceCoord )
1364
1370
{
1365
- int coord = PG_GETARG_INT32 (1 );
1371
+ int coord = PG_GETARG_INT32 (1 );
1366
1372
1367
- if IS_POINT (cube )
1368
- {
1369
- retval = (cube )-> x [(coord - 1 )%DIM (cube )];
1370
- }
1373
+ if (IS_POINT (cube ))
1374
+ retval = cube -> x [(coord - 1 ) % DIM (cube )];
1371
1375
else
1372
- {
1373
- retval = Min (
1374
- (cube )-> x [(coord - 1 )%DIM (cube )],
1375
- (cube )-> x [(coord - 1 )%DIM (cube ) + DIM (cube )]
1376
- );
1377
- }
1376
+ retval = Min (cube -> x [(coord - 1 ) % DIM (cube )],
1377
+ cube -> x [(coord - 1 ) % DIM (cube ) + DIM (cube )]);
1378
1378
}
1379
1379
else
1380
1380
{
1381
- NDBOX * query = PG_GETARG_NDBOX (1 );
1382
- switch (strategy )
1381
+ NDBOX * query = PG_GETARG_NDBOX (1 );
1382
+
1383
+ switch (strategy )
1383
1384
{
1384
- case CubeKNNDistanceTaxicab :
1385
- retval = DatumGetFloat8 (DirectFunctionCall2 (distance_taxicab ,
1386
- PointerGetDatum (cube ), PointerGetDatum (query )));
1387
- break ;
1388
- case CubeKNNDistanceEuclid :
1389
- retval = DatumGetFloat8 (DirectFunctionCall2 (cube_distance ,
1390
- PointerGetDatum (cube ), PointerGetDatum (query )));
1391
- break ;
1392
- case CubeKNNDistanceChebyshev :
1393
- retval = DatumGetFloat8 (DirectFunctionCall2 (distance_chebyshev ,
1394
- PointerGetDatum (cube ), PointerGetDatum (query )));
1395
- break ;
1396
- default :
1397
- elog (ERROR , "Cube: unknown strategy number." );
1385
+ case CubeKNNDistanceTaxicab :
1386
+ retval = DatumGetFloat8 (DirectFunctionCall2 (distance_taxicab ,
1387
+ PointerGetDatum (cube ), PointerGetDatum (query )));
1388
+ break ;
1389
+ case CubeKNNDistanceEuclid :
1390
+ retval = DatumGetFloat8 (DirectFunctionCall2 (cube_distance ,
1391
+ PointerGetDatum (cube ), PointerGetDatum (query )));
1392
+ break ;
1393
+ case CubeKNNDistanceChebyshev :
1394
+ retval = DatumGetFloat8 (DirectFunctionCall2 (distance_chebyshev ,
1395
+ PointerGetDatum (cube ), PointerGetDatum (query )));
1396
+ break ;
1397
+ default :
1398
+ elog (ERROR , "unrecognized cube strategy number: %d" , strategy );
1399
+ retval = 0 ; /* keep compiler quiet */
1400
+ break ;
1398
1401
}
1399
1402
}
1400
1403
PG_RETURN_FLOAT8 (retval );
@@ -1466,7 +1469,7 @@ Datum
1466
1469
cube_ll_coord (PG_FUNCTION_ARGS )
1467
1470
{
1468
1471
NDBOX * c = PG_GETARG_NDBOX (0 );
1469
- int n = PG_GETARG_INT16 (1 );
1472
+ int n = PG_GETARG_INT32 (1 );
1470
1473
double result ;
1471
1474
1472
1475
if (DIM (c ) >= n && n > 0 )
@@ -1483,7 +1486,7 @@ Datum
1483
1486
cube_ur_coord (PG_FUNCTION_ARGS )
1484
1487
{
1485
1488
NDBOX * c = PG_GETARG_NDBOX (0 );
1486
- int n = PG_GETARG_INT16 (1 );
1489
+ int n = PG_GETARG_INT32 (1 );
1487
1490
double result ;
1488
1491
1489
1492
if (DIM (c ) >= n && n > 0 )
@@ -1504,21 +1507,17 @@ Datum
1504
1507
cube_coord (PG_FUNCTION_ARGS )
1505
1508
{
1506
1509
NDBOX * cube = PG_GETARG_NDBOX (0 );
1507
- int coord = PG_GETARG_INT16 (1 );
1510
+ int coord = PG_GETARG_INT32 (1 );
1508
1511
1509
- if ((coord > 0 ) && (coord <= 2 * DIM (cube )))
1510
- {
1511
- if IS_POINT (cube )
1512
- PG_RETURN_FLOAT8 ( (cube )-> x [(coord - 1 )%DIM (cube )] );
1513
- else
1514
- PG_RETURN_FLOAT8 ( (cube )-> x [coord - 1 ] );
1515
- }
1516
- else
1517
- {
1512
+ if (coord <= 0 || coord > 2 * DIM (cube ))
1518
1513
ereport (ERROR ,
1519
- (errcode (ERRCODE_ARRAY_ELEMENT_ERROR ),
1520
- errmsg ("Cube index out of bounds" )));
1521
- }
1514
+ (errcode (ERRCODE_ARRAY_ELEMENT_ERROR ),
1515
+ errmsg ("cube index %d is out of bounds" , coord )));
1516
+
1517
+ if (IS_POINT (cube ))
1518
+ PG_RETURN_FLOAT8 (cube -> x [(coord - 1 ) % DIM (cube )]);
1519
+ else
1520
+ PG_RETURN_FLOAT8 (cube -> x [coord - 1 ]);
1522
1521
}
1523
1522
1524
1523
@@ -1536,27 +1535,28 @@ Datum
1536
1535
cube_coord_llur (PG_FUNCTION_ARGS )
1537
1536
{
1538
1537
NDBOX * cube = PG_GETARG_NDBOX (0 );
1539
- int coord = PG_GETARG_INT16 (1 );
1538
+ int coord = PG_GETARG_INT32 (1 );
1540
1539
1541
- if ((coord > 0 ) && (coord <= DIM (cube )))
1542
- {
1543
- if IS_POINT (cube )
1544
- PG_RETURN_FLOAT8 ( (cube )-> x [coord - 1 ] );
1545
- else
1546
- PG_RETURN_FLOAT8 ( Min ((cube )-> x [coord - 1 ], (cube )-> x [coord - 1 + DIM (cube )]) );
1547
- }
1548
- else if ((coord > DIM (cube )) && (coord <= 2 * DIM (cube )))
1540
+ if (coord <= 0 || coord > 2 * DIM (cube ))
1541
+ ereport (ERROR ,
1542
+ (errcode (ERRCODE_ARRAY_ELEMENT_ERROR ),
1543
+ errmsg ("cube index %d is out of bounds" , coord )));
1544
+
1545
+ if (coord <= DIM (cube ))
1549
1546
{
1550
- if IS_POINT (cube )
1551
- PG_RETURN_FLOAT8 ( ( cube ) -> x [( coord - 1 )% DIM ( cube )] );
1547
+ if ( IS_POINT (cube ) )
1548
+ PG_RETURN_FLOAT8 (cube -> x [coord - 1 ] );
1552
1549
else
1553
- PG_RETURN_FLOAT8 ( Max ((cube )-> x [coord - 1 ], (cube )-> x [coord - 1 - DIM (cube )]) );
1550
+ PG_RETURN_FLOAT8 (Min (cube -> x [coord - 1 ],
1551
+ cube -> x [coord - 1 + DIM (cube )]));
1554
1552
}
1555
1553
else
1556
1554
{
1557
- ereport (ERROR ,
1558
- (errcode (ERRCODE_ARRAY_ELEMENT_ERROR ),
1559
- errmsg ("Cube index out of bounds" )));
1555
+ if (IS_POINT (cube ))
1556
+ PG_RETURN_FLOAT8 (cube -> x [(coord - 1 ) % DIM (cube )]);
1557
+ else
1558
+ PG_RETURN_FLOAT8 (Max (cube -> x [coord - 1 ],
1559
+ cube -> x [coord - 1 - DIM (cube )]));
1560
1560
}
1561
1561
}
1562
1562
0 commit comments