-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathobjset.c
67 lines (58 loc) · 989 Bytes
/
objset.c
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
#include <u.h>
#include <libc.h>
#include "git.h"
void
osinit(Objset *s)
{
s->sz = 16;
s->nobj = 0;
s->obj = eamalloc(s->sz, sizeof(Hash));
}
void
osclear(Objset *s)
{
free(s->obj);
}
void
osadd(Objset *s, Object *o)
{
u32int probe;
Object **obj;
int i, sz;
probe = GETBE32(o->hash.h) % s->sz;
while(s->obj[probe]){
if(hasheq(&s->obj[probe]->hash, &o->hash)){
s->obj[probe] = o;
return;
}
probe = (probe + 1) % s->sz;
}
assert(s->obj[probe] == nil);
s->obj[probe] = o;
s->nobj++;
if(s->sz < 2*s->nobj){
sz = s->sz;
obj = s->obj;
s->sz *= 2;
s->nobj = 0;
s->obj = eamalloc(s->sz, sizeof(Hash));
for(i = 0; i < sz; i++)
if(obj[i])
osadd(s, obj[i]);
free(obj);
}
}
Object*
osfind(Objset *s, Hash h)
{
u32int probe;
for(probe = GETBE32(h.h) % s->sz; s->obj[probe]; probe = (probe + 1) % s->sz)
if(hasheq(&s->obj[probe]->hash, &h))
return s->obj[probe];
return 0;
}
int
oshas(Objset *s, Hash h)
{
return osfind(s, h) != nil;
}