function.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* HashKit
  2. * Copyright (C) 2010 Brian Aker
  3. * All rights reserved.
  4. *
  5. * Use and distribution licensed under the BSD license. See
  6. * the COPYING file in the parent directory for full text.
  7. */
  8. #include <libhashkit/common.h>
  9. static hashkit_return_t _set_function(struct hashkit_st::hashkit_function_st *self, hashkit_hash_algorithm_t hash_algorithm)
  10. {
  11. if (self == NULL)
  12. {
  13. return HASHKIT_INVALID_ARGUMENT;
  14. }
  15. switch (hash_algorithm)
  16. {
  17. case HASHKIT_HASH_MD5:
  18. self->function= hashkit_md5;
  19. break;
  20. case HASHKIT_HASH_CRC:
  21. self->function= hashkit_crc32;
  22. break;
  23. case HASHKIT_HASH_FNV1_64:
  24. if (libhashkit_has_algorithm(HASHKIT_HASH_FNV1_64))
  25. {
  26. self->function= hashkit_fnv1_64;
  27. break;
  28. }
  29. return HASHKIT_INVALID_ARGUMENT;
  30. case HASHKIT_HASH_FNV1A_64:
  31. if (libhashkit_has_algorithm(HASHKIT_HASH_FNV1_64))
  32. {
  33. self->function= hashkit_fnv1a_64;
  34. break;
  35. }
  36. return HASHKIT_INVALID_ARGUMENT;
  37. case HASHKIT_HASH_FNV1_32:
  38. self->function= hashkit_fnv1_32;
  39. break;
  40. case HASHKIT_HASH_FNV1A_32:
  41. self->function= hashkit_fnv1a_32;
  42. break;
  43. case HASHKIT_HASH_HSIEH:
  44. if (libhashkit_has_algorithm(HASHKIT_HASH_HSIEH))
  45. {
  46. self->function= hashkit_hsieh;
  47. break;
  48. }
  49. return HASHKIT_INVALID_ARGUMENT;
  50. case HASHKIT_HASH_MURMUR3:
  51. case HASHKIT_HASH_MURMUR:
  52. if (libhashkit_has_algorithm(HASHKIT_HASH_MURMUR))
  53. {
  54. self->function= hashkit_murmur;
  55. break;
  56. }
  57. return HASHKIT_INVALID_ARGUMENT;
  58. case HASHKIT_HASH_JENKINS:
  59. self->function= hashkit_jenkins;
  60. break;
  61. case HASHKIT_HASH_CUSTOM:
  62. return HASHKIT_INVALID_ARGUMENT;
  63. case HASHKIT_HASH_DEFAULT:
  64. self->function= hashkit_one_at_a_time;
  65. break;
  66. case HASHKIT_HASH_MAX:
  67. self->function= hashkit_one_at_a_time;
  68. return HASHKIT_INVALID_HASH;
  69. }
  70. self->context= NULL;
  71. return HASHKIT_SUCCESS;
  72. }
  73. hashkit_return_t hashkit_set_function(hashkit_st *self, hashkit_hash_algorithm_t hash_algorithm)
  74. {
  75. return _set_function(&self->base_hash, hash_algorithm);
  76. }
  77. hashkit_return_t hashkit_set_distribution_function(hashkit_st *self, hashkit_hash_algorithm_t hash_algorithm)
  78. {
  79. return _set_function(&self->distribution_hash, hash_algorithm);
  80. }
  81. static hashkit_return_t _set_custom_function(struct hashkit_st::hashkit_function_st *self, hashkit_hash_fn function, void *context)
  82. {
  83. if (self == NULL)
  84. {
  85. return HASHKIT_INVALID_ARGUMENT;
  86. }
  87. if (function)
  88. {
  89. self->function= function;
  90. self->context= context;
  91. return HASHKIT_SUCCESS;
  92. }
  93. return HASHKIT_FAILURE;
  94. }
  95. hashkit_return_t hashkit_set_custom_function(hashkit_st *self, hashkit_hash_fn function, void *context)
  96. {
  97. if (self == NULL)
  98. {
  99. return HASHKIT_INVALID_ARGUMENT;
  100. }
  101. return _set_custom_function(&self->base_hash, function, context);
  102. }
  103. hashkit_return_t hashkit_set_custom_distribution_function(hashkit_st *self, hashkit_hash_fn function, void *context)
  104. {
  105. if (self == NULL)
  106. {
  107. return HASHKIT_INVALID_ARGUMENT;
  108. }
  109. return _set_custom_function(&self->distribution_hash, function, context);
  110. }
  111. static hashkit_hash_algorithm_t get_function_type(const hashkit_hash_fn function)
  112. {
  113. if (function == hashkit_one_at_a_time)
  114. {
  115. return HASHKIT_HASH_DEFAULT;
  116. }
  117. else if (function == hashkit_md5)
  118. {
  119. return HASHKIT_HASH_MD5;
  120. }
  121. else if (function == hashkit_crc32)
  122. {
  123. return HASHKIT_HASH_CRC;
  124. }
  125. else if (function == hashkit_fnv1_64)
  126. {
  127. return HASHKIT_HASH_FNV1_64;
  128. }
  129. else if (function == hashkit_fnv1a_64)
  130. {
  131. return HASHKIT_HASH_FNV1A_64;
  132. }
  133. else if (function == hashkit_fnv1_32)
  134. {
  135. return HASHKIT_HASH_FNV1_32;
  136. }
  137. else if (function == hashkit_fnv1a_32)
  138. {
  139. return HASHKIT_HASH_FNV1A_32;
  140. }
  141. else if (function == hashkit_hsieh)
  142. {
  143. return HASHKIT_HASH_HSIEH;
  144. }
  145. else if (function == hashkit_murmur)
  146. {
  147. return HASHKIT_HASH_MURMUR;
  148. }
  149. else if (function == hashkit_jenkins)
  150. {
  151. return HASHKIT_HASH_JENKINS;
  152. }
  153. return HASHKIT_HASH_CUSTOM;
  154. }
  155. hashkit_hash_algorithm_t hashkit_get_function(const hashkit_st *self)
  156. {
  157. if (self == NULL)
  158. {
  159. return HASHKIT_HASH_DEFAULT;
  160. }
  161. return get_function_type(self->base_hash.function);
  162. }
  163. hashkit_hash_algorithm_t hashkit_get_distribution_function(const hashkit_st *self)
  164. {
  165. if (self == NULL)
  166. {
  167. return HASHKIT_HASH_DEFAULT;
  168. }
  169. return get_function_type(self->distribution_hash.function);
  170. }