2008-04-17 14:09:13 +00:00
|
|
|
/* apk_hash.h - Alpine Package Keeper (APK)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005-2008 Natanael Copa <n@tanael.org>
|
2011-09-13 08:53:01 +00:00
|
|
|
* Copyright (C) 2008-2011 Timo Teräs <timo.teras@iki.fi>
|
2008-04-17 14:09:13 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2009-07-14 07:47:20 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
2008-04-17 14:09:13 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef APK_HASH_H
|
|
|
|
#define APK_HASH_H
|
|
|
|
|
2013-04-17 23:59:35 +00:00
|
|
|
#include <stdlib.h>
|
2008-04-17 14:09:13 +00:00
|
|
|
#include "apk_defines.h"
|
2008-11-27 18:25:01 +00:00
|
|
|
#include "apk_blob.h"
|
2008-04-17 14:09:13 +00:00
|
|
|
|
|
|
|
typedef void *apk_hash_item;
|
|
|
|
|
2008-11-27 18:25:01 +00:00
|
|
|
typedef unsigned long (*apk_hash_f)(apk_blob_t);
|
|
|
|
typedef int (*apk_hash_compare_f)(apk_blob_t, apk_blob_t);
|
2009-01-14 08:44:47 +00:00
|
|
|
typedef int (*apk_hash_compare_item_f)(apk_hash_item, apk_blob_t);
|
2008-04-17 14:09:13 +00:00
|
|
|
typedef void (*apk_hash_delete_f)(apk_hash_item);
|
|
|
|
typedef int (*apk_hash_enumerator_f)(apk_hash_item, void *ctx);
|
|
|
|
|
|
|
|
struct apk_hash_ops {
|
|
|
|
ptrdiff_t node_offset;
|
2008-11-27 18:25:01 +00:00
|
|
|
apk_blob_t (*get_key)(apk_hash_item item);
|
|
|
|
unsigned long (*hash_key)(apk_blob_t key);
|
2009-01-14 08:44:47 +00:00
|
|
|
unsigned long (*hash_item)(apk_hash_item item);
|
|
|
|
int (*compare)(apk_blob_t itemkey, apk_blob_t key);
|
|
|
|
int (*compare_item)(apk_hash_item item, apk_blob_t key);
|
2008-04-17 14:09:13 +00:00
|
|
|
void (*delete_item)(apk_hash_item item);
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct hlist_node apk_hash_node;
|
|
|
|
APK_ARRAY(apk_hash_array, struct hlist_head);
|
|
|
|
|
|
|
|
struct apk_hash {
|
|
|
|
const struct apk_hash_ops *ops;
|
|
|
|
struct apk_hash_array *buckets;
|
|
|
|
int num_items;
|
|
|
|
};
|
|
|
|
|
|
|
|
void apk_hash_init(struct apk_hash *h, const struct apk_hash_ops *ops,
|
|
|
|
int num_buckets);
|
|
|
|
void apk_hash_free(struct apk_hash *h);
|
|
|
|
|
|
|
|
int apk_hash_foreach(struct apk_hash *h, apk_hash_enumerator_f e, void *ctx);
|
2009-07-14 07:47:20 +00:00
|
|
|
apk_hash_item apk_hash_get_hashed(struct apk_hash *h, apk_blob_t key, unsigned long hash);
|
|
|
|
void apk_hash_insert_hashed(struct apk_hash *h, apk_hash_item item, unsigned long hash);
|
|
|
|
void apk_hash_delete_hashed(struct apk_hash *h, apk_blob_t key, unsigned long hash);
|
|
|
|
|
|
|
|
static inline unsigned long apk_hash_from_key(struct apk_hash *h, apk_blob_t key)
|
|
|
|
{
|
|
|
|
return h->ops->hash_key(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned long apk_hash_from_item(struct apk_hash *h, apk_hash_item item)
|
|
|
|
{
|
|
|
|
if (h->ops->hash_item != NULL)
|
|
|
|
return h->ops->hash_item(item);
|
|
|
|
return apk_hash_from_key(h, h->ops->get_key(item));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline apk_hash_item apk_hash_get(struct apk_hash *h, apk_blob_t key)
|
|
|
|
{
|
|
|
|
return apk_hash_get_hashed(h, key, apk_hash_from_key(h, key));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void apk_hash_insert(struct apk_hash *h, apk_hash_item item)
|
|
|
|
{
|
|
|
|
return apk_hash_insert_hashed(h, item, apk_hash_from_item(h, item));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void apk_hash_delete(struct apk_hash *h, apk_blob_t key)
|
|
|
|
{
|
|
|
|
return apk_hash_delete_hashed(h, key, apk_hash_from_key(h, key));
|
|
|
|
}
|
2008-04-17 14:09:13 +00:00
|
|
|
|
|
|
|
#endif
|