OpenRadioss 2025.1.11
OpenRadioss project
Loading...
Searching...
No Matches
umap.cpp File Reference
#include <unordered_map>
#include <cstdint>
#include <stdexcept>

Go to the source code of this file.

Functions

void * cpp_create_umap () noexcept
void cpp_free_umap (void *umap_ptr) noexcept
void cpp_add_entry_umap (void *umap_ptr, int key, int value) noexcept
int cpp_get_value_umap (void *umap_ptr, int key, int default_value) noexcept
void cpp_reserve_umap (void *umap_ptr, std::size_t n) noexcept

Function Documentation

◆ cpp_add_entry_umap()

void cpp_add_entry_umap ( void * umap_ptr,
int key,
int value )
noexcept

Definition at line 46 of file umap.cpp.

46 {
47 // Insert or update the value at the given key.
48 // This is not thread-safe if called concurrently with reads/writes.
49 auto umap = static_cast<std::unordered_map<int,int>*>(umap_ptr);
50 (*umap)[key] = value;
51}

◆ cpp_create_umap()

void * cpp_create_umap ( )
noexcept

Definition at line 33 of file umap.cpp.

33 {
34 // Create a new unordered_map on the heap and return it as a void pointer.
35 auto* umap = new std::unordered_map<int,int>();
36 return static_cast<void*>(umap);
37}

◆ cpp_free_umap()

void cpp_free_umap ( void * umap_ptr)
noexcept

Definition at line 39 of file umap.cpp.

39 {
40 // Delete the map, freeing the allocated memory.
41 // Caller must ensure umap_ptr is a pointer returned by create_umap.
42 auto umap = static_cast<std::unordered_map<int,int>*>(umap_ptr);
43 delete umap;
44}

◆ cpp_get_value_umap()

int cpp_get_value_umap ( void * umap_ptr,
int key,
int default_value )
noexcept

Definition at line 53 of file umap.cpp.

53 {
54 // Retrieve a value from the map. If the key is not found, return default_value.
55 // Safe for concurrent calls *only if* no other thread modifies the map.
56 auto umap = static_cast<std::unordered_map<int,int>*>(umap_ptr);
57 auto it = umap->find(key);
58 return (it != umap->end()) ? it->second : default_value;
59}

◆ cpp_reserve_umap()

void cpp_reserve_umap ( void * umap_ptr,
std::size_t n )
noexcept

Definition at line 61 of file umap.cpp.

61 {
62 // Reserve space for at least n elements.
63 // This can improve performance of subsequent insertions by avoiding rehashing.
64 // Must be called before concurrent reads, and never during them.
65 auto umap = static_cast<std::unordered_map<int,int>*>(umap_ptr);
66 umap->reserve(n);
67}
n