Esempio n. 1
0
/*
 insert the following values into a hashLink, you must create this hashLink but
 only after you confirm that this key does not already exist in the table. For example, you
 cannot have two hashLinks for the word "taco".

 if a hashLink already exists in the table for the key provided you should
 replace that hashLink--this requires freeing up the old memory pointed by hashLink->value
 and then pointing hashLink->value to value v.

 also, you must monitor the load factor and resize when the load factor is greater than
 or equal LOAD_FACTOR_THRESHOLD (defined in hashMap.h).
 */
void insertMap (struct hashMap * ht, KeyType k, ValueType v)
{

	int hash;
	if(HASHING_FUNCTION == 1){
		hash = stringHash1(k);
	}
	if(HASHING_FUNCTION == 2){
		hash = stringHash2(k);
	}

	if(tableLoad(ht) >= LOAD_FACTOR_THRESHOLD){
		_setTableSize(ht, ht->tableSize *2);
	}
	int idx;
	idx = hash % ht->tableSize;

	if (containsKey(ht, k)) {
		ht->table[idx]->value++;
	}
	else {
		struct hashLink *newLink = (struct hashLink *)malloc(sizeof(struct hashLink));
		newLink->value = v;
		newLink->key = k;
		newLink->next = ht->table[idx];
		ht->table[idx] = newLink;
		ht->count++;
	}
}
Esempio n. 2
0
bool
sd::core::AttributeKey::addNewKey(const std::string& name) {
  if(containsKey(name))
    return false;
  m_existingKeys.push_back(AttributeKey(name));
  return true;
}
Esempio n. 3
0
/*
 find the hashlink for the supplied key and remove it, also freeing the memory
 for that hashlink. it is not an error to be unable to find the hashlink, if it
 cannot be found do nothing (or print a message) but do not use an assert which
 will end your program.
 */
void removeKey (struct hashMap * ht, KeyType k)
{  
   assert(ht != NULL);
   int loc = stringHash1(k) % ht->tableSize;
   if(containsKey(ht, k)){
      
      struct hashLink* pLink = ht->table[loc];
      struct hashLink* pLink2 = pLink;
      while(strcmp(pLink->key,k)){
	 pLink2 = pLink;
	 pLink = pLink->next;
      }

      if(pLink == pLink2){
	  ht->table[loc] = pLink->next;
      } else {
	  pLink2->next = pLink->next;
      }	 
      free(pLink->key);
      free(pLink);

   
   } else {
      //Do nothing	
   }
}
Esempio n. 4
0
/*
 this returns the value (which is void*) stored in a hashLink specified by the key k.
 
 if the user supplies the key "taco" you should find taco in the hashTable, then
 return the value member of the hashLink that represents taco.
 
 if the supplied key is not in the hashtable return NULL.
 */
ValueType atMap (struct hashMap * ht, KeyType k)
{ 
	hashLink * cur;
	int hashVal;
	assert(ht != NULL);
	if (!containsKey(ht, k))
		return NULL;

	else {
		if (HASHING_FUNCTION == 1)
			hashVal = stringHash1(k) % ht->tableSize;
		else if (HASHING_FUNCTION == 2)
			hashVal = stringHash2(k) % ht->tableSize;

		if (hashVal < 0)
			hashVal += ht->tableSize;

		cur = ht->table[hashVal];

		while (strcmp(cur->key, k) != 0)
			cur = cur->next;

		return cur->value;
	}
}
Esempio n. 5
0
/*
 this returns the value (which is void*) stored in a hashLink specified by the key k.

 if the user supplies the key "taco" you should find taco in the hashTable, then
 return the value member of the hashLink that represents taco.

 if the supplied key is not in the hashtable return NULL.
 */
