]>
git.saurik.com Git - redis.git/blob - src/intset.c
7 /* Note that these encodings are ordered, so:
8 * INTSET_ENC_INT16 < INTSET_ENC_INT32 < INTSET_ENC_INT64. */
9 #define INTSET_ENC_INT16 (sizeof(int16_t))
10 #define INTSET_ENC_INT32 (sizeof(int32_t))
11 #define INTSET_ENC_INT64 (sizeof(int64_t))
13 /* Return the required encoding for the provided value. */
14 static uint8_t _intsetValueEncoding(int64_t v
) {
15 if (v
< INT32_MIN
|| v
> INT32_MAX
)
16 return INTSET_ENC_INT64
;
17 else if (v
< INT16_MIN
|| v
> INT16_MAX
)
18 return INTSET_ENC_INT32
;
19 return INTSET_ENC_INT16
;
22 /* Return the value at pos, given an encoding. */
23 static int64_t _intsetGetEncoded(intset
*is
, int pos
, uint8_t enc
) {
24 if (enc
== INTSET_ENC_INT64
)
25 return ((int64_t*)is
->contents
)[pos
];
26 else if (enc
== INTSET_ENC_INT32
)
27 return ((int32_t*)is
->contents
)[pos
];
28 return ((int16_t*)is
->contents
)[pos
];
31 /* Return the value at pos, using the configured encoding. */
32 static int64_t _intsetGet(intset
*is
, int pos
) {
33 return _intsetGetEncoded(is
,pos
,is
->encoding
);
36 /* Set the value at pos, using the configured encoding. */
37 static void _intsetSet(intset
*is
, int pos
, int64_t value
) {
38 if (is
->encoding
== INTSET_ENC_INT64
)
39 ((int64_t*)is
->contents
)[pos
] = value
;
40 else if (is
->encoding
== INTSET_ENC_INT32
)
41 ((int32_t*)is
->contents
)[pos
] = value
;
43 ((int16_t*)is
->contents
)[pos
] = value
;
46 /* Create an empty intset. */
47 intset
*intsetNew(void) {
48 intset
*is
= zmalloc(sizeof(intset
));
49 is
->encoding
= INTSET_ENC_INT16
;
54 /* Resize the intset */
55 static intset
*intsetResize(intset
*is
, uint32_t len
) {
56 uint32_t size
= len
*is
->encoding
;
57 is
= zrealloc(is
,sizeof(intset
)+size
);
61 /* Search for the position of "value". Return 1 when the value was found and
62 * sets "pos" to the position of the value within the intset. Return 0 when
63 * the value is not present in the intset and sets "pos" to the position
64 * where "value" can be inserted. */
65 static uint8_t intsetSearch(intset
*is
, int64_t value
, uint32_t *pos
) {
66 int min
= 0, max
= is
->length
-1, mid
= -1;
69 /* The value can never be found when the set is empty */
70 if (is
->length
== 0) {
74 /* Check for the case where we know we cannot find the value,
75 * but do know the insert position. */
76 if (value
> _intsetGet(is
,is
->length
-1)) {
77 if (pos
) *pos
= is
->length
;
79 } else if (value
< _intsetGet(is
,0)) {
87 cur
= _intsetGet(is
,mid
);
90 } else if (value
< cur
) {
106 /* Upgrades the intset to a larger encoding and inserts the given integer. */
107 static intset
*intsetUpgradeAndAdd(intset
*is
, int64_t value
) {
108 uint8_t curenc
= is
->encoding
;
109 uint8_t newenc
= _intsetValueEncoding(value
);
110 int length
= is
->length
;
111 int prepend
= value
< 0 ? 1 : 0;
113 /* First set new encoding and resize */
114 is
->encoding
= newenc
;
115 is
= intsetResize(is
,is
->length
+1);
117 /* Upgrade back-to-front so we don't overwrite values.
118 * Note that the "prepend" variable is used to make sure we have an empty
119 * space at either the beginning or the end of the intset. */
121 _intsetSet(is
,length
+prepend
,_intsetGetEncoded(is
,length
,curenc
));
123 /* Set the value at the beginning or the end. */
125 _intsetSet(is
,0,value
);
127 _intsetSet(is
,is
->length
,value
);
132 static void intsetMoveTail(intset
*is
, uint32_t from
, uint32_t to
) {
134 uint32_t bytes
= is
->length
-from
;
135 if (is
->encoding
== INTSET_ENC_INT64
) {
136 src
= (int64_t*)is
->contents
+from
;
137 dst
= (int64_t*)is
->contents
+to
;
138 bytes
*= sizeof(int64_t);
139 } else if (is
->encoding
== INTSET_ENC_INT32
) {
140 src
= (int32_t*)is
->contents
+from
;
141 dst
= (int32_t*)is
->contents
+to
;
142 bytes
*= sizeof(int32_t);
144 src
= (int16_t*)is
->contents
+from
;
145 dst
= (int16_t*)is
->contents
+to
;
146 bytes
*= sizeof(int16_t);
148 memmove(dst
,src
,bytes
);
151 /* Insert an integer in the intset */
152 intset
*intsetAdd(intset
*is
, int64_t value
, uint8_t *success
) {
153 uint8_t valenc
= _intsetValueEncoding(value
);
155 if (success
) *success
= 1;
157 /* Upgrade encoding if necessary. If we need to upgrade, we know that
158 * this value should be either appended (if > 0) or prepended (if < 0),
159 * because it lies outside the range of existing values. */
160 if (valenc
> is
->encoding
) {
161 /* This always succeeds, so we don't need to curry *success. */
162 return intsetUpgradeAndAdd(is
,value
);
164 /* Abort if the value is already present in the set.
165 * This call will populate "pos" with the right position to insert
166 * the value when it cannot be found. */
167 if (intsetSearch(is
,value
,&pos
)) {
168 if (success
) *success
= 0;
172 is
= intsetResize(is
,is
->length
+1);
173 if (pos
< is
->length
) intsetMoveTail(is
,pos
,pos
+1);
176 _intsetSet(is
,pos
,value
);
181 /* Delete integer from intset */
182 intset
*intsetRemove(intset
*is
, int64_t value
, int *success
) {
183 uint8_t valenc
= _intsetValueEncoding(value
);
185 if (success
) *success
= 0;
187 if (valenc
<= is
->encoding
&& intsetSearch(is
,value
,&pos
)) {
188 /* We know we can delete */
189 if (success
) *success
= 1;
191 /* Overwrite value with tail and update length */
192 if (pos
< (is
->length
-1)) intsetMoveTail(is
,pos
+1,pos
);
193 is
= intsetResize(is
,is
->length
-1);
199 /* Determine whether a value belongs to this set */
200 uint8_t intsetFind(intset
*is
, int64_t value
) {
201 uint8_t valenc
= _intsetValueEncoding(value
);
202 return valenc
<= is
->encoding
&& intsetSearch(is
,value
,NULL
);
205 /* Return random member */
206 int64_t intsetRandom(intset
*is
) {
207 return _intsetGet(is
,rand()%is
->length
);
210 /* Sets the value to the value at the given position. When this position is
211 * out of range the function returns 0, when in range it returns 1. */
212 uint8_t intsetGet(intset
*is
, uint32_t pos
, int64_t *value
) {
213 if (pos
< is
->length
) {
214 *value
= _intsetGet(is
,pos
);
220 /* Return intset length */
221 uint32_t intsetLen(intset
*is
) {
225 /* Return intset blob size in bytes. */
226 size_t intsetBlobLen(intset
*is
) {
227 return sizeof(intset
)+is
->length
*is
->encoding
;
230 #ifdef INTSET_TEST_MAIN
231 #include <sys/time.h>
233 void intsetRepr(intset
*is
) {
235 for (i
= 0; i
< is
->length
; i
++) {
236 printf("%lld\n", (uint64_t)_intsetGet(is
,i
));
241 void error(char *err
) {
250 long long usec(void) {
252 gettimeofday(&tv
,NULL
);
253 return (((long long)tv
.tv_sec
)*1000000)+tv
.tv_usec
;
256 #define assert(_e) ((_e)?(void)0:(_assert(#_e,__FILE__,__LINE__),exit(1)))
257 void _assert(char *estr
, char *file
, int line
) {
258 printf("\n\n=== ASSERTION FAILED ===\n");
259 printf("==> %s:%d '%s' is not true\n",file
,line
,estr
);
262 intset
*createSet(int bits
, int size
) {
263 uint64_t mask
= (1<<bits
)-1;
265 intset
*is
= intsetNew();
267 for (i
= 0; i
< size
; i
++) {
269 value
= (rand()*rand()) & mask
;
271 value
= rand() & mask
;
273 is
= intsetAdd(is
,value
,NULL
);
278 void checkConsistency(intset
*is
) {
281 for (i
= 0; i
< (is
->length
-1); i
++) {
282 if (is
->encoding
== INTSET_ENC_INT16
) {
283 int16_t *i16
= (int16_t*)is
->contents
;
284 assert(i16
[i
] < i16
[i
+1]);
285 } else if (is
->encoding
== INTSET_ENC_INT32
) {
286 int32_t *i32
= (int32_t*)is
->contents
;
287 assert(i32
[i
] < i32
[i
+1]);
289 int64_t *i64
= (int64_t*)is
->contents
;
290 assert(i64
[i
] < i64
[i
+1]);
295 int main(int argc
, char **argv
) {
301 printf("Value encodings: "); {
302 assert(_intsetValueEncoding(-32768) == INTSET_ENC_INT16
);
303 assert(_intsetValueEncoding(+32767) == INTSET_ENC_INT16
);
304 assert(_intsetValueEncoding(-32769) == INTSET_ENC_INT32
);
305 assert(_intsetValueEncoding(+32768) == INTSET_ENC_INT32
);
306 assert(_intsetValueEncoding(-2147483648) == INTSET_ENC_INT32
);
307 assert(_intsetValueEncoding(+2147483647) == INTSET_ENC_INT32
);
308 assert(_intsetValueEncoding(-2147483649) == INTSET_ENC_INT64
);
309 assert(_intsetValueEncoding(+2147483648) == INTSET_ENC_INT64
);
310 assert(_intsetValueEncoding(-9223372036854775808ull) == INTSET_ENC_INT64
);
311 assert(_intsetValueEncoding(+9223372036854775807ull) == INTSET_ENC_INT64
);
315 printf("Basic adding: "); {
317 is
= intsetAdd(is
,5,&success
); assert(success
);
318 is
= intsetAdd(is
,6,&success
); assert(success
);
319 is
= intsetAdd(is
,4,&success
); assert(success
);
320 is
= intsetAdd(is
,4,&success
); assert(!success
);
324 printf("Large number of random adds: "); {
327 for (i
= 0; i
< 1024; i
++) {
328 is
= intsetAdd(is
,rand()%0x800
,&success
);
329 if (success
) inserts
++;
331 assert(is
->length
== inserts
);
332 checkConsistency(is
);
336 printf("Upgrade from int16 to int32: "); {
338 is
= intsetAdd(is
,32,NULL
);
339 assert(is
->encoding
== INTSET_ENC_INT16
);
340 is
= intsetAdd(is
,65535,NULL
);
341 assert(is
->encoding
== INTSET_ENC_INT32
);
342 assert(intsetFind(is
,32));
343 assert(intsetFind(is
,65535));
344 checkConsistency(is
);
347 is
= intsetAdd(is
,32,NULL
);
348 assert(is
->encoding
== INTSET_ENC_INT16
);
349 is
= intsetAdd(is
,-65535,NULL
);
350 assert(is
->encoding
== INTSET_ENC_INT32
);
351 assert(intsetFind(is
,32));
352 assert(intsetFind(is
,-65535));
353 checkConsistency(is
);
357 printf("Upgrade from int16 to int64: "); {
359 is
= intsetAdd(is
,32,NULL
);
360 assert(is
->encoding
== INTSET_ENC_INT16
);
361 is
= intsetAdd(is
,4294967295,NULL
);
362 assert(is
->encoding
== INTSET_ENC_INT64
);
363 assert(intsetFind(is
,32));
364 assert(intsetFind(is
,4294967295));
365 checkConsistency(is
);
368 is
= intsetAdd(is
,32,NULL
);
369 assert(is
->encoding
== INTSET_ENC_INT16
);
370 is
= intsetAdd(is
,-4294967295,NULL
);
371 assert(is
->encoding
== INTSET_ENC_INT64
);
372 assert(intsetFind(is
,32));
373 assert(intsetFind(is
,-4294967295));
374 checkConsistency(is
);
378 printf("Upgrade from int32 to int64: "); {
380 is
= intsetAdd(is
,65535,NULL
);
381 assert(is
->encoding
== INTSET_ENC_INT32
);
382 is
= intsetAdd(is
,4294967295,NULL
);
383 assert(is
->encoding
== INTSET_ENC_INT64
);
384 assert(intsetFind(is
,65535));
385 assert(intsetFind(is
,4294967295));
386 checkConsistency(is
);
389 is
= intsetAdd(is
,65535,NULL
);
390 assert(is
->encoding
== INTSET_ENC_INT32
);
391 is
= intsetAdd(is
,-4294967295,NULL
);
392 assert(is
->encoding
== INTSET_ENC_INT64
);
393 assert(intsetFind(is
,65535));
394 assert(intsetFind(is
,-4294967295));
395 checkConsistency(is
);
399 printf("Stress lookups: "); {
400 long num
= 100000, size
= 10000;
403 is
= createSet(bits
,size
);
404 checkConsistency(is
);
407 for (i
= 0; i
< num
; i
++) intsetSearch(is
,rand() % ((1<<bits
)-1),NULL
);
408 printf("%ld lookups, %ld element set, %lldusec\n",num
,size
,usec()-start
);
411 printf("Stress add+delete: "); {
414 for (i
= 0; i
< 0xffff; i
++) {
416 is
= intsetAdd(is
,v1
,NULL
);
417 assert(intsetFind(is
,v1
));
420 is
= intsetRemove(is
,v2
,NULL
);
421 assert(!intsetFind(is
,v2
));
423 checkConsistency(is
);