Advertisement
Guest User

Untitled

a guest
May 29th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.17 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<assert.h>
  5. #include"Dictionary.h"
  6. #define MAX_LEN 180
  7.  
  8. const int tableSize = 101;
  9.  
  10. // hash functions ------------------------------------------------------------
  11.  
  12. // rotate_left()
  13. // rotate the bits in an unsigned int
  14. unsigned int rotate_left(unsigned int value, int shift) {
  15.  
  16. int sizeInBits = 8*sizeof(unsigned int);
  17.  
  18. shift = shift & (sizeInBits - 1);
  19.  
  20. if ( shift == 0 )
  21.  
  22. return value;
  23.  
  24. return (value << shift) | (value >> (sizeInBits - shift));
  25. }
  26.  
  27. // pre_hash()
  28. // turn a string into an unsigned int
  29. unsigned int pre_hash(char* input) {
  30.  
  31. unsigned int result = 0xBAE86554;
  32.  
  33. while (*input) {
  34.  
  35. result ^= *input++;
  36.  
  37. result = rotate_left(result, 5);
  38. }
  39.  
  40. return result;
  41. }
  42.  
  43. // hash()
  44. // turns a string into an int in the range 0 to tableSize-1
  45. int hash(char* key){
  46.  
  47. return pre_hash(key)%tableSize;
  48. }
  49.  
  50. // showBits()
  51. // print out the bits in an unsigned int
  52. void showBits(unsigned int x) {
  53.  
  54. int i;
  55.  
  56. int sizeInBits = 8*sizeof(unsigned int);
  57.  
  58. char symbol[2] = {'0','1'};
  59.  
  60. char* binary = malloc(sizeInBits + 1);
  61.  
  62. memset(binary, 0, sizeInBits + 1);
  63.  
  64. for (i=0; i<sizeInBits; i++) {
  65.  
  66. binary[sizeInBits-i-1] = symbol[(x>>i) & 0x01];
  67. }
  68.  
  69. printf("%sn", binary);
  70.  
  71. free(binary);
  72. }
  73.  
  74. // private types --------------------------------------------------------------
  75.  
  76. // NodeObj
  77. typedef struct NodeObj{
  78.  
  79. char* key;
  80.  
  81. char* value;
  82.  
  83. struct NodeObj* next;
  84.  
  85. } NodeObj;
  86.  
  87. // Node
  88. typedef NodeObj* Node;
  89.  
  90. // newNode()
  91. // constructor of the Node type
  92. Node newNode(char* k, char* v) {
  93.  
  94. Node N = malloc(sizeof(NodeObj));
  95.  
  96. assert(N!=NULL);
  97.  
  98. N->key = k;
  99.  
  100. N->value = v;
  101.  
  102. N->next = NULL;
  103.  
  104. return(N);
  105. }
  106.  
  107. // freeNode()
  108. // destructor for the Node type
  109. void freeNode(Node* pN){
  110.  
  111. if( pN!=NULL && *pN!=NULL ){
  112.  
  113. free(*pN);
  114.  
  115. *pN = NULL;
  116. }
  117. }
  118.  
  119. // ListObj
  120. typedef struct ListObj{
  121.  
  122. Node first;
  123.  
  124. } ListObj;
  125.  
  126. // List
  127. typedef ListObj* List;
  128.  
  129. // newList()
  130. // constructor of the Node type
  131. List newList(void) {
  132.  
  133. List L = malloc(sizeof(ListObj));
  134.  
  135. assert(L!=NULL);
  136.  
  137. L->first = NULL;
  138.  
  139. return L;
  140. }
  141.  
  142.  
  143. // DictionaryObj
  144. typedef struct DictionaryObj{
  145.  
  146. List table;
  147.  
  148. int numOfItems;
  149.  
  150. } DictionaryObj;
  151.  
  152.  
  153. // private helper functions --------------------------------------------------
  154. //deleteAll
  155. void deleteEverything(Node N){
  156.  
  157. if (N!=NULL){
  158.  
  159. deleteEverything(N->next);
  160.  
  161. freeNode(&N);
  162. }
  163. }
  164.  
  165. //findKey()
  166. //returns a reference to the NodeObj containing its argument key or return null if no such NodeObj exists
  167. Node findKey(Node A, char* targetKey){
  168.  
  169. while(A != NULL){
  170.  
  171. if(strcmp(A->key, targetKey)==0){
  172.  
  173. break;
  174. }
  175.  
  176. A = A->next;
  177. }
  178.  
  179. return A;
  180. }
  181.  
  182.  
  183. // public functions -----------------------------------------------------------
  184.  
  185. // newDictionary()
  186. // constructor for the Dictionary type
  187. Dictionary newDictionary(void){
  188.  
  189. Dictionary D = malloc(sizeof(DictionaryObj));
  190.  
  191. assert(D!=NULL);
  192.  
  193. D->table = calloc(tableSize, sizeof(ListObj));
  194.  
  195. D->numOfItems = 0;
  196.  
  197. return D;
  198. }
  199.  
  200. // Dictionary()
  201. // destructor for the Dictionary type
  202. void freeDictionary(Dictionary* pD){
  203.  
  204. if( pD!=NULL && *pD!=NULL ){
  205.  
  206. if( !isEmpty(*pD) ) makeEmpty(*pD);
  207.  
  208. free(*pD);
  209.  
  210. *pD = NULL;
  211. }
  212. }
  213.  
  214. // isEmpty()
  215. // returns 1 (true) if S is empty, 0 (false) otherwise
  216. // pre: none
  217. int isEmpty(Dictionary D){
  218.  
  219. if( D==NULL ){
  220.  
  221. fprintf(stderr, "DICTIONARY Error: calling isEmpty() on NULL Dictionary referencen");
  222.  
  223. exit(EXIT_FAILURE);
  224. }
  225.  
  226. return(D->numOfItems==0);
  227. }
  228.  
  229. //size()
  230. // returns the number of (key, value) pairs in D
  231. // pre none
  232. int size(Dictionary D){
  233.  
  234. return D->numOfItems;
  235. }
  236.  
  237. // lookup()
  238. // returns the value v such that (k,v) is in D, or returns NULL if no such value v exists
  239. // pre: none
  240. char* lookup(Dictionary D, char* k){
  241.  
  242. int indexOftable;
  243.  
  244. List L;
  245.  
  246. Node N;
  247.  
  248. indexOftable = hash(k);
  249.  
  250. L = &D->table[indexOftable];
  251.  
  252. N = findKey(L->first, k);
  253.  
  254. if(N==NULL){
  255.  
  256. return NULL;
  257.  
  258. }else{
  259.  
  260. return N->value;
  261. }
  262. }
  263.  
  264. // insert()
  265. // inserts new (key, value) pair into D
  266. // pre: lookup(D, k)==NULL
  267. void insert(Dictionary D, char* k, char* v){
  268.  
  269. List L;
  270.  
  271. Node N;
  272.  
  273. int indexOfTable;
  274.  
  275. indexOfTable = hash(k);
  276.  
  277.  
  278. L = &D->table[indexOfTable];
  279.  
  280. if(findKey(L-> first, k)!=NULL){
  281.  
  282. fprintf(stderr, "DICTIONARY Error: calling insert() when key already existsn");
  283.  
  284. exit(EXIT_FAILURE);
  285. }
  286.  
  287. N = newNode(k, v);
  288.  
  289. N->next = L->first;
  290.  
  291. L->first = N;
  292.  
  293. N = NULL;
  294.  
  295. D->numOfItems++;
  296.  
  297. }
  298.  
  299.  
  300. // delete()
  301. // deletes pair with the key k
  302. // pre: lookup(D, k)!=NULL
  303. void delete(Dictionary D, char* k){
  304.  
  305. if( D==NULL ){
  306.  
  307. fprintf(stderr, "DICTIONARY Error: calling delete() on NULL Dictionary referencen");
  308.  
  309. exit(EXIT_FAILURE);
  310. }
  311.  
  312. List L;
  313.  
  314. Node N;
  315.  
  316. int indexOfTable;
  317.  
  318. indexOfTable = hash(k);
  319.  
  320. L = &D->table[indexOfTable];
  321.  
  322. if(findKey(L->first, k)==L->first){
  323.  
  324. N = L->first;
  325.  
  326. L->first = L->first->next;
  327.  
  328. N->next = NULL;
  329.  
  330. }else{
  331.  
  332. N = findKey(L->first, k);
  333.  
  334. Node before = L->first;
  335.  
  336. Node temporary = L->first->next;
  337.  
  338. while(temporary !=N){
  339.  
  340. temporary = temporary->next;
  341.  
  342. before = before->next;
  343. }
  344.  
  345. before->next = N->next;
  346.  
  347. N->next = NULL;
  348. }
  349.  
  350. D->numOfItems--;
  351.  
  352. freeNode(&N);
  353. }
  354.  
  355. // makeEmpty()
  356. // resets D to the empty state
  357. // pre: !isEmpty(D)
  358. void makeEmpty(Dictionary D){
  359.  
  360. List L;
  361.  
  362. if( D==NULL ){
  363.  
  364. fprintf(stderr, "DICTIONARY Error: calling makeEmpty() on NULL Dictionary referencen");
  365.  
  366. exit(EXIT_FAILURE);
  367. }
  368.  
  369. if( D->numOfItems==0 ){
  370.  
  371. fprintf(stderr, "DICTIONARY Error: calling makeEmpty() on empty Dictionaryn");
  372.  
  373. exit(EXIT_FAILURE);
  374. }
  375.  
  376. int i;
  377.  
  378. for(i = 0; i<tableSize; i++){
  379.  
  380. L = &D->table[i];
  381.  
  382. deleteEverything(L->first);
  383. }
  384.  
  385. D->table = NULL;
  386.  
  387. D->numOfItems = 0;
  388.  
  389. }
  390.  
  391. // printDictionary()
  392. // prints a text representation of D to the file pointed to by out
  393. // pre: none
  394. void printDictionary(FILE* out, Dictionary D){
  395.  
  396. if( D==NULL ){
  397.  
  398. fprintf(stderr, "DICTIONARY Error: calling printDictionary() on NULL Dictionary referencen");
  399.  
  400. exit(EXIT_FAILURE);
  401. }
  402.  
  403. Node N;
  404.  
  405. List L;
  406.  
  407. int i;
  408.  
  409. for(i = 0; i<tableSize; i++){
  410.  
  411. L = &D->table[i];
  412.  
  413. N = L->first;
  414.  
  415. while(N!=NULL){
  416.  
  417. fprintf(out, "%s %sn", N->key, N->value);
  418.  
  419. N = N->next;
  420. }
  421. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement