netatalk: hash.h File Reference

netatalk 4.5.0
Free and Open Source Apple Filing Protocol (AFP) Server
Loading...
Searching...
No Matches
hash.h File Reference
`#include <limits.h>`
`#include <atalk/hash.h>`

Go to the source code of this file.

## Macros
#define | hash_isfull(H)
#define | hash_isempty(H)
#define | hash_count(H)
#define | hash_size(H)
#define | hnode_get(N)
#define | hnode_getkey(N)
#define | hnode_put(N, V)

## Functions
hash_t * | hash_create (hashcount_t, hash_comp_t, hash_fun_t)
| Create a dynamic hash table.
void | hash_set_allocator (hash_t *, hnode_alloc_t, hnode_free_t, void *)
void | hash_destroy (hash_t *)
void | hash_free_nodes (hash_t *)
void | hash_free (hash_t *)
hash_t * | hash_init (hash_t *, hashcount_t, hash_comp_t, hash_fun_t, hnode_t **, hashcount_t)
void | hash_insert (hash_t *, hnode_t *, const void *)
| Insert a node into the hash table.
hnode_t * | hash_lookup (hash_t *, const void *)
| Find a node in the hash table and return a pointer to it.
hnode_t * | hash_delete (hash_t *, hnode_t *)
| Delete the given node from the hash table. Since the chains are singly linked, we must locate the start of the node's chain and traverse.
int | hash_alloc_insert (hash_t *, const void *, void *)
void | hash_delete_free (hash_t *, hnode_t *)
void | hnode_put (hnode_t *, void *)
void * | hnode_get (hnode_t *)
const void * | hnode_getkey (hnode_t *)
hashcount_t | hash_count (hash_t *)
hashcount_t | hash_size (hash_t *)
int | hash_isfull (hash_t *)
int | hash_isempty (hash_t *)
void | hash_scan_begin (hscan_t *, hash_t *)
| Initialize a hash scanner.
hnode_t * | hash_scan_next (hscan_t *)
| Retrieve the next node from the hash table, and update the pointer for the next invocation of hash_scan_next().
hnode_t * | hash_scan_delete (hash_t *, hnode_t *)
void | hash_scan_delfree (hash_t *, hnode_t *)
int | hash_verify (hash_t *)
| Verify whether the given object is a valid hash table.
hnode_t * | hnode_create (void *)
hnode_t * | hnode_init (hnode_t *, void *)
void | hnode_destroy (hnode_t *)

## Macro Definition Documentation

## ◆ hash_count

#define hash_count | ( |  | H | ) |

Value:
((H)->hash_nodecount)

## ◆ hash_isempty

#define hash_isempty | ( |  | H | ) |

Value:
((H)->hash_nodecount == 0)

## ◆ hash_isfull

#define hash_isfull | ( |  | H | ) |

Value:
((H)->hash_nodecount == (H)->hash_maxcount)

## ◆ hash_size

#define hash_size | ( |  | H | ) |

Value:
((H)->hash_nchains)

## ◆ hnode_get

#define hnode_get | ( |  | N | ) |

Value:
((N)->hash_data)

## ◆ hnode_getkey

#define hnode_getkey | ( |  | N | ) |

Value:
((N)->hash_key)

## ◆ hnode_put

#define hnode_put | ( |  | N,
|  |  | V )

Value:
((N)->hash_data = (V))

## Function Documentation

## ◆ hash_alloc_insert()

int hash_alloc_insert | ( | hash_t * | hash,
|  | const void * | key,
|  | void * | data )
| extern

## ◆ hash_count()

hashcount_t hash_count | ( | hash_t * | hash | ) |
| extern

## ◆ hash_create()

hash_t * hash_create | ( | hashcount_t | maxcount,
|  | hash_comp_t | compfun,
|  | hash_fun_t | hashfun )
| extern

Create a dynamic hash table.

Both the hash table structure and the table itself are dynamically allocated. Furthermore, the table is extendible in that it will automatically grow as its load factor increases beyond a certain threshold.

Note
  1. If the number of bits in the hash_val_t type has not been computed yet, we do so here, because this is likely to be the first function that the user calls.
  2. Allocate a hash table control structure.
  3. If a hash table control structure is successfully allocated, we proceed to initialize it. Otherwise we return a null pointer.
  4. We try to allocate the table of hash chains.
  5. If we were able to allocate the hash chain table, we can finish initializing the hash structure and the table. Otherwise, we must backtrack by freeing the hash structure.
  6. INIT_SIZE should be a power of two. The high and low marks are always set to be twice the table size and half the table size respectively. When the number of nodes in the table grows beyond the high size (beyond load factor 2), it will double in size to cut the load factor down to about about 1. If the table shrinks down to or beneath load factor 0.5, it will shrink, bringing the load up to about 1. However, the table will never shrink beneath INIT_SIZE even if it's emptied.
  7. This indicates that the table is dynamically allocated and dynamically resized on the fly. A table that has this value set to zero is assumed to be statically allocated and will not be resized.
  8. The table of chains must be properly reset to all null pointers.

## ◆ hash_delete()

hnode_t * hash_delete | ( | hash_t * | hash,
|  | hnode_t * | node )
| extern

Delete the given node from the hash table. Since the chains are singly linked, we must locate the start of the node's chain and traverse.

Note
  1. The node must belong to this hash table, and its key must not have been tampered with.
  2. If this deletion will take the node count below the low mark, we shrink the table now.
  3. Determine which chain the node belongs to, and fetch the pointer to the first node in this chain.
  4. If the node being deleted is the first node in the chain, then simply update the chain head pointer.
  5. Otherwise advance to the node's predecessor, and splice out by updating the predecessor's next pointer.
  6. Indicate that the node is no longer in a hash table.

