Exemple #1
0
int main( int argc, char **argv )
{
	hashset *hs = hashset_create();
      if ( hs != NULL )
      {
           char utmp[32];
           hashset_put( hs, str2ustr("banana",utmp,32) );
           hashset_put( hs, str2ustr("apple",utmp,32) );
           hashset_put( hs, str2ustr("pineapple",utmp,32) );
           hashset_put( hs, str2ustr("guava",utmp,32) );
           hashset_put( hs, str2ustr("watermelon",utmp,32) );
           hashset_put( hs, str2ustr("orange",utmp,32) );
           hashset_put( hs, str2ustr("starfruit",utmp,32) );
           hashset_put( hs, str2ustr("durian",utmp,32) );
           hashset_put( hs, str2ustr("cherry",utmp,32) );
           hashset_put( hs, str2ustr("apricot",utmp,32) );
           hashset_put( hs, str2ustr("peach",utmp,32) );
           hashset_put( hs, str2ustr("pear",utmp,32) );
           hashset_put( hs, str2ustr("nectarine",utmp,32) );
           hashset_put( hs, str2ustr("plum",utmp,32) );
           hashset_put( hs, str2ustr("grape",utmp,32) );
           hashset_put( hs, str2ustr("mandarin",utmp,32) );
           hashset_put( hs, str2ustr("lemon",utmp,32) );
           hashset_put( hs, str2ustr("clementine",utmp,32) );
           hashset_put( hs, str2ustr("cumquat",utmp,32) );
           hashset_put( hs, str2ustr("custard apple",utmp,32) );
           hashset_put( hs, str2ustr("asian pear",utmp,32) );
           hashset_put( hs, str2ustr("jakfruit",utmp,32) );
           hashset_put( hs, str2ustr("rambutan",utmp,32) );
           hashset_put( hs, str2ustr("lime",utmp,32) );
           hashset_put( hs, str2ustr("lychee",utmp,32) );
           hashset_put( hs, str2ustr("mango",utmp,32) );
           hashset_put( hs, str2ustr("mangosteen",utmp,32) );
           hashset_put( hs, str2ustr("avocado",utmp,32) );
           hashset_put( hs, str2ustr("grandilla",utmp,32) );
           hashset_put( hs, str2ustr("grumichama",utmp,32) );
           hashset_put( hs, str2ustr("breadfruit",utmp,32) );
		// repeats
		   hashset_put( hs, str2ustr("banana",utmp,32) );
           hashset_put( hs, str2ustr("apple",utmp,32) );
           hashset_put( hs, str2ustr("pineapple",utmp,32) );
           hashset_put( hs, str2ustr("guava",utmp,32) );
           hashset_put( hs, str2ustr("watermelon",utmp,32) );
           hashset_print( hs );
		printf("number of elements in set=%d\n",hashset_size(hs));
      }
}
Exemple #2
0
int main(int argc, char **argv) {
	char buf[BUF_SIZE];

	HashSet *hashset = hashset_new(&string_hashcode, &string_equals, &string_copy, &string_delete);
	if(!hashset) {
		fprintf(stderr, "hashset_new() returned NULL\n");
		return 1;
	}
        
	while(1) {
		printf("> ");
		fflush(stdout);

		if(fgets(buf, BUF_SIZE, stdin)==NULL)
			break;

		char *curr;
		char *next = buf;
		next_word(&curr, &next);

		if(!*curr) {
			//Blank line -- do nothing
			continue;
		} else if(!strcasecmp(curr, "a")) {
			next_word(&curr, &next);

			if(*(skip_ws(next))) {
				printf("Too many arguments\n");
			} else {
				if(hashset_add(hashset, (void *)curr))
					printf("Element added\n");
				else
					printf("Element already exists\n");
				hashset_print(hashset, stdout, &string_print);
				printf("\n");
			}
		} else if(!strcasecmp(curr, "c")) {
			next_word(&curr, &next);
			if(*(skip_ws(next))) {
				printf("Too many arguments\n");
			} else {
				printf(hashset_contains(hashset, (void *)curr) ? "true\n" : "false\n");
			}
		} else if(!strcasecmp(curr, "r")) {
			next_word(&curr, &next);
			if(*(skip_ws(next))) {
				printf("Too many arguments\n");
			} else {
				if(hashset_remove(hashset, (void *)curr))
					printf("Element removed\n");
				else
					printf("Element not found\n");
				hashset_print(hashset, stdout, &string_print);
				printf("\n");
			}
		} else if(!strcasecmp(curr, "x")) {
			next_word(&curr, &next);
			if(*(skip_ws(next))) {
				printf("Too many arguments\n");
			} else {
				hashset_delete(hashset);
				hashset = hashset_new(&string_hashcode, &string_equals, &string_copy, &string_delete);
				if(!hashset) {
					fprintf(stderr, "hashset_new() returned NULL\n");
					return 1;
				}
				hashset_print(hashset, stdout, &string_print);
				printf("\n");
			}
		} else if(!strcasecmp(curr, "s")) {
			if(*(skip_ws(next))) {
				printf("Too many arguments\n");
			} else {
				printf("%d\n", hashset_size(hashset));
			}
		} else if(!strcasecmp(curr, "p")) {
			if(*(skip_ws(next))) {
				printf("Too many arguments\n");
			} else {
				hashset_print(hashset, stdout, &string_print);
				printf("\n");
			}
		} else if (!strcasecmp(curr, "fa")) {
			next_word(&curr, &next);
			
			if (*(skip_ws(next))) {
				printf("Too many arguments\n");
			} else {
				if (add_file(hashset, curr))
					printf("error loading file.\n");
				else {
					hashset_print(hashset, stdout, &string_print);
					printf("\n");
				}
			}
		} else if (!strcasecmp(curr, "fr")) {
			next_word(&curr, &next);
			
			if (*(skip_ws(next))) {
				printf("Too many arguments\n");
			} else {
				if (remove_file(hashset, curr))
					printf("error loading file.\n");
				else {
					hashset_print(hashset, stdout, &string_print);
					printf("\n");
				}
			}
		} else if(!strcasecmp(curr, "quit")) {
			if(*(skip_ws(next))) {
				printf("Too many arguments\n");
			} else {
				break;
			}
		} else if(!strcasecmp(curr, "h") || !strcasecmp(curr, "help")) {
			print_help();
		} else {
			printf("Invalid command\n");
		}

		printf("\n");
	}
	
	if(ferror(stdin))
		perror("fgets() failed");

	hashset_delete(hashset);

	return 0;
}