From 8f85b0e194fbb427f04b9c0cb7ab50c004d48567 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sat, 28 May 2011 09:49:22 -0400 Subject: [PATCH] Utils - combine - Implemented combine, which takes two integer vectors and combines them resulting in a single vector contianing the unique values of both. --- src/util.cpp | 21 +++++++++++++++++++++ src/util.h | 1 + test/util.t.cpp | 29 ++++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/util.cpp b/src/util.cpp index 6648a28b2..9726eba20 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -381,6 +381,27 @@ std::string compressIds (const std::vector & ids) return result.str (); } +//////////////////////////////////////////////////////////////////////////////// +void combine (std::vector & dest, const std::vector & source) +{ + // Create a map using the sequence elements as keys. This will create a + // unique list, with no duplicates. + std::map both; + std::vector ::iterator i1; + for (i1 = dest.begin (); i1 != dest.end (); ++i1) both[*i1] = 0; + + std::vector ::const_iterator i2; + for (i2 = source.begin (); i2 != source.end (); ++i2) both[*i2] = 0; + + // Now make a sequence out of the keys of the map. + dest.clear (); + std::map ::iterator i3; + for (i3 = both.begin (); i3 != both.end (); ++i3) + dest.push_back (i3->first); + + std::sort (dest.begin (), dest.end ()); +} + //////////////////////////////////////////////////////////////////////////////// // Run an external executable with execvp. This means stdio goes to // the child process, so that it can receive user input (e.g. passwords). diff --git a/src/util.h b/src/util.h index b1410bcca..41e616381 100644 --- a/src/util.h +++ b/src/util.h @@ -72,6 +72,7 @@ int execute (const std::string&, std::vector); #endif std::string compressIds (const std::vector &); +void combine (std::vector &, const std::vector &); #endif //////////////////////////////////////////////////////////////////////////////// diff --git a/test/util.t.cpp b/test/util.t.cpp index a9b8ff0f6..a7d1ba423 100644 --- a/test/util.t.cpp +++ b/test/util.t.cpp @@ -34,7 +34,7 @@ Context context; //////////////////////////////////////////////////////////////////////////////// int main (int argc, char** argv) { - UnitTest t (19); + UnitTest t (30); // TODO bool confirm (const std::string&); // TODO int confirm3 (const std::string&); @@ -85,6 +85,33 @@ int main (int argc, char** argv) output = taskDifferences (right, rightAgain); t.ok (output.find ("No changes will be made") != std::string::npos, "No changes detected"); + // void combine (std::vector &, const std::vector &); + std::vector vleft; + vleft.push_back (1); + vleft.push_back (2); + vleft.push_back (3); + + std::vector vright; + vright.push_back (4); + + combine (vleft, vright); + t.is (vleft.size (), (size_t)4, "1,2,3 + 4 -> [4]"); + t.is (vleft[0], 1, "1,2,3 + 4 -> 1,2,3,4"); + t.is (vleft[1], 2, "1,2,3 + 4 -> 1,2,3,4"); + t.is (vleft[2], 3, "1,2,3 + 4 -> 1,2,3,4"); + t.is (vleft[3], 4, "1,2,3 + 4 -> 1,2,3,4"); + + vright.push_back (3); + vright.push_back (5); + combine (vleft, vright); + + t.is (vleft.size (), (size_t)5, "1,2,3,4 + 3,4,5 -> [5]"); + t.is (vleft[0], 1, "1,2,3,4 + 3,4,5 -> 1,2,3,4,5"); + t.is (vleft[1], 2, "1,2,3,4 + 3,4,5 -> 1,2,3,4,5"); + t.is (vleft[2], 3, "1,2,3,4 + 3,4,5 -> 1,2,3,4,5"); + t.is (vleft[3], 4, "1,2,3,4 + 3,4,5 -> 1,2,3,4,5"); + t.is (vleft[4], 5, "1,2,3,4 + 3,4,5 -> 1,2,3,4,5"); + return 0; }