File: mtable.c

package info (click to toggle)
docbook2x 0.8.8-15
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 4,720 kB
  • ctags: 389
  • sloc: xml: 16,229; sh: 3,674; perl: 3,461; ansic: 639; makefile: 409; sed: 11
file content (175 lines) | stat: -rw-r--r-- 3,760 bytes parent folder | download | duplicates (6)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "mtable.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#define mtable_left_index(k, e)   ((~((1<<(e))-1) & (k))>>(e))
#define mtable_right_index(k, e)  (((1<<(e))-1) & (k))

static void *
mallocc(size_t size)
{
    void *p = malloc(size);
    if(p == NULL) {
        fprintf(stderr, "out of memory\n");
        abort();
    }
    return p;
}

static mtable_t 
mtable_new2(int *exponents)
{
    void **p = mallocc(sizeof(void *) * (1 + (1<<exponents[0])));
    p[0] = exponents;
    memset(p+1, 0, sizeof(void *) * (1<<exponents[0]));
    return (mtable_t) p;
}

mtable_t 
mtable_new(int *exponents)
{
    int n;
    int *exponents_copy;
    
    for(n=0; exponents[n++] != 0; );
    exponents_copy = mallocc(sizeof(int) * n);
    while(n > 0) {
        n--;
        exponents_copy[n] = exponents[n];
    }

    return mtable_new2(exponents_copy);
}

static void 
mtable_delete2(mtable_t mtab, int e_sum, int start)
{
    if(mtab->exponents[1] != 0) {
        int k;
        int e = mtab->exponents[0];
        void **p = &(mtab->slots[0]);
        for(k=0; k<(1<<e); k++) {
            if(*p != NULL)
                mtable_delete2(*p, e_sum-e, start+(k<<(e_sum-e)));
            p++;
        }
    }
    free(mtab);
}
    
void 
mtable_delete(mtable_t mtab)
{
    int i, e_sum=0, *exponents;
    for(i=0; mtab->exponents[i] != 0; i++)
        e_sum += mtab->exponents[i];

    exponents = mtab->exponents;
    mtable_delete2(mtab, e_sum, 0);
    free(exponents);
}

#if 0
static void
mtable_change_exponents(mtable_t mtab, int *exponents)
{
    if(mtab->exponents[1] != 0) {
        int k;
        int e = mtab->exponents[0];
        void **p = &(mtab->slots[0]);
        for(k=0; k<(1<<e); k++) {
            if(*p != NULL)
                mtable_change_exponents(*p, exponents+1);
            p++;
        }
    }
    mtab->exponents = exponents;
}

mtable_t
mtable_extend(mtable_t mtab, int *exponents)
{
    struct mtable *p;
    int *all_exponents, *old_exponents;
    int n = 0;
    int i = 0;

    for(n=0; exponents[n++] != 0; );
    for(i=0; mtab->exponents[i++] != 0; );
    all_exponents = mallocc(sizeof(int) * (n-1+i));
    for(i=0; exponents[i] != 0; i++)
        all_exponents[i] = exponents[i];
    for(i=0; mtab->exponents[i] != 0; i++)
        all_exponents[n-1+i] = mtab->exponents[i];
    all_exponents[n-1+i] = 0;
        
    p = mtable_new2(exponents);
    mtable_set(p, 0, mtab);

    old_exponents = mtab->exponents;
    mtable_change_exponents(p, all_exponents);
    mtable_change_exponents(mtab, all_exponents+n-1);
    free(old_exponents);

    return p;
}
#endif

void
mtable_set(mtable_t mtab, mtable_key key, void *data)
{
    int i, e=0;
    struct mtable *p;
    mtable_key k;
    
    for(i=0; mtab->exponents[i] != 0; i++)
        e += mtab->exponents[i];

    p = mtab;
    k = key;
    
    for(;;) {
        if(p->exponents[1] == 0) {
            p->slots[k] = data;
            return;
        } else {
            e -= p->exponents[0];
            i = mtable_left_index(k, e);
            k = mtable_right_index(k, e);

            if(p->slots[i] == NULL) 
                p->slots[i] = mtable_new2(p->exponents+1);

            p = p->slots[i];
        }
    }
}
        
void 
*mtable_get(mtable_t mtab, mtable_key key)
{
    int i, e=0;
    struct mtable *p;
    mtable_key k;
    
    for(i=0; mtab->exponents[i] != 0; i++)
        e += mtab->exponents[i];

    p = mtab;
    k = key;
  
    while(p != NULL) {
        if(p->exponents[1] == 0) {
            return p->slots[k];
        } else {
            e -= p->exponents[0];
            i = mtable_left_index(k, e);
            k = mtable_right_index(k, e);
            
            p = p->slots[i];
        }
    }

    return NULL;
}