OLD | NEW |
1 /* | 1 /* |
2 ** 2008 February 16 | 2 ** 2008 February 16 |
3 ** | 3 ** |
4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
6 ** | 6 ** |
7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
10 ** | 10 ** |
(...skipping 23 matching lines...) Expand all Loading... |
34 ** start of a transaction, and is thus usually less than a few thousand, | 34 ** start of a transaction, and is thus usually less than a few thousand, |
35 ** but can be as large as 2 billion for a really big database. | 35 ** but can be as large as 2 billion for a really big database. |
36 */ | 36 */ |
37 #include "sqliteInt.h" | 37 #include "sqliteInt.h" |
38 | 38 |
39 /* Size of the Bitvec structure in bytes. */ | 39 /* Size of the Bitvec structure in bytes. */ |
40 #define BITVEC_SZ 512 | 40 #define BITVEC_SZ 512 |
41 | 41 |
42 /* Round the union size down to the nearest pointer boundary, since that's how | 42 /* Round the union size down to the nearest pointer boundary, since that's how |
43 ** it will be aligned within the Bitvec struct. */ | 43 ** it will be aligned within the Bitvec struct. */ |
44 #define BITVEC_USIZE (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(B
itvec*)) | 44 #define BITVEC_USIZE \ |
| 45 (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*)) |
45 | 46 |
46 /* Type of the array "element" for the bitmap representation. | 47 /* Type of the array "element" for the bitmap representation. |
47 ** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE. | 48 ** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE. |
48 ** Setting this to the "natural word" size of your CPU may improve | 49 ** Setting this to the "natural word" size of your CPU may improve |
49 ** performance. */ | 50 ** performance. */ |
50 #define BITVEC_TELEM u8 | 51 #define BITVEC_TELEM u8 |
51 /* Size, in bits, of the bitmap element. */ | 52 /* Size, in bits, of the bitmap element. */ |
52 #define BITVEC_SZELEM 8 | 53 #define BITVEC_SZELEM 8 |
53 /* Number of elements in a bitmap array. */ | 54 /* Number of elements in a bitmap array. */ |
54 #define BITVEC_NELEM (BITVEC_USIZE/sizeof(BITVEC_TELEM)) | 55 #define BITVEC_NELEM (BITVEC_USIZE/sizeof(BITVEC_TELEM)) |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 p->iSize = iSize; | 120 p->iSize = iSize; |
120 } | 121 } |
121 return p; | 122 return p; |
122 } | 123 } |
123 | 124 |
124 /* | 125 /* |
125 ** Check to see if the i-th bit is set. Return true or false. | 126 ** Check to see if the i-th bit is set. Return true or false. |
126 ** If p is NULL (if the bitmap has not been created) or if | 127 ** If p is NULL (if the bitmap has not been created) or if |
127 ** i is out of range, then return false. | 128 ** i is out of range, then return false. |
128 */ | 129 */ |
129 int sqlite3BitvecTest(Bitvec *p, u32 i){ | 130 int sqlite3BitvecTestNotNull(Bitvec *p, u32 i){ |
130 if( p==0 ) return 0; | 131 assert( p!=0 ); |
131 if( i>p->iSize || i==0 ) return 0; | |
132 i--; | 132 i--; |
| 133 if( i>=p->iSize ) return 0; |
133 while( p->iDivisor ){ | 134 while( p->iDivisor ){ |
134 u32 bin = i/p->iDivisor; | 135 u32 bin = i/p->iDivisor; |
135 i = i%p->iDivisor; | 136 i = i%p->iDivisor; |
136 p = p->u.apSub[bin]; | 137 p = p->u.apSub[bin]; |
137 if (!p) { | 138 if (!p) { |
138 return 0; | 139 return 0; |
139 } | 140 } |
140 } | 141 } |
141 if( p->iSize<=BITVEC_NBIT ){ | 142 if( p->iSize<=BITVEC_NBIT ){ |
142 return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0; | 143 return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0; |
143 } else{ | 144 } else{ |
144 u32 h = BITVEC_HASH(i++); | 145 u32 h = BITVEC_HASH(i++); |
145 while( p->u.aHash[h] ){ | 146 while( p->u.aHash[h] ){ |
146 if( p->u.aHash[h]==i ) return 1; | 147 if( p->u.aHash[h]==i ) return 1; |
147 h = (h+1) % BITVEC_NINT; | 148 h = (h+1) % BITVEC_NINT; |
148 } | 149 } |
149 return 0; | 150 return 0; |
150 } | 151 } |
151 } | 152 } |
| 153 int sqlite3BitvecTest(Bitvec *p, u32 i){ |
| 154 return p!=0 && sqlite3BitvecTestNotNull(p,i); |
| 155 } |
152 | 156 |
153 /* | 157 /* |
154 ** Set the i-th bit. Return 0 on success and an error code if | 158 ** Set the i-th bit. Return 0 on success and an error code if |
155 ** anything goes wrong. | 159 ** anything goes wrong. |
156 ** | 160 ** |
157 ** This routine might cause sub-bitmaps to be allocated. Failing | 161 ** This routine might cause sub-bitmaps to be allocated. Failing |
158 ** to get the memory needed to hold the sub-bitmap is the only | 162 ** to get the memory needed to hold the sub-bitmap is the only |
159 ** that can go wrong with an insert, assuming p and i are valid. | 163 ** that can go wrong with an insert, assuming p and i are valid. |
160 ** | 164 ** |
161 ** The calling function must ensure that p is a valid Bitvec object | 165 ** The calling function must ensure that p is a valid Bitvec object |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 Bitvec *pBitvec = 0; | 338 Bitvec *pBitvec = 0; |
335 unsigned char *pV = 0; | 339 unsigned char *pV = 0; |
336 int rc = -1; | 340 int rc = -1; |
337 int i, nx, pc, op; | 341 int i, nx, pc, op; |
338 void *pTmpSpace; | 342 void *pTmpSpace; |
339 | 343 |
340 /* Allocate the Bitvec to be tested and a linear array of | 344 /* Allocate the Bitvec to be tested and a linear array of |
341 ** bits to act as the reference */ | 345 ** bits to act as the reference */ |
342 pBitvec = sqlite3BitvecCreate( sz ); | 346 pBitvec = sqlite3BitvecCreate( sz ); |
343 pV = sqlite3MallocZero( (sz+7)/8 + 1 ); | 347 pV = sqlite3MallocZero( (sz+7)/8 + 1 ); |
344 pTmpSpace = sqlite3_malloc(BITVEC_SZ); | 348 pTmpSpace = sqlite3_malloc64(BITVEC_SZ); |
345 if( pBitvec==0 || pV==0 || pTmpSpace==0 ) goto bitvec_end; | 349 if( pBitvec==0 || pV==0 || pTmpSpace==0 ) goto bitvec_end; |
346 | 350 |
347 /* NULL pBitvec tests */ | 351 /* NULL pBitvec tests */ |
348 sqlite3BitvecSet(0, 1); | 352 sqlite3BitvecSet(0, 1); |
349 sqlite3BitvecClear(0, 1, pTmpSpace); | 353 sqlite3BitvecClear(0, 1, pTmpSpace); |
350 | 354 |
351 /* Run the program */ | 355 /* Run the program */ |
352 pc = 0; | 356 pc = 0; |
353 while( (op = aOp[pc])!=0 ){ | 357 while( (op = aOp[pc])!=0 ){ |
354 switch( op ){ | 358 switch( op ){ |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 } | 402 } |
399 | 403 |
400 /* Free allocated structure */ | 404 /* Free allocated structure */ |
401 bitvec_end: | 405 bitvec_end: |
402 sqlite3_free(pTmpSpace); | 406 sqlite3_free(pTmpSpace); |
403 sqlite3_free(pV); | 407 sqlite3_free(pV); |
404 sqlite3BitvecDestroy(pBitvec); | 408 sqlite3BitvecDestroy(pBitvec); |
405 return rc; | 409 return rc; |
406 } | 410 } |
407 #endif /* SQLITE_OMIT_BUILTIN_TEST */ | 411 #endif /* SQLITE_OMIT_BUILTIN_TEST */ |
OLD | NEW |