Changeset 7799d1d6f07fd24cc0cf0b784340f994374fc5b1

Show
Ignore:
Timestamp:
04/23/08 11:39:11 (9 months ago)
Author:
David Balmain <dbalmain@…>
Parents:
b39568e8202274e6aed953a924659cef1f971d55
Children:
8f9e905705f715e2dc4745a098e9a1819271873a
git-committer:
David Balmain <dbalmain@gmail.com> / 2008-04-23T11:39:11Z+1000
Message:

Added hs_new_ptr which corresponds to h_new_ptr

hs_new_ptr creates a HashSet? which is great for storing interned strings. It
just uses the address as the hash so no string hash function is called.

Location:
c
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • c/include/hashset.h

    r48290f r7799d1  
    3434 
    3535/** 
    36  * Create a new FrtHashSet. The function will allocate a FrtHashSet Struct setting 
    37  * the functions used to hash the objects it will contain and the eq function. 
    38  * This should be used for non-string types. 
     36 * Create a new FrtHashSet. The function will allocate a FrtHashSet Struct 
     37 * setting the functions used to hash the objects it will contain and the eq 
     38 * function. This should be used for non-string types. 
    3939 * 
    4040 * @param hash function to hash objects added to the FrtHashSet 
     
    4949 
    5050/** 
    51  * Create a new FrtHashSet specifically for strings. This will create a FrtHashSet 
    52  * as if you used frt_hs_new with the standard string hash and eq functions. 
     51 * Create a new FrtHashSet specifically for strings. This will create a 
     52 * FrtHashSet as if you used frt_hs_new with the standard string hash and eq 
     53 * functions. 
    5354 * 
    5455 * @param free_elem function used to free elements as added to the FrtHashSet 
     
    5758 */ 
    5859extern FrtHashSet *frt_hs_new_str(frt_free_ft free_func); 
     60 
     61/** 
     62 * Create a new FrtHashSet specifically for pointers. Note that the only way 
     63 * two pointers will be considered equal is if they have the same address. So 
     64 * you can add the string "key" twice if it is stored at two different 
     65 * addresses. 
     66 * 
     67 * @param free_elem function used to free elements as added to the FrtHashSet 
     68 *   when the FrtHashSet if destroyed or duplicate elements are added to the Set 
     69 * @return a newly allocated FrtHashSet structure 
     70 */ 
     71extern FrtHashSet *frt_hs_new_ptr(frt_free_ft free_func); 
    5972 
    6073/** 
  • c/include/internal.h

    rb39568 r7799d1  
    546546#define hs_merge                                frt_hs_merge 
    547547#define hs_new                                  frt_hs_new 
     548#define hs_new_ptr                              frt_hs_new_ptr 
    548549#define hs_new_str                              frt_hs_new_str 
    549550#define hs_orig                                 frt_hs_orig 
  • c/src/hashset.c

    r48290f r7799d1  
    2929    HashSet *hs = hs_alloc(free_func); 
    3030    hs->ht = h_new_str((free_ft) NULL, NULL); 
     31    return hs; 
     32} 
     33 
     34HashSet *hs_new_ptr(free_ft free_func) 
     35{ 
     36    HashSet *hs = hs_alloc(free_func); 
     37    hs->ht = h_new_ptr(NULL); 
    3138    return hs; 
    3239} 
  • c/test/test_hashset.c

    r48290f r7799d1  
    11#include "hashset.h" 
     2#include "intern.h" 
    23#include "test.h" 
    34 
     
    5556    Aiequal(0, hs->size); 
    5657 
     58    hs_destroy(hs); 
     59} 
     60 
     61static void test_hs_ptr(TestCase *tc, void *data) 
     62{ 
     63    HashSet *hs = hs_new_ptr(NULL); 
     64    const char *word1 = intern("one"); 
     65    const char *word2 = intern("two"); 
     66    char *word_one = estrdup("one"); 
     67    (void)data; /* suppress unused argument warning */ 
     68 
     69    Aiequal(0, hs->size); 
     70 
     71    Aiequal(HASH_KEY_DOES_NOT_EXIST, hs_add(hs, (void *)word1)); 
     72    Aiequal(1, hs->size); 
     73    Aiequal(HASH_KEY_SAME, hs_exists(hs, word1)); 
     74    Aiequal(HASH_KEY_DOES_NOT_EXIST, hs_exists(hs, "one")); 
     75    Aiequal(HASH_KEY_SAME, hs_add(hs, (void *)word1)); 
     76    Aiequal(1, hs->size); 
     77 
     78    Aiequal(HASH_KEY_DOES_NOT_EXIST, hs_add(hs, (void *)word2)); 
     79    Aiequal(2, hs->size); 
     80    Aiequal(HASH_KEY_SAME, hs_exists(hs, word2)); 
     81    Aiequal(HASH_KEY_DOES_NOT_EXIST, hs_exists(hs, "two")); 
     82    Aiequal(HASH_KEY_SAME, hs_add(hs, (void *)word2)); 
     83    Aiequal(2, hs->size); 
     84 
     85    Aiequal(HASH_KEY_DOES_NOT_EXIST, hs_add(hs, (void *)word_one)); 
     86    Aiequal(3, hs->size); 
     87    Aiequal(HASH_KEY_SAME, hs_exists(hs, word_one)); 
     88    Aiequal(HASH_KEY_DOES_NOT_EXIST, hs_exists(hs, "one")); 
     89    Aiequal(HASH_KEY_SAME, hs_add(hs, (void *)word_one)); 
     90    Aiequal(3, hs->size); 
     91 
     92    free(word_one); 
    5793    hs_destroy(hs); 
    5894} 
     
    244280 
    245281    tst_run_test(suite, test_hs, NULL); 
     282    tst_run_test(suite, test_hs_ptr, NULL); 
    246283    tst_run_test(suite, test_hs_add_safe, NULL); 
    247284    tst_run_test(suite, test_hs_merge, NULL);