ValueType atMap (struct hashMap *ht, KeyType k)
{
	/*write this*/
	int hash;
	int idx;
	struct hashLink *link;
	//printf("TableSize:%d\n",ht->tableSize);

	if(HASHING_FUNCTION == 1){
		hash = stringHash1(k);
	}
	if(HASHING_FUNCTION == 2){
		hash = stringHash2(k);
	}

	idx = hash % ht->tableSize;
	//printf("hash:%d\nIndex:%d\n", hash, Index);
	if(containsKey(ht,k)!=0){
		link = ht->table[idx];
		while (link != NULL){
			if (strcmp(link->key, k) == 0){
				//printf("Found It\n");
				//printf("hash:%d\nIndex:%d\n", hash, Index);
				return link->value;
			}
			else{
				link = link->next;
				//printf("Checking Next\n");
			}
		}
	}
	//printf("Exiting AtMap now\n");
	//assert(0);
	return NULL;
}
	template<typename T> T config::getParam(string key, T def)
	{
		//Checks for key in the file.
		T val = def;
		bool exists = containsKey(key);

		//If it exists grab the value.
		if (exists == true)
		{
			val = dynamic_cast<T>(options[key]);
		}

		//If we want output then write what happened.
		if (suppressOutput == false)
		{
			if (exists == false)
			{
				std::cout << "-Option: '" << key << "' missing\n";
				std::cout << "-Using default.\n";
			}
			std::cout << "---" << key << ": " << val << "\n";
		}

		return val;

	}
Esempio n. 7
0
/*
 insert the following values into a hashLink, you must create this hashLink but
 only after you confirm that this key does not already exist in the table. For example, you
 cannot have two hashLinks for the word "taco".
 
 if a hashLink already exists in the table for the key provided you should
 replace that hashLink--this requires freeing up the old memory pointed by hashLink->value
 and then pointing hashLink->value to value v.
 
 also, you must monitor the load factor and resize when the load factor is greater than
 or equal LOAD_FACTOR_THRESHOLD (defined in hashMap.h).
 */
void insertMap (struct hashMap * ht, KeyType k, ValueType v){  
	int hashIndex;
    struct hashLink * newHashLink = (struct hashLink *) malloc(sizeof(struct hashLink));
    char * newKeyType = (char *) malloc(strlen(k) + 1);     //Allocate memory for new char string

    /* Create hash index using one of two hashing function supplied */
    if(HASHING_FUNCTION == 1)
        hashIndex = stringHash1(k) % ht->tableSize;
    else
        hashIndex = stringHash2(k) % ht->tableSize;

    /* Ensure hashIndex is positive */
    if(hashIndex < 0)
        hashIndex += ht->tableSize;

    assert(newHashLink);
    
    /* Remove duplicate keys so new key replaces old key */
    if(containsKey(ht, k))
        removeKey(ht, k);

    /* Initialize new hashLink and add to appropriate hash index */
    strcpy(newKeyType, k);      //copy string stream into allocated memory reserved for this hashLink

    newHashLink->key = newKeyType;
    newHashLink->value = v;
    newHashLink->next = ht->table[hashIndex];
    ht->table[hashIndex] = newHashLink;

    ht->count++;
    
    /* Test table load and resize if necessary */
    if(tableLoad(ht) >= LOAD_FACTOR_THRESHOLD)
        _setTableSize(ht, ht->tableSize * 2);
}
Esempio n. 8
0
/* Desc: Inserts key, value pair into the hash map
 * Pre:  ht is not null
 * Post: new link with key k and value v added
 */
void insertMap (struct hashMap * ht, KeyType k, ValueType v)
{
    assert( ht != NULL );
    int idx = findKeyIndex( ht, k );

    /* Check if key is already in table */
    if( containsKey( ht, k ) )
    {
        /* Replace the hash link as it already exists */
        ht->table[ idx ]->value = v;
    }
    else
    {
        /* Not in table, so create a hashLink */
        struct hashLink *new_lnk = malloc( sizeof( struct hashLink ) );

        /* Fill hashLink with data */
        new_lnk->value = v;
        new_lnk->key = k;
        new_lnk->next = ht->table[ idx ];

        /* Add the new link to the table */
        ht->table[ idx ] = new_lnk;
        ht->count += 1;
    }
}
Esempio n. 9
0
/*
 insert the following values into a hashLink, you must create this hashLink but
 only after you confirm that this key does not already exist in the table. For example, you
 cannot have two hashLinks for the word "taco".
 if a hashLink already exists in the table for the key provided you should
 replace that hashLink--this requires freeing up the old memory pointed by hashLink->value
 and then pointing hashLink->value to value v.
 also, you must monitor the load factor and resize when the load factor is greater than
 or equal LOAD_FACTOR_THRESHOLD (defined in hashMap.h).
 */
