Skip to content

Commit 1263b08

Browse files
bradkingkwrobot
authored andcommitted
Merge topic 'cmDefinitions_cleanups'
1a47d36 cmDefinitions: Cleanups and optimizations Acked-by: Kitware Robot <[email protected]> Merge-request: !3666
2 parents 7891f69 + 1a47d36 commit 1263b08

File tree

2 files changed

+42
-35
lines changed

2 files changed

+42
-35
lines changed

Source/cmDefinitions.cxx

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
file Copyright.txt or https://cmake.org/licensing for details. */
33
#include "cmDefinitions.h"
44

5+
#include "cm_string_view.hxx"
6+
57
#include <assert.h>
6-
#include <set>
8+
#include <functional>
9+
#include <unordered_set>
710
#include <utility>
811

912
cmDefinitions::Def cmDefinitions::NoDef;
@@ -14,7 +17,7 @@ cmDefinitions::Def const& cmDefinitions::GetInternal(const std::string& key,
1417
{
1518
assert(begin != end);
1619
{
17-
MapType::iterator it = begin->Map.find(key);
20+
auto it = begin->Map.find(key);
1821
if (it != begin->Map.end()) {
1922
it->second.Used = true;
2023
return it->second;
@@ -56,33 +59,10 @@ bool cmDefinitions::HasKey(const std::string& key, StackIter begin,
5659
return false;
5760
}
5861

59-
void cmDefinitions::Set(const std::string& key, cm::string_view value)
60-
{
61-
this->Map[key] = Def(value);
62-
}
63-
64-
void cmDefinitions::Unset(const std::string& key)
65-
{
66-
this->Map[key] = Def();
67-
}
68-
69-
std::vector<std::string> cmDefinitions::UnusedKeys() const
70-
{
71-
std::vector<std::string> keys;
72-
keys.reserve(this->Map.size());
73-
// Consider local definitions.
74-
for (auto const& mi : this->Map) {
75-
if (!mi.second.Used) {
76-
keys.push_back(mi.first);
77-
}
78-
}
79-
return keys;
80-
}
81-
8262
cmDefinitions cmDefinitions::MakeClosure(StackIter begin, StackIter end)
8363
{
8464
cmDefinitions closure;
85-
std::set<std::string> undefined;
65+
std::unordered_set<cm::string_view> undefined;
8666
for (StackIter it = begin; it != end; ++it) {
8767
// Consider local definitions.
8868
for (auto const& mi : it->Map) {
@@ -92,7 +72,7 @@ cmDefinitions cmDefinitions::MakeClosure(StackIter begin, StackIter end)
9272
if (mi.second.Exists) {
9373
closure.Map.insert(mi);
9474
} else {
95-
undefined.insert(mi.first);
75+
undefined.emplace(mi.first);
9676
}
9777
}
9878
}
@@ -104,17 +84,40 @@ std::vector<std::string> cmDefinitions::ClosureKeys(StackIter begin,
10484
StackIter end)
10585
{
10686
std::vector<std::string> defined;
107-
std::set<std::string> bound;
87+
std::unordered_set<cm::string_view> bound;
10888

10989
for (StackIter it = begin; it != end; ++it) {
11090
defined.reserve(defined.size() + it->Map.size());
11191
for (auto const& mi : it->Map) {
11292
// Use this key if it is not already set or unset.
113-
if (bound.insert(mi.first).second && mi.second.Exists) {
93+
if (bound.emplace(mi.first).second && mi.second.Exists) {
11494
defined.push_back(mi.first);
11595
}
11696
}
11797
}
11898

11999
return defined;
120100
}
101+
102+
void cmDefinitions::Set(const std::string& key, cm::string_view value)
103+
{
104+
this->Map[key] = Def(value);
105+
}
106+
107+
void cmDefinitions::Unset(const std::string& key)
108+
{
109+
this->Map[key] = Def();
110+
}
111+
112+
std::vector<std::string> cmDefinitions::UnusedKeys() const
113+
{
114+
std::vector<std::string> keys;
115+
keys.reserve(this->Map.size());
116+
// Consider local definitions.
117+
for (auto const& mi : this->Map) {
118+
if (!mi.second.Used) {
119+
keys.push_back(mi.first);
120+
}
121+
}
122+
return keys;
123+
}

Source/cmDefinitions.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,30 @@ class cmDefinitions
2525
typedef cmLinkedTree<cmDefinitions>::iterator StackIter;
2626

2727
public:
28+
// -- Static member functions
29+
2830
static const std::string* Get(const std::string& key, StackIter begin,
2931
StackIter end);
3032

3133
static void Raise(const std::string& key, StackIter begin, StackIter end);
3234

3335
static bool HasKey(const std::string& key, StackIter begin, StackIter end);
3436

37+
static std::vector<std::string> ClosureKeys(StackIter begin, StackIter end);
38+
39+
static cmDefinitions MakeClosure(StackIter begin, StackIter end);
40+
41+
// -- Member functions
42+
3543
/** Set a value associated with a key. */
3644
void Set(const std::string& key, cm::string_view value);
3745

3846
/** Unset a definition. */
3947
void Unset(const std::string& key);
4048

49+
/** List of unused keys. */
4150
std::vector<std::string> UnusedKeys() const;
4251

43-
static std::vector<std::string> ClosureKeys(StackIter begin, StackIter end);
44-
45-
static cmDefinitions MakeClosure(StackIter begin, StackIter end);
46-
4752
private:
4853
/** String with existence boolean. */
4954
struct Def
@@ -61,8 +66,7 @@ class cmDefinitions
6166
};
6267
static Def NoDef;
6368

64-
typedef std::unordered_map<std::string, Def> MapType;
65-
MapType Map;
69+
std::unordered_map<std::string, Def> Map;
6670

6771
static Def const& GetInternal(const std::string& key, StackIter begin,
6872
StackIter end, bool raise);

0 commit comments

Comments
 (0)