Facebook
From Baby Teal, 3 Years ago, written in C.
This paste is a reply to Untitled from Sludgy Cat - view diff
Embed
Download Paste or View Raw
Hits: 134
  1. #define INT_TYPE 0
  2. #define STR_TYPE 1
  3.  
  4. typedef struct Object {
  5.         void* data;
  6.         struct Object* nextObj;
  7.        
  8. }Object;
  9.  
  10. typedef struct Table {
  11.         Object** arr;
  12.         int dataType;
  13.         int listLen;
  14.         int originalSize;
  15.         int ratio;
  16.         int currentSize;
  17.         int timesDup; // Number of times the table duplicated.
  18.        
  19. }Table;
  20.  
  21. /**
  22. * The function gets the original size, the type of the data in the table elements and the maximum number of element in each entry.
  23. * it initializes the Table struct members.
  24. * On success, the function returns a pointer to the new created Table, otherwise, it returns NULL.
  25. */
  26. Table* createTable(int size, int dType, int listLength);
  27. /**
  28. * The function release all the allocated members of struct Table.
  29. */
  30. void freeTable(Table* table);
  31.  
  32. /**
  33. * The function adds data to the hashtable (as described in the exe definition)
  34. * On success, the function returns the array index of the added data, otherwise, it return -1.
  35. */
  36. int add(Table* table, void* data);
  37.  
  38. /**
  39. * The function removes the Object which its data equals to data, if there are more than one, it removes the first one.
  40. * On success, the function returns the array index of the removed data, otherwise, it returns -1.
  41. * -1 is also returned in the case where there is no such object.
  42. */
  43. int removeObj(Table* table, void* data);
  44.  
  45. /**
  46. * The function searches for an object that its data is equal to given data and returns a pointer to that object.
  47. * If there is no such object or in a case of an error, NULL is returned.
  48. */
  49. Object* search(Table* table, void* data);
  50.  
  51. /**
  52. * The function prints the table (the format is in the exe definition)
  53. */
  54. void printTable(Table* table);
  55.  
  56. /**
  57. * This function creates an object and returns the pointer to it or NULL if there is some error.
  58. */
  59. Object* createObject(void* data);
  60.  
  61. /**
  62. * This function frees an object, note the if you allocated the data inside the element, it should also be freed.
  63. * the type parameter is given for cases where not all types are dynamically allocated during Object creation.
  64. */
  65. void freeObject(Object* obj, int type);
  66.  
  67. /**
  68. * check the equality of the data of two objects. The implementation is different depending the type of the data.
  69. * the function returns 0 if they are equal or some other value if they are not equal.
  70. */
  71. int isEqual(int type, void* data1, void* data2);
  72.  
  73. /**
  74. * returns the hash value of an integer, which is key mod origSize
  75. */
  76. int intHashFun(int* key, int origSize);
  77.  
  78. /**
  79. * returns the hash value of an string, which is m mod origSize, where m is the sum of the ascii value of all the
  80. * character in key.
  81. */
  82. int strHashFun(char* key, int origSize);
  83.  
  84. /**
  85. * The function gets the table pointer and duplicated it's size, it also moves all the original data to their new location.
  86. */
  87. void duplicateTable(Table *table);
  88. /**
  89. * The function gets the list pointer and counts elements in the list.
  90. */
  91. int listLength(Object* obj);