void insertMap(struct hashMap * ht, KeyType k, ValueType v)
{
    
    int hashIndex;
    
    if (HASHING_FUNCTION == 1)
    {
        hashIndex = stringHash1(k) % ht->tableSize; //reduce the index
    }
    else if (HASHING_FUNCTION == 2)
    {
        hashIndex = stringHash2(k) % ht->tableSize;
    }
    
    if (hashIndex < 0) //check to make sure indez is not negative
    {
        hashIndex = hashIndex + ht->tableSize;
    }
    
    //Check if hashLink already exists and delete if it does not
    if (containsKey(ht, k))
    {
        removeKey(ht, k);
    }
    
    //add a new link in
    hashLink *newLink = malloc(sizeof(struct hashLink));
    assert(newLink != NULL);
    
    newLink->key = k;
    newLink->value = v;
    newLink->next = NULL;
    
    if (ht->table[hashIndex] != NULL)
    {
        hashLink *temp = ht->table[hashIndex];
        
        //find the end of chain
        while (temp->next != NULL)
        {
            temp = temp->next;
        }
        
        temp->next = newLink;
    }
    else
    {
        ht->table[hashIndex] = newLink;
    }
    
    ht->count++;
    
    // monitor the load factor
    if (tableLoad(ht) >= LOAD_FACTOR_THRESHOLD)
    {
        _setTableSize(ht, ht->tableSize * 2);
    }
    
}
Esempio n. 10
0
/* Desc: Removes the hashlink with key k
 * Pre:  ht is not null
 * Post: link freed
 */
void removeKey (struct hashMap * ht, KeyType k)
{
    assert( ht != NULL );

    if( containsKey( ht, k ) )
    {
        free( ht->table[ findKeyIndex( ht, k ) ] );
        ht->count -= 1;
    }
}
Esempio n. 11
0
void testMapModule(void) {
   Map map, map2;
   string key;
   string str;

   trace(map = newMap());
   test(size(map), 0);
   test(isEmpty(map), true);
   trace(put(map, "H", "Hydrogen"));
   test(size(map), 1);
   test(isEmpty(map), false);
   trace(put(map, "He", "Helium"));
   trace(put(map, "Al", "Aluminum"));
   test(containsKey(map, "H"), true);
   test(get(map, "H"), "Hydrogen");
   test(get(map, "He"), "Helium");
   test(get(map, "Al"), "Aluminum");
   test(containsKey(map, "Li"), false);
   test(get(map, "Li"), NULL);
   trace(put(map, "Al", "Aluminium"));
   test(get(map, "Al"), "Aluminium");
   trace(remove(map, "Al"));
   test(containsKey(map, "Al"), false);
   test(get(map, "He"), "Helium");
   trace(put(map, "Li", "Lithium"));
   test(get(map, "Li"), "Lithium");
   trace(put(map, "Be", "Beryllium"));
   test(size(map), 4);
   trace(str = "");
   trace (foreach (key in map) str = concat(str, key));
   test(str, "BeHHeLi");
   trace(str = "");
   trace(foreach (key in map) str = concat(str, get(map, key)));
   test(str, "BerylliumHydrogenHeliumLithium");
   trace(map2 = clone(map));
   trace(str = "");
   trace(foreach (key in map2) str = concat(str, get(map2, key)));
   test(str, "BerylliumHydrogenHeliumLithium");
}
Esempio n. 12
0
/*
 insert the following values into a hashLink, you must create this hashLink but
 only after you confirm that this key does not already exist in the table. For example, you
 cannot have two hashLinks for the word "taco".

 if a hashLink already exists in the table for the key provided you should
 replace that hashLink--this requires freeing up the old memory pointed by hashLink->value
 and then pointing hashLink->value to value v.

 also, you must monitor the load factor and resize when the load factor is greater than
 or equal LOAD_FACTOR_THRESHOLD (defined in hashMap.h).
 */
