sq_bitvector.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. #ifdef USE_BITVECTOR
  2. #include "squirrel.h"
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdlib.h> /* for malloc */
  6. #include <assert.h> /* for a few sanity tests */
  7. #include "sqlite3.h"
  8. //copy from sqliteInt.h
  9. #ifndef UINT32_TYPE
  10. # ifdef HAVE_UINT32_T
  11. # define UINT32_TYPE uint32_t
  12. # else
  13. # define UINT32_TYPE unsigned int
  14. # endif
  15. #endif
  16. #ifndef UINT16_TYPE
  17. # ifdef HAVE_UINT16_T
  18. # define UINT16_TYPE uint16_t
  19. # else
  20. # define UINT16_TYPE unsigned short int
  21. # endif
  22. #endif
  23. #ifndef INT16_TYPE
  24. # ifdef HAVE_INT16_T
  25. # define INT16_TYPE int16_t
  26. # else
  27. # define INT16_TYPE short int
  28. # endif
  29. #endif
  30. #ifndef UINT8_TYPE
  31. # ifdef HAVE_UINT8_T
  32. # define UINT8_TYPE uint8_t
  33. # else
  34. # define UINT8_TYPE unsigned char
  35. # endif
  36. #endif
  37. #ifndef INT8_TYPE
  38. # ifdef HAVE_INT8_T
  39. # define INT8_TYPE int8_t
  40. # else
  41. # define INT8_TYPE signed char
  42. # endif
  43. #endif
  44. #ifndef LONGDOUBLE_TYPE
  45. # define LONGDOUBLE_TYPE long double
  46. #endif
  47. //typedef sqlite_int64 i64; /* 8-byte signed integer */
  48. //typedef sqlite_uint64 u64; /* 8-byte unsigned integer */
  49. typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
  50. typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
  51. typedef INT16_TYPE i16; /* 2-byte signed integer */
  52. typedef UINT8_TYPE u8; /* 1-byte unsigned integer */
  53. typedef INT8_TYPE i8; /* 1-byte signed integer */
  54. void *sqlite3MallocZero(size_t size)
  55. {
  56. void *p = sq_malloc(size);
  57. memset(p, 0, size);
  58. return p;
  59. }
  60. /*
  61. ** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
  62. ** that can be stored in a u32 without loss of data. The value
  63. ** is 0x00000000ffffffff. But because of quirks of some compilers, we
  64. ** have to specify the value in the less intuitive manner shown:
  65. */
  66. #define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
  67. /*
  68. ** On systems with ample stack space and that support alloca(), make
  69. ** use of alloca() to obtain space for large automatic objects. By default,
  70. ** obtain space from malloc().
  71. **
  72. ** The alloca() routine never returns NULL. This will cause code paths
  73. ** that deal with sqlite3StackAlloc() failures to be unreachable.
  74. */
  75. # define sqlite3StackAllocRaw(D,N) alloca(N)
  76. # define sqlite3StackAllocZero(D,N) memset(alloca(N), 0, N)
  77. # define sqlite3StackFree(D,P)
  78. # define SQLITE_NOMEM_BKPT SQLITE_NOMEM
  79. # define SQLITE_IOERR_NOMEM_BKPT SQLITE_IOERR_NOMEM
  80. /*
  81. ** 2008 February 16
  82. **
  83. ** The author disclaims copyright to this source code. In place of
  84. ** a legal notice, here is a blessing:
  85. **
  86. ** May you do good and not evil.
  87. ** May you find forgiveness for yourself and forgive others.
  88. ** May you share freely, never taking more than you give.
  89. **
  90. *************************************************************************
  91. ** This file implements an object that represents a fixed-length
  92. ** bitmap. Bits are numbered starting with 1.
  93. **
  94. ** A bitmap is used to record which pages of a database file have been
  95. ** journalled during a transaction, or which pages have the "dont-write"
  96. ** property. Usually only a few pages are meet either condition.
  97. ** So the bitmap is usually sparse and has low cardinality.
  98. ** But sometimes (for example when during a DROP of a large table) most
  99. ** or all of the pages in a database can get journalled. In those cases,
  100. ** the bitmap becomes dense with high cardinality. The algorithm needs
  101. ** to handle both cases well.
  102. **
  103. ** The size of the bitmap is fixed when the object is created.
  104. **
  105. ** All bits are clear when the bitmap is created. Individual bits
  106. ** may be set or cleared one at a time.
  107. **
  108. ** Test operations are about 100 times more common that set operations.
  109. ** Clear operations are exceedingly rare. There are usually between
  110. ** 5 and 500 set operations per Bitvec object, though the number of sets can
  111. ** sometimes grow into tens of thousands or larger. The size of the
  112. ** Bitvec object is the number of pages in the database file at the
  113. ** start of a transaction, and is thus usually less than a few thousand,
  114. ** but can be as large as 2 billion for a really big database.
  115. */
  116. //#include "sqliteInt.h"
  117. /* Size of the Bitvec structure in bytes. */
  118. #define BITVEC_SZ 512
  119. /* Round the union size down to the nearest pointer boundary, since that's how
  120. ** it will be aligned within the Bitvec struct. */
  121. #define BITVEC_USIZE \
  122. (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
  123. /* Type of the array "element" for the bitmap representation.
  124. ** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE.
  125. ** Setting this to the "natural word" size of your CPU may improve
  126. ** performance. */
  127. #define BITVEC_TELEM u8
  128. /* Size, in bits, of the bitmap element. */
  129. #define BITVEC_SZELEM 8
  130. /* Number of elements in a bitmap array. */
  131. #define BITVEC_NELEM (BITVEC_USIZE/sizeof(BITVEC_TELEM))
  132. /* Number of bits in the bitmap array. */
  133. #define BITVEC_NBIT (BITVEC_NELEM*BITVEC_SZELEM)
  134. /* Number of u32 values in hash table. */
  135. #define BITVEC_NINT (BITVEC_USIZE/sizeof(u32))
  136. /* Maximum number of entries in hash table before
  137. ** sub-dividing and re-hashing. */
  138. #define BITVEC_MXHASH (BITVEC_NINT/2)
  139. /* Hashing function for the aHash representation.
  140. ** Empirical testing showed that the *37 multiplier
  141. ** (an arbitrary prime)in the hash function provided
  142. ** no fewer collisions than the no-op *1. */
  143. #define BITVEC_HASH(X) (((X)*1)%BITVEC_NINT)
  144. #define BITVEC_NPTR (BITVEC_USIZE/sizeof(Bitvec *))
  145. /*
  146. ** A bitmap is an instance of the following structure.
  147. **
  148. ** This bitmap records the existence of zero or more bits
  149. ** with values between 1 and iSize, inclusive.
  150. **
  151. ** There are three possible representations of the bitmap.
  152. ** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
  153. ** bitmap. The least significant bit is bit 1.
  154. **
  155. ** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
  156. ** a hash table that will hold up to BITVEC_MXHASH distinct values.
  157. **
  158. ** Otherwise, the value i is redirected into one of BITVEC_NPTR
  159. ** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap
  160. ** handles up to iDivisor separate values of i. apSub[0] holds
  161. ** values between 1 and iDivisor. apSub[1] holds values between
  162. ** iDivisor+1 and 2*iDivisor. apSub[N] holds values between
  163. ** N*iDivisor+1 and (N+1)*iDivisor. Each subbitmap is normalized
  164. ** to hold deal with values between 1 and iDivisor.
  165. */
  166. struct Bitvec {
  167. u32 iSize; /* Maximum bit index. Max iSize is 4,294,967,296. */
  168. u32 nSet; /* Number of bits that are set - only valid for aHash
  169. ** element. Max is BITVEC_NINT. For BITVEC_SZ of 512,
  170. ** this would be 125. */
  171. u32 iDivisor; /* Number of bits handled by each apSub[] entry. */
  172. /* Should >=0 for apSub element. */
  173. /* Max iDivisor is max(u32) / BITVEC_NPTR + 1. */
  174. /* For a BITVEC_SZ of 512, this would be 34,359,739. */
  175. union {
  176. BITVEC_TELEM aBitmap[BITVEC_NELEM]; /* Bitmap representation */
  177. u32 aHash[BITVEC_NINT]; /* Hash table representation */
  178. Bitvec *apSub[BITVEC_NPTR]; /* Recursive representation */
  179. } u;
  180. };
  181. /*
  182. ** Create a new bitmap object able to handle bits between 0 and iSize,
  183. ** inclusive. Return a pointer to the new object. Return NULL if
  184. ** malloc fails.
  185. */
  186. static Bitvec *sqlite3BitvecCreate(u32 iSize){
  187. Bitvec *p;
  188. assert( sizeof(*p)==BITVEC_SZ );
  189. p = (Bitvec*)sqlite3MallocZero( sizeof(*p) );
  190. if( p ){
  191. p->iSize = iSize;
  192. }
  193. return p;
  194. }
  195. /*
  196. ** Check to see if the i-th bit is set. Return true or false.
  197. ** If p is NULL (if the bitmap has not been created) or if
  198. ** i is out of range, then return false.
  199. */
  200. static int sqlite3BitvecTestNotNull(Bitvec *p, u32 i){
  201. assert( p!=0 );
  202. i--;
  203. if( i>=p->iSize ) return 0;
  204. while( p->iDivisor ){
  205. u32 bin = i/p->iDivisor;
  206. i = i%p->iDivisor;
  207. p = p->u.apSub[bin];
  208. if (!p) {
  209. return 0;
  210. }
  211. }
  212. if( p->iSize<=BITVEC_NBIT ){
  213. return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
  214. } else{
  215. u32 h = BITVEC_HASH(i++);
  216. while( p->u.aHash[h] ){
  217. if( p->u.aHash[h]==i ) return 1;
  218. h = (h+1) % BITVEC_NINT;
  219. }
  220. return 0;
  221. }
  222. }
  223. static int sqlite3BitvecTest(Bitvec *p, u32 i){
  224. return p!=0 && sqlite3BitvecTestNotNull(p,i);
  225. }
  226. /*
  227. ** Set the i-th bit. Return 0 on success and an error code if
  228. ** anything goes wrong.
  229. **
  230. ** This routine might cause sub-bitmaps to be allocated. Failing
  231. ** to get the memory needed to hold the sub-bitmap is the only
  232. ** that can go wrong with an insert, assuming p and i are valid.
  233. **
  234. ** The calling function must ensure that p is a valid Bitvec object
  235. ** and that the value for "i" is within range of the Bitvec object.
  236. ** Otherwise the behavior is undefined.
  237. */
  238. static int sqlite3BitvecSet(Bitvec *p, u32 i){
  239. u32 h;
  240. if( p==0 ) return SQLITE_OK;
  241. assert( i>0 );
  242. assert( i<=p->iSize );
  243. i--;
  244. while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
  245. u32 bin = i/p->iDivisor;
  246. i = i%p->iDivisor;
  247. if( p->u.apSub[bin]==0 ){
  248. p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
  249. if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT;
  250. }
  251. p = p->u.apSub[bin];
  252. }
  253. if( p->iSize<=BITVEC_NBIT ){
  254. p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
  255. return SQLITE_OK;
  256. }
  257. h = BITVEC_HASH(i++);
  258. /* if there wasn't a hash collision, and this doesn't */
  259. /* completely fill the hash, then just add it without */
  260. /* worring about sub-dividing and re-hashing. */
  261. if( !p->u.aHash[h] ){
  262. if (p->nSet<(BITVEC_NINT-1)) {
  263. goto bitvec_set_end;
  264. } else {
  265. goto bitvec_set_rehash;
  266. }
  267. }
  268. /* there was a collision, check to see if it's already */
  269. /* in hash, if not, try to find a spot for it */
  270. do {
  271. if( p->u.aHash[h]==i ) return SQLITE_OK;
  272. h++;
  273. if( h>=BITVEC_NINT ) h = 0;
  274. } while( p->u.aHash[h] );
  275. /* we didn't find it in the hash. h points to the first */
  276. /* available free spot. check to see if this is going to */
  277. /* make our hash too "full". */
  278. bitvec_set_rehash:
  279. if( p->nSet>=BITVEC_MXHASH ){
  280. unsigned int j;
  281. int rc;
  282. u32 *aiValues = (u32*)sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
  283. if( aiValues==0 ){
  284. return SQLITE_NOMEM_BKPT;
  285. }else{
  286. memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
  287. memset(p->u.apSub, 0, sizeof(p->u.apSub));
  288. p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
  289. rc = sqlite3BitvecSet(p, i);
  290. for(j=0; j<BITVEC_NINT; j++){
  291. if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
  292. }
  293. sqlite3StackFree(0, aiValues);
  294. return rc;
  295. }
  296. }
  297. bitvec_set_end:
  298. p->nSet++;
  299. p->u.aHash[h] = i;
  300. return SQLITE_OK;
  301. }
  302. /*
  303. ** Clear the i-th bit.
  304. **
  305. ** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
  306. ** that BitvecClear can use to rebuilt its hash table.
  307. */
  308. static void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
  309. if( p==0 ) return;
  310. assert( i>0 );
  311. i--;
  312. while( p->iDivisor ){
  313. u32 bin = i/p->iDivisor;
  314. i = i%p->iDivisor;
  315. p = p->u.apSub[bin];
  316. if (!p) {
  317. return;
  318. }
  319. }
  320. if( p->iSize<=BITVEC_NBIT ){
  321. p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
  322. }else{
  323. unsigned int j;
  324. u32 *aiValues = (u32*)pBuf;
  325. memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
  326. memset(p->u.aHash, 0, sizeof(p->u.aHash));
  327. p->nSet = 0;
  328. for(j=0; j<BITVEC_NINT; j++){
  329. if( aiValues[j] && aiValues[j]!=(i+1) ){
  330. u32 h = BITVEC_HASH(aiValues[j]-1);
  331. p->nSet++;
  332. while( p->u.aHash[h] ){
  333. h++;
  334. if( h>=BITVEC_NINT ) h = 0;
  335. }
  336. p->u.aHash[h] = aiValues[j];
  337. }
  338. }
  339. }
  340. }
  341. /*
  342. ** Destroy a bitmap object. Reclaim all memory used.
  343. */
  344. static void sqlite3BitvecDestroy(Bitvec *p){
  345. if( p==0 ) return;
  346. if( p->iDivisor ){
  347. unsigned int i;
  348. for(i=0; i<BITVEC_NPTR; i++){
  349. sqlite3BitvecDestroy(p->u.apSub[i]);
  350. }
  351. }
  352. sq_free(p, 0);
  353. }
  354. /*
  355. ** Return the value of the iSize parameter specified when Bitvec *p
  356. ** was created.
  357. */
  358. static u32 sqlite3BitvecSize(Bitvec *p){
  359. return p->iSize;
  360. }
  361. /*
  362. ** Return the value of the BITVEC_SZ.
  363. */
  364. static u32 sqlite3BITVEC_SZ(){
  365. return BITVEC_SZ;
  366. }
  367. //SQ_OPT_STRING_STRLEN();
  368. static const SQChar SQ_LIBNAME[] = _SC("BitVector");
  369. static const SQChar BitVector_Tag[] = _SC("BitVector_TAG");
  370. #define GET_BitVector_INSTANCE() SQ_GET_INSTANCE(v, 1, Bitvec, BitVector_Tag) \
  371. if(self == NULL) return sq_throwerror(v, _SC("BitVector object already closed"));
  372. static SQRESULT BitVector_release_hook(SQUserPointer p, SQInteger size, void */*ep*/)
  373. {
  374. Bitvec *self = (Bitvec*)p;
  375. if(self) sqlite3BitvecDestroy(self);
  376. return 0;
  377. }
  378. /*
  379. static SQRESULT BitVector_free(HSQUIRRELVM v)
  380. {
  381. SQ_FUNC_VARS_NO_TOP(v);
  382. GET_BitVector_INSTANCE();
  383. BitVector_release_hook(self, 0, v);
  384. sq_setinstanceup(v, 1, 0);
  385. return 0;
  386. }
  387. */
  388. static SQRESULT sq_BitVector_constructor(HSQUIRRELVM v){
  389. SQ_FUNC_VARS_NO_TOP(v);
  390. SQ_GET_INTEGER(v, 2, int_size);
  391. // Bitvec *sqlite3BitvecCreate(u32)
  392. Bitvec *bv = sqlite3BitvecCreate((u32)int_size);
  393. SQInteger rc = sq_setinstanceup(v, 1, bv);
  394. sq_setreleasehook(v,1, BitVector_release_hook);
  395. return rc;
  396. }
  397. static SQRESULT sq_BitVector_clear(HSQUIRRELVM v){
  398. SQ_FUNC_VARS_NO_TOP(v);
  399. GET_BitVector_INSTANCE();
  400. SQ_GET_INTEGER(v, 2, int_pos);
  401. // void sqlite3BitvecClear(Bitvec*, u32, void*)
  402. SQChar *bv_buf = sq_getscratchpad(v, sqlite3BITVEC_SZ());
  403. sqlite3BitvecClear(self, int_pos, bv_buf);
  404. return 0;
  405. }
  406. static SQRESULT sq_BitVector_set(HSQUIRRELVM v){
  407. SQ_FUNC_VARS_NO_TOP(v);
  408. GET_BitVector_INSTANCE();
  409. SQ_GET_INTEGER(v, 2, int_pos);
  410. // int sqlite3BitvecSet(Bitvec*, u32)
  411. sq_pushinteger(v, sqlite3BitvecSet(self, (u32)int_pos));
  412. return 1;
  413. }
  414. static SQRESULT sq_BitVector_size(HSQUIRRELVM v){
  415. SQ_FUNC_VARS_NO_TOP(v);
  416. GET_BitVector_INSTANCE();
  417. // u32 sqlite3BitvecSize(Bitvec*)
  418. sq_pushinteger(v, sqlite3BitvecSize(self));
  419. return 1;
  420. }
  421. static SQRESULT sq_BitVector_test(HSQUIRRELVM v){
  422. SQ_FUNC_VARS_NO_TOP(v);
  423. GET_BitVector_INSTANCE();
  424. SQ_GET_INTEGER(v, 2, int_pos);
  425. // int sqlite3BitvecTest(Bitvec*, u32)
  426. sq_pushinteger(v, sqlite3BitvecTest(self, (u32)int_pos));
  427. return 1;
  428. }
  429. static SQRESULT sq_BitVector_test_not_null(HSQUIRRELVM v){
  430. SQ_FUNC_VARS_NO_TOP(v);
  431. GET_BitVector_INSTANCE();
  432. SQ_GET_INTEGER(v, 2, int_pos);
  433. // int sqlite3BitvecTestNotNull(Bitvec*, u32)
  434. sq_pushinteger(v, sqlite3BitvecTestNotNull(self, (u32)int_pos));
  435. return 1;
  436. }
  437. #define _DECL_BITVECTOR_FUNC(name,nparams,pmask) {_SC(#name),sq_BitVector_##name,nparams,pmask}
  438. static SQRegFunction BitVector_obj_funcs[]={
  439. _DECL_BITVECTOR_FUNC(constructor, 2, _SC("xi")),
  440. _DECL_BITVECTOR_FUNC(clear, 2, _SC("xi")),
  441. _DECL_BITVECTOR_FUNC(set, 2, _SC("xi")),
  442. _DECL_BITVECTOR_FUNC(size, 1, _SC("x")),
  443. _DECL_BITVECTOR_FUNC(test, 2, _SC("xi")),
  444. _DECL_BITVECTOR_FUNC(test_not_null, 2, _SC("xi")),
  445. {0,0}
  446. };
  447. #undef _DECL_BITVECTOR_FUNC
  448. extern "C" {
  449. /* This defines a function that opens up your library. */
  450. SQRESULT sqext_register_BitVector (HSQUIRRELVM v) {
  451. //add a namespace BitVector
  452. sq_pushstring(v, SQ_LIBNAME, -1);
  453. sq_newclass(v,SQFalse);
  454. sq_settypetag(v,-1,(SQUserPointer)BitVector_Tag);
  455. sq_insert_reg_funcs(v, BitVector_obj_funcs);
  456. sq_newslot(v,-3,SQFalse); //add BitVector table to the root table
  457. return SQ_OK;
  458. }
  459. }
  460. #endif //USE_BITVECTOR