xref: /redis-3.2.3/src/intset.c (revision 9feee428)
1 /*
2  * Copyright (c) 2009-2012, Pieter Noordhuis <pcnoordhuis at gmail dot com>
3  * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *   * Redistributions of source code must retain the above copyright notice,
10  *     this list of conditions and the following disclaimer.
11  *   * Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  *   * Neither the name of Redis nor the names of its contributors may be used
15  *     to endorse or promote products derived from this software without
16  *     specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include "intset.h"
35 #include "zmalloc.h"
36 #include "endianconv.h"
37 
38 /* Note that these encodings are ordered, so:
39  * INTSET_ENC_INT16 < INTSET_ENC_INT32 < INTSET_ENC_INT64. */
40 #define INTSET_ENC_INT16 (sizeof(int16_t))
41 #define INTSET_ENC_INT32 (sizeof(int32_t))
42 #define INTSET_ENC_INT64 (sizeof(int64_t))
43 
44 /* Return the required encoding for the provided value. */
_intsetValueEncoding(int64_t v)45 static uint8_t _intsetValueEncoding(int64_t v) {
46     if (v < INT32_MIN || v > INT32_MAX)
47         return INTSET_ENC_INT64;
48     else if (v < INT16_MIN || v > INT16_MAX)
49         return INTSET_ENC_INT32;
50     else
51         return INTSET_ENC_INT16;
52 }
53 
54 /* Return the value at pos, given an encoding. */
_intsetGetEncoded(intset * is,int pos,uint8_t enc)55 static int64_t _intsetGetEncoded(intset *is, int pos, uint8_t enc) {
56     int64_t v64;
57     int32_t v32;
58     int16_t v16;
59 
60     if (enc == INTSET_ENC_INT64) {
61         memcpy(&v64,((int64_t*)is->contents)+pos,sizeof(v64));
62         memrev64ifbe(&v64);
63         return v64;
64     } else if (enc == INTSET_ENC_INT32) {
65         memcpy(&v32,((int32_t*)is->contents)+pos,sizeof(v32));
66         memrev32ifbe(&v32);
67         return v32;
68     } else {
69         memcpy(&v16,((int16_t*)is->contents)+pos,sizeof(v16));
70         memrev16ifbe(&v16);
71         return v16;
72     }
73 }
74 
75 /* Return the value at pos, using the configured encoding. */
_intsetGet(intset * is,int pos)76 static int64_t _intsetGet(intset *is, int pos) {
77     return _intsetGetEncoded(is,pos,intrev32ifbe(is->encoding));
78 }
79 
80 /* Set the value at pos, using the configured encoding. */
_intsetSet(intset * is,int pos,int64_t value)81 static void _intsetSet(intset *is, int pos, int64_t value) {
82     uint32_t encoding = intrev32ifbe(is->encoding);
83 
84     if (encoding == INTSET_ENC_INT64) {
85         ((int64_t*)is->contents)[pos] = value;
86         memrev64ifbe(((int64_t*)is->contents)+pos);
87     } else if (encoding == INTSET_ENC_INT32) {
88         ((int32_t*)is->contents)[pos] = value;
89         memrev32ifbe(((int32_t*)is->contents)+pos);
90     } else {
91         ((int16_t*)is->contents)[pos] = value;
92         memrev16ifbe(((int16_t*)is->contents)+pos);
93     }
94 }
95 
96 /* Create an empty intset. */
intsetNew(void)97 intset *intsetNew(void) {
98     intset *is = zmalloc(sizeof(intset));
99     is->encoding = intrev32ifbe(INTSET_ENC_INT16);
100     is->length = 0;
101     return is;
102 }
103 
104 /* Resize the intset */
intsetResize(intset * is,uint32_t len)105 static intset *intsetResize(intset *is, uint32_t len) {
106     uint32_t size = len*intrev32ifbe(is->encoding);
107     is = zrealloc(is,sizeof(intset)+size);
108     return is;
109 }
110 
111 /* Search for the position of "value". Return 1 when the value was found and
112  * sets "pos" to the position of the value within the intset. Return 0 when
113  * the value is not present in the intset and sets "pos" to the position
114  * where "value" can be inserted. */
intsetSearch(intset * is,int64_t value,uint32_t * pos)115 static uint8_t intsetSearch(intset *is, int64_t value, uint32_t *pos) {
116     int min = 0, max = intrev32ifbe(is->length)-1, mid = -1;
117     int64_t cur = -1;
118 
119     /* The value can never be found when the set is empty */
120     if (intrev32ifbe(is->length) == 0) {
121         if (pos) *pos = 0;
122         return 0;
123     } else {
124         /* Check for the case where we know we cannot find the value,
125          * but do know the insert position. */
126         if (value > _intsetGet(is,intrev32ifbe(is->length)-1)) {
127             if (pos) *pos = intrev32ifbe(is->length);
128             return 0;
129         } else if (value < _intsetGet(is,0)) {
130             if (pos) *pos = 0;
131             return 0;
132         }
133     }
134 
135     while(max >= min) {
136         mid = ((unsigned int)min + (unsigned int)max) >> 1;
137         cur = _intsetGet(is,mid);
138         if (value > cur) {
139             min = mid+1;
140         } else if (value < cur) {
141             max = mid-1;
142         } else {
143             break;
144         }
145     }
146 
147     if (value == cur) {
148         if (pos) *pos = mid;
149         return 1;
150     } else {
151         if (pos) *pos = min;
152         return 0;
153     }
154 }
155 
156 /* Upgrades the intset to a larger encoding and inserts the given integer. */
intsetUpgradeAndAdd(intset * is,int64_t value)157 static intset *intsetUpgradeAndAdd(intset *is, int64_t value) {
158     uint8_t curenc = intrev32ifbe(is->encoding);
159     uint8_t newenc = _intsetValueEncoding(value);
160     int length = intrev32ifbe(is->length);
161     int prepend = value < 0 ? 1 : 0;
162 
163     /* First set new encoding and resize */
164     is->encoding = intrev32ifbe(newenc);
165     is = intsetResize(is,intrev32ifbe(is->length)+1);
166 
167     /* Upgrade back-to-front so we don't overwrite values.
168      * Note that the "prepend" variable is used to make sure we have an empty
169      * space at either the beginning or the end of the intset. */
170     while(length--)
171         _intsetSet(is,length+prepend,_intsetGetEncoded(is,length,curenc));
172 
173     /* Set the value at the beginning or the end. */
174     if (prepend)
175         _intsetSet(is,0,value);
176     else
177         _intsetSet(is,intrev32ifbe(is->length),value);
178     is->length = intrev32ifbe(intrev32ifbe(is->length)+1);
179     return is;
180 }
181 
intsetMoveTail(intset * is,uint32_t from,uint32_t to)182 static void intsetMoveTail(intset *is, uint32_t from, uint32_t to) {
183     void *src, *dst;
184     uint32_t bytes = intrev32ifbe(is->length)-from;
185     uint32_t encoding = intrev32ifbe(is->encoding);
186 
187     if (encoding == INTSET_ENC_INT64) {
188         src = (int64_t*)is->contents+from;
189         dst = (int64_t*)is->contents+to;
190         bytes *= sizeof(int64_t);
191     } else if (encoding == INTSET_ENC_INT32) {
192         src = (int32_t*)is->contents+from;
193         dst = (int32_t*)is->contents+to;
194         bytes *= sizeof(int32_t);
195     } else {
196         src = (int16_t*)is->contents+from;
197         dst = (int16_t*)is->contents+to;
198         bytes *= sizeof(int16_t);
199     }
200     memmove(dst,src,bytes);
201 }
202 
203 /* Insert an integer in the intset */
intsetAdd(intset * is,int64_t value,uint8_t * success)204 intset *intsetAdd(intset *is, int64_t value, uint8_t *success) {
205     uint8_t valenc = _intsetValueEncoding(value);
206     uint32_t pos;
207     if (success) *success = 1;
208 
209     /* Upgrade encoding if necessary. If we need to upgrade, we know that
210      * this value should be either appended (if > 0) or prepended (if < 0),
211      * because it lies outside the range of existing values. */
212     if (valenc > intrev32ifbe(is->encoding)) {
213         /* This always succeeds, so we don't need to curry *success. */
214         return intsetUpgradeAndAdd(is,value);
215     } else {
216         /* Abort if the value is already present in the set.
217          * This call will populate "pos" with the right position to insert
218          * the value when it cannot be found. */
219         if (intsetSearch(is,value,&pos)) {
220             if (success) *success = 0;
221             return is;
222         }
223 
224         is = intsetResize(is,intrev32ifbe(is->length)+1);
225         if (pos < intrev32ifbe(is->length)) intsetMoveTail(is,pos,pos+1);
226     }
227 
228     _intsetSet(is,pos,value);
229     is->length = intrev32ifbe(intrev32ifbe(is->length)+1);
230     return is;
231 }
232 
233 /* Delete integer from intset */
intsetRemove(intset * is,int64_t value,int * success)234 intset *intsetRemove(intset *is, int64_t value, int *success) {
235     uint8_t valenc = _intsetValueEncoding(value);
236     uint32_t pos;
237     if (success) *success = 0;
238 
239     if (valenc <= intrev32ifbe(is->encoding) && intsetSearch(is,value,&pos)) {
240         uint32_t len = intrev32ifbe(is->length);
241 
242         /* We know we can delete */
243         if (success) *success = 1;
244 
245         /* Overwrite value with tail and update length */
246         if (pos < (len-1)) intsetMoveTail(is,pos+1,pos);
247         is = intsetResize(is,len-1);
248         is->length = intrev32ifbe(len-1);
249     }
250     return is;
251 }
252 
253 /* Determine whether a value belongs to this set */
intsetFind(intset * is,int64_t value)254 uint8_t intsetFind(intset *is, int64_t value) {
255     uint8_t valenc = _intsetValueEncoding(value);
256     return valenc <= intrev32ifbe(is->encoding) && intsetSearch(is,value,NULL);
257 }
258 
259 /* Return random member */
intsetRandom(intset * is)260 int64_t intsetRandom(intset *is) {
261     return _intsetGet(is,rand()%intrev32ifbe(is->length));
262 }
263 
264 /* Sets the value to the value at the given position. When this position is
265  * out of range the function returns 0, when in range it returns 1. */
intsetGet(intset * is,uint32_t pos,int64_t * value)266 uint8_t intsetGet(intset *is, uint32_t pos, int64_t *value) {
267     if (pos < intrev32ifbe(is->length)) {
268         *value = _intsetGet(is,pos);
269         return 1;
270     }
271     return 0;
272 }
273 
274 /* Return intset length */
intsetLen(intset * is)275 uint32_t intsetLen(intset *is) {
276     return intrev32ifbe(is->length);
277 }
278 
279 /* Return intset blob size in bytes. */
intsetBlobLen(intset * is)280 size_t intsetBlobLen(intset *is) {
281     return sizeof(intset)+intrev32ifbe(is->length)*intrev32ifbe(is->encoding);
282 }
283 
284 #ifdef REDIS_TEST
285 #include <sys/time.h>
286 #include <time.h>
287 
288 #if 0
289 static void intsetRepr(intset *is) {
290     for (uint32_t i = 0; i < intrev32ifbe(is->length); i++) {
291         printf("%lld\n", (uint64_t)_intsetGet(is,i));
292     }
293     printf("\n");
294 }
295 
296 static void error(char *err) {
297     printf("%s\n", err);
298     exit(1);
299 }
300 #endif
301 
ok(void)302 static void ok(void) {
303     printf("OK\n");
304 }
305 
usec(void)306 static long long usec(void) {
307     struct timeval tv;
308     gettimeofday(&tv,NULL);
309     return (((long long)tv.tv_sec)*1000000)+tv.tv_usec;
310 }
311 
312 #define assert(_e) ((_e)?(void)0:(_assert(#_e,__FILE__,__LINE__),exit(1)))
_assert(char * estr,char * file,int line)313 static void _assert(char *estr, char *file, int line) {
314     printf("\n\n=== ASSERTION FAILED ===\n");
315     printf("==> %s:%d '%s' is not true\n",file,line,estr);
316 }
317 
createSet(int bits,int size)318 static intset *createSet(int bits, int size) {
319     uint64_t mask = (1<<bits)-1;
320     uint64_t value;
321     intset *is = intsetNew();
322 
323     for (int i = 0; i < size; i++) {
324         if (bits > 32) {
325             value = (rand()*rand()) & mask;
326         } else {
327             value = rand() & mask;
328         }
329         is = intsetAdd(is,value,NULL);
330     }
331     return is;
332 }
333 
checkConsistency(intset * is)334 static void checkConsistency(intset *is) {
335     for (uint32_t i = 0; i < (intrev32ifbe(is->length)-1); i++) {
336         uint32_t encoding = intrev32ifbe(is->encoding);
337 
338         if (encoding == INTSET_ENC_INT16) {
339             int16_t *i16 = (int16_t*)is->contents;
340             assert(i16[i] < i16[i+1]);
341         } else if (encoding == INTSET_ENC_INT32) {
342             int32_t *i32 = (int32_t*)is->contents;
343             assert(i32[i] < i32[i+1]);
344         } else {
345             int64_t *i64 = (int64_t*)is->contents;
346             assert(i64[i] < i64[i+1]);
347         }
348     }
349 }
350 
351 #define UNUSED(x) (void)(x)
intsetTest(int argc,char ** argv)352 int intsetTest(int argc, char **argv) {
353     uint8_t success;
354     int i;
355     intset *is;
356     srand(time(NULL));
357 
358     UNUSED(argc);
359     UNUSED(argv);
360 
361     printf("Value encodings: "); {
362         assert(_intsetValueEncoding(-32768) == INTSET_ENC_INT16);
363         assert(_intsetValueEncoding(+32767) == INTSET_ENC_INT16);
364         assert(_intsetValueEncoding(-32769) == INTSET_ENC_INT32);
365         assert(_intsetValueEncoding(+32768) == INTSET_ENC_INT32);
366         assert(_intsetValueEncoding(-2147483648) == INTSET_ENC_INT32);
367         assert(_intsetValueEncoding(+2147483647) == INTSET_ENC_INT32);
368         assert(_intsetValueEncoding(-2147483649) == INTSET_ENC_INT64);
369         assert(_intsetValueEncoding(+2147483648) == INTSET_ENC_INT64);
370         assert(_intsetValueEncoding(-9223372036854775808ull) ==
371                     INTSET_ENC_INT64);
372         assert(_intsetValueEncoding(+9223372036854775807ull) ==
373                     INTSET_ENC_INT64);
374         ok();
375     }
376 
377     printf("Basic adding: "); {
378         is = intsetNew();
379         is = intsetAdd(is,5,&success); assert(success);
380         is = intsetAdd(is,6,&success); assert(success);
381         is = intsetAdd(is,4,&success); assert(success);
382         is = intsetAdd(is,4,&success); assert(!success);
383         ok();
384     }
385 
386     printf("Large number of random adds: "); {
387         uint32_t inserts = 0;
388         is = intsetNew();
389         for (i = 0; i < 1024; i++) {
390             is = intsetAdd(is,rand()%0x800,&success);
391             if (success) inserts++;
392         }
393         assert(intrev32ifbe(is->length) == inserts);
394         checkConsistency(is);
395         ok();
396     }
397 
398     printf("Upgrade from int16 to int32: "); {
399         is = intsetNew();
400         is = intsetAdd(is,32,NULL);
401         assert(intrev32ifbe(is->encoding) == INTSET_ENC_INT16);
402         is = intsetAdd(is,65535,NULL);
403         assert(intrev32ifbe(is->encoding) == INTSET_ENC_INT32);
404         assert(intsetFind(is,32));
405         assert(intsetFind(is,65535));
406         checkConsistency(is);
407 
408         is = intsetNew();
409         is = intsetAdd(is,32,NULL);
410         assert(intrev32ifbe(is->encoding) == INTSET_ENC_INT16);
411         is = intsetAdd(is,-65535,NULL);
412         assert(intrev32ifbe(is->encoding) == INTSET_ENC_INT32);
413         assert(intsetFind(is,32));
414         assert(intsetFind(is,-65535));
415         checkConsistency(is);
416         ok();
417     }
418 
419     printf("Upgrade from int16 to int64: "); {
420         is = intsetNew();
421         is = intsetAdd(is,32,NULL);
422         assert(intrev32ifbe(is->encoding) == INTSET_ENC_INT16);
423         is = intsetAdd(is,4294967295,NULL);
424         assert(intrev32ifbe(is->encoding) == INTSET_ENC_INT64);
425         assert(intsetFind(is,32));
426         assert(intsetFind(is,4294967295));
427         checkConsistency(is);
428 
429         is = intsetNew();
430         is = intsetAdd(is,32,NULL);
431         assert(intrev32ifbe(is->encoding) == INTSET_ENC_INT16);
432         is = intsetAdd(is,-4294967295,NULL);
433         assert(intrev32ifbe(is->encoding) == INTSET_ENC_INT64);
434         assert(intsetFind(is,32));
435         assert(intsetFind(is,-4294967295));
436         checkConsistency(is);
437         ok();
438     }
439 
440     printf("Upgrade from int32 to int64: "); {
441         is = intsetNew();
442         is = intsetAdd(is,65535,NULL);
443         assert(intrev32ifbe(is->encoding) == INTSET_ENC_INT32);
444         is = intsetAdd(is,4294967295,NULL);
445         assert(intrev32ifbe(is->encoding) == INTSET_ENC_INT64);
446         assert(intsetFind(is,65535));
447         assert(intsetFind(is,4294967295));
448         checkConsistency(is);
449 
450         is = intsetNew();
451         is = intsetAdd(is,65535,NULL);
452         assert(intrev32ifbe(is->encoding) == INTSET_ENC_INT32);
453         is = intsetAdd(is,-4294967295,NULL);
454         assert(intrev32ifbe(is->encoding) == INTSET_ENC_INT64);
455         assert(intsetFind(is,65535));
456         assert(intsetFind(is,-4294967295));
457         checkConsistency(is);
458         ok();
459     }
460 
461     printf("Stress lookups: "); {
462         long num = 100000, size = 10000;
463         int i, bits = 20;
464         long long start;
465         is = createSet(bits,size);
466         checkConsistency(is);
467 
468         start = usec();
469         for (i = 0; i < num; i++) intsetSearch(is,rand() % ((1<<bits)-1),NULL);
470         printf("%ld lookups, %ld element set, %lldusec\n",
471                num,size,usec()-start);
472     }
473 
474     printf("Stress add+delete: "); {
475         int i, v1, v2;
476         is = intsetNew();
477         for (i = 0; i < 0xffff; i++) {
478             v1 = rand() % 0xfff;
479             is = intsetAdd(is,v1,NULL);
480             assert(intsetFind(is,v1));
481 
482             v2 = rand() % 0xfff;
483             is = intsetRemove(is,v2,NULL);
484             assert(!intsetFind(is,v2));
485         }
486         checkConsistency(is);
487         ok();
488     }
489 
490     return 0;
491 }
492 #endif
493