_string_hash.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (c) 1997-1999
  3. * Silicon Graphics Computer Systems, Inc.
  4. *
  5. * Copyright (c) 1999
  6. * Boris Fomitchev
  7. *
  8. * This material is provided "as is", with absolutely no warranty expressed
  9. * or implied. Any use is at your own risk.
  10. *
  11. * Permission to use or copy this software for any purpose is hereby granted
  12. * without fee, provided the above notices are retained on all copies.
  13. * Permission to modify the code and to distribute modified code is granted,
  14. * provided the above notices are retained, and a notice that the code was
  15. * modified is included with the above copyright notice.
  16. *
  17. */
  18. #ifndef _STLP_STRING_HASH_H
  19. #define _STLP_STRING_HASH_H
  20. #ifndef _STLP_HASH_FUN_H
  21. # include <stl/_hash_fun.h>
  22. #endif
  23. #ifndef _STLP_INTERNAL_STRING_H
  24. # include <stl/_string.h>
  25. #endif
  26. _STLP_BEGIN_NAMESPACE
  27. template <class _CharT, class _Traits, class _Alloc>
  28. _STLP_INLINE_LOOP size_t
  29. __stl_string_hash(const basic_string<_CharT,_Traits,_Alloc>& __s) {
  30. unsigned long __h = 0;
  31. size_t __len = __s.size();
  32. const _CharT* __data = __s.data();
  33. for ( size_t __i = 0; __i < __len; ++__i)
  34. __h = /* 5 *__h */(__h << 2) + __h + __data[__i];
  35. return size_t(__h);
  36. }
  37. #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) && \
  38. (!defined(__BORLANDC__) || (__BORLANDC__ >= 0x560))
  39. template <class _CharT, class _Traits, class _Alloc>
  40. struct hash<basic_string<_CharT,_Traits,_Alloc> > {
  41. size_t operator()(const basic_string<_CharT,_Traits,_Alloc>& __s) const
  42. { return __stl_string_hash(__s); }
  43. };
  44. #else
  45. _STLP_TEMPLATE_NULL
  46. struct _STLP_CLASS_DECLSPEC hash<string> {
  47. size_t operator()(const string& __s) const
  48. { return __stl_string_hash(__s); }
  49. };
  50. # if defined (_STLP_HAS_WCHAR_T)
  51. _STLP_TEMPLATE_NULL
  52. struct _STLP_CLASS_DECLSPEC hash<wstring> {
  53. size_t operator()(const wstring& __s) const
  54. { return __stl_string_hash(__s); }
  55. };
  56. # endif
  57. #endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */
  58. _STLP_END_NAMESPACE
  59. #endif