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.
diff --git a/utils_unittest.cc b/utils_unittest.cc
index 3514f8a..a5550d3 100644
--- a/utils_unittest.cc
+++ b/utils_unittest.cc
@@ -6,12 +6,14 @@
 #include <sys/types.h>
 #include <errno.h>
 
+#include <map>
 #include <string>
 #include <vector>
 
 #include "gtest/gtest.h"
 #include "update_engine/utils.h"
 
+using std::map;
 using std::string;
 using std::vector;
 
@@ -183,4 +185,24 @@
   }
 }
 
+TEST(UtilsTest, ApplyMapTest) {
+  int initial_values[] = {1, 2, 3, 4, 6};
+  vector<int> collection(&initial_values[0],
+                         initial_values + arraysize(initial_values));
+  EXPECT_EQ(arraysize(initial_values), collection.size());
+  int expected_values[] = {1, 2, 5, 4, 8};
+  map<int, int> value_map;
+  value_map[3] = 5;
+  value_map[6] = 8;
+  value_map[5] = 10;
+
+  utils::ApplyMap(&collection, value_map);
+
+  size_t index = 0;
+  for (vector<int>::iterator it = collection.begin(), e = collection.end();
+       it != e; ++it) {
+    EXPECT_EQ(expected_values[index++], *it);
+  }
+}
+
 }  // namespace chromeos_update_engine