mirror of
https://github.com/sqlite/sqlite.git
synced 2025-10-21 11:13:54 +03:00
Try to reduce the number of malloc() for user-defined functions. Begin
transfering built-in functions over to the user-define function mechanism. (CVS 399) FossilOrigin-Name: c4f9e017b449d4036fa8d2bf77b931d4c31d74f7
This commit is contained in:
52
src/func.c
52
src/func.c
@@ -16,7 +16,7 @@
|
||||
** sqliteRegisterBuildinFunctions() found at the bottom of the file.
|
||||
** All other code has file scope.
|
||||
**
|
||||
** $Id: func.c,v 1.4 2002/02/27 19:00:22 drh Exp $
|
||||
** $Id: func.c,v 1.5 2002/02/27 19:50:59 drh Exp $
|
||||
*/
|
||||
#include <ctype.h>
|
||||
#include <math.h>
|
||||
@@ -47,6 +47,44 @@ static void lowerFunc(sqlite_func *context, int argc, const char **argv){
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** An instance of the following structure holds the context of a
|
||||
** sum() or avg() aggregate computation.
|
||||
*/
|
||||
typedef struct SumCtx SumCtx;
|
||||
struct SumCtx {
|
||||
double sum; /* Sum of terms */
|
||||
};
|
||||
|
||||
/*
|
||||
** Routines used to compute the sum or average.
|
||||
*/
|
||||
static void sumStep(sqlite_func *context, int argc, const char **argv){
|
||||
SumCtx *p;
|
||||
double x;
|
||||
if( argc<1 ) return;
|
||||
p = sqlite_aggregate_context(context, sizeof(*p));
|
||||
if( p==0 ) return;
|
||||
x = argv[0] ? atof(argv[0]) : 0.0;
|
||||
p->sum += x;
|
||||
}
|
||||
static void sumFinalize(sqlite_func *context){
|
||||
SumCtx *p;
|
||||
p = sqlite_aggregate_context(context, sizeof(*p));
|
||||
if( p ){
|
||||
sqlite_set_result_double(context, p->sum);
|
||||
}
|
||||
}
|
||||
static void avgFinalize(sqlite_func *context){
|
||||
SumCtx *p;
|
||||
double rN;
|
||||
p = sqlite_aggregate_context(context, sizeof(*p));
|
||||
rN = sqlite_aggregate_count(context);
|
||||
if( p && rN>0.0 ){
|
||||
sqlite_set_result_double(context, p->sum/rN);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** An instance of the following structure holds the context of a
|
||||
** variance or standard deviation computation.
|
||||
@@ -55,7 +93,6 @@ typedef struct StdDevCtx StdDevCtx;
|
||||
struct StdDevCtx {
|
||||
double sum; /* Sum of terms */
|
||||
double sum2; /* Sum of the squares of terms */
|
||||
int n; /* Number of terms seen so far */
|
||||
};
|
||||
|
||||
/*
|
||||
@@ -67,20 +104,21 @@ static void stdDevStep(sqlite_func *context, int argc, const char **argv){
|
||||
if( argc<1 ) return;
|
||||
p = sqlite_aggregate_context(context, sizeof(*p));
|
||||
if( p==0 ) return;
|
||||
x = atof(argv[0]);
|
||||
x = argv[0] ? atof(argv[0]) : 0.0;
|
||||
p->sum += x;
|
||||
p->sum2 += x*x;
|
||||
p->n++;
|
||||
}
|
||||
static void stdDevFinalize(sqlite_func *context){
|
||||
double rN = sqlite_aggregate_count(context);
|
||||
StdDevCtx *p = sqlite_aggregate_context(context, sizeof(*p));
|
||||
if( p && p->n>1 ){
|
||||
double rN = p->n;
|
||||
if( p && rN>1.0 ){
|
||||
sqlite_set_result_double(context,
|
||||
sqrt((p->sum2 - p->sum*p->sum/rN)/(rN-1.0)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** This function registered all of the above C functions as SQL
|
||||
** functions. This should be the only routine in this file with
|
||||
@@ -90,4 +128,6 @@ void sqliteRegisterBuildinFunctions(sqlite *db){
|
||||
sqlite_create_function(db, "upper", 1, upperFunc, 0);
|
||||
sqlite_create_function(db, "lower", 1, lowerFunc, 0);
|
||||
sqlite_create_aggregate(db, "stddev", 1, stdDevStep, stdDevFinalize, 0);
|
||||
sqlite_create_aggregate(db, "x_sum", 1, sumStep, sumFinalize, 0);
|
||||
sqlite_create_aggregate(db, "x_avg", 1, sumStep, avgFinalize, 0);
|
||||
}
|
||||
|
Reference in New Issue
Block a user