AU: ApplyMap utility function
ApplyMap takes a vector<ValueType> and a map of
ValueType->ValueType. For each object in the collection that is a key
in the map, replace that element with the corresponding value in the
map.
E.g. ApplyMap({1, 2, 3, 4, 5}, {3->6, 5->7, 8->10}) changes the
collection to be: {1, 2, 6, 4, 7}.
BUG=7294
TEST=attached unittest
Review URL: http://codereview.chromium.org/3530011
diff --git a/utils.h b/utils.h
index a46ffef..bfe7378 100644
--- a/utils.h
+++ b/utils.h
@@ -182,6 +182,19 @@
}
}
+template<typename ValueType>
+void ApplyMap(std::vector<ValueType>* collection,
+ const std::map<ValueType, ValueType>& the_map) {
+ for (typename std::vector<ValueType>::iterator it = collection->begin();
+ it != collection->end(); ++it) {
+ typename std::map<ValueType, ValueType>::const_iterator map_it =
+ the_map.find(*it);
+ if (map_it != the_map.end()) {
+ *it = map_it->second;
+ }
+ }
+}
+
// Returns the currently booted device. "/dev/sda3", for example.
// This will not interpret LABEL= or UUID=. You'll need to use findfs
// or something with equivalent funcionality to interpret those.