]>
git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - lib/ext2fs/hashmap.c
4 uint32_t ext2fs_djb2_hash(const void *str
, size_t size
)
12 hash
= ((hash
<< 5) + hash
) + c
;
17 struct ext2fs_hashmap
*ext2fs_hashmap_create(
18 uint32_t(*hash_fct
)(const void*, size_t),
19 void(*free_fct
)(void*), size_t size
)
21 struct ext2fs_hashmap
*h
= calloc(sizeof(struct ext2fs_hashmap
) +
22 sizeof(struct ext2fs_hashmap_entry
) * size
, 1);
26 h
->first
= h
->last
= NULL
;
30 void ext2fs_hashmap_add(struct ext2fs_hashmap
*h
, void *data
, const void *key
,
33 uint32_t hash
= h
->hash(key
, key_len
) % h
->size
;
34 struct ext2fs_hashmap_entry
*e
= malloc(sizeof(*e
));
39 e
->next
= h
->entries
[hash
];
43 e
->list_next
= h
->first
;
45 h
->first
->list_prev
= e
;
51 void *ext2fs_hashmap_lookup(struct ext2fs_hashmap
*h
, const void *key
,
54 struct ext2fs_hashmap_entry
*iter
;
55 uint32_t hash
= h
->hash(key
, key_len
) % h
->size
;
57 for (iter
= h
->entries
[hash
]; iter
; iter
= iter
->next
)
58 if (iter
->key_len
== key_len
&& !memcmp(iter
->key
, key
, key_len
))
63 void *ext2fs_hashmap_iter_in_order(struct ext2fs_hashmap
*h
,
64 struct ext2fs_hashmap_entry
**it
)
66 *it
= *it
? (*it
)->list_next
: h
->first
;
67 return *it
? (*it
)->data
: NULL
;
70 void ext2fs_hashmap_free(struct ext2fs_hashmap
*h
)
74 for (i
= 0; i
< h
->size
; ++i
) {
75 struct ext2fs_hashmap_entry
*it
= h
->entries
[i
];
77 struct ext2fs_hashmap_entry
*tmp
= it
->next
;