Guest User

Untitled

a guest
Apr 11th, 2023
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.92 KB | Source Code | 0 0
  1. // Implements a dictionary's functionality
  2.  
  3. #include <ctype.h>
  4. #include <stdbool.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <strings.h>
  9.  
  10. #include "dictionary.h"
  11.  
  12. // Represents a node in a hash table
  13. typedef struct node
  14. {
  15.     char word[LENGTH + 1];
  16.     struct node *next;
  17. }
  18. node;
  19.  
  20. // TODO: Choose number of buckets in hash table
  21. const unsigned int N = 1125;
  22.  
  23. // Hash table
  24. node *table[N];
  25.  
  26. //Stores whether or not the dictionary was loaded
  27. bool loaded = false;
  28.  
  29. //Stores number of words in dictionary
  30. unsigned int num_words = 0;
  31.  
  32. // Returns true if word is in dictionary, else false
  33. bool check(const char *word)
  34. {
  35.     //Hash word to be checked
  36.     char *copy = malloc(strlen(word) + 1);
  37.     strcpy(copy, word);
  38.     for (int i = 0; i < strlen(copy); i++)
  39.     {
  40.         copy[i] = tolower(copy[i]);
  41.     }
  42.  
  43.     unsigned int hash_val = hash(copy);
  44.     free(copy);
  45.  
  46.     //Allocate memory for a node pointer to index through the hash table and point
  47.     //to the appropriate hash table head
  48.     node *index = malloc(sizeof(node));
  49.     index = table[hash_val];
  50.  
  51.     //Index through list until matching word is found
  52.     while (index != NULL)
  53.     {
  54.         if(!strcasecmp(index->word, word))
  55.         {
  56.             index = NULL;
  57.             free(index);
  58.             return true;
  59.         }
  60.         index = index->next;
  61.     }
  62.     index = NULL;
  63.     free(index);
  64.     return false;
  65. }
  66.  
  67. // Hashes word to a number
  68. unsigned int hash(const char *word)
  69. {
  70.     //Hash word to lowercase ASCII - 'a' per letter
  71.     unsigned int hash_val = 0;
  72.     for(int i = 0; i < strlen(word); i++)
  73.     {
  74.         if(word[i] == '\'')
  75.         {
  76.             hash_val += 1;
  77.         }
  78.         if(tolower(word[i]) >= 'a' && tolower(word[i]) <= 'z')
  79.         {
  80.             hash_val += (tolower(word[i]) - 'a');
  81.         }
  82.     }
  83.  
  84.     return hash_val;
  85. }
  86.  
  87. // Loads dictionary into memory, returning true if successful, else false
  88. bool load(const char *dictionary)
  89. {
  90.     //Open dictionary
  91.     FILE *file = fopen(dictionary, "r");
  92.  
  93.     //Check that dictionary opened correctly/exists
  94.     if (file == NULL)
  95.     {
  96.         return false;
  97.     }
  98.  
  99.     unsigned int hash_val;
  100.  
  101.     //Load the words from the dictionary into the hash table
  102.     char *buffer = malloc((sizeof(char) * LENGTH) + 1);
  103.     while (fscanf(file, "%s", buffer) != EOF)
  104.     {
  105.         //Allocate space for new node
  106.         node *n = malloc(sizeof(node));
  107.         if (n == NULL)
  108.         {
  109.             unload();
  110.             free(n);
  111.             free(buffer);
  112.             return false;
  113.         }
  114.  
  115.         //Copy new word into new node
  116.         strcpy(n->word, buffer);
  117.         n->next = NULL;
  118.  
  119.         //Hash new word
  120.         hash_val = hash(n->word);
  121.  
  122.         //Link new word into hash table
  123.         n->next = table[hash_val];
  124.         table[hash_val] = n;
  125.         num_words++;
  126.     }
  127.     fclose(file);
  128.     free(buffer);
  129.     loaded = true;
  130.     return true;
  131. }
  132.  
  133. // Returns number of words in dictionary if loaded, else 0 if not yet loaded
  134. unsigned int size(void)
  135. {
  136.     if (loaded)
  137.     {
  138.         return num_words;
  139.     }
  140.     else
  141.     {
  142.         return 0;
  143.     }
  144. }
  145.  
  146. // Unloads dictionary from memory, returning true if successful, else false
  147. bool unload(void)
  148. {
  149.     //Allocate memory for a cursor and temp pointer
  150.     node *cursor = malloc(sizeof(node));
  151.     node *tmp = malloc(sizeof(node));
  152.  
  153.     //Variable to count how many items were freed
  154.     int count = 0;
  155.  
  156.     //Itterate through hash table to free dictionary
  157.     for (int i = 0; i < N; i++)
  158.     {
  159.         //Change table head
  160.         cursor = table[i];
  161.         while (cursor != NULL)
  162.         {
  163.             ////Itterate through linked list
  164.             tmp = cursor;
  165.             cursor = cursor->next;
  166.             free(tmp);
  167.             count++;
  168.         }
  169.     }
  170.     free(cursor);
  171.  
  172.     if(count == num_words)
  173.     {
  174.         return true;
  175.     }
  176.     return false;
  177. }
  178.  
Advertisement
Add Comment
Please, Sign In to add comment