simplify code
authorMichael Andres <ma@suse.de>
Tue, 30 Mar 2010 13:24:59 +0000 (15:24 +0200)
committerMichael Andres <ma@suse.de>
Tue, 30 Mar 2010 13:30:56 +0000 (15:30 +0200)
zypp/CountryCode.cc
zypp/LanguageCode.cc

index 4151f47..d34c2d5 100644 (file)
  *
 */
 #include <iostream>
-#include <map>
 
 #include "zypp/base/Logger.h"
 #include "zypp/base/String.h"
 #include "zypp/base/Gettext.h"
+#include "zypp/base/Tr1hash.h"
 
 #include "zypp/CountryCode.h"
 
@@ -31,7 +31,7 @@ namespace zypp
     /** Wrap static codemap data. */
     struct CodeMaps // singleton
     {
-      typedef std::map<std::string,std::string> CodeMap;
+      typedef std::tr1::unordered_map<std::string,std::string> CodeMap;
       typedef CodeMap::const_iterator Index;
 
       /** Return the CodeMap Index for \a code_r. */
@@ -50,37 +50,15 @@ namespace zypp
       /** Make shure the code is in the code maps and return it's index. */
       inline Index lookup( const std::string & code_r );
 
-      /** Return index of \a code_r, if it's in the code maps. */
-      inline Index lookupCode( const std::string & code_r );
-
     private:
-      /** Two letter codes. */
-      CodeMap iso3166;
-      /** All the stuff the application injects. */
-      CodeMap others;
+      /** All the codes. */
+      CodeMap codes;
     };
 
-    inline CodeMaps::Index CodeMaps::lookupCode( const std::string & code_r )
-    {
-      switch ( code_r.size() )
-        {
-        case 2:
-          {
-            Index it = iso3166.find( code_r );
-            if ( it != iso3166.end() )
-              return it;
-          }
-          break;
-        }
-      // not found: check others
-      // !!! not found at all returns others.end()
-      return others.find( code_r );
-    }
-
     inline CodeMaps::Index CodeMaps::lookup( const std::string & code_r )
     {
-      Index it = lookupCode( code_r );
-      if ( it != others.end() )
+      Index it = codes.find( code_r );
+      if ( it != codes.end() )
         return it;
 
       // not found: Remember a new code
@@ -95,13 +73,13 @@ namespace zypp
           WAR << "Malformed CountryCode '" << code_r << "' (not upper case)" << endl;
           // but maybe we're lucky with the upper case code
           // and find a country name.
-          it = lookupCode( lcode );
-          if ( it != others.end() )
+          it = codes.find( lcode );
+          if ( it != codes.end() )
             nval.second = it->second;
         }
 
       MIL << "Remember CountryCode '" << code_r << "': '" << nval.second << "'" << endl;
-      return others.insert( nval ).first;
+      return codes.insert( nval ).first;
     }
 
     /////////////////////////////////////////////////////////////////
@@ -212,7 +190,7 @@ namespace zypp
     CodeMaps::CodeMaps()
     {
       // Defined CountryCode constants
-      others[""]        = N_( "No Code" );
+      codes[""]        = N_( "No Code" );
 
       struct Init
       {
@@ -472,7 +450,7 @@ namespace zypp
       };
 
       for (const Init * i = init; i->iso3166 != NULL; ++i)
-         iso3166[i->iso3166] = i->name;
+         codes[i->iso3166] = i->name;
     }
 
     /////////////////////////////////////////////////////////////////
index fb8c053..ab368b5 100644 (file)
  *
 */
 #include <iostream>
-#include <map>
 
 #include "zypp/base/Logger.h"
 #include "zypp/base/String.h"
 #include "zypp/base/Gettext.h"
+#include "zypp/base/Tr1hash.h"
 
 #include "zypp/LanguageCode.h"
 
@@ -31,7 +31,7 @@ namespace zypp
     /** Wrap static codemap data. */
     struct CodeMaps // singleton
     {
-      typedef std::map<std::string,std::string> CodeMap;
+      typedef std::tr1::unordered_map<std::string,std::string> CodeMap;
       typedef CodeMap::const_iterator Index;
 
       /** Return the CodeMap Index for \a code_r. */
@@ -50,47 +50,15 @@ namespace zypp
       /** Make shure the code is in the code maps and return it's index. */
       inline Index lookup( const std::string & code_r );
 
-      /** Return index of \a code_r, if it's in the code maps. */
-      inline Index lookupCode( const std::string & code_r );
-
     private:
-      /** Two letter codes. */
-      CodeMap iso639_1;
-      /** Three letter codes. */
-      CodeMap iso639_2;
-      /** All the stuff the application injects. */
-      CodeMap others;
+      /** All the codes. */
+      CodeMap codes;
     };
 
-    inline CodeMaps::Index CodeMaps::lookupCode( const std::string & code_r )
-    {
-      switch ( code_r.size() )
-        {
-        case 2:
-          {
-            Index it = iso639_1.find( code_r );
-            if ( it != iso639_1.end() )
-              return it;
-          }
-          break;
-
-        case 3:
-          {
-            Index it = iso639_2.find( code_r );
-            if ( it != iso639_2.end() )
-              return it;
-          }
-          break;
-        }
-      // not found: check others
-      // !!! not found at all returns others.end()
-      return others.find( code_r );
-    }
-
     inline CodeMaps::Index CodeMaps::lookup( const std::string & code_r )
     {
-      Index it = lookupCode( code_r );
-      if ( it != others.end() )
+      Index it = codes.find( code_r );
+      if ( it != codes.end() )
         return it;
 
       // not found: Remember a new code
@@ -105,13 +73,13 @@ namespace zypp
           WAR << "Malformed LanguageCode '" << code_r << "' (not lower case)" << endl;
           // but maybe we're lucky with the lower case code
           // and find a language name.
-          it = lookupCode( lcode );
-          if ( it != others.end() )
+          it = codes.find( lcode );
+          if ( it != codes.end() )
             nval.second = it->second;
         }
 
       MIL << "Remember LanguageCode '" << code_r << "': '" << nval.second << "'" << endl;
-      return others.insert( nval ).first;
+      return codes.insert( nval ).first;
     }
 
     /////////////////////////////////////////////////////////////////
@@ -222,7 +190,7 @@ namespace zypp
     CodeMaps::CodeMaps()
     {
       // Defined LanguageCode constants
-      others[""]        = N_("No Code");
+      codes[""]        = N_("No Code");
 
       struct LangInit
       {
@@ -1230,9 +1198,10 @@ namespace zypp
 
       for (const LangInit * i = langInit; i->iso639_2 != NULL; ++i)
       {
-         iso639_2[i->iso639_2] = i->name;
+         std::string name( i->name );
+         codes[i->iso639_2] = name;
          if (i->iso639_1 != NULL)
-             iso639_1[i->iso639_1] = i->name;
+             codes[i->iso639_1] = name;
       }
     }