Пример #1
0
const void * HashSet::add( const void * item )
{
   unsigned int hash = hashfn( item );
   unsigned index = hash % bucketCount;
   HashSetEntry *entry;
      
   /* search the item in the set*/
   for ( entry = buckets[index]; entry; entry = entry->next )
      if ( entry->hash == hash && comparefn ( entry->item, item ) == 0 )
         return item;
    
   /* if count exceeds threshold, rehash all the items */
   if ( count >= threshold ) 
   {
      this->rehash();
      index = hash % this->bucketCount;
   }
      
   /* save the new item */ 
   entry = ( HashSetEntry * )malloc( sizeof( HashSetEntry ) );
   entry->hash = hash;
   entry->item = (void *) item;
   entry->next = buckets[index];
   this->buckets[index] = entry;
   ++this->count;
   return NULL;
}   
Пример #2
0
int exists_in_list(list_t *list, void *data, 
		   int (*comparefn)(const void *, const void *))
{
	while (list) {
		if (!comparefn((void *) car(list), data))
			return 1;
		list = cdr(list);
	}
	return 0;
}
Пример #3
0
const void * HashSet::get( void * item )
{
   unsigned int hash = this->hashfn( item );
   unsigned int index = hash % this->bucketCount;
   HashSetEntry * entry;
     
   for ( entry = buckets[index]; entry; entry = entry->next )
   {   
      if ( entry->hash == hash && comparefn( entry->item, item ) == 0)
         return ( entry->item );
   }
   return (NULL);
}
Пример #4
0
const void * HashSet::remove( const void * item )
{
   unsigned int hash = hashfn( item );
   unsigned int indx = hash % bucketCount;
   HashSetEntry *entry, *prev;
     
   for ( entry = this->buckets[indx], prev = NULL; entry; prev = entry, entry = entry->next ) 
   {
      if ( entry->hash == hash && comparefn( entry->item, item )==0 ) 
      {
         item = entry->item;
         if ( prev )
            prev->next = entry->next;
         else
            this->buckets[indx] = entry->next;
         free( entry );
         --this->count;
         return ( item );
      }
   }
     
   return (NULL);
}