void insertMap (struct hashMap * ht, KeyType k, ValueType v){
	/*write this*/
    assert (ht != 0);
    if (containsKey(ht, k))
    ht->table[_findKey(ht, k)]->value = v;
    else {
        struct hashLink *newLink = malloc(sizeof(struct hashLink));
        newLink->value = v;
        newLink->key = k;
        newLink->next = ht->table[_findKey(ht, k)];
        ht->table[_findKey(ht, k)] = newLink;
        ht->count++;
    }
    if (tableLoad(ht) > LOAD_FACTOR_THRESHOLD)
    _setTableSize(ht, ht->tableSize * 2);
}
Esempio n. 13
0
void removeKey (struct hashMap * ht, KeyType k)
{
    struct hashLink* curr, prev;
    if(containsKey(k))
    {
        curr = ht->table[stringHash2(k) % ht->tableSize];
        while(curr->key != k)
        {
            prev = curr;
            curr = curr->next;
        }
        prev->next = curr->next;
        free(curr);
        ht->count --;
    }
}
Esempio n. 14
0
XMLSize_t NameIdPool<TElem>::put(TElem* const elemToAdopt)
{
    // First see if the key exists already. If so, its an error
    if(containsKey(elemToAdopt->getKey()))
    {
        ThrowXMLwithMemMgr1
        (
            IllegalArgumentException
            , XMLExcepts::Pool_ElemAlreadyExists
            , elemToAdopt->getKey()
            , fMemoryManager
        );
    }

    fBucketList.put((void*)elemToAdopt->getKey(), elemToAdopt);

    //
    //  Give this new one the next available id and add to the pointer list.
    //  Expand the list if that is now required.
    //
    if (fIdCounter + 1 == fIdPtrsCount)
    {
        // Create a new count 1.5 times larger and allocate a new array
        XMLSize_t newCount = (XMLSize_t)(fIdPtrsCount * 1.5);
        TElem** newArray = (TElem**) fMemoryManager->allocate
        (
            newCount * sizeof(TElem*)
        ); //new TElem*[newCount];

        // Copy over the old contents to the new array
        memcpy(newArray, fIdPtrs, fIdPtrsCount * sizeof(TElem*));

        // Ok, toss the old array and store the new data
        fMemoryManager->deallocate(fIdPtrs); //delete [] fIdPtrs;
        fIdPtrs = newArray;
        fIdPtrsCount = newCount;
    }
    const XMLSize_t retId = ++fIdCounter;
    fIdPtrs[retId] = elemToAdopt;

    // Set the id on the passed element
    elemToAdopt->setId(retId);

    // Return the id that we gave to this element
    return retId;
}
Esempio n. 15
0
/*
 find the hashlink for the supplied key and remove it, also freeing the memory
 for that hashlink. it is not an error to be unable to find the hashlink, if it
 cannot be found do nothing (or print a message) but do not use an assert which
 will end your program.
 */
