Untitled


SUBMITTED BY: Guest

DATE: Dec. 14, 2016, 9:46 p.m.

FORMAT: C

SIZE: 8.8 kB

HITS: 354

  1. #include "header.h"
  2. long hash_key(char songtitle[], char interpreter[], long hash_max)
  3. {
  4. unsigned long i, index = 0;
  5. for (i = 0; i < strlen(songtitle); ++i)
  6. index = 64 * index + (long)(songtitle[i]);
  7. for (i = 0; i < strlen(interpreter); ++i)
  8. index = 64 * index + (long)(interpreter[i]);
  9. return index % hash_max;
  10. }
  11. hashcontainer_t *create_hash(long hashsize)
  12. {
  13. unsigned long i;
  14. hashcontainer_t *new = 0;
  15. if (!(new = malloc(sizeof(hashcontainer_t))))
  16. {
  17. fprintf(stderr, "Memory allocation error in create_hash()");
  18. exit(-1);
  19. }
  20. if (!(new->harrays = calloc(hashsize, sizeof(hasharray_t))))
  21. {
  22. fprintf(stderr, "Memory allocation error in create_hash()");
  23. exit(-1);
  24. }
  25. new->hashsize = hashsize;
  26. for (i = 0; i < hashsize; i++)
  27. {
  28. new->harrays[i];
  29. }
  30. return new;
  31. }
  32. void delete_hash(hashcontainer_t *hashcontainer)
  33. {
  34. unsigned long i;
  35. for (i = 0; i < hashcontainer->hashsize; i++)
  36. {
  37. if (hashcontainer->harrays[i].entries)
  38. {
  39. free(hashcontainer->harrays[i].entries);
  40. hashcontainer->harrays[i].entries = 0;
  41. hashcontainer->harrays[i].num_entries = 0;
  42. }
  43. }
  44. free(hashcontainer->harrays);
  45. hashcontainer->harrays = 0;
  46. hashcontainer->hashsize = 0;
  47. }
  48. void insert_entry(hashcontainer_t *hashcontainer, char songtitle[], char interpreter[])
  49. {
  50. unsigned long index = hash_key(songtitle, interpreter, hashcontainer->hashsize);
  51. hashcontainer->harrays[index].num_entries++;
  52. hashcontainer->harrays[index].entries = realloc(hashcontainer->harrays[index].entries, hashcontainer->harrays[index].num_entries * sizeof(hashentry_t));
  53. strcpy(hashcontainer->harrays[index].entries[hashcontainer->harrays[index].num_entries - 1].songtitle, songtitle);
  54. strcpy(hashcontainer->harrays[index].entries[hashcontainer->harrays[index].num_entries - 1].interpreter, interpreter);
  55. }
  56. hashentry_t *search_entry(hashcontainer_t *hashcontainer, char songtitle[], char interpreter[])
  57. {
  58. unsigned long index = hash_key(songtitle, interpreter, hashcontainer->hashsize);
  59. unsigned long len = hashcontainer->harrays[index].num_entries, i;
  60. for (i = 0; i < len; i++)
  61. {
  62. if ((strcmp(hashcontainer->harrays[index].entries[i].interpreter, interpreter) == 0) && (strcmp(hashcontainer->harrays[index].entries[i].songtitle, songtitle) == 0))
  63. return (hashcontainer->harrays[index].entries)+i;
  64. }
  65. return 0;
  66. }
  67. void delete_entry(hashcontainer_t *hashcontainer, char songtitle[], char interpreter[])
  68. {
  69. unsigned long index = hash_key(songtitle, interpreter, hashcontainer->hashsize);
  70. unsigned long len = hashcontainer->harrays[index].num_entries, i;
  71. for (i = 0; i < len; i++)
  72. {
  73. if ((strcmp(hashcontainer->harrays[index].entries[i].interpreter, interpreter) == 0) && (strcmp(hashcontainer->harrays[index].entries[i].songtitle, songtitle) == 0))
  74. {
  75. if (i == 0 && len == 1)
  76. {
  77. free(hashcontainer->harrays[index].entries);
  78. hashcontainer->harrays[index].entries = 0;
  79. hashcontainer->harrays[index].num_entries = 0;
  80. }
  81. else
  82. {
  83. if (i != len - 1)
  84. memcpy(hashcontainer->harrays[index].entries + i, hashcontainer->harrays[index].entries + i + 1, (len - i - 1)* sizeof(hashentry_t));
  85. hashcontainer->harrays[index].entries = realloc(hashcontainer->harrays[index].entries, --hashcontainer->harrays[index].num_entries * sizeof(hashentry_t));
  86. }
  87. }
  88. }
  89. }
  90. void print_hash(hashcontainer_t *hashcontainer)
  91. {
  92. unsigned long i, j;
  93. for (i = 0; i < hashcontainer->hashsize; i++)
  94. {
  95. fprintf(stdout, "Hash index %ld\n", i);
  96. for (j = 0; j < hashcontainer->harrays[i].num_entries; j++)
  97. {
  98. fprintf(stdout, "%s\t%s\n", hashcontainer->harrays[i].entries[j].songtitle, hashcontainer->harrays[i].entries[j].interpreter);
  99. }
  100. fprintf(stdout, "\n");
  101. }
  102. }
  103. long count_entries(hashcontainer_t *hashcontainer)
  104. {
  105. unsigned long i, j, count = 0;
  106. for (i = 0; i < hashcontainer->hashsize; i++)
  107. {
  108. for (j = 0; j < hashcontainer->harrays[i].num_entries; j++)
  109. {
  110. count++;
  111. }
  112. }
  113. return count;
  114. }
  115. long count_entries_with_songtitle(hashcontainer_t *hashcontainer, char songtitle[])
  116. {
  117. unsigned long i, j, count = 0;
  118. for (i = 0; i < hashcontainer->hashsize; i++)
  119. {
  120. for (j = 0; j < hashcontainer->harrays[i].num_entries; j++)
  121. {
  122. if (strcmp(hashcontainer->harrays[i].entries[j].songtitle, songtitle) == 0)
  123. count++;
  124. }
  125. }
  126. return count;
  127. }
  128. void insert_entry_ext_exp(hashcontainer_t *hashcontainer, char songtitle[], char interpreter[], long(*hashkey_gen)(hashentry_t *entry, long hash_max))
  129. {
  130. hashentry_t *new = malloc(sizeof(hashentry_t));
  131. strcpy(new->songtitle, songtitle);
  132. strcpy(new->interpreter, interpreter);
  133. unsigned long index = hashkey_gen(new, hashcontainer->hashsize);
  134. hashcontainer->harrays[index].num_entries++;
  135. hashcontainer->harrays[index].entries = realloc(hashcontainer->harrays[index].entries, hashcontainer->harrays[index].num_entries * sizeof(hashentry_t));
  136. memcpy(hashcontainer->harrays[index].entries + hashcontainer->harrays[index].num_entries - 1, new, sizeof(hashentry_t));
  137. }
  138. void insert_entry_ext(hashcontainer_t *hashcontainer, hashentry_t *new, long(*hashkey_gen)(hashentry_t *entry, long hash_max))
  139. {
  140. unsigned long index = hashkey_gen(new, hashcontainer->hashsize);
  141. hashcontainer->harrays[index].num_entries++;
  142. hashcontainer->harrays[index].entries = realloc(hashcontainer->harrays[index].entries, hashcontainer->harrays[index].num_entries * sizeof(hashentry_t));
  143. memcpy(hashcontainer->harrays[index].entries + hashcontainer->harrays[index].num_entries - 1, new, sizeof(hashentry_t));
  144. }
  145. hashentry_t *search_entry_ext(hashcontainer_t *hashcontainer, char songtitle[], char interpreter[], long(*hashkey_gen)(hashentry_t *entry, long hash_max))
  146. {
  147. hashentry_t *new = malloc(sizeof(hashentry_t));
  148. strcpy(new->songtitle, songtitle);
  149. strcpy(new->interpreter, interpreter);
  150. unsigned long index = hashkey_gen(new, hashcontainer->hashsize);
  151. unsigned long i;
  152. for (i = 0; i < hashcontainer->harrays[index].num_entries; i++)
  153. {
  154. if ((strcmp(hashcontainer->harrays[index].entries[i].interpreter, interpreter) == 0) && (strcmp(hashcontainer->harrays[index].entries[i].songtitle, songtitle) == 0))
  155. return (hashcontainer->harrays[index].entries)+i;
  156. }
  157. return 0;
  158. }
  159. void rehash1(hashcontainer_t *hashcontainer, long(*hashkey_gen)(hashentry_t *entry, long hash_max), long hash_max)
  160. {
  161. hashcontainer_t *new = create_hash(hash_max);
  162. unsigned long i, j;
  163. for (i = 0; i < hashcontainer->hashsize; i++)
  164. {
  165. for (j = 0; j < hashcontainer->harrays[i].num_entries; j++)
  166. {
  167. insert_entry_ext(new, hashcontainer->harrays[i].entries + j, hashkey_gen);
  168. }
  169. }
  170. delete_hash(hashcontainer);
  171. hashcontainer->harrays = new->harrays;
  172. hashcontainer->hashsize = new->hashsize;
  173. }
  174. void rehash2(hashcontainer_t *hashcontainer, long(*hashkey_gen)(hashentry_t *entry, long hash_max), long hash_max)
  175. {
  176. unsigned long i, j, count = 0, len = count_entries(hashcontainer);
  177. hashentry_t temp[len];
  178. for (i = 0; i < hashcontainer->hashsize; i++)
  179. {
  180. for (j = 0; j < hashcontainer->harrays[i].num_entries; j++)
  181. {
  182. strcpy(temp[count].songtitle, hashcontainer->harrays[i].entries[j].songtitle);
  183. strcpy(temp[count].interpreter, hashcontainer->harrays[i].entries[j].interpreter);
  184. count++;
  185. }
  186. }
  187. delete_hash(hashcontainer);
  188. hashcontainer_t *new = create_hash(hash_max);
  189. for (i = 0; i < len; i++)
  190. {
  191. insert_entry_ext(new, &temp[i], hashkey_gen);
  192. }
  193. hashcontainer->harrays = new->harrays;
  194. hashcontainer->hashsize = new->hashsize;
  195. free(new);
  196. }

comments powered by Disqus