File: resize.c

package info (click to toggle)
python-bitarray 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,288 kB
  • sloc: python: 11,456; ansic: 7,657; makefile: 73; sh: 6
file content (113 lines) | stat: -rw-r--r-- 2,321 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <stdio.h>
#include <stdlib.h>


typedef struct {
    int size;
    int nbits;
    int allocated;
} bitarrayobject;


/* number of bytes necessary to store given bits */
#define BYTES(bits)  (((bits) + 7) >> 3)

uint64_t s = 290797;

int bbs(void)
{
    s *= s;
    s %= 50515093;
    return s % 8000;
}

void resize(bitarrayobject *self, int nbits)
{
    int size = self->size, allocated = self->allocated;
    int newsize = BYTES(nbits), new_allocated;

    if (newsize == size) {
        self->nbits = nbits;
        return;
    }

    if (newsize == 0) {
        /* free(self->ob_item) */
        self->size = 0;
        self->allocated = 0;
        self->nbits = 0;
        return;
    }

    if (allocated >= newsize) {
        if (newsize >= allocated / 2) {
            self->size = newsize;
            self->nbits = nbits;
            return;
        }
        new_allocated = newsize;
    }
    else {
        new_allocated = newsize;
        if (size != 0 && newsize / 2 <= allocated) {
            new_allocated += (newsize >> 4) + (newsize < 8 ? 3 : 7);
            new_allocated &= ~(int) 3;
        }
    }

    /* realloc(self->ob_item) */
    self->size = newsize;
    self->allocated = new_allocated;
    self->nbits = nbits;
}


int main()
{
    int i, nbits, prev_alloc = -1;
    bitarrayobject x;

#define SHOW  printf("%d  %d\n", x.size, x.allocated)

    x.size = 0;
    x.allocated = 0;
    for (nbits = 0; nbits < 1000; nbits++) {
        if (prev_alloc != x.allocated)
            SHOW;
        prev_alloc = x.allocated;
        resize(&x, nbits);
    }

    resize(&x, 800000);  SHOW;
    resize(&x, 400000);  SHOW;
    resize(&x, 399992);  SHOW;
    resize(&x, 500000);  SHOW;
    resize(&x, 0);       SHOW;
    resize(&x, 0);       SHOW;
    resize(&x, 10000);   SHOW;
    resize(&x,   400);   SHOW;
    resize(&x,   600);   SHOW;
    resize(&x,  2000);   SHOW;

    for (nbits = 2000; nbits >= 0; nbits--) {
        if (prev_alloc != x.allocated)
            SHOW;
        prev_alloc = x.allocated;
        resize(&x, nbits);
    }
    SHOW;

    for (nbits = 0; nbits < 100; nbits += 8) {
        x.size = 0;
        x.allocated = 0;
        resize(&x, nbits);
        SHOW;
    }

    for (i = 0; i < 100000; i++) {
        resize(&x, bbs());
        SHOW;
    }

    return 0;
}