void removeKey (struct hashMap * ht, KeyType k)
{ 
	hashLink * cur, * temp;
	assert(ht != NULL);
	if (containsKey(ht, k)) {
		int hashVal;
		if (HASHING_FUNCTION == 1)
			hashVal = stringHash1(k) % ht->tableSize;
		else if (HASHING_FUNCTION == 2)
			hashVal = stringHash2(k) % ht->tableSize;

		if (hashVal < 0)
			hashVal += ht->tableSize;

		cur = ht->table[hashVal];
		
		//if node to remove is at a parent link
		if (strcmp(cur->key, k) == 0) {
			temp = cur->next;
			free(cur->key);
			free(cur);
			ht->table[hashVal] = temp;
			ht->count--;
			return;
		}

		//If node to remove is at a child link
		while (cur->next != NULL) {
			if (strcmp(cur->next->key, k) == 0) {
				temp = cur->next->next;
				free(cur->next->key);
				free(cur->next);
				cur->next = temp;
				ht->count--;
				return;
			}
			cur = cur->next;
		}
	}
	
	else {
		printf("\nError: tried to remove a key that is not in the map.");
		return;
	}
}
Esempio n. 16
0
/*
 insert the following values into a hashLink, you must create this hashLink but
 only after you confirm that this key does not already exist in the table. For example, you
 cannot have two hashLinks for the word "taco".

 if a hashLink already exists in the table for the key provided you should
 replace that hashLink--this requires freeing up the old memory pointed by hashLink->value
 and then pointing hashLink->value to value v.

 also, you must monitor the load factor and resize when the load factor is greater than
 or equal LOAD_FACTOR_THRESHOLD (defined in hashMap.h).
 */
void insertMap (struct hashMap * ht, KeyType k, ValueType v)
{
    int index;

    if(tableLoad(ht) >= LOAD_FACTOR_THRESHOLD)
    {
        _setTableSize(ht, (2 * ht -> tableSize));
    }

    struct hashLink *link = malloc(sizeof(struct hashLink));

    index = stringHash2(k) % ht -> tableSize;

    if(index < 0)
    {
        index += ht -> tableSize;
    }

    assert(link);
    link -> next = 0;
    link -> key = k;
    link -> value = v;

    if(containsKey(ht, k))
    {
        removeKey(ht, k);
    }

    if(!ht -> table[index])
    {
        ht -> table[index] = link;
    }
    else
    {
        struct hashLink *current = ht -> table[index];

        while(current -> next)
        {
            current = current -> next;
        }
        current -> next = link;
    }

    ht -> count++;
}
Esempio n. 17
0
/*
 this returns the value (which is void*) stored in a hashLink specified by the key k.
 
 if the user supplies the key "taco" you should find taco in the hashTable, then
 return the value member of the hashLink that represents taco.
 
 if the supplied key is not in the hashtable return NULL.
 */
