NuriKit v0.1.0b2
Loading...
Searching...
No Matches
property_map.h
Go to the documentation of this file.
1//
2// Project NuriKit - Copyright 2025 SNU Compbio Lab.
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#ifndef NURI_CORE_CONTAINER_PROPERTY_MAP_H_
7#define NURI_CORE_CONTAINER_PROPERTY_MAP_H_
8
10#include <functional>
11#include <string>
12#include <string_view>
13#include <type_traits>
14#include <utility>
15#include <vector>
16
17#include <boost/container/flat_map.hpp>
19
20namespace nuri {
21namespace internal {
22 using PropertyMap = boost::container::flat_map<
23 std::string, std::string, std::less<>,
24 std::vector<std::pair<std::string, std::string>>>;
25
26 // RDKit-compatible key for name
27 constexpr std::string_view kNameKey = "_Name";
28
29 template <
30 class PT,
31 std::enable_if_t<std::is_same_v<PropertyMap, std::decay_t<PT>>, int> = 0>
32 auto find_key(PT &props, std::string_view key) {
33 return props.find(key);
34 }
35
36 template <
37 class PT,
38 std::enable_if_t<std::is_same_v<PropertyMap, std::decay_t<PT>>, int> = 0>
39 bool has_key(PT &props, std::string_view key) {
40 return props.contains(key);
41 }
42
43 template <
44 class PT,
45 std::enable_if_t<std::is_same_v<PropertyMap, std::decay_t<PT>>, int> = 0>
46 std::string_view get_key(PT &props, std::string_view key) {
47 auto it = find_key(props, key);
48 if (it == props.end())
49 return "";
50 return it->second;
51 }
52
53 template <
54 class PT, class ST,
55 std::enable_if_t<std::is_same_v<PropertyMap, std::decay_t<PT>>, int> = 0>
56 void set_key(PT &props, std::string_view key, ST &&value) {
57 auto it = props.lower_bound(key);
58
59 if (it != props.end() && it->first == key) {
60 it->second = std::forward<ST>(value);
61 } else {
62 props.emplace_hint(it, key, std::forward<ST>(value));
63 }
64 }
65
66 template <
67 class PT,
68 std::enable_if_t<std::is_same_v<PropertyMap, std::decay_t<PT>>, int> = 0>
69 auto find_name(PT &props) -> decltype(props.begin()) {
70 return find_key(props, kNameKey);
71 }
72
73 template <
74 class PT,
75 std::enable_if_t<std::is_same_v<PropertyMap, std::decay_t<PT>>, int> = 0>
76 bool has_name(PT &props) {
77 return has_key(props, kNameKey);
78 }
79
80 template <
81 class PT,
82 std::enable_if_t<std::is_same_v<PropertyMap, std::decay_t<PT>>, int> = 0>
83 std::string_view get_name(PT &props) {
84 return get_key(props, kNameKey);
85 }
86
87 template <
88 class PT, class ST,
89 std::enable_if_t<std::is_same_v<PropertyMap, std::decay_t<PT>>, int> = 0>
90 void set_name(PT &props, ST &&name) {
91 set_key(props, kNameKey, std::forward<ST>(name));
92 }
93} // namespace internal
94} // namespace nuri
95
96#endif /* NURI_CORE_CONTAINER_PROPERTY_MAP_H_ */
Definition crdgen.h:16