1 /*************************************************************************//**
3 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 ****************************************************************************/
17 /**************************************************************************//**
21 * @note No thread protection so you will need to use appropriate
22 * synchronization if use spans multiple threads.
24 ****************************************************************************/
31 #include "hashtable.h"
33 /**************************************************************************//**
34 * Hashtable initialization.
36 * Initialize the list supplied to be empty.
38 * @param size Size of hashtable
40 * @returns Hashtable pointer
41 ******************************************************************************/
42 /* Create a new hashtable. */
43 HASHTABLE_T *ht_create( size_t size ) {
45 HASHTABLE_T *hashtable = NULL;
48 if( size < 1 ) return NULL;
50 /* Allocate the table itself. */
51 if( ( hashtable = malloc( sizeof( HASHTABLE_T ) ) ) == NULL ) {
55 /* Allocate pointers to the head nodes. */
56 if( ( hashtable->table = malloc( sizeof( ENTRY_T * ) * size ) ) == NULL ) {
59 for( i = 0; i < size; i++ ) {
60 hashtable->table[i] = NULL;
63 hashtable->size = size;
68 /**************************************************************************//**
69 * Hash a string for a particular hash table.
71 * Initialize the list supplied to be empty.
73 * @param hashtable Pointer to the hashtable
77 ******************************************************************************/
78 size_t ht_hash( HASHTABLE_T *hashtable, char *key )
83 #ifdef HASHTABLE_USE_SIMPLE_HASH
84 for ( hash = i = 0; i < strlen(key); hash = hash << 8, hash += key[ i++ ] );
85 #else /* Use: Jenkins' "One At a Time Hash" === Perl "Like" Hashing */
86 // http://en.wikipedia.org/wiki/Jenkins_hash_function
87 for ( hash = i = 0; i < strlen(key); ++i ) {
88 hash += key[i], hash += ( hash << 10 ), hash ^= ( hash >> 6 );
90 hash += ( hash << 3 ), hash ^= ( hash >> 11 ), hash += ( hash << 15 );
93 return hash % hashtable->size;
97 /**************************************************************************//**
98 * Create a key-value pair.
100 * @param key key string
101 * @param value value string
103 * @returns hashtable entry
104 ******************************************************************************/
105 ENTRY_T *ht_newpair( char *key, void *value )
109 if( ( newpair = malloc( sizeof( ENTRY_T ) ) ) == NULL ) {
113 if( ( newpair->key = strdup( key ) ) == NULL ) {
117 if( ( newpair->value = value ) == NULL ) {
121 newpair->next = NULL;
126 /**************************************************************************//**
127 * Insert a key-value pair into a hash table.
129 * @param key key string
130 * @param value value string
133 ******************************************************************************/
134 void ht_set( HASHTABLE_T *hashtable, char *key, void *value ) {
136 ENTRY_T *newpair = NULL;
137 ENTRY_T *next = NULL;
138 ENTRY_T *last = NULL;
140 bin = ht_hash( hashtable, key );
142 next = hashtable->table[ bin ];
144 while( next != NULL && next->key != NULL && strcmp( key, next->key ) > 0 ) {
149 /* There's already a pair. Let's replace that string. */
150 if( next != NULL && next->key != NULL && strcmp( key, next->key ) == 0 ) {
153 next->value = value ;
155 /* Nope, could't find it. Time to grow a pair. */
157 newpair = ht_newpair( key, value );
159 /* We're at the start of the linked list in this bin. */
160 if( next == hashtable->table[ bin ] ) {
161 newpair->next = next;
162 hashtable->table[ bin ] = newpair;
164 /* We're at the end of the linked list in this bin. */
165 } else if ( next == NULL ) {
166 last->next = newpair;
168 /* We're in the middle of the list. */
170 newpair->next = next;
171 last->next = newpair;
176 /**************************************************************************//**
177 * Retrieve a key-value pair from a hash table.
179 * @param key key string
181 * @returns value string
182 ******************************************************************************/
183 void *ht_get( HASHTABLE_T *hashtable, char *key ) {
187 bin = ht_hash( hashtable, key );
189 /* Step through the bin, looking for our value. */
190 pair = hashtable->table[ bin ];
191 while( pair != NULL && pair->key != NULL && strcmp( key, pair->key ) > 0 ) {
195 /* Did we actually find anything? */
196 if( pair == NULL || pair->key == NULL || strcmp( key, pair->key ) != 0 ) {
206 int main( int argc, char **argv ) {
208 HASHTABLE_T *hashtable = ht_create( 65536 );
210 ht_set( hashtable, "key1", "inky" );
211 ht_set( hashtable, "key2", "pinky" );
212 ht_set( hashtable, "key3", "blinky" );
213 ht_set( hashtable, "key4", "floyd" );
215 printf( "%s\n", ht_get( hashtable, "key1" ) );
216 printf( "%s\n", ht_get( hashtable, "key2" ) );
217 printf( "%s\n", ht_get( hashtable, "key3" ) );
218 printf( "%s\n", ht_get( hashtable, "key4" ) );