ValueType* atMap (struct hashMap * ht, KeyType k)
{ 
	assert(ht != NULL);

	int loc = stringHash1(k);

	if(containsKey(ht, k)){
		struct hashLink* pLink = ht->table[loc];
		while(strcmp(pLink->key, k)){
			pLink = pLink->next;
		}
		ValueType* temp;
		temp = &(pLink->value);
		return temp;
	} else {
		return NULL;
	}
}
Esempio n. 18
0
int main (int argc, const char * argv[]) {
  clock_t timer;
  int tableSize = 1000;
  struct hashMap* hashTable;
  hashTable = createMap(tableSize);

  timer = clock();
  //createMap(tableSize);
  //_initMap(hashTable,tableSize);

  FILE* dictionary; // = malloc(sizeof(FILE));

  loadDictionary(dictionary,hashTable);
  timer = clock() - timer;
	printf("Dictionary loaded in %f seconds\n", (float)timer / (float)CLOCKS_PER_SEC);

  char* word = (char*)malloc(256*sizeof(char));
  int quit=0;
  while(!quit){
    printf("Enter a word: ");
    scanf("%s",word);
    /*
      ... spell checker code goes here ...
      ... You write this               ...
    */

    if (containsKey(hashTable, word) == 0)
	{
		printf("%s is NOT a word in the dictionary\n", word);
	}
	else
	{
		printf("%s is a word in the dictionary\n", word);
	}


    /* Don't remove this. It is used for grading*/
    if(strcmp(word,"quit")==0)
      quit=!quit;
  }
  free(word);

  return 0;
}
Esempio n. 19
0
int main (int argc, const char * argv[]) {
    const char *filename;
    clock_t timer;
    int tableSize = 1000;
    struct hashMap* hashTable = createMap(tableSize);
    timer = clock();

    if(argc >= 2)
        filename = argv[1];
    else
        filename = "dictionary.txt"; /*specify your input text file here*/

    FILE* dictionary = fopen(filename, "r");
    if(dictionary == NULL) {
        char err[255];
        sprintf(err, "Failure opening file %s; exiting.\n", filename);
        perror(err);
        exit(EXIT_FAILURE);
    }

    loadDictionary(dictionary,hashTable);
    timer = clock() - timer;
    printf("Dictionary loaded in %f seconds\n", (float)timer / (float)CLOCKS_PER_SEC);

    char* word = (char*)malloc(256*sizeof(char));
    int quit=0;
    while(!quit) {
        printf("Enter a word: ");
        scanf("%s",word);
        if(containsKey(hashTable, lowercase(word)))
            printf("'%s' is spelled correctly.\n", word);
        else
            printf("'%s' is spelled incorrectly; please try again.\n", word);
        /* Don't remove this. It is used for grading*/
        if(strcmp(word,"quit")==0)
          quit=!quit;
    }
    free(word);

    fclose(dictionary);

    return 0;
}
Esempio n. 20
0
File: main.c Progetto: Merfoo/CS-261
void processFile(FILE *file)
{
    int i;
    char *word;
    struct hashMap *map;
    struct hashLink *cur;

    word = 0;
    map = (struct hashMap *) malloc(sizeof(struct hashMap));
    initMap(map, 100);

    while(1)
    {
        word = getWord(file);
        
        if(!word)
            break;

        /*printf("Words! %s\n", word);*/
       
        if(containsKey(map, word))
            insertMap(map, word, *atMap(map, word) + 1);

        else
            insertMap(map, word, 1);
    }

    i = 0;

    for(i = 0; i < map->tableSize; i++)
    {
        cur = map->table[i];

        while(cur)
        {
            printf("%s: %d\n", cur->key, cur->value);
            cur = cur->next;
        }
    }

    freeMap(map);
    free(map);
}
Esempio n. 21
0
/*
 insert the following values into a hashLink, you must create this hashLink but
 only after you confirm that this key does not already exist in the table. For example, you
 cannot have two hashLinks for the word "taco".

 if a hashLink already exists in the table for the key provided you should
 replace that hashLink--this requires freeing up the old memory pointed by hashLink->value
 and then pointing hashLink->value to value v.

 also, you must monitor the load factor and resize when the load factor is greater than
 or equal LOAD_FACTOR_THRESHOLD (defined in hashMap.h).
 */
void insertMap (struct hashMap * ht, KeyType k, ValueType v)
{
	/*write this*/
	int index = HASH(k) % ht->tableSize;
	if (containsKey(ht, k))
	{
		// DO NOT removeKey(ht, k) and add newLink; Instead, only replace value and return
		*atMap(ht, k) = v;
		return;
	}
	struct hashLink * newLink = (struct hashLink *) malloc(sizeof(struct hashLink));
	assert(newLink != 0);
	newLink->key = k;
	newLink->value = v;
	newLink->next = ht->table[index];
	ht->table[index] = newLink;
	ht->count++;
	if ((float)ht->count / (float)ht->tableSize > LOAD_FACTOR_THRESHOLD)
		_setTableSize(ht, 2 * ht->tableSize);
}
Esempio n. 22
0
/*
 insert the following values into a hashLink, you must create this hashLink but
 only after you confirm that this key does not already exist in the table. For example, you
 cannot have two hashLinks for the word "taco".
 
 if a hashLink already exists in the table for the key provided you should
 replace that hashLink--this requires freeing up the old memory pointed by hashLink->value
 and then pointing hashLink->value to value v.
 
 also, you must monitor the load factor and resize when the load factor is greater than
 or equal LOAD_FACTOR_THRESHOLD (defined in hashMap.h).
 */
