use make_unique where appropriate

use unordered_map/set where ordering isn't important (faster for searching and smaller footprint)
This commit is contained in:
Jonathan 2022-07-18 20:16:50 -04:00
parent 6f4f3a56ad
commit b6153e3084
10 changed files with 38 additions and 30 deletions

View File

@ -33,7 +33,7 @@ struct bouncelight_t
std::vector<qvec3f> poly;
std::vector<qvec4f> poly_edgeplanes;
qvec3f pos;
std::map<int, qvec3d> colorByStyle;
std::unordered_map<int, qvec3d> colorByStyle;
qvec3d componentwiseMaxColor; // cached maximum color in the colorByStyle, used for culling so we don't need to loop
// through colorByStyle
qvec3f surfnormal;

View File

@ -47,7 +47,7 @@ extern std::atomic<uint32_t> fully_transparent_lightmaps;
void PrintFaceInfo(const mface_t *face, const mbsp_t *bsp);
// FIXME: remove light param. add normal param and dir params.
vec_t GetLightValue(const settings::worldspawn_keys &cfg, const light_t *entity, vec_t dist);
std::map<int, qvec3f> GetDirectLighting(
std::unordered_map<int, qvec3f> GetDirectLighting(
const mbsp_t *bsp, const settings::worldspawn_keys &cfg, const qvec3d &origin, const qvec3d &normal);
void SetupDirt(settings::worldspawn_keys &cfg);
float DirtAtPoint(const settings::worldspawn_keys &cfg, raystream_intersection_t *rs, const qvec3d &point,

View File

@ -133,6 +133,22 @@ struct hashvert_t
size_t num;
};
struct VertexHash
{
size_t operator()(const qvec3i &v) const noexcept {
return (v[0] / 3) << 10 |
(static_cast<size_t>(v[1] / 3)) << 20 |
(v[2] / 3);
}
};
struct EdgeHash
{
size_t operator()(const std::pair<size_t, size_t> &pair) const noexcept {
return (pair.first << (sizeof(size_t) * 4)) | pair.second;
}
};
struct mapdata_t
{
/* Arrays of actual items */
@ -150,7 +166,7 @@ struct mapdata_t
std::unordered_map<int, std::vector<int>> planehash;
// hashed vertices; generated by EmitVertices
std::map<qvec3i, std::list<hashvert_t>> hashverts;
std::unordered_map<qvec3i, std::list<hashvert_t>, VertexHash> hashverts;
// find vector of points in hash closest to vec
inline auto find_hash_vector(const qvec3d &vec)
@ -189,7 +205,7 @@ struct mapdata_t
}
// hashed edges; generated by EmitEdges
std::map<std::pair<size_t, size_t>, int64_t> hashedges;
std::unordered_map<std::pair<size_t, size_t>, int64_t, EdgeHash> hashedges;
inline void add_hash_edge(size_t v1, size_t v2, int64_t i)
{

View File

@ -48,7 +48,7 @@ using namespace polylib;
mutex bouncelights_lock;
static std::vector<bouncelight_t> bouncelights;
std::map<int, std::vector<int>> bouncelightsByFacenum;
std::unordered_map<int, std::vector<int>> bouncelightsByFacenum;
static bool Face_ShouldBounce(const mbsp_t *bsp, const mface_t *face)
{
@ -91,7 +91,7 @@ qvec3b Face_LookupTextureColor(const mbsp_t *bsp, const mface_t *face)
return {127};
}
static void AddBounceLight(const qvec3d &pos, const std::map<int, qvec3d> &colorByStyle, const qvec3d &surfnormal,
static void AddBounceLight(const qvec3d &pos, const std::unordered_map<int, qvec3d> &colorByStyle, const qvec3d &surfnormal,
vec_t area, const mface_t *face, const mbsp_t *bsp)
{
for (const auto &styleColor : colorByStyle) {
@ -180,7 +180,7 @@ static void MakeBounceLightsThread(const settings::worldspawn_keys &cfg, const m
facemidpoint += faceplane.normal; // lift 1 unit
// average them, area weighted
map<int, qvec3d> sum;
std::unordered_map<int, qvec3d> sum;
for (const auto &lightmap : surf.lightmapsByStyle) {
for (const auto &sample : lightmap.samples) {
@ -206,7 +206,7 @@ static void MakeBounceLightsThread(const settings::worldspawn_keys &cfg, const m
qvec3d blendedcolor = mix(qvec3d{127. / 255.}, texturecolor, cfg.bouncecolorscale.value());
// final colors to emit
map<int, qvec3d> emitcolors;
std::unordered_map<int, qvec3d> emitcolors;
for (const auto &styleColor : sum) {
emitcolors[styleColor.first] = styleColor.second * blendedcolor;

View File

@ -171,7 +171,7 @@ static float AngleBetweenPoints(const qvec3f &p1, const qvec3f &p2, const qvec3f
}
static bool s_builtPhongCaches;
static std::map<const mface_t *, std::vector<face_normal_t>> vertex_normals;
static std::unordered_map<const mface_t *, std::vector<face_normal_t>> vertex_normals;
static map<const mface_t *, set<const mface_t *>> smoothFaces;
static map<int, vector<const mface_t *>> vertsToFaces;
static map<int, vector<const mface_t *>> planesToFaces;
@ -554,7 +554,7 @@ void CalculateVertexNormals(const mbsp_t *bsp)
std::copy(neighboursToSmooth.begin(), neighboursToSmooth.end(), std::back_inserter(fPlusNeighbours));
// global vertex index -> smoothed normal
std::map<int, face_normal_t> smoothedNormals;
std::unordered_map<int, face_normal_t> smoothedNormals;
// walk fPlusNeighbours
for (auto f2 : fPlusNeighbours) {

View File

@ -35,9 +35,7 @@ const maptexinfo_t& side_t::get_texinfo() const
std::unique_ptr<bspbrush_t> bspbrush_t::copy_unique() const
{
bspbrush_t *copy = new bspbrush_t{*this};
return std::unique_ptr<bspbrush_t>(copy);
return std::make_unique<bspbrush_t>(*this);
}
/*

View File

@ -101,7 +101,7 @@ Creates a new axial brush
*/
std::unique_ptr<bspbrush_t> BrushFromBounds(const aabb3d &bounds)
{
auto b = std::unique_ptr<bspbrush_t>(new bspbrush_t{});
auto b = std::make_unique<bspbrush_t>();
b->sides.resize(6);
for (int i = 0; i < 3; i++)
@ -858,9 +858,8 @@ static void BuildTree_r(node_t *node, std::vector<std::unique_ptr<bspbrush_t>> b
// allocate children before recursing
for (int i = 0; i < 2; i++)
{
auto* newnode = new node_t{};
auto &newnode = node->children[i] = std::make_unique<node_t>();
newnode->parent = node;
node->children[i] = std::unique_ptr<node_t>(newnode);
}
auto children_volumes = SplitBrush(node->volume->copy_unique(), node->planenum);
@ -881,7 +880,7 @@ BrushBSP
*/
static std::unique_ptr<tree_t> BrushBSP(mapentity_t *entity, std::vector<std::unique_ptr<bspbrush_t>> brushlist)
{
auto tree = std::unique_ptr<tree_t>(new tree_t{});
auto tree = std::make_unique<tree_t>();
logging::print(logging::flag::PROGRESS, "---- {} ----\n", __func__);
@ -895,7 +894,7 @@ static std::unique_ptr<tree_t> BrushBSP(mapentity_t *entity, std::vector<std::un
double volume = BrushVolume(*b);
if (volume < qbsp_options.microvolume.value())
{
logging::print("WARNING: microbrush");
logging::print("WARNING: microbrush\n");
// fixme-brushbsp: add entitynum, brushnum in mapbrush_t
// printf ("WARNING: entity %i, brush %i: microbrush\n",
// b->original->entitynum, b->original->brushnum);
@ -925,13 +924,13 @@ static std::unique_ptr<tree_t> BrushBSP(mapentity_t *entity, std::vector<std::un
* collision hull for the engine. Probably could be done a little
* smarter, but this works.
*/
auto headnode = std::unique_ptr<node_t>(new node_t{});
auto headnode = std::make_unique<node_t>();
headnode->bounds = entity->bounds;
headnode->children[0] = std::unique_ptr<node_t>(new node_t{});
headnode->children[0] = std::make_unique<node_t>();
headnode->children[0]->planenum = PLANENUM_LEAF;
headnode->children[0]->contents = qbsp_options.target_game->create_empty_contents();
headnode->children[0]->parent = headnode.get();
headnode->children[1] = std::unique_ptr<node_t>(new node_t{});
headnode->children[1] = std::make_unique<node_t>();
headnode->children[1]->planenum = PLANENUM_LEAF;
headnode->children[1]->contents = qbsp_options.target_game->create_empty_contents();
headnode->children[1]->parent = headnode.get();
@ -946,8 +945,7 @@ static std::unique_ptr<tree_t> BrushBSP(mapentity_t *entity, std::vector<std::un
logging::print(logging::flag::STAT, "{:5} visible faces\n", c_faces);
logging::print(logging::flag::STAT, "{:5} nonvisible faces\n", c_nonvisfaces);
auto node = std::unique_ptr<node_t>(new node_t{});
auto node = std::make_unique<node_t>();
node->volume = BrushFromBounds(tree->bounds.grow(SIDESPACE));

View File

@ -49,7 +49,7 @@ MergeFace.
*/
std::unique_ptr<face_t> NewFaceFromFace(const face_t *in)
{
auto newf = std::unique_ptr<face_t>(new face_t{});
auto newf = std::make_unique<face_t>();
newf->planenum = in->planenum;
newf->texinfo = in->texinfo;

View File

@ -482,7 +482,7 @@ static std::unique_ptr<face_t> FaceFromPortal(portal_t *p, int pside)
if (!side)
return nullptr; // portal does not bridge different visible contents
auto f = std::unique_ptr<face_t>(new face_t{});
auto f = std::make_unique<face_t>();
f->texinfo = side->texinfo;
f->planenum = side->planenum;

View File

@ -30,11 +30,7 @@
portal_t *tree_t::create_portal()
{
auto *result = new portal_t{};
portals.push_back(std::unique_ptr<portal_t>(result));
return result;
return portals.emplace_back(std::make_unique<portal_t>()).get();
}
/*