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
176
177
178
|
/* Copyright (C) 2009 The Android Open Source Project
**
** This software is licensed under the terms of the GNU General Public
** License version 2, as published by the Free Software Foundation, and
** may be copied, distributed, and modified under those terms.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
*/
#include <android/utils/refset.h>
#include <stddef.h>
#define AREFSET_STEP 5
AINLINED uint32_t
_arefSet_hashItem( void* item )
{
uint32_t hash;
hash = (uint32_t)(ptrdiff_t)item >> 2;
if (sizeof(item) > 4)
hash ^= ((uint64_t)(ptrdiff_t)item >> 32);
return hash;
}
static void**
_arefSet_lookup( ARefSet* s, void* item)
{
uint32_t hash = _arefSet_hashItem(item);
unsigned index = hash & (s->max_buckets-1);
for (;;) {
void** pnode = &s->buckets[index];
if (*pnode == item || *pnode == NULL)
return pnode;
index += AREFSET_STEP;
if (index >= s->max_buckets)
index -= s->max_buckets;
}
}
static void**
_arefSet_lookupInsert( ARefSet* s, void* item)
{
uint32_t hash = _arefSet_hashItem(item);
unsigned index = hash & (s->max_buckets-1);
void** insert = NULL;
for (;;) {
void** pnode = &s->buckets[index];
if (*pnode == NULL) {
return insert ? insert : pnode;
} else if (*pnode == AREFSET_DELETED) {
if (!insert)
insert = pnode;
} else if (*pnode == item)
return pnode;
index = (index + AREFSET_STEP) & (s->max_buckets-1);
}
}
extern ABool
arefSet_has( ARefSet* s, void* item )
{
void** lookup;
if (item == NULL || s->max_buckets == 0)
return 0;
lookup = _arefSet_lookup(s, item);
return (*lookup == item);
}
/* the code below assumes, in the case of a down-size,
* that there aren't too many items in the set.
*/
static void
_arefSet_resize( ARefSet* s, unsigned newSize )
{
ARefSet newSet;
unsigned nn, count = s->num_buckets;
AVECTOR_INIT_ALLOC(&newSet,buckets, newSize);
for (nn = 0; nn < s->max_buckets; nn++) {
void* item = s->buckets[nn];
if (item != NULL && item != AREFSET_DELETED) {
void** lookup = _arefSet_lookup(&newSet, item);
*lookup = item;
}
}
AVECTOR_DONE(s,buckets);
s->buckets = newSet.buckets;
s->num_buckets = count;
s->max_buckets = newSet.max_buckets;
}
extern void
arefSet_add( ARefSet* s, void* item )
{
void** lookup;
if (item == NULL)
return;
/* You can't add items to a set during iteration! */
AASSERT(s->iteration == 0);
if (s->max_buckets == 0)
AVECTOR_INIT_ALLOC(s,buckets,4);
lookup = _arefSet_lookupInsert(s, item);
if (*lookup == item)
return;
*lookup = item;
s->num_buckets += 1;
if (s->iteration == 0) {
if (s->num_buckets > s->max_buckets*0.85)
_arefSet_resize(s, s->max_buckets*2);
}
}
extern void
arefSet_del( ARefSet* s, void* item )
{
void** lookup;
if (item == NULL || s->max_buckets == 0)
return;
lookup = _arefSet_lookup(s, item);
if (*lookup != item)
return;
if (s->iteration == 0) {
/* direct deletion */
*lookup = NULL;
s->num_buckets -= 1;
if (s->num_buckets < s->max_buckets*0.25)
_arefSet_resize(s, s->max_buckets/2);
} else {
/* deferred deletion */
*lookup = AREFSET_DELETED;
s->num_buckets -= 1;
s->iteration |= 1;
}
}
void
_arefSet_removeDeferred( ARefSet* s )
{
unsigned nn, newSize;
for (nn = 0; nn < s->max_buckets; nn++) {
if (s->buckets[nn] == AREFSET_DELETED) {
s->buckets[nn] = NULL;
}
}
s->iteration = 0;
newSize = s->max_buckets;
while (s->num_buckets < newSize*0.25)
newSize /= 2;
if (newSize != s->max_buckets)
_arefSet_resize(s, newSize);
}
|