static void test_boolean_ops (void (*function)(struct string_set *a, struct string_set *b, unsigned int *a_pat, unsigned int *b_pat)) { enum { MAX_STRINGS = 7 }; unsigned int a_pat, b_pat; for (a_pat = 0; a_pat < (1u << MAX_STRINGS); a_pat++) for (b_pat = 0; b_pat < (1u << MAX_STRINGS); b_pat++) { unsigned int new_a_pat = a_pat; unsigned int new_b_pat = b_pat; struct string_set a, b; int a_strings[MAX_STRINGS], b_strings[MAX_STRINGS]; size_t i, n_a, n_b; string_set_init (&a); string_set_init (&b); for (i = 0; i < MAX_STRINGS; i++) { if (a_pat & (1u << i)) string_set_insert (&a, make_string (i)); if (b_pat & (1u << i)) string_set_insert (&b, make_string (i)); } function (&a, &b, &new_a_pat, &new_b_pat); n_a = n_b = 0; for (i = 0; i < MAX_STRINGS; i++) { if (new_a_pat & (1u << i)) a_strings[n_a++] = i; if (new_b_pat & (1u << i)) b_strings[n_b++] = i; } check_string_set (&a, a_strings, n_a); check_string_set (&b, b_strings, n_b); string_set_destroy (&a); string_set_destroy (&b); } }
/* Initializes SET as a new string set that initially contains the same strings as OLD. */ void string_set_clone (struct string_set *set, const struct string_set *old) { const struct string_set_node *node; const char *s; string_set_init (set); hmap_reserve (&set->hmap, string_set_count (old)); STRING_SET_FOR_EACH (s, node, old) string_set_insert__ (set, xstrdup (s), node->hmap_node.hash); }
OBJECT * object_new( const char * string ) { #ifdef BJAM_NO_MEM_CACHE int l = strlen( string ); struct hash_item * m = (struct hash_item *)BJAM_MALLOC( sizeof(struct hash_header) + l + 1 ); strtotal += l + 1; memcpy( m->data, string, l + 1 ); m->header.magic = OBJECT_MAGIC; return (OBJECT *)m->data; #else if ( ! strhash.data ) string_set_init( &strhash ); strcount_in += 1; return (OBJECT *)string_set_insert( &strhash, string ); #endif }
OBJECT * object_new_range( char const * const string, int const size ) { ++strcount_in; #ifdef BJAM_NO_MEM_CACHE { struct hash_item * const m = (struct hash_item *)BJAM_MALLOC( sizeof( struct hash_header ) + size + 1 ); strtotal += size + 1; memcpy( m->data, string, size ); m->data[ size ] = '\0'; m->header.magic = OBJECT_MAGIC; return (OBJECT *)m->data; } #else if ( !strhash.data ) string_set_init( &strhash ); return (OBJECT *)string_set_insert( &strhash, string, size ); #endif }
/* Inserts the CNT strings from 0 to CNT - 1 (inclusive) into a set in the order specified by INSERTIONS, then deletes them in the order specified by DELETIONS, checking the set's contents for correctness after each operation. */ static void test_insert_delete (const int insertions[], const int deletions[], size_t cnt) { struct string_set set; size_t i; string_set_init (&set); check_string_set (&set, NULL, 0); for (i = 0; i < cnt; i++) { check (string_set_insert (&set, make_string (insertions[i]))); check_string_set (&set, insertions, i + 1); } for (i = 0; i < cnt; i++) { check (string_set_delete (&set, make_string (deletions[i]))); check_string_set (&set, deletions + i + 1, cnt - i - 1); } string_set_destroy (&set); }
/* Inserts strings into a set in ascending order, then delete in ascending order. */ static void test_insert_ordered (void) { const int max_elems = 64; int *values; struct string_set set; int i; string_set_init (&set); values = xnmalloc (max_elems, sizeof *values); for (i = 0; i < max_elems; i++) { values[i] = i; string_set_insert_nocopy (&set, xstrdup (make_string (i))); check_string_set (&set, values, i + 1); } for (i = 0; i < max_elems; i++) { string_set_delete (&set, make_string (i)); check_string_set (&set, values + i + 1, max_elems - i - 1); } string_set_destroy (&set); free (values); }