[Lldb-commits] [lldb] r139477 - /lldb/trunk/include/lldb/Core/UniqueCStringMap.h

Greg Clayton gclayton at apple.com
Sat Sep 10 17:06:05 PDT 2011


Author: gclayton
Date: Sat Sep 10 19:06:05 2011
New Revision: 139477

URL: http://llvm.org/viewvc/llvm-project?rev=139477&view=rev
Log:
Added extra calls to the UniqueCStringMap to allow it to be used
more efficiently when it contains a large number of items. Since
the map is actually a vector of "const char *" and type T values,
it will double in size every time you append to it. The extra
added functions allow the collection to be sized to fit the data
after all entries have been appended, and lookups by name or by
regex have been built in to the class to allow efficient lookup.


Modified:
    lldb/trunk/include/lldb/Core/UniqueCStringMap.h

Modified: lldb/trunk/include/lldb/Core/UniqueCStringMap.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/UniqueCStringMap.h?rev=139477&r1=139476&r2=139477&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Core/UniqueCStringMap.h (original)
+++ lldb/trunk/include/lldb/Core/UniqueCStringMap.h Sat Sep 10 19:06:05 2011
@@ -15,6 +15,8 @@
 #include <algorithm>
 #include <vector>
 
+#include "lldb/Core/RegularExpression.h"
+
 namespace lldb_private {
 
 
@@ -103,17 +105,36 @@
     }
 
     //------------------------------------------------------------------
-    // Get an entry by index.
+    // Get an entries by index in a variety of forms.
     //
     // The caller is responsible for ensuring that the collection does
-    // not change during while using the returned pointer.
+    // not change during while using the returned values.
     //------------------------------------------------------------------
-    const T *
-    GetValueAtIndex (uint32_t idx) const
+    bool
+    GetValueAtIndex (uint32_t idx, T &value) const
     {
         if (idx < m_map.size())
-            return &m_map[idx].value;
-        return NULL;
+        {
+            value = m_map[idx].value;
+            return true;
+        }
+        return false;
+    }
+
+    // Use this function if you have simple types in your map that you
+    // can easily copy when accessing values by index.
+    T
+    GetValueAtIndexUnchecked (uint32_t idx) const
+    {
+        return m_map[idx].value;        
+    }
+
+    // Use this function if you have complex types in your map that you
+    // don't want to copy when accessing values by index.
+    const T &
+    GetValueRefAtIndexUnchecked (uint32_t idx) const
+    {
+        return m_map[idx].value;
     }
 
     const char *
@@ -155,7 +176,7 @@
     // not change during while using the returned pointer.
     //------------------------------------------------------------------
     const Entry *
-    FindNextValueForName (const char *unique_cstr, const Entry *entry_ptr) const
+    FindNextValueForName (const Entry *entry_ptr) const
     {
         if (!m_map.empty())
         {
@@ -164,13 +185,46 @@
             const Entry *next_entry = entry_ptr + 1;
             if (first_entry <= next_entry && next_entry < after_last_entry)
             {
-                if (next_entry->cstring == unique_cstr)
+                if (next_entry->cstring == entry_ptr->cstring)
                     return next_entry;
             }
         }
         return NULL;
     }
 
+    size_t
+    GetValues (const char *unique_cstr, std::vector<T> &values) const
+    {
+        const size_t start_size = values.size();
+
+        Entry search_entry (unique_cstr);
+        const_iterator pos, end = m_map.end();
+        for (pos = std::lower_bound (m_map.begin(), end, search_entry); pos != end; ++pos)
+        {
+            if (pos->cstring == unique_cstr)
+                values.push_back (pos->value);
+            else
+                break;
+        }
+
+        return values.size() - start_size;
+    }
+    
+    size_t
+    GetValues (const RegularExpression& regex, std::vector<T> &values) const
+    {
+        const size_t start_size = values.size();
+
+        const_iterator pos, end = m_map.end();
+        for (pos = m_map.begin(); pos != end; ++pos)
+        {
+            if (regex.Execute(pos->cstring))
+                values.push_back (pos->value);
+        }
+
+        return values.size() - start_size;
+    }
+
     //------------------------------------------------------------------
     // Get the total number of entries in this map.
     //------------------------------------------------------------------
@@ -219,6 +273,24 @@
     {
         std::sort (m_map.begin(), m_map.end());
     }
+    
+    //------------------------------------------------------------------
+    // Since we are using a vector to contain our items it will always 
+    // double its memory consumption as things are added to the vector,
+    // so if you intend to keep a UniqueCStringMap around and have
+    // a lot of entries in the map, you will want to call this function
+    // to create a new vector and copy _only_ the exact size needed as
+    // part of the finalization of the string map.
+    //------------------------------------------------------------------
+    void
+    SizeToFit ()
+    {
+        if (m_map.size() < m_map.capacity())
+        {
+            collection temp (m_map.begin(), m_map.end());
+            m_map.swap(temp);
+        }
+    }
 
 protected:
     typedef std::vector<Entry> collection;





More information about the lldb-commits mailing list