mirror of
https://gitlab.freedesktop.org/monado/monado.git
synced 2024-12-29 11:06:18 +00:00
aux/util: Add hashmap
This commit is contained in:
parent
2d6cb08c48
commit
de6bf55116
|
@ -24,6 +24,8 @@ set(UTIL_SOURCE_FILES
|
|||
util/u_device.c
|
||||
util/u_device.h
|
||||
util/u_documentation.h
|
||||
util/u_hashmap.cpp
|
||||
util/u_hashmap.h
|
||||
util/u_hashset.cpp
|
||||
util/u_hashset.h
|
||||
util/u_time.cpp
|
||||
|
|
94
src/xrt/auxiliary/util/u_hashmap.cpp
Normal file
94
src/xrt/auxiliary/util/u_hashmap.cpp
Normal file
|
@ -0,0 +1,94 @@
|
|||
// Copyright 2019, Collabora, Ltd.
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
/*!
|
||||
* @file
|
||||
* @brief Hashmap for integer values header.
|
||||
* @author Jakob Bornecrantz <jakob@collabora.com>
|
||||
* @ingroup aux_util
|
||||
*/
|
||||
|
||||
#include "util/u_hashmap.h"
|
||||
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
* Private structs and defines.
|
||||
*
|
||||
*/
|
||||
|
||||
struct u_hashmap_int
|
||||
{
|
||||
std::unordered_map<uint64_t, void *> map = {};
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
* "Exported" functions.
|
||||
*
|
||||
*/
|
||||
|
||||
extern "C" int
|
||||
u_hashmap_int_create(struct u_hashmap_int **out_hashmap_int)
|
||||
{
|
||||
auto hs = new u_hashmap_int;
|
||||
*out_hashmap_int = hs;
|
||||
return 0;
|
||||
}
|
||||
|
||||
extern "C" int
|
||||
u_hashmap_int_destroy(struct u_hashmap_int **hmi)
|
||||
{
|
||||
delete *hmi;
|
||||
*hmi = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
u_hashmap_int_find(struct u_hashmap_int *hmi, uint64_t key, void **out_item)
|
||||
{
|
||||
auto search = hmi->map.find(key);
|
||||
|
||||
if (search != hmi->map.end()) {
|
||||
*out_item = search->second;
|
||||
return 0;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" int
|
||||
u_hashmap_int_insert(struct u_hashmap_int *hmi, uint64_t key, void *value)
|
||||
{
|
||||
hmi->map[key] = value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
extern "C" int
|
||||
u_hashmap_int_erase(struct u_hashmap_int *hmi, uint64_t key)
|
||||
{
|
||||
hmi->map.erase(key);
|
||||
return 0;
|
||||
}
|
||||
|
||||
extern "C" void
|
||||
u_hashmap_int_clear_and_call_for_each(struct u_hashmap_int *hmi,
|
||||
u_hashmap_int_callback cb,
|
||||
void *priv)
|
||||
{
|
||||
std::vector<void *> tmp;
|
||||
tmp.reserve(hmi->map.size());
|
||||
|
||||
for (auto &n : hmi->map) {
|
||||
tmp.push_back(n.second);
|
||||
}
|
||||
|
||||
hmi->map.clear();
|
||||
|
||||
for (auto n : tmp) {
|
||||
cb(n, priv);
|
||||
}
|
||||
}
|
58
src/xrt/auxiliary/util/u_hashmap.h
Normal file
58
src/xrt/auxiliary/util/u_hashmap.h
Normal file
|
@ -0,0 +1,58 @@
|
|||
// Copyright 2019, Collabora, Ltd.
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
/*!
|
||||
* @file
|
||||
* @brief Hashmap for integer values header.
|
||||
* @author Jakob Bornecrantz <jakob@collabora.com>
|
||||
* @ingroup aux_util
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "xrt/xrt_compiler.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/*!
|
||||
* @struct u_hashmap_int
|
||||
* @ingroup aux_util
|
||||
*
|
||||
* A simple uint64_t key to a void pointer hashmap.
|
||||
*/
|
||||
struct u_hashmap_int;
|
||||
|
||||
typedef void (*u_hashmap_int_callback)(void *item, void *priv);
|
||||
|
||||
int
|
||||
u_hashmap_int_create(struct u_hashmap_int **out_hashmap);
|
||||
|
||||
int
|
||||
u_hashmap_int_destroy(struct u_hashmap_int **hs);
|
||||
|
||||
int
|
||||
u_hashmap_int_find(struct u_hashmap_int *hs, uint64_t key, void **out_item);
|
||||
|
||||
int
|
||||
u_hashmap_int_insert(struct u_hashmap_int *hs, uint64_t key, void *value);
|
||||
|
||||
int
|
||||
u_hashmap_int_erase(struct u_hashmap_int *hs, uint64_t key);
|
||||
|
||||
/*!
|
||||
* First clear the hashmap and then call the given callback with each item that
|
||||
* was in the hashmap.
|
||||
*
|
||||
* @ingroup aux_util
|
||||
*/
|
||||
void
|
||||
u_hashmap_int_clear_and_call_for_each(struct u_hashmap_int *hs,
|
||||
u_hashmap_int_callback cb,
|
||||
void *priv);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
Loading…
Reference in a new issue