Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/ext/misc/zorder.c # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / ext / misc / zorder.c` 134 lines ยท 3451 bytes Raw Blame History 1 /* 2 ** 2018-02-09 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 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. 10 ** 11 ****************************************************************************** 12 ** 13 ** SQL functions for z-order (Morton code) transformations. 14 ** 15 ** zorder(X0,X0,..,xN) Generate an N+1 dimension Morton code 16 ** 17 ** unzorder(Z,N,I) Extract the I-th dimension from N-dimensional 18 ** Morton code Z. 19 ** 20 ** Compiling: 21 ** 22 ** (linux) gcc -fPIC -shared zorder.c -o zorder.so 23 ** (mac) clang -fPIC -dynamiclib zorder.c -o zorder.dylib 24 ** (windows) cl zorder.c -link -dll -out:zorder.dll 25 ** 26 ** Usage example: 27 ** 28 ** .load ./zorder 29 ** SELECT zorder(1,2,3,4); 30 */ 31 #include "sqlite3ext.h" 32 SQLITE_EXTENSION_INIT1 33 #include 34 #include 35 36 /* 37 ** Functions: zorder(X0,X1,....) 38 ** 39 ** Convert integers X0, X1, ... into morton code. There must be at least 40 ** two arguments. There may be no more than 24 arguments. 41 ** 42 ** The output is a signed 64-bit integer. If any argument is too large 43 ** to be successfully encoded into a morton code, an error is raised. 44 */ 45 static void zorderFunc( 46 sqlite3_context *context, 47 int argc, 48 sqlite3_value **argv 49 ){ 50 sqlite3_int64 z, x[24]; 51 int i, j; 52 z = 0; 53 if( argc<2 || argc>24 ){ 54 sqlite3_result_error(context, 55 "zorder() needs between 2 and 24 arguments4", -1); 56 return; 57 } 58 for(i=0; i>= 1; 65 } 66 sqlite3_result_int64(context, z); 67 for(i=0; i24 ){ 97 sqlite3_result_error(context, 98 "N argument to unzorder(Z,N,K) should be between 2 and 24", 99 -1); 100 return; 101 } 102 i = sqlite3_value_int64(argv[2]); 103 if( i<0 || i>=n ){ 104 sqlite3_result_error(context, 105 "K argument to unzorder(Z,N,K) should be between 0 and N-1", -1); 106 return; 107 } 108 x = 0; 109 for(k=0, j=i; j<63; j+=n, k++){ 110 x |= ((z>>j)&1)<