hash.c 2.72 KB
Newer Older
1 2 3 4 5 6
/* hash.c - Alpine Package Keeper (APK)
 *
 * Copyright (C) 2005-2008 Natanael Copa <n@tanael.org>
 * Copyright (C) 2008 Timo Teräs <timo.teras@iki.fi>
 * All rights reserved.
 *
7
 * This program is free software; you can redistribute it and/or modify it
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
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation. See http://www.gnu.org/ for details.
 */

#include "apk_defines.h"
#include "apk_hash.h"

void apk_hash_init(struct apk_hash *h, const struct apk_hash_ops *ops,
		   int num_buckets)
{
	h->ops = ops;
	h->buckets = apk_hash_array_resize(NULL, num_buckets);
	h->num_items = 0;
}

void apk_hash_free(struct apk_hash *h)
{
	apk_hash_foreach(h, (apk_hash_enumerator_f) h->ops->delete_item, NULL);
	free(h->buckets);
}

int apk_hash_foreach(struct apk_hash *h, apk_hash_enumerator_f e, void *ctx)
{
	apk_hash_node *pos, *n;
	ptrdiff_t offset = h->ops->node_offset;
	int i, r;

	for (i = 0; i < h->buckets->num; i++) {
		hlist_for_each_safe(pos, n, &h->buckets->item[i]) {
			r = e(((void *) pos) - offset, ctx);
38
			if (r != 0 && ctx != NULL)
39 40 41 42 43 44 45
				return r;
		}
	}

	return 0;
}

46
apk_hash_item apk_hash_get_hashed(struct apk_hash *h, apk_blob_t key, unsigned long hash)
47 48 49 50
{
	ptrdiff_t offset = h->ops->node_offset;
	apk_hash_node *pos;
	apk_hash_item item;
51
	apk_blob_t itemkey;
52

53
	hash %= h->buckets->num;
54 55 56 57 58 59 60 61 62 63 64 65 66
	if (h->ops->compare_item != NULL) {
		hlist_for_each(pos, &h->buckets->item[hash]) {
			item = ((void *) pos) - offset;
			if (h->ops->compare_item(item, key) == 0)
				return item;
		}
	} else {
		hlist_for_each(pos, &h->buckets->item[hash]) {
			item = ((void *) pos) - offset;
			itemkey = h->ops->get_key(item);
			if (h->ops->compare(key, itemkey) == 0)
				return item;
		}
67 68 69 70 71
	}

	return NULL;
}

72
void apk_hash_insert_hashed(struct apk_hash *h, apk_hash_item item, unsigned long hash)
73 74 75
{
	apk_hash_node *node;

76
	hash %= h->buckets->num;
77 78 79 80 81
	node = (apk_hash_node *) (item + h->ops->node_offset);
	hlist_add_head(node, &h->buckets->item[hash]);
	h->num_items++;
}

82
void apk_hash_delete_hashed(struct apk_hash *h, apk_blob_t key, unsigned long hash)
83
{
84 85 86 87 88
	ptrdiff_t offset = h->ops->node_offset;
	apk_hash_node *pos;
	apk_hash_item item;
	apk_blob_t itemkey;

89
	hash %= h->buckets->num;
90 91 92 93 94 95
	if (h->ops->compare_item != NULL) {
		hlist_for_each(pos, &h->buckets->item[hash]) {
			item = ((void *) pos) - offset;
			if (h->ops->compare_item(item, key) == 0) {
				hlist_del(pos, &h->buckets->item[hash]);
				h->ops->delete_item(item);
96
				break;
97 98 99 100 101 102 103 104 105
			}
		}
	} else {
		hlist_for_each(pos, &h->buckets->item[hash]) {
			item = ((void *) pos) - offset;
			itemkey = h->ops->get_key(item);
			if (h->ops->compare(key, itemkey) == 0) {
				hlist_del(pos, &h->buckets->item[hash]);
				h->ops->delete_item(item);
106
				break;
107 108 109
			}
		}
	}
110 111
}