_hash_fun.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 1996-1998
  3. * Silicon Graphics Computer Systems, Inc.
  4. *
  5. * Permission to use, copy, modify, distribute and sell this software
  6. * and its documentation for any purpose is hereby granted without fee,
  7. * provided that the above copyright notice appear in all copies and
  8. * that both that copyright notice and this permission notice appear
  9. * in supporting documentation. Silicon Graphics makes no
  10. * representations about the suitability of this software for any
  11. * purpose. It is provided "as is" without express or implied warranty.
  12. *
  13. *
  14. * Copyright (c) 1994
  15. * Hewlett-Packard Company
  16. *
  17. * Permission to use, copy, modify, distribute and sell this software
  18. * and its documentation for any purpose is hereby granted without fee,
  19. * provided that the above copyright notice appear in all copies and
  20. * that both that copyright notice and this permission notice appear
  21. * in supporting documentation. Hewlett-Packard Company makes no
  22. * representations about the suitability of this software for any
  23. * purpose. It is provided "as is" without express or implied warranty.
  24. *
  25. */
  26. /* NOTE: This is an internal header file, included by other STL headers.
  27. * You should not attempt to use it directly.
  28. */
  29. #ifndef _STLP_HASH_FUN_H
  30. #define _STLP_HASH_FUN_H
  31. #ifndef _STLP_INTERNAL_CSTDDEF
  32. # include <stl/_cstddef.h>
  33. #endif
  34. _STLP_BEGIN_NAMESPACE
  35. template <class _Key> struct hash { };
  36. _STLP_MOVE_TO_PRIV_NAMESPACE
  37. inline size_t __stl_hash_string(const char* __s) {
  38. _STLP_FIX_LITERAL_BUG(__s)
  39. unsigned long __h = 0;
  40. for ( ; *__s; ++__s)
  41. __h = 5*__h + *__s;
  42. return size_t(__h);
  43. }
  44. _STLP_MOVE_TO_STD_NAMESPACE
  45. _STLP_TEMPLATE_NULL
  46. struct hash<char*> {
  47. size_t operator()(const char* __s) const {
  48. _STLP_FIX_LITERAL_BUG(__s)
  49. return _STLP_PRIV __stl_hash_string(__s);
  50. }
  51. };
  52. _STLP_TEMPLATE_NULL
  53. struct hash<const char*> {
  54. size_t operator()(const char* __s) const {
  55. _STLP_FIX_LITERAL_BUG(__s)
  56. return _STLP_PRIV __stl_hash_string(__s);
  57. }
  58. };
  59. _STLP_TEMPLATE_NULL struct hash<char> {
  60. size_t operator()(char __x) const { return __x; }
  61. };
  62. _STLP_TEMPLATE_NULL struct hash<unsigned char> {
  63. size_t operator()(unsigned char __x) const { return __x; }
  64. };
  65. #if !defined (_STLP_NO_SIGNED_BUILTINS)
  66. _STLP_TEMPLATE_NULL struct hash<signed char> {
  67. size_t operator()(unsigned char __x) const { return __x; }
  68. };
  69. #endif
  70. _STLP_TEMPLATE_NULL struct hash<short> {
  71. size_t operator()(short __x) const { return __x; }
  72. };
  73. _STLP_TEMPLATE_NULL struct hash<unsigned short> {
  74. size_t operator()(unsigned short __x) const { return __x; }
  75. };
  76. _STLP_TEMPLATE_NULL struct hash<int> {
  77. size_t operator()(int __x) const { return __x; }
  78. };
  79. #if !defined (_STLP_MSVC) || (_STLP_MSVC < 1300) || defined (_WIN64)
  80. _STLP_TEMPLATE_NULL struct hash<unsigned int> {
  81. size_t operator()(unsigned int __x) const { return __x; }
  82. };
  83. #else
  84. /* MSVC .Net since 2002 has a 64 bits portability warning feature. typedef
  85. * like size_t are tagged as potential 64 bits variables making them different from
  86. * unsigned int. To avoid the warning when a hash container is instanciated with
  87. * the size_t key we prefer to grant the size_t specialization rather than the
  88. * unsigned int one.
  89. */
  90. _STLP_TEMPLATE_NULL struct hash<size_t> {
  91. size_t operator()(size_t __x) const { return __x; }
  92. };
  93. #endif
  94. _STLP_TEMPLATE_NULL struct hash<long> {
  95. size_t operator()(long __x) const { return __x; }
  96. };
  97. _STLP_TEMPLATE_NULL struct hash<unsigned long> {
  98. size_t operator()(unsigned long __x) const { return __x; }
  99. };
  100. #if defined (_STLP_LONG_LONG)
  101. _STLP_TEMPLATE_NULL struct hash<_STLP_LONG_LONG> {
  102. size_t operator()(_STLP_LONG_LONG x) const { return (size_t)x; }
  103. };
  104. _STLP_TEMPLATE_NULL struct hash<unsigned _STLP_LONG_LONG> {
  105. size_t operator()(unsigned _STLP_LONG_LONG x) const { return (size_t)x; }
  106. };
  107. #endif
  108. _STLP_TEMPLATE_NULL
  109. struct hash<void *>
  110. {
  111. union __vp {
  112. size_t s;
  113. void *p;
  114. };
  115. size_t operator()(void *__x) const
  116. {
  117. __vp vp;
  118. vp.p = __x;
  119. return vp.s;
  120. }
  121. };
  122. _STLP_END_NAMESPACE
  123. #endif /* _STLP_HASH_FUN_H */
  124. // Local Variables:
  125. // mode:C++
  126. // End: