21
21
#include "utils/array.h"
22
22
#include "utils/fmgrprotos.h"
23
23
24
- static const float weights [] = {0.1f , 0.2f , 0.4f , 1.0f };
24
+ #define NUM_WEIGHTS 4
25
+ static const float default_weights [NUM_WEIGHTS ] = {0.1f , 0.2f , 0.4f , 1.0f };
25
26
26
27
#define wpos (wep ) ( w[ WEP_GETWEIGHT(wep) ] )
27
28
@@ -396,22 +397,24 @@ calc_rank(const float *w, TSVector t, TSQuery q, int32 method)
396
397
return res ;
397
398
}
398
399
399
- static const float *
400
- getWeights (ArrayType * win )
400
+ /*
401
+ * Extract weights from an array. The weights are stored in *ws, which must
402
+ * have space for NUM_WEIGHTS elements.
403
+ */
404
+ static void
405
+ getWeights (ArrayType * win , float * ws )
401
406
{
402
- static float ws [lengthof (weights )];
403
407
int i ;
404
408
float4 * arrdata ;
405
409
406
- if (win == NULL )
407
- return weights ;
410
+ Assert (win != NULL );
408
411
409
412
if (ARR_NDIM (win ) != 1 )
410
413
ereport (ERROR ,
411
414
(errcode (ERRCODE_ARRAY_SUBSCRIPT_ERROR ),
412
415
errmsg ("array of weight must be one-dimensional" )));
413
416
414
- if (ArrayGetNItems (ARR_NDIM (win ), ARR_DIMS (win )) < lengthof ( weights ) )
417
+ if (ArrayGetNItems (ARR_NDIM (win ), ARR_DIMS (win )) < NUM_WEIGHTS )
415
418
ereport (ERROR ,
416
419
(errcode (ERRCODE_ARRAY_SUBSCRIPT_ERROR ),
417
420
errmsg ("array of weight is too short" )));
@@ -422,16 +425,14 @@ getWeights(ArrayType *win)
422
425
errmsg ("array of weight must not contain nulls" )));
423
426
424
427
arrdata = (float4 * ) ARR_DATA_PTR (win );
425
- for (i = 0 ; i < lengthof ( weights ) ; i ++ )
428
+ for (i = 0 ; i < NUM_WEIGHTS ; i ++ )
426
429
{
427
- ws [i ] = (arrdata [i ] >= 0 ) ? arrdata [i ] : weights [i ];
430
+ ws [i ] = (arrdata [i ] >= 0 ) ? arrdata [i ] : default_weights [i ];
428
431
if (ws [i ] > 1.0 )
429
432
ereport (ERROR ,
430
433
(errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
431
434
errmsg ("weight out of range" )));
432
435
}
433
-
434
- return ws ;
435
436
}
436
437
437
438
Datum
@@ -441,9 +442,11 @@ ts_rank_wttf(PG_FUNCTION_ARGS)
441
442
TSVector txt = PG_GETARG_TSVECTOR (1 );
442
443
TSQuery query = PG_GETARG_TSQUERY (2 );
443
444
int method = PG_GETARG_INT32 (3 );
445
+ float weights [NUM_WEIGHTS ];
444
446
float res ;
445
447
446
- res = calc_rank (getWeights (win ), txt , query , method );
448
+ getWeights (win , weights );
449
+ res = calc_rank (weights , txt , query , method );
447
450
448
451
PG_FREE_IF_COPY (win , 0 );
449
452
PG_FREE_IF_COPY (txt , 1 );
@@ -457,9 +460,11 @@ ts_rank_wtt(PG_FUNCTION_ARGS)
457
460
ArrayType * win = (ArrayType * ) PG_DETOAST_DATUM (PG_GETARG_DATUM (0 ));
458
461
TSVector txt = PG_GETARG_TSVECTOR (1 );
459
462
TSQuery query = PG_GETARG_TSQUERY (2 );
463
+ float weights [NUM_WEIGHTS ];
460
464
float res ;
461
465
462
- res = calc_rank (getWeights (win ), txt , query , DEF_NORM_METHOD );
466
+ getWeights (win , weights );
467
+ res = calc_rank (weights , txt , query , DEF_NORM_METHOD );
463
468
464
469
PG_FREE_IF_COPY (win , 0 );
465
470
PG_FREE_IF_COPY (txt , 1 );
@@ -475,7 +480,7 @@ ts_rank_ttf(PG_FUNCTION_ARGS)
475
480
int method = PG_GETARG_INT32 (2 );
476
481
float res ;
477
482
478
- res = calc_rank (getWeights ( NULL ) , txt , query , method );
483
+ res = calc_rank (default_weights , txt , query , method );
479
484
480
485
PG_FREE_IF_COPY (txt , 0 );
481
486
PG_FREE_IF_COPY (query , 1 );
@@ -489,7 +494,7 @@ ts_rank_tt(PG_FUNCTION_ARGS)
489
494
TSQuery query = PG_GETARG_TSQUERY (1 );
490
495
float res ;
491
496
492
- res = calc_rank (getWeights ( NULL ) , txt , query , DEF_NORM_METHOD );
497
+ res = calc_rank (default_weights , txt , query , DEF_NORM_METHOD );
493
498
494
499
PG_FREE_IF_COPY (txt , 0 );
495
500
PG_FREE_IF_COPY (query , 1 );
@@ -855,16 +860,16 @@ calc_rank_cd(const float4 *arrdata, TSVector txt, TSQuery query, int method)
855
860
doclen = 0 ;
856
861
CoverExt ext ;
857
862
double Wdoc = 0.0 ;
858
- double invws [lengthof ( weights ) ];
863
+ double invws [NUM_WEIGHTS ];
859
864
double SumDist = 0.0 ,
860
865
PrevExtPos = 0.0 ;
861
866
int NExtent = 0 ;
862
867
QueryRepresentation qr ;
863
868
864
869
865
- for (i = 0 ; i < lengthof ( weights ) ; i ++ )
870
+ for (i = 0 ; i < NUM_WEIGHTS ; i ++ )
866
871
{
867
- invws [i ] = ((double ) ((arrdata [i ] >= 0 ) ? arrdata [i ] : weights [i ]));
872
+ invws [i ] = ((double ) ((arrdata [i ] >= 0 ) ? arrdata [i ] : default_weights [i ]));
868
873
if (invws [i ] > 1.0 )
869
874
ereport (ERROR ,
870
875
(errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
@@ -956,9 +961,11 @@ ts_rankcd_wttf(PG_FUNCTION_ARGS)
956
961
TSVector txt = PG_GETARG_TSVECTOR (1 );
957
962
TSQuery query = PG_GETARG_TSQUERY (2 );
958
963
int method = PG_GETARG_INT32 (3 );
964
+ float weights [NUM_WEIGHTS ];
959
965
float res ;
960
966
961
- res = calc_rank_cd (getWeights (win ), txt , query , method );
967
+ getWeights (win , weights );
968
+ res = calc_rank_cd (weights , txt , query , method );
962
969
963
970
PG_FREE_IF_COPY (win , 0 );
964
971
PG_FREE_IF_COPY (txt , 1 );
@@ -972,9 +979,11 @@ ts_rankcd_wtt(PG_FUNCTION_ARGS)
972
979
ArrayType * win = (ArrayType * ) PG_DETOAST_DATUM (PG_GETARG_DATUM (0 ));
973
980
TSVector txt = PG_GETARG_TSVECTOR (1 );
974
981
TSQuery query = PG_GETARG_TSQUERY (2 );
982
+ float weights [NUM_WEIGHTS ];
975
983
float res ;
976
984
977
- res = calc_rank_cd (getWeights (win ), txt , query , DEF_NORM_METHOD );
985
+ getWeights (win , weights );
986
+ res = calc_rank_cd (weights , txt , query , DEF_NORM_METHOD );
978
987
979
988
PG_FREE_IF_COPY (win , 0 );
980
989
PG_FREE_IF_COPY (txt , 1 );
@@ -990,7 +999,7 @@ ts_rankcd_ttf(PG_FUNCTION_ARGS)
990
999
int method = PG_GETARG_INT32 (2 );
991
1000
float res ;
992
1001
993
- res = calc_rank_cd (getWeights ( NULL ) , txt , query , method );
1002
+ res = calc_rank_cd (default_weights , txt , query , method );
994
1003
995
1004
PG_FREE_IF_COPY (txt , 0 );
996
1005
PG_FREE_IF_COPY (query , 1 );
@@ -1004,7 +1013,7 @@ ts_rankcd_tt(PG_FUNCTION_ARGS)
1004
1013
TSQuery query = PG_GETARG_TSQUERY (1 );
1005
1014
float res ;
1006
1015
1007
- res = calc_rank_cd (getWeights ( NULL ) , txt , query , DEF_NORM_METHOD );
1016
+ res = calc_rank_cd (default_weights , txt , query , DEF_NORM_METHOD );
1008
1017
1009
1018
PG_FREE_IF_COPY (txt , 0 );
1010
1019
PG_FREE_IF_COPY (query , 1 );
0 commit comments