void insertMap (struct hashMap * ht, KeyType k, ValueType v)
{  
	assert(ht != NULL);

	int loc = stringHash1(k) % ht->tableSize;

	if(containsKey(ht, k)){
		struct hashLink* pLink = ht->table[loc];
	        while(strcmp(pLink->key, k)){
		     pLink = pLink->next;
	        }
		pLink->value = pLink->value + v;
 
	} else {

		struct hashLink* newLink = (struct hashLink*)malloc(sizeof(struct hashLink));
		newLink->key = (char*)malloc((1 + strlen(k)) * sizeof(char));
		strcpy(newLink->key, k);
		newLink->value = v;
		newLink->next = NULL;


	        struct hashLink* pLink = ht->table[loc];
	        if(pLink == NULL){
		   ht->table[loc] = newLink;
	        } else {
		   while(pLink->next != NULL){
			pLink = pLink->next;
		   } 
		   pLink->next = newLink;
	        }
	        ht->count++;
	}

	if(tableLoad(ht) >= LOAD_FACTOR_THRESHOLD){
		_setTableSize(ht, 2 * ht->tableSize);
	}


	return;
}
Esempio n. 23
0
			OntologyBase *  OntologyBase::get_or_create_ontology(QString _acronym){
				//if(this->ontologies-)
				std::cerr << "entering get_or_create_ontology(QString " <<_acronym.toStdString()<<")"<< std::endl;

				if (containsKey(_acronym)==false){
					std::cerr << "this ontology not exists" << std::endl;
					OntologyBase * ontology = new OntologyBase();
					ontology->set_acronym(_acronym);
					ontology->registering();
				}

				if (_acronym == "XEO_Positioning")
				{
					return get_ontology_by_key("XPO");

				}
				return get_ontology_by_key(_acronym);



			}
Esempio n. 24
0
/*
 find the hashlink for the supplied key and remove it, also freeing the memory
 for that hashlink. it is not an error to be unable to find the hashlink, if it
 cannot be found do nothing (or print a message) but do not use an assert which
 will end your program.
 */
void removeKey (struct hashMap * ht, KeyType k)
{
	/*write this*/



	int hash;
	if(HASHING_FUNCTION == 1){
		hash = stringHash1(k);
	}
	if(HASHING_FUNCTION == 2){
		hash = stringHash2(k);
	}
	struct hashLink *cur, *prev;
	int idx = hash % ht->tableSize;

	prev = 0;
	cur = ht->table[idx];
	if(containsKey(ht, k)!=0){
		while(cur)
		{
			if(strcmp(cur->key, k)==0)
			{
				printf("###########Removing:%s ##############\n", cur->key);
				if(!prev){
					ht->table[idx] = cur->next;
				}
				else{
					prev->next = cur->next;
				}
				free(cur);
				ht->count--;
				return;
			}

			prev = cur;
			cur = cur->next;
		}
	}
}
Esempio n. 25
0
/*
	removeKey: find the hash link for the key and remove it, also free the memory for the hash link
	If unable to find the key, then print an appropriate message
	param1: ht - the map
	param2: k - the key to remove
	pre: ht is not NULL
	post: key has been removed from the table
	post: count has been decremented
	HINT - If the map only contains a single hash link, it is a special case
	removing the solitary link should set the table entry to NULL
 */
