ObjFW  Diff

Differences From Artifact [0f765c0951]:

To Artifact [e12d752357]:


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
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







-
-
+
+










-
+

-
+



-
+


-
+







-
+

-
-
+
+


-
+

-
+
+





-
+




-
-
+
+



-
+








	return table;
}

static void
resize(struct objc_hashtable *table, uint32_t count)
{
	uint32_t fullness, nsize;
	struct objc_hashtable_bucket **ndata;
	uint32_t fullness, newSize;
	struct objc_hashtable_bucket **newData;

	if (count > UINT32_MAX / sizeof(*table->data) || count > UINT32_MAX / 8)
		OBJC_ERROR("Integer overflow!");

	fullness = count * 8 / table->size;

	if (fullness >= 6) {
		if (table->size > UINT32_MAX / 2)
			return;

		nsize = table->size * 2;
		newSize = table->size * 2;
	} else if (fullness <= 1)
		nsize = table->size / 2;
		newSize = table->size / 2;
	else
		return;

	if (count < table->count && nsize < 16)
	if (count < table->count && newSize < 16)
		return;

	if ((ndata = calloc(nsize, sizeof(sizeof(*ndata)))) == NULL)
	if ((newData = calloc(newSize, sizeof(sizeof(*newData)))) == NULL)
		OBJC_ERROR("Not enough memory to resize hash table!");

	for (uint32_t i = 0; i < table->size; i++) {
		if (table->data[i] != NULL &&
		    table->data[i] != &objc_deleted_bucket) {
			uint32_t j, last;

			last = nsize;
			last = newSize;

			for (j = table->data[i]->hash & (nsize - 1);
			    j < last && ndata[j] != NULL; j++);
			for (j = table->data[i]->hash & (newSize - 1);
			    j < last && newData[j] != NULL; j++);

			if (j >= last) {
				last = table->data[i]->hash & (nsize - 1);
				last = table->data[i]->hash & (newSize - 1);

				for (j = 0; j < last && ndata[j] != NULL; j++);
				for (j = 0; j < last && newData[j] != NULL;
				    j++);
			}

			if (j >= last)
				OBJC_ERROR("No free bucket!");

			ndata[j] = table->data[i];
			newData[j] = table->data[i];
		}
	}

	free(table->data);
	table->data = ndata;
	table->size = nsize;
	table->data = newData;
	table->size = newSize;
}

static inline bool
index_for_key(struct objc_hashtable *table, const void *key, uint32_t *idx)
indexForKey(struct objc_hashtable *table, const void *key, uint32_t *idx)
{
	uint32_t i, hash;

	hash = table->hash(key) & (table->size - 1);

	for (i = hash; i < table->size && table->data[i] != NULL; i++) {
		if (table->data[i] == &objc_deleted_bucket)
162
163
164
165
166
167
168
169

170
171
172
173
174
175


176
177
178
179
180
181
182
163
164
165
166
167
168
169

170
171
172
173
174


175
176
177
178
179
180
181
182
183







-
+




-
-
+
+







	}

	return false;
}

void
objc_hashtable_set(struct objc_hashtable *table, const void *key,
    const void *obj)
    const void *object)
{
	uint32_t i, hash, last;
	struct objc_hashtable_bucket *bucket;

	if (index_for_key(table, key, &i)) {
		table->data[i]->obj = obj;
	if (indexForKey(table, key, &i)) {
		table->data[i]->object = object;
		return;
	}

	resize(table, table->count + 1);

	hash = table->hash(key);
	last = table->size;
195
196
197
198
199
200
201
202

203
204
205
206
207
208
209
210
211
212
213

214
215
216

217
218
219
220
221
222
223
224

225
226
227
228
229
230
231
196
197
198
199
200
201
202

203
204
205
206
207
208
209
210
211
212
213

214
215
216

217
218
219
220
221
222
223
224

225
226
227
228
229
230
231
232







-
+










-
+


-
+







-
+







		OBJC_ERROR("No free bucket!");

	if ((bucket = malloc(sizeof(*bucket))) == NULL)
		OBJC_ERROR("Not enough memory to allocate hash table bucket!");

	bucket->key = key;
	bucket->hash = hash;
	bucket->obj = obj;
	bucket->object = object;

	table->data[i] = bucket;
	table->count++;
}

void *
objc_hashtable_get(struct objc_hashtable *table, const void *key)
{
	uint32_t idx;

	if (!index_for_key(table, key, &idx))
	if (!indexForKey(table, key, &idx))
		return NULL;

	return (void *)table->data[idx]->obj;
	return (void *)table->data[idx]->object;
}

void
objc_hashtable_delete(struct objc_hashtable *table, const void *key)
{
	uint32_t idx;

	if (!index_for_key(table, key, &idx))
	if (!indexForKey(table, key, &idx))
		return;

	free(table->data[idx]);
	table->data[idx] = &objc_deleted_bucket;

	table->count--;
	resize(table, table->count);