Use std::move (C++11) in map and set adaptors where possible.

In the std::map adaptor, remove the complicated iterator/insert
code and go with the cpp11/unordered_map.hpp approach instead.
Makes the code more consistent, avoids an extra copy, and the
previous complexity was unnecessary since std::map only maps to
a single element per key, unlike std::multimap.
This commit is contained in:
Jakob Petsovits
2015-05-06 17:19:39 -04:00
committed by Jakob Petsovits
parent e8e3052d1a
commit 9725bac5c4
4 changed files with 31 additions and 14 deletions

View File

@@ -42,9 +42,9 @@ struct convert<std::unordered_map<K, V>> {
for(; p != pend; ++p) {
K key;
p->key.convert(key);
p->val.convert(tmp[key]);
p->val.convert(tmp[std::move(key)]);
}
tmp.swap(v);
v = std::move(tmp);
return o;
}
};
@@ -100,9 +100,9 @@ struct convert<std::unordered_multimap<K, V>> {
std::pair<K, V> value;
p->key.convert(value.first);
p->val.convert(value.second);
tmp.insert(value);
tmp.insert(std::move(value));
}
tmp.swap(v);
v = std::move(tmp);
return o;
}
};

View File

@@ -43,7 +43,7 @@ struct convert<std::unordered_set<T>> {
--p;
tmp.insert(p->as<T>());
}
tmp.swap(v);
v = std::move(tmp);
return o;
}
};
@@ -97,7 +97,7 @@ struct convert<std::unordered_multiset<T>> {
--p;
tmp.insert(p->as<T>());
}
tmp.swap(v);
v = std::move(tmp);
return o;
}
};

View File

@@ -115,16 +115,17 @@ struct convert<std::map<K, V> > {
for(; p != pend; ++p) {
K key;
p->key.convert(key);
typename std::map<K,V>::iterator it(tmp.lower_bound(key));
if(it != tmp.end() && !(key < it->first)) {
p->val.convert(it->second);
} else {
V val;
p->val.convert(val);
tmp.insert(it, std::pair<K,V>(key, val));
}
#if __cplusplus >= 201103L
p->val.convert(tmp[std::move(key)]);
#else
p->val.convert(tmp[key]);
#endif
}
#if __cplusplus >= 201103L
v = std::move(tmp);
#else
tmp.swap(v);
#endif
return o;
}
};
@@ -180,9 +181,17 @@ struct convert<std::multimap<K, V> > {
std::pair<K, V> value;
p->key.convert(value.first);
p->val.convert(value.second);
#if __cplusplus >= 201103L
tmp.insert(std::move(value));
#else
tmp.insert(value);
#endif
}
#if __cplusplus >= 201103L
v = std::move(tmp);
#else
tmp.swap(v);
#endif
return o;
}
};

View File

@@ -43,7 +43,11 @@ struct convert<std::set<T> > {
--p;
tmp.insert(p->as<T>());
}
#if __cplusplus >= 201103L
v = std::move(tmp);
#else
tmp.swap(v);
#endif
return o;
}
};
@@ -96,7 +100,11 @@ struct convert<std::multiset<T> > {
--p;
tmp.insert(p->as<T>());
}
#if __cplusplus >= 201103L
v = std::move(tmp);
#else
tmp.swap(v);
#endif
return o;
}
};