Skip to content

Commit d5a27bf

Browse files
authoredMar 16, 2023
Merge pull request #308 from jcapriot/try_emplace
Switch to try_emplace
2 parents 9f58a50 + 2c05f80 commit d5a27bf

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed
 

‎discretize/_extensions/tree.cpp

+15-24
Original file line numberDiff line numberDiff line change
@@ -137,31 +137,25 @@ Face::Face(Node& p1, Node& p2, Node& p3, Node& p4){
137137
Node * set_default_node(node_map_t& nodes, int_t x, int_t y, int_t z,
138138
double *xs, double *ys, double *zs){
139139
int_t key = key_func(x, y, z);
140-
Node * point;
141-
if(nodes.count(key) == 0){
142-
point = new Node(x, y, z, xs, ys, zs);
143-
nodes[key] = point;
140+
auto [it, inserted] = nodes.try_emplace(key, nullptr);
141+
if(inserted){
142+
// construct a new item at the emplaced location
143+
it->second = new Node(x, y, z, xs, ys, zs);
144144
}
145-
else{
146-
point = nodes[key];
147-
}
148-
return point;
145+
return it->second;
149146
}
150147

151148
Edge * set_default_edge(edge_map_t& edges, Node& p1, Node& p2){
152149
int_t xC = (p1.location_ind[0]+p2.location_ind[0])/2;
153150
int_t yC = (p1.location_ind[1]+p2.location_ind[1])/2;
154151
int_t zC = (p1.location_ind[2]+p2.location_ind[2])/2;
155152
int_t key = key_func(xC, yC, zC);
156-
Edge * edge;
157-
if(edges.count(key) == 0){
158-
edge = new Edge(p1, p2);
159-
edges[key] = edge;
160-
}
161-
else{
162-
edge = edges[key];
153+
auto [it, inserted] = edges.try_emplace(key, nullptr);
154+
if(inserted){
155+
// construct a new item at the emplaced location
156+
it->second = new Edge(p1, p2);
163157
}
164-
return edge;
158+
return it->second;
165159
};
166160

167161
Face * set_default_face(face_map_t& faces, Node& p1, Node& p2, Node& p3, Node& p4){
@@ -170,15 +164,12 @@ Face * set_default_face(face_map_t& faces, Node& p1, Node& p2, Node& p3, Node& p
170164
y = (p1.location_ind[1]+p2.location_ind[1]+p3.location_ind[1]+p4.location_ind[1])/4;
171165
z = (p1.location_ind[2]+p2.location_ind[2]+p3.location_ind[2]+p4.location_ind[2])/4;
172166
key = key_func(x, y, z);
173-
Face * face;
174-
if(faces.count(key) == 0){
175-
face = new Face(p1, p2, p3, p4);
176-
faces[key] = face;
177-
}
178-
else{
179-
face = faces[key];
167+
auto [it, inserted] = faces.try_emplace(key, nullptr);
168+
if(inserted){
169+
// construct a new item at the emplaced location
170+
it->second = new Face(p1, p2, p3, p4);
180171
}
181-
return face;
172+
return it->second;
182173
}
183174

184175
Cell::Cell(Node *pts[8], int_t ndim, int_t maxlevel){

‎setup.py

+21
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
metadata["install_requires"] = install_requires
7676
else:
7777
from setuptools.extension import Extension
78+
from setuptools.command.build_ext import build_ext
7879
from Cython.Build import cythonize
7980
import numpy as np
8081

@@ -103,6 +104,26 @@
103104
),
104105
]
105106

107+
class build_ext_cpp_standard(build_ext):
108+
# add compiler specific standard argument specifier
109+
def build_extension(self, ext):
110+
# This module requires c++17 standard
111+
if ext.name == "discretize._extensions.tree_ext":
112+
comp_type = self.compiler.compiler_type
113+
if comp_type == "msvc":
114+
std_arg = "/std:c++17"
115+
elif comp_type == "bcpp":
116+
raise Exception(
117+
"Must use cpp compiler that support C++17 standard."
118+
)
119+
else:
120+
std_arg = "-std=c++17"
121+
ext.extra_compile_args = [
122+
std_arg,
123+
]
124+
super().build_extension(ext)
125+
106126
metadata["ext_modules"] = cythonize(extensions)
127+
metadata["cmdclass"] = {"build_ext": build_ext_cpp_standard}
107128

108129
setup(**metadata)

0 commit comments

Comments
 (0)
Please sign in to comment.