OpenRadioss 2025.1.11
OpenRadioss project
Loading...
Searching...
No Matches
umap.cpp
Go to the documentation of this file.
1//Copyright> OpenRadioss
2//Copyright> Copyright (C) 1986-2025 Altair Engineering Inc.
3//Copyright>
4//Copyright> This program is free software: you can redistribute it and/or modify
5//Copyright> it under the terms of the GNU Affero General Public License as published by
6//Copyright> the Free Software Foundation, either version 3 of the License, or
7//Copyright> (at your option) any later version.
8//Copyright>
9//Copyright> This program is distributed in the hope that it will be useful,
10//Copyright> but WITHOUT ANY WARRANTY; without even the implied warranty of
11//Copyright> MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12//Copyright> GNU Affero General Public License for more details.
13//Copyright>
14//Copyright> You should have received a copy of the GNU Affero General Public License
15//Copyright> along with this program. If not, see <https://www.gnu.org/licenses/>.
16//Copyright>
17//Copyright>
18//Copyright> Commercial Alternative: Altair Radioss Software
19//Copyright>
20//Copyright> As an alternative to this open-source version, Altair also offers Altair Radioss
21//Copyright> software under a commercial license. Contact Altair to discuss further if the
22//Copyright> commercial version may interest you: https://www.altair.com/radioss/.
23
24#include <unordered_map>
25#include <cstdint>
26#include <stdexcept>
27
28// Using C++14, we have no std::shared_mutex, so no internal locking is shown here.
29// The container should be fully populated before concurrent reads to ensure safety.
30
31extern "C" {
32
33void* cpp_create_umap() noexcept {
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}
38
39void cpp_free_umap(void* umap_ptr) noexcept {
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}
45
46void cpp_add_entry_umap(void* umap_ptr, int key, int value) noexcept {
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}
52
53int cpp_get_value_umap(void* umap_ptr, int key, int default_value) noexcept {
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}
60
61void cpp_reserve_umap(void* umap_ptr, std::size_t n) noexcept {
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}
68
69} // extern "C"
n
void cpp_free_umap(void *umap_ptr) noexcept
Definition umap.cpp:39
void cpp_reserve_umap(void *umap_ptr, std::size_t n) noexcept
Definition umap.cpp:61
int cpp_get_value_umap(void *umap_ptr, int key, int default_value) noexcept
Definition umap.cpp:53
void * cpp_create_umap() noexcept
Definition umap.cpp:33
void cpp_add_entry_umap(void *umap_ptr, int key, int value) noexcept
Definition umap.cpp:46