/*
 * Faculty of Applied Science and Engineering, University of Toronto
 * CSC181: Introduction to Computer Programming, Fall 2000
 *
 * Assignment: 3
 * File: dictionary.c
 * Author: Ray Ortigas (rayo@dgp.toronto.edu)
 * Contains: Function definitions for Dictionary module.
 */

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "dictionary.h"

/* Returns an empty dictionary. */
DictionaryPtr dictionaryInit(void) {
	DictionaryPtr result = malloc(sizeof(Dictionary));
	assert(result != NULL);

	result->entries = bstInit(stringCompare);
	return result;
}

/* If no entry exists for word w in dictionary di, then an entry is
   added to di matching w to definition d. Otherwise, d is added to the
   list of definitions for w. */
void dictionaryPut
(DictionaryPtr di, const StringPtr w, const StringPtr d) {
	ListPtr definitions = bstRetrieve(di->entries, w);
	if (definitions == NULL) {
		definitions = listInit();
		bstInsert(di->entries, w, definitions);
	}
	listAppend(definitions, d);
}

/* Returns the list of definitions for w if it has an entry in di, NULL
   otherwise. */
ListPtr dictionaryGetDefinitions
(DictionaryPtr di, const StringPtr w) {
	return bstRetrieve(di->entries, w);
}
