Skip to content

Commit

Permalink
Merge pull request #308 from jcapriot/try_emplace
Browse files Browse the repository at this point in the history
Switch to try_emplace
  • Loading branch information
jcapriot authored Mar 16, 2023
2 parents 9f58a50 + 2c05f80 commit d5a27bf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
39 changes: 15 additions & 24 deletions discretize/_extensions/tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,31 +137,25 @@ Face::Face(Node& p1, Node& p2, Node& p3, Node& p4){
Node * set_default_node(node_map_t& nodes, int_t x, int_t y, int_t z,
double *xs, double *ys, double *zs){
int_t key = key_func(x, y, z);
Node * point;
if(nodes.count(key) == 0){
point = new Node(x, y, z, xs, ys, zs);
nodes[key] = point;
auto [it, inserted] = nodes.try_emplace(key, nullptr);
if(inserted){
// construct a new item at the emplaced location
it->second = new Node(x, y, z, xs, ys, zs);
}
else{
point = nodes[key];
}
return point;
return it->second;
}

Edge * set_default_edge(edge_map_t& edges, Node& p1, Node& p2){
int_t xC = (p1.location_ind[0]+p2.location_ind[0])/2;
int_t yC = (p1.location_ind[1]+p2.location_ind[1])/2;
int_t zC = (p1.location_ind[2]+p2.location_ind[2])/2;
int_t key = key_func(xC, yC, zC);
Edge * edge;
if(edges.count(key) == 0){
edge = new Edge(p1, p2);
edges[key] = edge;
}
else{
edge = edges[key];
auto [it, inserted] = edges.try_emplace(key, nullptr);
if(inserted){
// construct a new item at the emplaced location
it->second = new Edge(p1, p2);
}
return edge;
return it->second;
};

Face * set_default_face(face_map_t& faces, Node& p1, Node& p2, Node& p3, Node& p4){
Expand All @@ -170,15 +164,12 @@ Face * set_default_face(face_map_t& faces, Node& p1, Node& p2, Node& p3, Node& p
y = (p1.location_ind[1]+p2.location_ind[1]+p3.location_ind[1]+p4.location_ind[1])/4;
z = (p1.location_ind[2]+p2.location_ind[2]+p3.location_ind[2]+p4.location_ind[2])/4;
key = key_func(x, y, z);
Face * face;
if(faces.count(key) == 0){
face = new Face(p1, p2, p3, p4);
faces[key] = face;
}
else{
face = faces[key];
auto [it, inserted] = faces.try_emplace(key, nullptr);
if(inserted){
// construct a new item at the emplaced location
it->second = new Face(p1, p2, p3, p4);
}
return face;
return it->second;
}

Cell::Cell(Node *pts[8], int_t ndim, int_t maxlevel){
Expand Down
21 changes: 21 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
metadata["install_requires"] = install_requires
else:
from setuptools.extension import Extension
from setuptools.command.build_ext import build_ext
from Cython.Build import cythonize
import numpy as np

Expand Down Expand Up @@ -103,6 +104,26 @@
),
]

class build_ext_cpp_standard(build_ext):
# add compiler specific standard argument specifier
def build_extension(self, ext):
# This module requires c++17 standard
if ext.name == "discretize._extensions.tree_ext":
comp_type = self.compiler.compiler_type
if comp_type == "msvc":
std_arg = "/std:c++17"
elif comp_type == "bcpp":
raise Exception(
"Must use cpp compiler that support C++17 standard."
)
else:
std_arg = "-std=c++17"
ext.extra_compile_args = [
std_arg,
]
super().build_extension(ext)

metadata["ext_modules"] = cythonize(extensions)
metadata["cmdclass"] = {"build_ext": build_ext_cpp_standard}

setup(**metadata)

0 comments on commit d5a27bf

Please sign in to comment.