use string_view to avoid temporary string allocation.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / builder / dictionary.h
index 22d2afe..65c7e18 100644 (file)
  */
 
 #include <dali/public-api/common/vector-wrapper.h>
+
 #include <algorithm>
+#include <string_view>
 
 namespace Dali
 {
-extern bool CaseInsensitiveStringCompare( const std::string& a, const std::string& b );
+extern bool CaseInsensitiveStringCompare(std::string_view a, std::string_view b);
 
 namespace Toolkit
 {
@@ -36,6 +38,23 @@ namespace Internal
  *
  * It enables lookup of keys via case-insensitive match.
  */
+
+
+typedef std::vector<std::string> DictionaryKeys;
+inline void Merge( DictionaryKeys& toDict, const DictionaryKeys& fromDict )
+{
+  for( DictionaryKeys::const_iterator fromIter = fromDict.begin(); fromIter != fromDict.end(); ++fromIter )
+  {
+    const std::string& fromKey = (*fromIter);
+    DictionaryKeys::iterator toIter = std::find( toDict.begin(), toDict.end(), fromKey );
+    if( toIter == toDict.end() )
+    {
+      toDict.push_back( fromKey );
+    }
+  }
+}
+
+
 template<typename EntryType>
 class Dictionary
 {
@@ -62,7 +81,6 @@ public:
    */
   typedef typename Elements::const_iterator iterator;
 
-
   /**
    * Constructor
    */
@@ -105,46 +123,57 @@ public:
   }
 
   /**
-   * Find the element in the dictionary pointed at by key, and
-   * return a pointer to it, or NULL.
+   * Remove a key value pair from the dictionary.
    */
-  EntryType* Find( const std::string& key ) const
+  void Remove( const std::string& name )
   {
-    EntryType* result=NULL;
-
-    if( ! key.empty() )
+    for( typename Elements::iterator iter = container.begin(); iter != container.end(); ++iter )
     {
-      for( typename Elements::iterator iter = container.begin(); iter != container.end(); ++iter )
+      if( iter->key == name )
       {
-        if( iter->key == key )
-        {
-          result = &(iter->entry);
-          break;
-        }
+        container.erase( iter );
+        break;
       }
     }
-    return result;
   }
 
   /**
-   * Find the element in the dictionary pointed at by key, and
-   * return a pointer to it, or NULL
+   * Remove a key value pair from the dictionary.
    */
-  EntryType* Find( const char* key ) const
+  void Remove( const char* name )
   {
-    if( key != NULL )
+    if( name != NULL )
     {
-      std::string theKey(key);
-      return Find(theKey);
+      std::string theName(name);
+      Remove(theName);
+    }
+  }
+
+  void Merge( const Dictionary<EntryType>& dictionary )
+  {
+    for( typename Elements::const_iterator fromIter = dictionary.container.begin(); fromIter != dictionary.container.end(); ++fromIter )
+    {
+      bool found=false;
+      for( typename Elements::iterator toIter = container.begin(); toIter != container.end(); ++toIter )
+      {
+        if( fromIter->key == toIter->key )
+        {
+          found=true;
+          toIter->entry = fromIter->entry;
+        }
+      }
+      if( !found )
+      {
+        container.push_back( Element(fromIter->key, fromIter->entry) );
+      }
     }
-    return NULL;
   }
 
   /**
-   * Find the element in the dictionary pointed at by key using a case
+   * Find the element in the dictionary pointed at by key, and
    * insensitive search, and return a const pointer to it, or NULL
    */
-  const EntryType* FindCaseInsensitiveC( const std::string& key ) const
+  const EntryType* FindConst( const std::string& key ) const
   {
     if( ! key.empty() )
     {
@@ -164,7 +193,7 @@ public:
    * Find the element in the dictionary pointed at by key using a case
    * insensitive search, and return a non-const pointer to it, or NULL
    */
-  EntryType* FindCaseInsensitive( const std::string& key ) const
+  EntryType* Find( const std::string& key ) const
   {
     EntryType* result = NULL;
     if( ! key.empty() )
@@ -185,12 +214,12 @@ public:
    * Find the element in the dictionary pointed at by key using a case
    * insensitive search, and return a const pointer to it, or NULL
    */
-  const EntryType* FindCaseInsensitiveC( const char* key ) const
+  const EntryType* FindConst( const char* key ) const
   {
     if( key != NULL )
     {
       std::string theKey(key);
-      return FindCaseInsensitiveC( theKey );
+      return FindConst( theKey );
     }
     return NULL;
   }
@@ -199,16 +228,15 @@ public:
    * Find the element in the dictionary pointed at by key using a case
    * insensitive search, and return a non-const pointer to it, or NULL
    */
-  EntryType* FindCaseInsensitive( const char* key ) const
+  EntryType* Find( const char* key ) const
   {
     if( key != NULL )
     {
       std::string theKey(key);
-      return FindCaseInsensitive( theKey );
+      return Find( theKey );
     }
     return NULL;
   }
-
   /**
    * Return an iterator pointing at the first entry in the dictionary
    */
@@ -224,6 +252,20 @@ public:
   {
     return container.end();
   }
+
+  void GetKeys( DictionaryKeys& keys ) const
+  {
+    keys.clear();
+    for( typename Elements::const_iterator iter = container.begin(); iter != container.end(); ++iter )
+    {
+      keys.push_back( (*iter).key );
+    }
+  }
+
+  void Clear()
+  {
+    container.clear();
+  }
 };