2 This library will provide common mathematical and string functions in
3 SQL queries using the operating system libraries or provided
4 definitions. It includes the following functions:
6 Math: acos, asin, atan, atn2, atan2, acosh, asinh, atanh, difference,
7 degrees, radians, cos, sin, tan, cot, cosh, sinh, tanh, coth, exp,
8 log, log10, power, sign, sqrt, square, ceil, floor, pi.
10 String: replicate, charindex, leftstr, rightstr, ltrim, rtrim, trim,
11 replace, reverse, proper, padl, padr, padc, strfilter.
13 Aggregate: stdev, variance, mode, median, lower_quartile,
16 The string functions ltrim, rtrim, trim, replace are included in
17 recent versions of SQLite and so by default do not build.
19 Compilation instructions:
20 Compile this C source file into a dynamic library as follows:
22 gcc -fPIC -lm -shared extension-functions.c -o libsqlitefunctions.so
24 gcc -fno-common -dynamiclib extension-functions.c -o libsqlitefunctions.dylib
25 (You may need to add flags
26 -I /opt/local/include/ -L/opt/local/lib -lsqlite3
27 if your sqlite3 is installed from Mac ports, or
28 -I /sw/include/ -L/sw/lib -lsqlite3
29 if installed with Fink.)
31 Usage instructions for applications calling the sqlite3 API functions:
32 In your application, call sqlite3_enable_load_extension(db,1) to
33 allow loading external libraries. Then load the library libsqlitefunctions
34 using sqlite3_load_extension; the third argument should be 0.
35 See http://www.sqlite.org/cvstrac/wiki?p=LoadableExtensions.
36 Select statements may now use these functions, as in
37 SELECT cos(radians(inclination)) FROM satsum WHERE satnum = 25544;
39 Usage instructions for the sqlite3 program:
40 If the program is built so that loading extensions is permitted,
41 the following will work:
42 sqlite> SELECT load_extension('./libsqlitefunctions.so');
43 sqlite> select cos(radians(45));
45 Note: Loading extensions is by default prohibited as a
46 security measure; see "Security Considerations" in
47 http://www.sqlite.org/cvstrac/wiki?p=LoadableExtensions.
48 If the sqlite3 program and library are built this
49 way, you cannot use these functions from the program, you
50 must write your own program using the sqlite3 API, and call
51 sqlite3_enable_load_extension as described above, or else
52 rebuilt the sqlite3 program to allow loadable extensions.
55 The instructions are for Linux or Mac OS X; users of other OSes may
56 need to modify this procedure. In particular, if your math library
57 lacks one or more of the needed trig or log functions, comment out the
58 appropriate HAVE_ #define at the top of file. If you do not
59 wish to make a loadable module, comment out the define for
60 COMPILE_SQLITE_EXTENSIONS_AS_LOADABLE_MODULE. If you are using a
61 version of SQLite without the trim functions and replace, comment out
62 the HAVE_TRIM #define.
67 2009-06-24 Correct check for argc in properFunc
68 2008-09-14 Add check that memory was actually allocated after
69 sqlite3_malloc or sqlite3StrDup, call sqlite3_result_error_nomem if
70 not. Thanks to Robert Simpson.
71 2008-06-13 Change to instructions to indicate use of the math library
72 and that program might work.
73 2007-10-01 Minor clarification to instructions.
74 2007-09-29 Compilation as loadable module is optional with
75 COMPILE_SQLITE_EXTENSIONS_AS_LOADABLE_MODULE.
76 2007-09-28 Use sqlite3_extension_init and macros
77 SQLITE_EXTENSION_INIT1, SQLITE_EXTENSION_INIT2, so that it works with
78 sqlite3_load_extension. Thanks to Eric Higashino and Joe Wilson.
79 New instructions for Mac compilation.
80 2007-09-17 With help from Joe Wilson and Nuno Luca, made use of
81 external interfaces so that compilation is no longer dependent on
82 SQLite source code. Merged source, header, and README into a single
83 file. Added casts so that Mac will compile without warnings (unsigned
85 2007-09-05 Included some definitions from sqlite 3.3.13 so that this
86 will continue to work in newer versions of sqlite. Completed
87 description of functions available.
88 2007-03-27 Revised description.
89 2007-03-23 Small cleanup and a bug fix on the code. This was mainly
90 letting errno flag errors encountered in the math library and checking
91 the result, rather than pre-checking. This fixes a bug in power that
92 would cause an error if any non-positive number was raised to any
94 2007-02-07 posted by Mikey C to sqlite mailing list.
95 Original code 2006 June 05 by relicoder.
101 //#define COMPILE_SQLITE_EXTENSIONS_AS_LOADABLE_MODULE 1
109 #define HAVE_ISBLANK 1
110 #define SQLITE_SOUNDEX 1
111 #define HAVE_TRIM 1 /* LMH 2007-03-25 if sqlite has trim functions */
113 #ifdef COMPILE_SQLITE_EXTENSIONS_AS_LOADABLE_MODULE
114 #include "sqlite3ext.h"
115 SQLITE_EXTENSION_INIT1
125 #include <errno.h> /* LMH 2007-03-25 */
133 //#include <stdint.h>
136 ** Simple binary tree implementation to use in median, mode and quartile calculations
137 ** Tree is not necessarily balanced. That would require something like red&black trees of AVL
140 typedef int(*cmp_func)(const void *, const void *);
141 typedef void(*map_iterator)(void*, sqlite_int64, void*);
157 ** creates a map given a comparison function
159 map map_make(cmp_func cmp);
162 ** inserts the element e into map m
164 void map_insert(map *m, void *e);
167 ** executes function iter over all elements in the map, in key increasing order
169 void map_iterate(map *m, map_iterator iter, void* p);
172 ** frees all memory used by a map
174 void map_destroy(map *m);
177 ** compares 2 integers
178 ** to use with map_make
180 int int_cmp(const void *a, const void *b);
183 ** compares 2 doubles
184 ** to use with map_make
186 int double_cmp(const void *a, const void *b);
191 static char *sqlite3StrDup( const char *z ) {
192 char *res = sqlite3_malloc( strlen(z)+1 );
193 return strcpy( res, z );
197 ** These are copied verbatim from fun.c so as to not have the names exported
200 /* LMH from sqlite3 3.3.13 */
202 ** This table maps from the first byte of a UTF-8 character to the number
203 ** of trailing bytes expected. A value '4' indicates that the table key
204 ** is not a legal first byte for a UTF-8 character.
206 static const u8 xtra_utf8_bytes[256] = {
208 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
209 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
210 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
211 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
212 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
213 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
214 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
215 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
218 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
219 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
220 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
221 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
224 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
225 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
228 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
231 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
236 ** This table maps from the number of trailing bytes in a UTF-8 character
237 ** to an integer constant that is effectively calculated for each character
238 ** read by a naive implementation of a UTF-8 character reader. The code
239 ** in the READ_UTF8 macro explains things best.
241 static const int xtra_utf8_bits[] = {
243 12416, /* (0xC0 << 6) + (0x80) */
244 925824, /* (0xE0 << 12) + (0x80 << 6) + (0x80) */
245 63447168 /* (0xF0 << 18) + (0x80 << 12) + (0x80 << 6) + 0x80 */
249 ** If a UTF-8 character contains N bytes extra bytes (N bytes follow
250 ** the initial byte so that the total character length is N+1) then
251 ** masking the character with utf8_mask[N] must produce a non-zero
252 ** result. Otherwise, we have an (illegal) overlong encoding.
254 static const int utf_mask[] = {
261 #define EXT_READ_UTF8(zIn, c) { \
264 xtra = xtra_utf8_bytes[c]; \
266 case 4: c = (int)0xFFFD; break; \
267 case 3: c = (c<<6) + *(zIn)++; \
268 case 2: c = (c<<6) + *(zIn)++; \
269 case 1: c = (c<<6) + *(zIn)++; \
270 c -= xtra_utf8_bits[xtra]; \
271 if( (utf_mask[xtra]&c)==0 \
272 || (c&0xFFFFF800)==0xD800 \
273 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
277 static int sqlite3ReadUtf8(const unsigned char *z){
283 #define SKIP_UTF8(zIn) { \
284 zIn += (xtra_utf8_bytes[*(u8 *)zIn] + 1); \
290 ** X is a pointer to the first byte of a UTF-8 character. Increment
291 ** X so that it points to the next character. This only works right
292 ** if X points to a well-formed UTF-8 string.
294 #define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){}
295 #define sqliteCharVal(X) sqlite3ReadUtf8(X)
298 ** This is a macro that facilitates writting wrappers for math.h functions
299 ** it creates code for a function to use in SQlite that gets one numeric input
300 ** and returns a floating point value.
302 ** Could have been implemented using pointers to functions but this way it's inline
303 ** and thus more efficient. Lower * ranking though...
306 ** name: function name to de defined (eg: sinFunc)
307 ** function: function defined in math.h to wrap (eg: sin)
308 ** domain: boolean condition that CAN'T happen in terms of the input parameter rVal
309 ** (eg: rval<0 for sqrt)
311 /* LMH 2007-03-25 Changed to use errno and remove domain; no pre-checking for errors. */
312 #define GEN_MATH_WRAP_DOUBLE_1(name, function) \
313 static void name(sqlite3_context *context, int argc, sqlite3_value **argv){\
314 double rVal = 0.0, val;\
316 switch( sqlite3_value_type(argv[0]) ){\
318 sqlite3_result_null(context);\
322 rVal = sqlite3_value_double(argv[0]);\
324 val = function(rVal);\
326 sqlite3_result_double(context, val);\
328 sqlite3_result_error(context, strerror(errno), errno);\
337 ** Example of GEN_MATH_WRAP_DOUBLE_1 usage
338 ** this creates function sqrtFunc to wrap the math.h standard function sqrt(x)=x^0.5
340 GEN_MATH_WRAP_DOUBLE_1(sqrtFunc, sqrt)
342 /* trignometric functions */
343 GEN_MATH_WRAP_DOUBLE_1(acosFunc, acos)
344 GEN_MATH_WRAP_DOUBLE_1(asinFunc, asin)
345 GEN_MATH_WRAP_DOUBLE_1(atanFunc, atan)
348 ** Many of systems don't have inverse hyperbolic trig functions so this will emulate
349 ** them on those systems in terms of log and sqrt (formulas are too trivial to demand
350 ** written proof here)
354 static double acosh(double x){
355 return log(x + sqrt(x*x - 1.0));
359 GEN_MATH_WRAP_DOUBLE_1(acoshFunc, acosh)
362 static double asinh(double x){
363 return log(x + sqrt(x*x + 1.0));
367 GEN_MATH_WRAP_DOUBLE_1(asinhFunc, asinh)
370 static double atanh(double x){
371 return (1.0/2.0)*log((1+x)/(1-x)) ;
375 GEN_MATH_WRAP_DOUBLE_1(atanhFunc, atanh)
378 ** math.h doesn't require cot (cotangent) so it's defined here
380 static double cot(double x){
384 GEN_MATH_WRAP_DOUBLE_1(sinFunc, sin)
385 GEN_MATH_WRAP_DOUBLE_1(cosFunc, cos)
386 GEN_MATH_WRAP_DOUBLE_1(tanFunc, tan)
387 GEN_MATH_WRAP_DOUBLE_1(cotFunc, cot)
389 static double coth(double x){
394 ** Many systems don't have hyperbolic trigonometric functions so this will emulate
395 ** them on those systems directly from the definition in terms of exp
398 static double sinh(double x){
399 return (exp(x)-exp(-x))/2.0;
403 GEN_MATH_WRAP_DOUBLE_1(sinhFunc, sinh)
406 static double cosh(double x){
407 return (exp(x)+exp(-x))/2.0;
411 GEN_MATH_WRAP_DOUBLE_1(coshFunc, cosh)
414 static double tanh(double x){
415 return sinh(x)/cosh(x);
419 GEN_MATH_WRAP_DOUBLE_1(tanhFunc, tanh)
421 GEN_MATH_WRAP_DOUBLE_1(cothFunc, coth)
424 ** Some systems lack log in base 10. This will emulate it
428 static double log10(double x){
429 static double l10 = -1.0;
437 GEN_MATH_WRAP_DOUBLE_1(logFunc, log)
438 GEN_MATH_WRAP_DOUBLE_1(log10Func, log10)
439 GEN_MATH_WRAP_DOUBLE_1(expFunc, exp)
442 ** Fallback for systems where math.h doesn't define M_PI
447 ** static double PI = acos(-1.0);
450 #define M_PI 3.14159265358979323846
453 /* Convert Degrees into Radians */
454 static double deg2rad(double x){
458 /* Convert Radians into Degrees */
459 static double rad2deg(double x){
463 GEN_MATH_WRAP_DOUBLE_1(rad2degFunc, rad2deg)
464 GEN_MATH_WRAP_DOUBLE_1(deg2radFunc, deg2rad)
466 /* constant function that returns the value of PI=3.1415... */
467 static void piFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
468 sqlite3_result_double(context, M_PI);
472 ** Implements the sqrt function, it has the peculiarity of returning an integer when the
473 ** the argument is an integer.
474 ** Since SQLite isn't strongly typed (almost untyped actually) this is a bit pedantic
476 static void squareFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
480 switch( sqlite3_value_type(argv[0]) ){
481 case SQLITE_INTEGER: {
482 iVal = sqlite3_value_int64(argv[0]);
483 sqlite3_result_int64(context, iVal*iVal);
487 sqlite3_result_null(context);
491 rVal = sqlite3_value_double(argv[0]);
492 sqlite3_result_double(context, rVal*rVal);
499 ** Wraps the pow math.h function
500 ** When both the base and the exponent are integers the result should be integer
501 ** (see sqrt just before this). Here the result is always double
503 /* LMH 2007-03-25 Changed to use errno; no pre-checking for errors. Also removes
504 but that was present in the pre-checking that called sqlite3_result_error on
505 a non-positive first argument, which is not always an error. */
506 static void powerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
513 if( sqlite3_value_type(argv[0]) == SQLITE_NULL || sqlite3_value_type(argv[1]) == SQLITE_NULL ){
514 sqlite3_result_null(context);
516 r1 = sqlite3_value_double(argv[0]);
517 r2 = sqlite3_value_double(argv[1]);
521 sqlite3_result_double(context, val);
523 sqlite3_result_error(context, strerror(errno), errno);
531 static void atn2Func(sqlite3_context *context, int argc, sqlite3_value **argv){
537 if( sqlite3_value_type(argv[0]) == SQLITE_NULL || sqlite3_value_type(argv[1]) == SQLITE_NULL ){
538 sqlite3_result_null(context);
540 r1 = sqlite3_value_double(argv[0]);
541 r2 = sqlite3_value_double(argv[1]);
542 sqlite3_result_double(context, atan2(r1,r2));
547 ** Implementation of the sign() function
548 ** return one of 3 possibilities +1,0 or -1 when the argument is respectively
549 ** positive, 0 or negative.
550 ** When the argument is NULL the result is also NULL (completly conventional)
552 static void signFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
556 switch( sqlite3_value_type(argv[0]) ){
557 case SQLITE_INTEGER: {
558 iVal = sqlite3_value_int64(argv[0]);
559 iVal = ( iVal > 0) ? 1: ( iVal < 0 ) ? -1: 0;
560 sqlite3_result_int64(context, iVal);
564 sqlite3_result_null(context);
568 /* 2nd change below. Line for abs was: if( rVal<0 ) rVal = rVal * -1.0; */
570 rVal = sqlite3_value_double(argv[0]);
571 rVal = ( rVal > 0) ? 1: ( rVal < 0 ) ? -1: 0;
572 sqlite3_result_double(context, rVal);
580 ** smallest integer value not less than argument
582 static void ceilFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
586 switch( sqlite3_value_type(argv[0]) ){
587 case SQLITE_INTEGER: {
588 i64 iVal = sqlite3_value_int64(argv[0]);
589 sqlite3_result_int64(context, iVal);
593 sqlite3_result_null(context);
597 rVal = sqlite3_value_double(argv[0]);
598 sqlite3_result_int64(context, (i64) ceil(rVal));
605 ** largest integer value not greater than argument
607 static void floorFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
611 switch( sqlite3_value_type(argv[0]) ){
612 case SQLITE_INTEGER: {
613 i64 iVal = sqlite3_value_int64(argv[0]);
614 sqlite3_result_int64(context, iVal);
618 sqlite3_result_null(context);
622 rVal = sqlite3_value_double(argv[0]);
623 sqlite3_result_int64(context, (i64) floor(rVal));
630 ** Given a string (s) in the first argument and an integer (n) in the second returns the
631 ** string that constains s contatenated n times
633 static void replicateFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
634 unsigned char *z; /* input string */
635 unsigned char *zo; /* result string */
636 i64 iCount; /* times to repeat */
637 i64 nLen; /* length of the input string (no multibyte considerations) */
638 i64 nTLen; /* length of the result string (no multibyte considerations) */
641 if( argc!=2 || SQLITE_NULL==sqlite3_value_type(argv[0]) )
644 iCount = sqlite3_value_int64(argv[1]);
647 sqlite3_result_error(context, "domain error", -1);
650 nLen = sqlite3_value_bytes(argv[0]);
652 z=sqlite3_malloc(nTLen+1);
653 zo=sqlite3_malloc(nLen+1);
655 sqlite3_result_error_nomem(context);
656 if (z) sqlite3_free(z);
657 if (zo) sqlite3_free(zo);
660 strcpy((char*)zo, (char*)sqlite3_value_text(argv[0]));
662 for(i=0; i<iCount; ++i){
663 strcpy((char*)(z+i*nLen), (char*)zo);
666 sqlite3_result_text(context, (char*)z, -1, SQLITE_TRANSIENT);
673 ** Some systems (win32 among others) don't have an isblank function, this will emulate it.
674 ** This function is not UFT-8 safe since it only analyses a byte character.
678 return( ' '==c || '\t'==c );
682 static void properFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
683 const unsigned char *z; /* input string */
684 unsigned char *zo; /* output string */
685 unsigned char *zt; /* iterator */
690 if( SQLITE_NULL==sqlite3_value_type(argv[0]) ){
691 sqlite3_result_null(context);
695 z = sqlite3_value_text(argv[0]);
696 zo = (unsigned char *)sqlite3StrDup((char *) z);
698 sqlite3_result_error_nomem(context);
703 while( (r = *(z++))!=0 ){
718 sqlite3_result_text(context, (char*)zo, -1, SQLITE_TRANSIENT);
723 ** given an input string (s) and an integer (n) adds spaces at the begining of s
724 ** until it has a length of n characters.
725 ** When s has a length >=n it's a NOP
728 static void padlFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
729 i64 ilen; /* length to pad to */
730 i64 zl; /* length of the input string (UTF-8 chars) */
732 const char *zi; /* input string */
733 char *zo; /* output string */
738 if( sqlite3_value_type(argv[0]) == SQLITE_NULL ){
739 sqlite3_result_null(context);
741 zi = (char *)sqlite3_value_text(argv[0]);
742 ilen = sqlite3_value_int64(argv[1]);
745 sqlite3_result_error(context, "domain error", -1);
748 zl = sqlite3Utf8CharLen(zi, -1);
750 /* string is longer than the requested pad length, return the same string (dup it) */
751 zo = sqlite3StrDup(zi);
753 sqlite3_result_error_nomem(context);
756 sqlite3_result_text(context, zo, -1, SQLITE_TRANSIENT);
758 zo = sqlite3_malloc(strlen(zi)+ilen-zl+1);
760 sqlite3_result_error_nomem(context);
764 for(i=1; i+zl<=ilen; ++i){
767 /* no need to take UTF-8 into consideration here */
770 sqlite3_result_text(context, zo, -1, SQLITE_TRANSIENT);
776 ** given an input string (s) and an integer (n) appends spaces at the end of s
777 ** until it has a length of n characters.
778 ** When s has a length >=n it's a NOP
781 static void padrFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
782 i64 ilen; /* length to pad to */
783 i64 zl; /* length of the input string (UTF-8 chars) */
784 i64 zll; /* length of the input string (bytes) */
786 const char *zi; /* input string */
787 char *zo; /* output string */
792 if( sqlite3_value_type(argv[0]) == SQLITE_NULL ){
793 sqlite3_result_null(context);
795 zi = (char *)sqlite3_value_text(argv[0]);
796 ilen = sqlite3_value_int64(argv[1]);
799 sqlite3_result_error(context, "domain error", -1);
802 zl = sqlite3Utf8CharLen(zi, -1);
804 /* string is longer than the requested pad length, return the same string (dup it) */
805 zo = sqlite3StrDup(zi);
807 sqlite3_result_error_nomem(context);
810 sqlite3_result_text(context, zo, -1, SQLITE_TRANSIENT);
813 zo = sqlite3_malloc(zll+ilen-zl+1);
815 sqlite3_result_error_nomem(context);
818 zt = strcpy(zo,zi)+zll;
819 for(i=1; i+zl<=ilen; ++i){
824 sqlite3_result_text(context, zo, -1, SQLITE_TRANSIENT);
830 ** given an input string (s) and an integer (n) appends spaces at the end of s
831 ** and adds spaces at the begining of s until it has a length of n characters.
832 ** Tries to add has many characters at the left as at the right.
833 ** When s has a length >=n it's a NOP
836 static void padcFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
837 i64 ilen; /* length to pad to */
838 i64 zl; /* length of the input string (UTF-8 chars) */
839 i64 zll; /* length of the input string (bytes) */
841 const char *zi; /* input string */
842 char *zo; /* output string */
847 if( sqlite3_value_type(argv[0]) == SQLITE_NULL ){
848 sqlite3_result_null(context);
850 zi = (char *)sqlite3_value_text(argv[0]);
851 ilen = sqlite3_value_int64(argv[1]);
854 sqlite3_result_error(context, "domain error", -1);
857 zl = sqlite3Utf8CharLen(zi, -1);
859 /* string is longer than the requested pad length, return the same string (dup it) */
860 zo = sqlite3StrDup(zi);
862 sqlite3_result_error_nomem(context);
865 sqlite3_result_text(context, zo, -1, SQLITE_TRANSIENT);
868 zo = sqlite3_malloc(zll+ilen-zl+1);
870 sqlite3_result_error_nomem(context);
874 for(i=1; 2*i+zl<=ilen; ++i){
879 for(; i+zl<=ilen; ++i){
884 sqlite3_result_text(context, zo, -1, SQLITE_TRANSIENT);
890 ** given 2 string (s1,s2) returns the string s1 with the characters NOT in s2 removed
891 ** assumes strings are UTF-8 encoded
893 static void strfilterFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
894 const char *zi1; /* first parameter string (searched string) */
895 const char *zi2; /* second parameter string (vcontains valid characters) */
899 char *zo; /* output string */
906 if( sqlite3_value_type(argv[0]) == SQLITE_NULL || sqlite3_value_type(argv[1]) == SQLITE_NULL ){
907 sqlite3_result_null(context);
909 zi1 = (char *)sqlite3_value_text(argv[0]);
910 zi2 = (char *)sqlite3_value_text(argv[1]);
912 ** maybe I could allocate less, but that would imply 2 passes, rather waste
913 ** (possibly) some memory
915 zo = sqlite3_malloc(strlen(zi1)+1);
917 sqlite3_result_error_nomem(context);
922 while( (c1=sqliteCharVal((unsigned char *)z1))!=0 ){
924 while( (c2=sqliteCharVal((unsigned char *)z21))!=0 && c2!=c1 ){
930 strncpy(zot, z21, z22-z21);
937 sqlite3_result_text(context, zo, -1, SQLITE_TRANSIENT);
943 ** Given a string z1, retutns the (0 based) index of it's first occurence
944 ** in z2 after the first s characters.
945 ** Returns -1 when there isn't a match.
946 ** updates p to point to the character where the match occured.
947 ** This is an auxiliary function.
949 static int _substr(const char* z1, const char* z2, int s, const char** p){
960 while( (sqliteCharVal((unsigned char *)z2) != 0) && (c++)<s){
965 while( (sqliteCharVal((unsigned char *)z2)) != 0 ){
970 c1 = sqliteCharVal((unsigned char *)zt1);
971 c2 = sqliteCharVal((unsigned char *)zt2);
974 }while( c1 == c2 && c1 != 0 && c2 != 0 );
987 return rVal >=0 ? rVal+s : rVal;
991 ** given 2 input strings (s1,s2) and an integer (n) searches from the nth character
992 ** for the string s1. Returns the position where the match occured.
993 ** Characters are counted from 1.
994 ** 0 is returned when no match occurs.
997 static void charindexFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
998 const u8 *z1; /* s1 string */
999 u8 *z2; /* s2 string */
1003 assert( argc==3 ||argc==2);
1005 if( SQLITE_NULL==sqlite3_value_type(argv[0]) || SQLITE_NULL==sqlite3_value_type(argv[1])){
1006 sqlite3_result_null(context);
1010 z1 = sqlite3_value_text(argv[0]);
1012 z2 = (u8*) sqlite3_value_text(argv[1]);
1014 s = sqlite3_value_int(argv[2])-1;
1022 rVal = _substr((char *)z1,(char *)z2,s,NULL);
1023 sqlite3_result_int(context, rVal+1);
1027 ** given a string (s) and an integer (n) returns the n leftmost (UTF-8) characters
1028 ** if the string has a length<=n or is NULL this function is NOP
1030 static void leftFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1034 const unsigned char *z; /* input string */
1035 const unsigned char *zt;
1036 unsigned char *rz; /* output string */
1040 if( SQLITE_NULL==sqlite3_value_type(argv[0]) || SQLITE_NULL==sqlite3_value_type(argv[1])){
1041 sqlite3_result_null(context);
1045 z = sqlite3_value_text(argv[0]);
1046 l = sqlite3_value_int(argv[1]);
1049 while( sqliteCharVal(zt) && c++<l)
1054 rz = sqlite3_malloc(zt-z+1);
1056 sqlite3_result_error_nomem(context);
1059 strncpy((char*) rz, (char*) z, zt-z);
1061 sqlite3_result_text(context, (char*)rz, -1, SQLITE_TRANSIENT);
1066 ** given a string (s) and an integer (n) returns the n rightmost (UTF-8) characters
1067 ** if the string has a length<=n or is NULL this function is NOP
1069 static void rightFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1080 if( SQLITE_NULL == sqlite3_value_type(argv[0]) || SQLITE_NULL == sqlite3_value_type(argv[1])){
1081 sqlite3_result_null(context);
1085 z = (char *)sqlite3_value_text(argv[0]);
1086 l = sqlite3_value_int(argv[1]);
1089 while( sqliteCharVal((unsigned char *)zt)!=0){
1105 rz = sqlite3_malloc(ze-zt+1);
1107 sqlite3_result_error_nomem(context);
1110 strcpy((char*) rz, (char*) (zt));
1111 sqlite3_result_text(context, (char*)rz, -1, SQLITE_TRANSIENT);
1117 ** removes the whitespaces at the begining of a string.
1119 const char* ltrim(const char* s){
1126 ** removes the whitespaces at the end of a string.
1127 ** !mutates the input string!
1129 void rtrim(char* s){
1130 char* ss = s+strlen(s)-1;
1131 while( ss>=s && *ss==' ' )
1137 ** Removes the whitespace at the begining of a string
1139 static void ltrimFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1144 if( SQLITE_NULL==sqlite3_value_type(argv[0]) ){
1145 sqlite3_result_null(context);
1148 z = sqlite3_value_text(argv[0]);
1149 sqlite3_result_text(context, ltrim(z), -1, SQLITE_TRANSIENT);
1153 ** Removes the whitespace at the end of a string
1155 static void rtrimFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1158 /* try not to change data in argv */
1162 if( SQLITE_NULL==sqlite3_value_type(argv[0]) ){
1163 sqlite3_result_null(context);
1166 z = sqlite3_value_text(argv[0]);
1167 rz = sqlite3StrDup(z);
1169 sqlite3_result_text(context, rz, -1, SQLITE_TRANSIENT);
1174 ** Removes the whitespace at the begining and end of a string
1176 static void trimFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1179 /* try not to change data in argv */
1183 if( SQLITE_NULL==sqlite3_value_type(argv[0]) ){
1184 sqlite3_result_null(context);
1187 z = sqlite3_value_text(argv[0]);
1188 rz = sqlite3StrDup(z);
1190 sqlite3_result_text(context, ltrim(rz), -1, SQLITE_TRANSIENT);
1196 ** given a pointer to a string s1, the length of that string (l1), a new string (s2)
1197 ** and it's length (l2) appends s2 to s1.
1198 ** All lengths in bytes.
1199 ** This is just an auxiliary function
1201 // static void _append(char **s1, int l1, const char *s2, int l2){
1202 // *s1 = realloc(*s1, (l1+l2+1)*sizeof(char));
1203 // strncpy((*s1)+l1, s2, l2);
1204 // *(*(s1)+l1+l2) = '\0';
1210 ** given strings s, s1 and s2 replaces occurrences of s1 in s by s2
1212 static void replaceFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1213 const char *z1; /* string s (first parameter) */
1214 const char *z2; /* string s1 (second parameter) string to look for */
1215 const char *z3; /* string s2 (third parameter) string to replace occurrences of s1 with */
1227 if( SQLITE_NULL==sqlite3_value_type(argv[0]) ){
1228 sqlite3_result_null(context);
1232 z1 = sqlite3_value_text(argv[0]);
1233 z2 = sqlite3_value_text(argv[1]);
1234 z3 = sqlite3_value_text(argv[2]);
1235 /* handle possible null values */
1248 /* special case when z2 is empty (or null) nothing will be changed */
1250 sqlite3_result_text(context, z1, -1, SQLITE_TRANSIENT);
1259 ret=_substr(z2,zt1 , 0, &zt2);
1264 _append(&zo, lzo, zt1, zt2-zt1);
1266 _append(&zo, lzo, z3, lz3);
1271 _append(&zo, lzo, zt1, lz1-(zt1-z1));
1272 sqlite3_result_text(context, zo, -1, SQLITE_TRANSIENT);
1278 ** given a string returns the same string but with the characters in reverse order
1280 static void reverseFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1290 if( SQLITE_NULL==sqlite3_value_type(argv[0]) ){
1291 sqlite3_result_null(context);
1294 z = (char *)sqlite3_value_text(argv[0]);
1296 rz = sqlite3_malloc(l+1);
1298 sqlite3_result_error_nomem(context);
1305 while( sqliteCharVal((unsigned char *)zt)!=0 ){
1308 for(i=1; zt-i>=z; ++i){
1313 sqlite3_result_text(context, rz, -1, SQLITE_TRANSIENT);
1318 ** An instance of the following structure holds the context of a
1319 ** stdev() or variance() aggregate computation.
1320 ** implementaion of http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Algorithm_II
1321 ** less prone to rounding errors
1323 typedef struct StdevCtx StdevCtx;
1327 i64 cnt; /* number of elements */
1331 ** An instance of the following structure holds the context of a
1332 ** mode() or median() aggregate computation.
1333 ** Depends on structures defined in map.c (see map & map)
1334 ** These aggregate functions only work for integers and floats although
1335 ** they could be made to work for strings. This is usually considered meaningless.
1336 ** Only usuall order (for median), no use of collation functions (would this even make sense?)
1338 typedef struct ModeCtx ModeCtx;
1340 i64 riM; /* integer value found so far */
1341 double rdM; /* double value found so far */
1342 i64 cnt; /* number of elements so far */
1343 double pcnt; /* number of elements smaller than a percentile */
1344 i64 mcnt; /* maximum number of occurrences (for mode) */
1345 i64 mn; /* number of occurrences (for mode and percentiles) */
1346 i64 is_double; /* whether the computation is being done for doubles (>0) or integers (=0) */
1347 map* m; /* map structure used for the computation */
1348 int done; /* whether the answer has been found */
1352 ** called for each value received during a calculation of stdev or variance
1354 static void varianceStep(sqlite3_context *context, int argc, sqlite3_value **argv){
1361 p = sqlite3_aggregate_context(context, sizeof(*p));
1362 /* only consider non-null values */
1363 if( SQLITE_NULL != sqlite3_value_numeric_type(argv[0]) ){
1365 x = sqlite3_value_double(argv[0]);
1367 p->rM += delta/p->cnt;
1368 p->rS += delta*(x-p->rM);
1373 ** called for each value received during a calculation of mode of median
1375 static void modeStep(sqlite3_context *context, int argc, sqlite3_value **argv){
1384 type = sqlite3_value_numeric_type(argv[0]);
1386 if( type == SQLITE_NULL)
1389 p = sqlite3_aggregate_context(context, sizeof(*p));
1392 p->m = calloc(1, sizeof(map));
1393 if( type==SQLITE_INTEGER ){
1394 /* map will be used for integers */
1395 *(p->m) = map_make(int_cmp);
1399 /* map will be used for doubles */
1400 *(p->m) = map_make(double_cmp);
1406 if( 0==p->is_double ){
1407 xi = sqlite3_value_int64(argv[0]);
1408 iptr = (i64*)calloc(1,sizeof(i64));
1410 map_insert(p->m, iptr);
1412 xd = sqlite3_value_double(argv[0]);
1413 dptr = (double*)calloc(1,sizeof(double));
1415 map_insert(p->m, dptr);
1420 ** Auxiliary function that iterates all elements in a map and finds the mode
1421 ** (most frequent value)
1423 static void modeIterate(void* e, i64 c, void* pp){
1426 ModeCtx *p = (ModeCtx*)pp;
1428 if( 0==p->is_double ){
1433 }else if( p->mcnt<c ){
1443 }else if(p->mcnt<c){
1452 ** Auxiliary function that iterates all elements in a map and finds the median
1453 ** (the value such that the number of elements smaller is equal the the number of
1456 static void medianIterate(void* e, i64 c, void* pp){
1463 ModeCtx *p = (ModeCtx*)pp;
1469 iR = p->cnt - p->pcnt;
1471 ir = p->cnt - p->mcnt;
1476 if( 0==p->is_double ){
1491 ** Returns the mode value
1493 static void modeFinalize(sqlite3_context *context){
1495 p = sqlite3_aggregate_context(context, 0);
1497 map_iterate(p->m, modeIterate, p);
1502 if( 0==p->is_double )
1503 sqlite3_result_int64(context, p->riM);
1505 sqlite3_result_double(context, p->rdM);
1511 ** auxiliary function for percentiles
1513 static void _medianFinalize(sqlite3_context *context){
1515 p = (ModeCtx*) sqlite3_aggregate_context(context, 0);
1518 map_iterate(p->m, medianIterate, p);
1522 if( 0==p->is_double )
1524 sqlite3_result_int64(context, p->riM);
1526 sqlite3_result_double(context, p->riM*1.0/p->mn);
1528 sqlite3_result_double(context, p->rdM/p->mn);
1533 ** Returns the median value
1535 static void medianFinalize(sqlite3_context *context){
1537 p = (ModeCtx*) sqlite3_aggregate_context(context, 0);
1539 p->pcnt = (p->cnt)/2.0;
1540 _medianFinalize(context);
1545 ** Returns the lower_quartile value
1547 static void lower_quartileFinalize(sqlite3_context *context){
1549 p = (ModeCtx*) sqlite3_aggregate_context(context, 0);
1551 p->pcnt = (p->cnt)/4.0;
1552 _medianFinalize(context);
1557 ** Returns the upper_quartile value
1559 static void upper_quartileFinalize(sqlite3_context *context){
1561 p = (ModeCtx*) sqlite3_aggregate_context(context, 0);
1563 p->pcnt = (p->cnt)*3/4.0;
1564 _medianFinalize(context);
1569 ** Returns the stdev value
1571 static void stdevFinalize(sqlite3_context *context){
1573 p = sqlite3_aggregate_context(context, 0);
1574 if( p && p->cnt>1 ){
1575 sqlite3_result_double(context, sqrt(p->rS/(p->cnt-1)));
1577 sqlite3_result_double(context, 0.0);
1582 ** Returns the variance value
1584 static void varianceFinalize(sqlite3_context *context){
1586 p = sqlite3_aggregate_context(context, 0);
1587 if( p && p->cnt>1 ){
1588 sqlite3_result_double(context, p->rS/(p->cnt-1));
1590 sqlite3_result_double(context, 0.0);
1594 #ifdef SQLITE_SOUNDEX
1596 /* relicoder factored code */
1598 ** Calculates the soundex value of a string
1601 static void soundex(const u8 *zIn, char *zResult){
1603 static const unsigned char iCode[] = {
1604 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1605 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1606 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1607 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1608 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
1609 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
1610 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
1611 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
1614 for(i=0; zIn[i] && !isalpha(zIn[i]); i++){}
1616 zResult[0] = toupper(zIn[i]);
1617 for(j=1; j<4 && zIn[i]; i++){
1618 int code = iCode[zIn[i]&0x7f];
1620 zResult[j++] = code + '0';
1628 strcpy(zResult, "?000");
1633 ** computes the number of different characters between the soundex value fo 2 strings
1635 static void differenceFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
1638 char *zR1 = zResult1;
1639 char *zR2 = zResult2;
1647 if( sqlite3_value_type(argv[0])==SQLITE_NULL || sqlite3_value_type(argv[1])==SQLITE_NULL ){
1648 sqlite3_result_null(context);
1652 zIn1 = (u8*)sqlite3_value_text(argv[0]);
1653 zIn2 = (u8*)sqlite3_value_text(argv[1]);
1659 if( sqliteCharVal((unsigned char *)zR1)==sqliteCharVal((unsigned char *)zR2) )
1661 sqliteNextChar(zR1);
1662 sqliteNextChar(zR2);
1664 sqlite3_result_int(context, rVal);
1669 ** This function registered all of the above C functions as SQL
1670 ** functions. This should be the only routine in this file with
1671 ** external linkage.
1673 int RegisterExtensionFunctions(sqlite3 *db){
1674 static const struct FuncDef {
1677 u8 argType; /* 0: none. 1: db 2: (-1) */
1678 u8 eTextRep; /* 1: UTF-16. 0: UTF-8 */
1680 void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
1683 { "acos", 1, 0, SQLITE_UTF8, 0, acosFunc },
1684 { "asin", 1, 0, SQLITE_UTF8, 0, asinFunc },
1685 { "atan", 1, 0, SQLITE_UTF8, 0, atanFunc },
1686 { "atn2", 2, 0, SQLITE_UTF8, 0, atn2Func },
1688 { "atan2", 2, 0, SQLITE_UTF8, 0, atn2Func },
1689 { "acosh", 1, 0, SQLITE_UTF8, 0, acoshFunc },
1690 { "asinh", 1, 0, SQLITE_UTF8, 0, asinhFunc },
1691 { "atanh", 1, 0, SQLITE_UTF8, 0, atanhFunc },
1693 { "difference", 2, 0, SQLITE_UTF8, 0, differenceFunc},
1694 { "degrees", 1, 0, SQLITE_UTF8, 0, rad2degFunc },
1695 { "radians", 1, 0, SQLITE_UTF8, 0, deg2radFunc },
1697 { "cos", 1, 0, SQLITE_UTF8, 0, cosFunc },
1698 { "sin", 1, 0, SQLITE_UTF8, 0, sinFunc },
1699 { "tan", 1, 0, SQLITE_UTF8, 0, tanFunc },
1700 { "cot", 1, 0, SQLITE_UTF8, 0, cotFunc },
1701 { "cosh", 1, 0, SQLITE_UTF8, 0, coshFunc },
1702 { "sinh", 1, 0, SQLITE_UTF8, 0, sinhFunc },
1703 { "tanh", 1, 0, SQLITE_UTF8, 0, tanhFunc },
1704 { "coth", 1, 0, SQLITE_UTF8, 0, cothFunc },
1706 { "exp", 1, 0, SQLITE_UTF8, 0, expFunc },
1707 { "log", 1, 0, SQLITE_UTF8, 0, logFunc },
1708 { "log10", 1, 0, SQLITE_UTF8, 0, log10Func },
1709 { "power", 2, 0, SQLITE_UTF8, 0, powerFunc },
1710 { "sign", 1, 0, SQLITE_UTF8, 0, signFunc },
1711 { "sqrt", 1, 0, SQLITE_UTF8, 0, sqrtFunc },
1712 { "square", 1, 0, SQLITE_UTF8, 0, squareFunc },
1714 { "ceil", 1, 0, SQLITE_UTF8, 0, ceilFunc },
1715 { "floor", 1, 0, SQLITE_UTF8, 0, floorFunc },
1717 { "pi", 0, 0, SQLITE_UTF8, 1, piFunc },
1721 { "replicate", 2, 0, SQLITE_UTF8, 0, replicateFunc },
1722 { "charindex", 2, 0, SQLITE_UTF8, 0, charindexFunc },
1723 { "charindex", 3, 0, SQLITE_UTF8, 0, charindexFunc },
1724 { "leftstr", 2, 0, SQLITE_UTF8, 0, leftFunc },
1725 { "rightstr", 2, 0, SQLITE_UTF8, 0, rightFunc },
1727 { "ltrim", 1, 0, SQLITE_UTF8, 0, ltrimFunc },
1728 { "rtrim", 1, 0, SQLITE_UTF8, 0, rtrimFunc },
1729 { "trim", 1, 0, SQLITE_UTF8, 0, trimFunc },
1730 { "replace", 3, 0, SQLITE_UTF8, 0, replaceFunc },
1732 { "reverse", 1, 0, SQLITE_UTF8, 0, reverseFunc },
1733 { "proper", 1, 0, SQLITE_UTF8, 0, properFunc },
1734 { "padl", 2, 0, SQLITE_UTF8, 0, padlFunc },
1735 { "padr", 2, 0, SQLITE_UTF8, 0, padrFunc },
1736 { "padc", 2, 0, SQLITE_UTF8, 0, padcFunc },
1737 { "strfilter", 2, 0, SQLITE_UTF8, 0, strfilterFunc },
1740 /* Aggregate functions */
1741 static const struct FuncDefAgg {
1746 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
1747 void (*xFinalize)(sqlite3_context*);
1749 { "stdev", 1, 0, 0, varianceStep, stdevFinalize },
1750 { "variance", 1, 0, 0, varianceStep, varianceFinalize },
1751 { "mode", 1, 0, 0, modeStep, modeFinalize },
1752 { "median", 1, 0, 0, modeStep, medianFinalize },
1753 { "lower_quartile", 1, 0, 0, modeStep, lower_quartileFinalize },
1754 { "upper_quartile", 1, 0, 0, modeStep, upper_quartileFinalize },
1758 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
1760 switch( aFuncs[i].argType ){
1761 case 1: pArg = db; break;
1762 case 2: pArg = (void *)(-1); break;
1765 /* LMH no error checking */
1766 sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg,
1767 aFuncs[i].eTextRep, pArg, aFuncs[i].xFunc, 0, 0);
1769 if( aFuncs[i].needCollSeq ){
1770 struct FuncDef *pFunc = sqlite3FindFunction(db, aFuncs[i].zName,
1771 strlen(aFuncs[i].zName), aFuncs[i].nArg, aFuncs[i].eTextRep, 0);
1772 if( pFunc && aFuncs[i].needCollSeq ){
1773 pFunc->needCollSeq = 1;
1779 for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
1781 switch( aAggs[i].argType ){
1782 case 1: pArg = db; break;
1783 case 2: pArg = (void *)(-1); break;
1786 /* LMH no error checking */
1787 sqlite3_create_function(db, aAggs[i].zName, aAggs[i].nArg, SQLITE_UTF8,
1788 pArg, 0, aAggs[i].xStep, aAggs[i].xFinalize);
1790 if( aAggs[i].needCollSeq ){
1791 struct FuncDefAgg *pFunc = sqlite3FindFunction( db, aAggs[i].zName,
1792 strlen(aAggs[i].zName), aAggs[i].nArg, SQLITE_UTF8, 0);
1793 if( pFunc && aAggs[i].needCollSeq ){
1794 pFunc->needCollSeq = 1;
1802 #ifdef COMPILE_SQLITE_EXTENSIONS_AS_LOADABLE_MODULE
1803 int sqlite3_extension_init(
1804 sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi){
1805 SQLITE_EXTENSION_INIT2(pApi);
1806 RegisterExtensionFunctions(db);
1809 #endif /* COMPILE_SQLITE_EXTENSIONS_AS_LOADABLE_MODULE */
1811 map map_make(cmp_func cmp){
1819 void* xcalloc(size_t nmemb, size_t size, char* s){
1820 void* ret = calloc(nmemb, size);
1824 void xfree(void* p){
1828 void node_insert(node** n, cmp_func cmp, void *e){
1832 nn = (node*)xcalloc(1,sizeof(node), "for node");
1837 c=cmp((*n)->data,e);
1842 /* put it right here */
1843 node_insert(&((*n)->l), cmp, e);
1845 node_insert(&((*n)->r), cmp, e);
1850 void map_insert(map *m, void *e){
1851 node_insert(&(m->base), m->cmp, e);
1854 void node_iterate(node *n, map_iterator iter, void* p){
1857 node_iterate(n->l, iter, p);
1858 iter(n->data, n->count, p);
1860 node_iterate(n->r, iter, p);
1864 void map_iterate(map *m, map_iterator iter, void* p){
1865 node_iterate(m->base, iter, p);
1868 void node_destroy(node *n){
1880 void map_destroy(map *m){
1881 node_destroy(m->base);
1884 int int_cmp(const void *a, const void *b){
1885 sqlite_int64 aa = *(sqlite_int64 *)(a);
1886 sqlite_int64 bb = *(sqlite_int64 *)(b);
1887 /* printf("cmp %d <=> %d\n",aa,bb); */
1896 int double_cmp(const void *a, const void *b){
1897 double aa = *(double *)(a);
1898 double bb = *(double *)(b);
1899 /* printf("cmp %d <=> %d\n",aa,bb); */
1908 void print_elem(void *e, sqlite_int64 c, void* p){
1909 int ee = *(int*)(e);
1910 printf("%d => %lld\n", ee,c);