1b88b2bbce4cfa78e37f303e55c66fd43719cbc4cZach Johnson/******************************************************************************
2b88b2bbce4cfa78e37f303e55c66fd43719cbc4cZach Johnson *
3b88b2bbce4cfa78e37f303e55c66fd43719cbc4cZach Johnson *  Copyright (C) 2014 Google, Inc.
4b88b2bbce4cfa78e37f303e55c66fd43719cbc4cZach Johnson *
5b88b2bbce4cfa78e37f303e55c66fd43719cbc4cZach Johnson *  Licensed under the Apache License, Version 2.0 (the "License");
6b88b2bbce4cfa78e37f303e55c66fd43719cbc4cZach Johnson *  you may not use this file except in compliance with the License.
7b88b2bbce4cfa78e37f303e55c66fd43719cbc4cZach Johnson *  You may obtain a copy of the License at:
8b88b2bbce4cfa78e37f303e55c66fd43719cbc4cZach Johnson *
9b88b2bbce4cfa78e37f303e55c66fd43719cbc4cZach Johnson *  http://www.apache.org/licenses/LICENSE-2.0
10b88b2bbce4cfa78e37f303e55c66fd43719cbc4cZach Johnson *
11b88b2bbce4cfa78e37f303e55c66fd43719cbc4cZach Johnson *  Unless required by applicable law or agreed to in writing, software
12b88b2bbce4cfa78e37f303e55c66fd43719cbc4cZach Johnson *  distributed under the License is distributed on an "AS IS" BASIS,
13b88b2bbce4cfa78e37f303e55c66fd43719cbc4cZach Johnson *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14b88b2bbce4cfa78e37f303e55c66fd43719cbc4cZach Johnson *  See the License for the specific language governing permissions and
15b88b2bbce4cfa78e37f303e55c66fd43719cbc4cZach Johnson *  limitations under the License.
16b88b2bbce4cfa78e37f303e55c66fd43719cbc4cZach Johnson *
17b88b2bbce4cfa78e37f303e55c66fd43719cbc4cZach Johnson ******************************************************************************/
18b88b2bbce4cfa78e37f303e55c66fd43719cbc4cZach Johnson
1918b53d2e194a71e390020ddc5ffb10cce2d64ed3Chris Manton#include <string.h>
2018b53d2e194a71e390020ddc5ffb10cce2d64ed3Chris Manton
210f9b91e150e153229235c163861198e23600e636Sharvil Nanavati#include "osi/include/hash_functions.h"
22b88b2bbce4cfa78e37f303e55c66fd43719cbc4cZach Johnson
23b88b2bbce4cfa78e37f303e55c66fd43719cbc4cZach Johnsonhash_index_t hash_function_naive(const void *key) {
24b88b2bbce4cfa78e37f303e55c66fd43719cbc4cZach Johnson  return (hash_index_t)key;
25b88b2bbce4cfa78e37f303e55c66fd43719cbc4cZach Johnson}
26b88b2bbce4cfa78e37f303e55c66fd43719cbc4cZach Johnson
27c0e2f9927b8d60123b388c3d117e8f82c90d46e3Zach Johnsonhash_index_t hash_function_integer(const void *key) {
28c0e2f9927b8d60123b388c3d117e8f82c90d46e3Zach Johnson  return ((hash_index_t)key) * 2654435761;
29c0e2f9927b8d60123b388c3d117e8f82c90d46e3Zach Johnson}
30c0e2f9927b8d60123b388c3d117e8f82c90d46e3Zach Johnson
31c0e2f9927b8d60123b388c3d117e8f82c90d46e3Zach Johnsonhash_index_t hash_function_pointer(const void *key) {
32b88b2bbce4cfa78e37f303e55c66fd43719cbc4cZach Johnson  return ((hash_index_t)key) * 2654435761;
33b88b2bbce4cfa78e37f303e55c66fd43719cbc4cZach Johnson}
3418b53d2e194a71e390020ddc5ffb10cce2d64ed3Chris Manton
3518b53d2e194a71e390020ddc5ffb10cce2d64ed3Chris Mantonhash_index_t hash_function_string(const void *key) {
3618b53d2e194a71e390020ddc5ffb10cce2d64ed3Chris Manton  hash_index_t hash = 5381;
3718b53d2e194a71e390020ddc5ffb10cce2d64ed3Chris Manton  const char *name = (const char *)key;
3818b53d2e194a71e390020ddc5ffb10cce2d64ed3Chris Manton  size_t string_len = strlen(name);
3918b53d2e194a71e390020ddc5ffb10cce2d64ed3Chris Manton  for (size_t i = 0; i < string_len; ++i)
4018b53d2e194a71e390020ddc5ffb10cce2d64ed3Chris Manton    hash = ((hash << 5) + hash ) + name[i];
4118b53d2e194a71e390020ddc5ffb10cce2d64ed3Chris Manton  return hash;
4218b53d2e194a71e390020ddc5ffb10cce2d64ed3Chris Manton}
43