The open source OpenXR runtime
at main 108 lines 1.8 kB view raw
1// Copyright 2019, Collabora, Ltd. 2// SPDX-License-Identifier: BSL-1.0 3/*! 4 * @file 5 * @brief Hashmap for integer values header. 6 * @author Jakob Bornecrantz <jakob@collabora.com> 7 * @author Korcan Hussein <korcan.hussein@collabora.com> 8 * @ingroup aux_util 9 */ 10 11#include "util/u_hashmap.h" 12 13#include <unordered_map> 14#include <vector> 15 16 17/* 18 * 19 * Private structs and defines. 20 * 21 */ 22 23struct u_hashmap_int 24{ 25 std::unordered_map<uint64_t, void *> map = {}; 26}; 27 28 29/* 30 * 31 * "Exported" functions. 32 * 33 */ 34 35extern "C" int 36u_hashmap_int_create(struct u_hashmap_int **out_hashmap_int) 37{ 38 auto *hs = new u_hashmap_int; 39 *out_hashmap_int = hs; 40 return 0; 41} 42 43extern "C" int 44u_hashmap_int_destroy(struct u_hashmap_int **hmi) 45{ 46 delete *hmi; 47 *hmi = NULL; 48 return 0; 49} 50 51int 52u_hashmap_int_find(struct u_hashmap_int *hmi, uint64_t key, void **out_item) 53{ 54 auto search = hmi->map.find(key); 55 56 if (search != hmi->map.end()) { 57 *out_item = search->second; 58 return 0; 59 } 60 return -1; 61} 62 63extern "C" int 64u_hashmap_int_insert(struct u_hashmap_int *hmi, uint64_t key, void *value) 65{ 66 hmi->map[key] = value; 67 return 0; 68} 69 70extern "C" int 71u_hashmap_int_erase(struct u_hashmap_int *hmi, uint64_t key) 72{ 73 hmi->map.erase(key); 74 return 0; 75} 76 77bool 78u_hashmap_int_empty(const struct u_hashmap_int *hmi) 79{ 80 return hmi->map.empty(); 81} 82 83void 84u_hashmap_int_for_each(const struct u_hashmap_int *hmi, u_hashmap_int_foreach_callback cb, void *priv_ctx) 85{ 86 if (hmi == NULL || cb == NULL) 87 return; 88 for (const auto &keyval : hmi->map) { 89 cb(keyval.first, keyval.second, priv_ctx); 90 } 91} 92 93extern "C" void 94u_hashmap_int_clear_and_call_for_each(struct u_hashmap_int *hmi, u_hashmap_int_callback cb, void *priv) 95{ 96 std::vector<void *> tmp; 97 tmp.reserve(hmi->map.size()); 98 99 for (auto &n : hmi->map) { 100 tmp.push_back(n.second); 101 } 102 103 hmi->map.clear(); 104 105 for (auto *n : tmp) { 106 cb(n, priv); 107 } 108}