/* An example for the C Hash Template.

   For latest version see: http://moonflare.com/code/ctl/hash.php

   Copyright (c) 2003, Derrick Coetzee
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

   - Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.

   - Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in
     the documentation and/or other materials provided with the
     distribution.

   - The name of Derrick Coetzee may not be used to endorse or promote
     products derived from this software without specific prior
     written permission.

   This software is provided by the copyright holders and contributors
   "as is" and any express or implied warranties, including, but not
   limited to, the implied warranties of merchantability and fitness
   for a particular purpose are disclaimed. In no event shall the
   copyright owner or contributors be liable for any direct, indirect,
   incidental, special, exemplary, or consequential damages
   (including, but not limited to, procurement of substitute goods or
   services; loss of use, data, or profits; or business interruption)
   however caused and on any theory of liability, whether in contract,
   strict liability, or tort (including negligence or otherwise)
   arising in any way out of the use of this software, even if advised
   of the possibility of such damage.
*/

#include <string.h>
#include <stdio.h>
#include "hash.h"

/* An excellent string hashing function, borrowed from Emacs 19.34 */
int hash_string(const char* str, const int hash_size)
{
    int hash = 0;
    int i;
    for (i=0; str[i] != '\0'; i++)
    {
	char c = str[i];
	if (c >= 0140)
	    c -= 40;
	hash = (hash<<3) + (hash>>28) + c;
    }
    return (hash & 07777777777) % hash_size;
}

int string_equal(const char* str1, const char* str2)
{
    return strcmp(str1, str2) == 0;
}

char* my_strdup(char* str)
{
    char* dup = (char *)malloc( (strlen(str)+1)*sizeof(char) );
    strcpy(dup, str);
    return dup;
}

DECLARE_HASH(string_int, char*, int)
DEFINE_HASH(string_int, char*, int, hash_string, string_equal)

int main(void)
{
    char buffer[256]; /* Prestring would be better */
    string_int_hash map = string_int_hash_new();
    
    for(;;)
    {
	fgets(buffer, 256, stdin);

	if (feof(stdin))
	    break;

	if (buffer[0] == '?')
	{
	    /* Get count of line in rest of buffer from hash */
	    int* pcount = string_int_hash_get(map, buffer + 1);
	    /* If it gave NULL, line wasn't in hash, so count is zero */
	    int count = pcount == NULL ? 0 : *pcount;
	    printf("%d occurrence%s seen\n", count, count==1 ? "" : "s");
	    continue;
	}
	else if (buffer[0] == '-')
	{
	    string_int_hash_remove(&map, buffer + 1);
	    puts("Removed");
	    continue;
	}

	/* Add to hash with count of 1 if not in hash, else increment */
	if (string_int_hash_contains_key(map, buffer))
	    string_int_hash_set(&map, buffer, *string_int_hash_get(map, buffer) + 1);
	else
	    string_int_hash_set(&map, my_strdup(buffer), 1);
    }

    /* Cleanup: Iterate over hash and free strdup'ed strings */
    HASH_ITERATE(string_int, map, iter,
	free(*string_int_hash_key(iter));
    )

    string_int_hash_clear(&map);
    return 0;
}