void removeKey(struct hashMap *ht, KeyType k) {
	/* FIX ME */
	assert(ht!=NULL);
    int hash;
    struct hashLink *cur;
	struct hashLink *prev;
    /* check for the key, if not found, then print message and return */
    if(!containsKey(ht, k)){
        printf("key '%s' not found!\n", k);
        return;
    }

    /* hash the key */
    hash = _hashValue(k, ht->hashID);
    hash = hash % ht->tableSize;
    /* special case for single link */
    if(ht->table[hash]->next==NULL && strcmp(ht->table[hash]->key, k)==0){
        ht->table[hash] = NULL;
    }
    /* find key and remove it */

    /*if it is a top key with links*/
    if(strcmp(ht->table[hash]->key, k)==0 && ht->table[hash]->next !=NULL){
       ht->table[hash] = ht->table[hash]->next;
    }

    /*some other level other than 0*/

    cur=ht->table[hash];
    while(strcmp(cur->key, k)!= 0){
        prev=cur;
        cur= cur->next;
    }
    prev->next = prev->next->next;




}
Esempio n. 26
0
void ConfigFile::extractContents(const std::string &line)
{
	// called from parse_line(), validated there

	std::string tmp = line;
	// remove leading whitespace
	tmp.erase(0, tmp.find_first_not_of("\t "));
	size_t sep_pos = tmp.find('=');

	std::string key, value;
	extractKey(key, sep_pos, tmp);
	extractValue(value, sep_pos, tmp);

	if (!containsKey(key))
	{
		contents.insert(std::pair<std::string, std::string>(key, value));
	}
	else
	{
		ConfigUtil::warn(key + " was declared at least twice in configuration file.");
	}
}
Esempio n. 27
0
/*
	valAtKey: return the value stored at the key
	param1: ht - the map
	param2: k - the key to search for
	pre: ht is not null
	pre: if key not found, then display an appropriate message
	post: none
	return: return the value found at the key
*/
ValueType valAtKey(struct hashMap *ht, KeyType k) {
/* FIX ME */
int hash;
struct hashLink *cur;

    /* check for the key, if not found, then print message */
    if(!containsKey(ht, k)){
        //printf("key '%s' not found!\n", k);
        return -1;
    }

    /* hash the key */
    hash = _hashValue(k, ht->hashID);
    hash = hash % ht->tableSize;

    /* search table for the key and return the current value */
    cur = ht->table[hash];
    while(strcmp(cur->key, k)!=0){
        cur=cur->next;
    }

return cur->value;
}
Esempio n. 28
0
	template<> string config::getParam<string>(string key, string def)
	{
		string val = def;
		bool exists = containsKey(key);

		if (exists == true)
		{
			val = options[key];
		}

		if (suppressOutput == false)
		{
			if (exists == false)
			{
				std::cout << "-Option: '" << key << "' missing\n"; 
				std::cout << "-Using default.\n";
			}
			std::cout << "---" << key << ": " << val << "\n";
		}

		return val;

	}
Esempio n. 29
0
	template<> float config::getParam<float>(string key, float def)
	{
		float val = def;
		bool exists = containsKey(key);

		if (exists == true)
		{
			val = std::stof(options[key],nullptr);
		}

		if (suppressOutput == false)
		{
			if (exists == false)
			{
				std::cout << "-Option: '" << key << "' missing\n";
				std::cout << "-Using default.\n";
			}
			std::cout << "---" << key << ": " << val << "\n";
		}

		return val;

	}
Esempio n. 30
0
int main(int argc, const char * argv[]) {
	clock_t timer;
	int tableSize = 1000;
	struct hashMap* hashTable = createMap(tableSize);
	timer = clock();
	//initMap(hashTable, tableSize); no longer in header

	FILE* dictionary = fopen("dictionary.txt", "r"); //open the dicitionary file for reading

	loadDictionary(dictionary, hashTable);
	timer = clock() - timer;
	printf("Dictionary loaded in %f seconds\n", (float)timer / (float)CLOCKS_PER_SEC);

	char* word = (char*)malloc(256 * sizeof(char));
	int quit = 0;
	while (!quit) {
		printf("Enter a word: "); //prompt to enter a word
		scanf("%s", word); //read the given input

						   //if the input value can be found in the hash table, then it was spelled correctly
		if (!containsKey(hashTable, word))
		{
			printf("%s is spelled wrong.\n", word);
		}
		else
		{
			printf("%s is spelled correctly.\n", word);
		}

		/* Don't remove this. It is used for grading*/
		if (strcmp(word, "quit") == 0)
			quit = !quit;
	}
	free(word);

	return 0;
}