sq_bitvector.cpp 15 KB

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