## ◆ hash_delete_free()

void hash_delete_free | ( | hash_t * | hash,
|  | hnode_t * | node )
| extern

## ◆ hash_destroy()

void hash_destroy | ( | hash_t * | hash | ) |
| extern

Free a dynamic hash table structure.

## ◆ hash_free()

void hash_free | ( | hash_t * |  | ) |
| extern

## ◆ hash_free_nodes()

void hash_free_nodes | ( | hash_t * | hash | ) |
| extern

Free every node in the hash using the hash->freenode() function pointer, and cause the hash to become empty.

## ◆ hash_init()

hash_t * hash_init | ( | hash_t * | ,
|  | hashcount_t | ,
|  | hash_comp_t | ,
|  | hash_fun_t | ,
|  | hnode_t ** | ,
|  | hashcount_t | )
| extern

## ◆ hash_insert()

void hash_insert | ( | hash_t * | hash,
|  | hnode_t * | node,
|  | const void * | key )
| extern

Insert a node into the hash table.

Note
  1. It's illegal to insert more than the maximum number of nodes. The client should verify that the hash table is not full before attempting an insertion.
  2. The same key may not be inserted into a table twice.
  3. If the table is dynamic and the load factor is already at >= 2, grow the table.
  4. We take the bottom N bits of the hash value to derive the chain index, where N is the base 2 logarithm of the size of the hash table.

## ◆ hash_isempty()

int hash_isempty | ( | hash_t * |  | ) |
| extern

## ◆ hash_isfull()

int hash_isfull | ( | hash_t * |  | ) |
| extern

## ◆ hash_lookup()

hnode_t * hash_lookup | ( | hash_t * | hash,
|  | const void * | key )
| externinline

Find a node in the hash table and return a pointer to it.

Note
  1. We hash the key and keep the entire hash value. As an optimization, when we descend down the chain, we can compare hash values first and only if hash values match do we perform a full key comparison.
  2. To locate the chain from among 2^N chains, we look at the lower N bits of the hash value by anding them with the current mask.
  3. Looping through the chain, we compare the stored hash value inside each node against our computed hash. If they match, then we do a full comparison between the unhashed keys. If these match, we have located the entry.

## ◆ hash_scan_begin()

void hash_scan_begin | ( | hscan_t * | scan,
|  | hash_t * | hash )
| extern

Initialize a hash scanner.

Reset the hash scanner so that the next element retrieved by hash_scan_next() shall be the first element on the first non-empty chain.

Note
  1. Locate the first non empty chain.
  2. If an empty chain is found, remember which one it is and set the next pointer to refer to its first element.
  3. Otherwise if a chain is not found, set the next pointer to NULL so that hash_scan_next() shall indicate failure.

## ◆ hash_scan_delete()

hnode_t * hash_scan_delete | ( | hash_t * | hash,
|  | hnode_t * | node )
| extern

Exactly like hash_delete, except does not trigger table shrinkage. This is to be used from within a hash table scan operation. See notes for hash_delete.

## ◆ hash_scan_delfree()

void hash_scan_delfree | ( | hash_t * | hash,
|  | hnode_t * | node )
| extern

Like hash_delete_free but based on hash_scan_delete.

## ◆ hash_scan_next()

hnode_t * hash_scan_next | ( | hscan_t * | scan | ) |
| extern

Retrieve the next node from the hash table, and update the pointer for the next invocation of hash_scan_next().

Note
  1. Remember the next pointer in a temporary value so that it can be returned.
  2. This assertion essentially checks whether the module has been properly initialized. The first point of interaction with the module should be either hash_create() or hash_init(), both of which set hash_val_t_bit to a non zero value.
  3. If the next pointer we are returning is not NULL, then the user is allowed to call hash_scan_next() again. We prepare the new next pointer for that call right now. That way the user is allowed to delete the node we are about to return, since we will no longer be needing it to locate the next node.
  4. If there is a next node in the chain (next->next), then that becomes the new next node, otherwise ...
  5. We have exhausted the current chain, and must locate the next subsequent non-empty chain in the table.
  6. If a non-empty chain is found, the first element of that chain becomes the new next node. Otherwise there is no new next node and we set the pointer to NULL so that the next time hash_scan_next() is called, a null pointer shall be immediately returned.

## ◆ hash_set_allocator()

void hash_set_allocator | ( | hash_t * | hash,
|  | hnode_alloc_t | al,
|  | hnode_free_t | fr,
|  | void * | context )
| extern

Select a different set of node allocator routines.

## ◆ hash_size()

hashcount_t hash_size | ( | hash_t * | hash | ) |
| extern

## ◆ hash_verify()

int hash_verify | ( | hash_t * | hash | ) |
| extern

Verify whether the given object is a valid hash table.

Note
  1. If the hash table is dynamic, verify whether the high and low expansion/shrinkage thresholds are powers of two.
  2. Count all nodes in the table, and test each hash value to see whether it is correct for the node's chain.

## ◆ hnode_create()

hnode_t * hnode_create | ( | void * |  | ) |
| extern

## ◆ hnode_destroy()

void hnode_destroy | ( | hnode_t * |  | ) |
| extern

## ◆ hnode_get()

void * hnode_get | ( | hnode_t * | node | ) |
| extern

## ◆ hnode_getkey()

const void * hnode_getkey | ( | hnode_t * | node | ) |
| extern

## ◆ hnode_init()

hnode_t * hnode_init | ( | hnode_t * | hnode,
|  | void * | data )
| extern

Initialize a client-supplied node

## ◆ hnode_put()

void hnode_put | ( | hnode_t * | ,
|  | void * | )
| extern