Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. #pragma once
  2. #include "Object.h"
  3. #include "QuickMap.h"
  4.  
  5.  
  6. typedef const char string_id[17];
  7. typedef unsigned long bin_id;
  8.  
  9. template <class T>
  10. class StringMap :
  11.     public Object {
  12. public:
  13.     StringMap(T* PO );
  14.     virtual ~StringMap( );
  15.  
  16.     void put( string_id str_id, T* pT );
  17.     T* get( string_id ) const;
  18.  
  19.     static bin_id calculateCheckSum( string_id );
  20.  
  21. private:
  22.    
  23.     QuickMap<bin_id, T*>* m_POMap;
  24.     T* m_POEmpty = NULL;
  25.  
  26. };
  27.  
  28. template<class T>
  29. inline StringMap<T>::StringMap(T* PO )
  30. {
  31.     m_POEmpty = PO;
  32.     m_POMap = new QuickMap<bin_id, T*>();
  33. }
  34.  
  35. template<class T>
  36. inline StringMap<T>::~StringMap( )
  37. {
  38.     delete m_POMap;
  39.  
  40. }
  41.  
  42. template<class T>
  43. inline void StringMap<T>::put( string_id str_id, T * pT )
  44. {
  45.     m_POMap->put( calculateCheckSum( str_id ), pT );
  46. }
  47.  
  48. template<class T>
  49. inline T * StringMap<T>::get( string_id  str_id) const
  50. {
  51.     bin_id id = calculateCheckSum( str_id );
  52.     if ( m_POMap->contains( id ) )
  53.         return m_POMap->get( id );
  54.  
  55.     return m_POEmpty;
  56. }
  57.  
  58. template<class T>
  59. inline bin_id StringMap<T>::calculateCheckSum( string_id  id)
  60. {
  61.     dword len = strlen( id );
  62.     bin_id result = 0;
  63.     byte b = 0;
  64.     for ( dword i = 0; i < len; i++ ) {
  65.         b = ( byte ) toupper( id[ i ] );
  66.         if ( isalnum( b ) )
  67.             result += (b * i * len) + b + len;
  68.     }
  69.  
  70.     return result;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement