Prevent storing redundant capture data

Some isolation nodes may capture the same registers. This commit will
check if the register info already exists in the capture data before
adding a new entry.

Signed-off-by: Zane Shelley <zshelle@us.ibm.com>
Change-Id: I3dcff15ca9efcf6ae6499f80333583610aba2538
diff --git a/src/hei_isolation_data.hpp b/src/hei_isolation_data.hpp
index f3a10ee..174fd2b 100644
--- a/src/hei_isolation_data.hpp
+++ b/src/hei_isolation_data.hpp
@@ -3,6 +3,7 @@
 #include <hei_signature.hpp>
 #include <util/hei_bit_string.hpp>
 
+#include <algorithm>
 #include <map>
 #include <memory>
 #include <vector>
@@ -44,6 +45,11 @@
         RegisterId_t regId;                    ///< 3-byte register ID
         Instance_t regInst;                    ///< 1-byte register instance
         std::shared_ptr<BitStringBuffer> data; ///< register data
+
+        bool operator==(const RegDumpEntry& r) const
+        {
+            return regId == r.regId && regInst == r.regInst && *data == *r.data;
+        }
     };
 
   private: // Instance variables
@@ -86,8 +92,26 @@
             // Make a copy of the register value.
             auto data = std::make_shared<BitStringBuffer>(*i_data);
 
-            // Add to the list.
-            iv_regDump[i_chip].emplace_back(i_regId, i_regInst, data);
+            // We'll want to avoid adding duplicate data if possible.
+            if (iv_regDump.end() == iv_regDump.find(i_chip))
+            {
+                // The chip doesn't exist in the map. Therefore, the data
+                // doesn't currently exist. So, add it.
+                iv_regDump[i_chip].emplace_back(i_regId, i_regInst, data);
+            }
+            else
+            {
+                // Search this chip for the data.
+                RegDumpEntry entry{i_regId, i_regInst, data};
+                auto itr = std::find(iv_regDump[i_chip].begin(),
+                                     iv_regDump[i_chip].end(), entry);
+
+                if (iv_regDump[i_chip].end() == itr)
+                {
+                    // The data doesn't currently exist. So, add it.
+                    iv_regDump[i_chip].push_back(entry);
+                }
+            }
         }
     }
 
diff --git a/test/bit_string_test.cpp b/test/bit_string_test.cpp
index 923d503..1c7290c 100644
--- a/test/bit_string_test.cpp
+++ b/test/bit_string_test.cpp
@@ -166,3 +166,23 @@
     ASSERT_EQ(field >> 32, (bsb >> 32).getFieldRight(0, 64));
     ASSERT_EQ(field << 32, (bsb << 32).getFieldRight(0, 64));
 }
+
+// operator ==
+TEST(BitStringTest, TestSet7)
+{
+    uint64_t field = 0x1234567890abcdef;
+
+    BitStringBuffer bsb0(64); // original
+    BitStringBuffer bsb1(64); // equal
+    BitStringBuffer bsb2(64); // not-equal
+    BitStringBuffer bsb3(32); // different size (not-equal)
+
+    bsb0.setFieldRight(0, 64, field);
+    bsb1.setFieldLeft(0, 64, field);
+    bsb2.setFieldRight(0, 64, field >> 1);
+    bsb3.setFieldRight(0, 32, field);
+
+    ASSERT_TRUE(bsb0 == bsb1);
+    ASSERT_FALSE(bsb0 == bsb2);
+    ASSERT_FALSE(bsb0 == bsb3);
+}