/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hashset.h"
#include "mountd.h"
/*
* HASHSET is hash table managing pointers to a set of keys
* (set is a collection without duplicates). The public interface
* of the HASHSET is similar to the java.util.Set interface.
* Unlike the libc `hsearch' based hash table, this implementation
* does allow multiple instances of HASHSET within a single application,
* and the HASHSET_ITERATOR allows to iterate through the entire set
* using h_next().
*
* HASHSET does not store actual keys but only pointers to keys. Hence the
* data remains intact when HASHSET grows (resizes itself). HASHSET accesses
* the actual key data only through the hash and equal functions given
* as arguments to h_create.
*
* Hash collisions are resolved with linked lists.
*/
typedef struct HashSetEntry {
} ENTRY;
struct HashSet {
int (*h_equal) (const void *, const void *);
};
struct HashSetIterator {
};
/*
* Create a HASHSET
* - HASHSET is a hash table of pointers to keys
* - duplicate keys are not allowed
* - the HASHSET is opaque and can be accessed only through the h_ functions
* - two keys k1 and k2 are considered equal if the result of equal(k1, k2)
* is non-zero
* - the function hash(key) is used to compute hash values for keys; if
* keys are "equal" the values returned by the hash function must be
* identical.
*/
int (*equal) (const void *, const void *),
float loadFactor)
{
HASHSET h;
if (initialCapacity == 0)
if (loadFactor < 0.0)
h = exmalloc(sizeof (*h));
if (h == NULL)
return (NULL);
free(h);
return (NULL);
}
h->h_tableSize = initialCapacity;
h->h_loadFactor = loadFactor;
h->h_count = 0;
return (h);
}
/*
* Return a pointer to a matching key
*/
const void *
{
ENTRY *e;
return (e->e_key);
return (NULL);
}
/*
* Rehash (grow) HASHSET
* - called when loadFactor exceeds threshold
* - new size is 2*old_size+1
*/
static void
{
uint_t i = h->h_tableSize;
while (i--) {
newtab[k] = e;
}
}
h->h_tableSize = newtabSize;
}
/*
* Store a key into a HASHSET
* - if there is already an "equal" key then the new key will not
* be stored and the function returns a pointer to an existing key
* - otherwise the function stores pointer to the new key and return NULL
*/
const void *
{
ENTRY *e;
return (key);
if (h->h_count >= h->h_threshold) {
rehash(h);
}
h->h_count++;
return (NULL);
}
/*
* Delete a key
* - if there is no "equal" key in the HASHSET the fuction returns NULL
* - otherwise the function returns a pointer to the deleted key
*/
const void *
{
if (prev)
else
free(e);
return (key);
}
}
return (NULL);
}
/*
* Return an opaque HASHSET_ITERATOR (to be used in h_next())
*/
{
HASHSET_ITERATOR i = exmalloc(sizeof (*i));
i->i_h = h;
i->i_indx = h->h_tableSize;
i->i_coll = 0;
return (i);
}
/*
* Return a pointer to a next key
*/
const void *
{
const void *key;
if (i->i_indx-- == 0)
return (NULL);
}
if (i->i_e)
i->i_coll++;
return (key);
}