/[pcsx2_0.9.7]/trunk/common/include/Utilities/HashMap.h
ViewVC logotype

Diff of /trunk/common/include/Utilities/HashMap.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 31 by william, Tue Sep 7 03:24:11 2010 UTC revision 62 by william, Tue Sep 7 11:08:22 2010 UTC
# Line 1  Line 1 
1    /*  PCSX2 - PS2 Emulator for PCs
2    *  Copyright (C) 2002-2010  PCSX2 Dev Team
3    *
4    *  PCSX2 is free software: you can redistribute it and/or modify it under the terms
5    *  of the GNU Lesser General Public License as published by the Free Software Found-
6    *  ation, either version 3 of the License, or (at your option) any later version.
7    *
8    *  PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9    *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10    *  PURPOSE.  See the GNU General Public License for more details.
11    *
12    *  You should have received a copy of the GNU General Public License along with PCSX2.
13    *  If not, see <http://www.gnu.org/licenses/>.
14    */
15    
16  #pragma once  #pragma once
17    
18  #include <google/type_traits.h>  #include <google/type_traits.h>
# Line 5  Line 20 
20  #include <google/dense_hash_map>  #include <google/dense_hash_map>
21  #include <google/sparsehash/densehashtable.h>  #include <google/sparsehash/densehashtable.h>
22    
23    #include <wx/string.h>
24    
25  namespace HashTools {  namespace HashTools {
26    
27  #define HashFriend(Key,T) friend class HashMap<Key,T>  #define HashFriend(Key,T) friend class HashMap<Key,T>
# Line 204  public: Line 221  public:
221                  return Hash( (const char *)src.data(), src.length() * sizeof( wchar_t ) );                  return Hash( (const char *)src.data(), src.length() * sizeof( wchar_t ) );
222          }          }
223    
224            hash_key_t operator()( const wxString& src ) const
225            {
226                    return Hash( (const char *)src.data(), src.length() * sizeof( wxChar ) );
227            }
228    
229          // Returns a hashcode for a character.          // Returns a hashcode for a character.
230          // This has function has been optimized to return an even distribution          // This has function has been optimized to return an even distribution
231          // across the range of an int value.  In theory that should be more rewarding          // across the range of an int value.  In theory that should be more rewarding
# Line 322  public: Line 344  public:
344                  hash_key_t key = (hash_key_t) addr;                  hash_key_t key = (hash_key_t) addr;
345                  return (hash_key_t)((key >> 3) * 2654435761ul);                  return (hash_key_t)((key >> 3) * 2654435761ul);
346          }          }
347            
348            
349  };  };
350    
351  /// <summary>  /// <summary>
# Line 528  public: Line 552  public:
552    
553          const T& GetValue( Key key ) const          const T& GetValue( Key key ) const
554          {          {
555                  return (this->find( key ))->second;                  return (find( key ))->second;
556          }          }
557  };  };
558    
# Line 546  public: Line 570  public:
570  ///   matter, don't use this class at all! Use the string-specialized classes <see cref="Dictionary" /> and  ///   matter, don't use this class at all! Use the string-specialized classes <see cref="Dictionary" /> and
571  ///   <see cref="UnicodeDictionary" />.  ///   <see cref="UnicodeDictionary" />.
572  /// </remarks>  /// </remarks>
573  template< class Key, class T >  template< class Key, class T, class HashFunctor=CommonHashClass >
574  class HashMap : public google::dense_hash_map<Key, T, CommonHashClass>  class HashMap : public google::dense_hash_map<Key, T, HashFunctor>
575  {  {
576          typedef typename google::dense_hash_map<Key, T, CommonHashClass> _parent;          DeclareNoncopyableObject( HashMap );
577    
578            typedef typename google::dense_hash_map<Key, T, HashFunctor> _parent;
579    
580  public:  public:
581          using _parent::operator[];          using _parent::operator[];
# Line 566  public: Line 592  public:
592          ///   are *not* used as actual values in the set.          ///   are *not* used as actual values in the set.
593          /// </remarks>          /// </remarks>
594          HashMap( const Key& emptyKey, const Key& deletedKey, int initialCapacity=33 ) :          HashMap( const Key& emptyKey, const Key& deletedKey, int initialCapacity=33 ) :
595                  google::dense_hash_map<Key, T, CommonHashClass>( initialCapacity )                  google::dense_hash_map<Key, T, HashFunctor>( initialCapacity )
596          {          {
597                  set_empty_key( emptyKey );                  set_empty_key( emptyKey );
598                  set_deleted_key( deletedKey );                  set_deleted_key( deletedKey );
# Line 580  public: Line 606  public:
606          ///   parameter.  This is a more favorable alternative to the indexer operator since the          ///   parameter.  This is a more favorable alternative to the indexer operator since the
607          ///   indexer implementation can and will create new entries for every request that          ///   indexer implementation can and will create new entries for every request that
608          /// </remarks>          /// </remarks>
609          void TryGetValue( const Key& key, T& outval ) const          bool TryGetValue( const Key& key, T& outval ) const
610          {          {
611                  const_iterator iter( find(key) );                  const_iterator iter( find(key) );
612                  if( iter != end() )                  if( iter != end() )
613                    {
614                          outval = iter->second;                          outval = iter->second;
615                            return true;
616                    }
617                    return false;
618          }          }
619    
620          const T& GetValue( Key key ) const          const T& GetValue( Key key ) const
621          {          {
622                  return (this->find( key ))->second;                  return (find( key ))->second;
623            }
624            
625            bool Find( Key key ) const
626            {
627                    return find(key) != end();
628          }          }
629  };  };
630    
# Line 607  class Dictionary : public HashMap<std::s Line 642  class Dictionary : public HashMap<std::s
642  public:  public:
643          virtual ~Dictionary() {}          virtual ~Dictionary() {}
644    
645          Dictionary( int initialCapacity=33, const std::string& emptyKey = "@@-EMPTY-@@", const std::string& deletedKey = "@@-DELETED-@@" ) :          Dictionary( int initialCapacity=33, const std::string& emptyKey = "@@-EMPTY-@@", const std::string& deletedKey = "@@-DELETED-@@" )
646                  HashMap<std::string, T>( emptyKey, deletedKey, initialCapacity)                  : HashMap<std::string, T>( emptyKey, deletedKey, initialCapacity)
647          {          {
648          }          }
 private:  
         Dictionary( const Dictionary& src ) {}  
649  };  };
650    
651  /// <summary>  /// <summary>
# Line 631  class UnicodeDictionary : public HashMap Line 664  class UnicodeDictionary : public HashMap
664  public:  public:
665          virtual ~UnicodeDictionary() {}          virtual ~UnicodeDictionary() {}
666    
667          UnicodeDictionary( int initialCapacity=33, const std::wstring& emptyKey = L"@@-EMPTY-@@", const std::wstring& deletedKey = L"@@-DELETED-@@" ) :          UnicodeDictionary( int initialCapacity=33, const std::wstring& emptyKey = L"@@-EMPTY-@@", const std::wstring& deletedKey = L"@@-DELETED-@@" )
668                  HashMap<std::wstring, T>( emptyKey, deletedKey, initialCapacity)                  : HashMap<std::wstring, T>( emptyKey, deletedKey, initialCapacity)
669          {          {
670          }          }
   
 private:  
         UnicodeDictionary( const UnicodeDictionary& src ) {}  
671  };  };
672    
673  }  }
674    
675    template< class T, class HashFunctor=HashTools::CommonHashClass >
676    class pxDictionary : public HashTools::HashMap<wxString, T, HashFunctor>
677    {
678    public:
679            virtual ~pxDictionary() {}
680    
681            pxDictionary( int initialCapacity=33, const wxString& emptyKey = L"@@-EMPTY-@@", const wxString& deletedKey = L"@@-DELETED-@@" )
682                    : HashTools::HashMap<wxString, T, HashFunctor>( emptyKey, deletedKey, initialCapacity)
683            {
684            }
685    };

Legend:
Removed from v.31  
changed lines
  Added in v.62

  ViewVC Help
Powered by ViewVC 1.1.22