4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 ******************************************************************************
13 ** SQL functions for z-order (Morton code) transformations.
15 ** zorder(X0,X0,..,xN) Generate an N+1 dimension Morton code
17 ** unzorder(Z,N,I) Extract the I-th dimension from N-dimensional
20 #include "sqlite3ext.h"
21 SQLITE_EXTENSION_INIT1
26 ** Functions: zorder(X0,X1,....)
28 ** Convert integers X0, X1, ... into morton code.
30 ** The output is a signed 64-bit integer. If any argument is too large,
31 ** an error is thrown.
33 static void zorderFunc(
34 sqlite3_context
*context
,
38 sqlite3_int64 z
, x
[63];
41 for(i
=0; i
<argc
; i
++){
42 x
[i
] = sqlite3_value_int64(argv
[i
]);
51 sqlite3_result_int64(context
, z
);
52 for(i
=0; i
<argc
; i
++){
54 sqlite3_result_error(context
, "parameter too large", -1);
61 ** Functions: unzorder(Z,N,I)
63 ** Assuming that Z is an N-dimensional Morton code, extract the I-th
66 static void unzorderFunc(
67 sqlite3_context
*context
,
71 sqlite3_int64 z
, n
, i
, x
;
73 z
= sqlite3_value_int64(argv
[0]);
74 n
= sqlite3_value_int64(argv
[1]);
75 i
= sqlite3_value_int64(argv
[2]);
77 for(k
=0, j
=i
; j
<63; j
+=n
, k
++){
80 sqlite3_result_int64(context
, x
);
87 int sqlite3_zorder_init(
90 const sqlite3_api_routines
*pApi
93 SQLITE_EXTENSION_INIT2(pApi
);
94 (void)pzErrMsg
; /* Unused parameter */
95 rc
= sqlite3_create_function(db
, "zorder", -1, SQLITE_UTF8
, 0,
98 rc
= sqlite3_create_function(db
, "unzorder", 3, SQLITE_UTF8
, 0,