Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.67 KB | None | 0 0
  1. #include "map.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5.  
  6. struct Map_t{
  7. MapDataElement *values;
  8. MapKeyElement *keys;
  9. int iterator;
  10. int size;
  11. copyMapDataElements copy_value;
  12. copyMapKeyElements copy_key;
  13. freeMapDataElements free_value;
  14. freeMapKeyElements free_key;
  15. compareMapKeyElements compare;
  16. };
  17.  
  18.  
  19. //=========================================================Static functions here
  20. /**
  21. * freePair - frees the i th pair in the map.
  22. * @param map - The map to free the pair from.
  23. * @param i - The index of the pair to be freed.
  24. * @return
  25. * MAP_NULL_ARGUMENT - if a NULL was sent as map.
  26. * MAP_SUCCESS - all the pairs of elements had been inserted successfully
  27. * or i is out of range.
  28. */
  29. static MapResult freePair(Map map, int i){
  30. if (map==NULL){
  31. return MAP_NULL_ARGUMENT;
  32. }
  33. if (i<0 || i>=mapGetSize(map)){
  34. return MAP_SUCCESS;
  35. }
  36. map->free_value(map->values[i]);
  37. map->free_key(map->keys[i]);
  38. return MAP_SUCCESS;
  39. }
  40.  
  41. /**
  42. * replacePair - replace the i th pair in map with a new pair and frees the old pair.
  43. * @param map - The map to replace the pair from.
  44. * @param i - The index of the pair to be freed.
  45. * @param dataElement - The data element to replace the old value with.
  46. * @param keyElement - The key element to replace the old value with.
  47. * @return
  48. * MAP_NULL_ARGUMENT - if a NULL was sent as map.
  49. * MAP_OUT_OF_MEMORY - if an allocation failed.
  50. * MAP_SUCCESS - all the pairs of elements had been inserted successfully
  51. * or i is out of range.
  52. */
  53. //static MapResult replacePair(Map map, int i, MapDataElement dataElement, MapKeyElement keyElement){
  54. // if (freePair(map, i)==MAP_NULL_ARGUMENT || dataElement==NULL || keyElement==NULL){
  55. // return MAP_NULL_ARGUMENT;
  56. // }
  57. // map->values[i] = map->copy_value(dataElement);
  58. // map->keys[i] = map->copy_key(keyElement);
  59. // if (map->values[i]==NULL || map->keys[i]==NULL){
  60. // return MAP_OUT_OF_MEMORY;
  61. // }
  62. // return MAP_SUCCESS;
  63. //}
  64.  
  65. /**
  66. * mapPutAll - copies all key-value pairs from one map to another map.
  67. * Iterator's value is undefined after this operation.
  68. * @param srcMap - Map that will be copied from.
  69. * @param destMap - Map that will be copied to.
  70. * @return
  71. * MAP_NULL_ARGUMENT - if a NULL was sent as map.
  72. * MAP_OUT_OF_MEMORY - if an allocation failed (Meaning the function for copying
  73. * an element failed)
  74. * MAP_SUCCESS - all the pairs of elements had been inserted successfully
  75. */
  76. static MapResult mapPutAll(Map srcMap, Map destMap) {
  77. MapResult result;
  78. for(int i = 0; i < srcMap->size; i++) {
  79. result = mapPut(destMap, srcMap->keys[i], mapGet(srcMap, srcMap->keys[i]));
  80. if(result != MAP_SUCCESS) {
  81. return result;
  82. }
  83. }
  84. return result;
  85. }
  86.  
  87. //=========================================================Static functions here
  88.  
  89.  
  90. Map mapCreate(copyMapDataElements copyDataElement,
  91. copyMapKeyElements copyKeyElement,
  92. freeMapDataElements freeDataElement,
  93. freeMapKeyElements freeKeyElement,
  94. compareMapKeyElements compareKeyElements) {
  95.  
  96. if (copyDataElement == NULL || copyKeyElement == NULL ||
  97. freeDataElement == NULL || freeKeyElement == NULL || compareKeyElements == NULL) {
  98. return NULL;
  99. }
  100.  
  101. Map map = malloc(sizeof(*map));
  102. if (map == NULL) {
  103. return NULL;
  104. }
  105.  
  106. map->values = malloc(0);
  107. map->keys = malloc(0);
  108. map->iterator = 0;
  109. map->size = 0;
  110. map->copy_value = copyDataElement;
  111. map->copy_key = copyKeyElement;
  112. map->free_value = freeDataElement;
  113. map->free_key = freeKeyElement;
  114. map->compare = compareKeyElements;
  115.  
  116. return(map);
  117. }
  118.  
  119. void mapDestroy(Map map) {
  120. if (map == NULL) {
  121. return;
  122. }
  123. mapClear(map);
  124. free(map->values);
  125. free(map->keys);
  126. free(map);
  127. return;
  128. }
  129.  
  130.  
  131. Map mapCopy(Map map) {
  132.  
  133. if (map == NULL) {
  134. return NULL;
  135. }
  136.  
  137. Map map_copy = mapCreate(map->copy_value, map->copy_key, map->free_value, map->free_key, map->compare);
  138. if (map_copy == NULL) {
  139. return NULL;
  140. }
  141.  
  142. map_copy->iterator = map->iterator;
  143. map_copy->size = map->size;
  144.  
  145. MapResult result = mapPutAll(map, map_copy);
  146. if (result != MAP_SUCCESS) {
  147. return NULL;
  148. }
  149.  
  150. return map_copy;
  151. }
  152.  
  153.  
  154.  
  155. int mapGetSize(Map map){
  156. return (map==NULL)? -1 : map->size;
  157. }
  158.  
  159. bool mapContains(Map map, MapKeyElement element) {
  160. if(map == NULL || element == NULL) {
  161. return false;
  162. }
  163.  
  164. MAP_FOREACH(MapKeyElement, iterator, map) {
  165. if(map->compare(iterator, element) == 0) {
  166. return true;
  167. }
  168. }
  169.  
  170. return false;
  171. }
  172.  
  173.  
  174. MapResult mapPut(Map map, MapKeyElement keyElement, MapDataElement dataElement){
  175. // if (map==NULL || keyElement==NULL || dataElement==NULL) {
  176. // return MAP_NULL_ARGUMENT;
  177. // }
  178. // for (int i=0; i<mapGetSize(map); i++){
  179. // printf("\n%d\n", *(int*)map->keys[i]);
  180. // printf("\n%d\n", *(int*)keyElement);
  181. // printf("\n%d\n", map->compare(keyElement, map->keys[i]));
  182. // if (map->compare(keyElement, map->keys[i])==0){ // replacing the value if keys are equal
  183. // map->free_value(map->values[i]);
  184. // map->values[i]=map->copy_value(dataElement);
  185. // return (map->values[i]!=NULL)? MAP_SUCCESS : MAP_OUT_OF_MEMORY;
  186. // } else if (map->compare(keyElement, map->keys[i])<0){
  187. // map->values = realloc(map->values, sizeof(*map->values) + sizeof(MapDataElement));
  188. // map->keys = realloc(map->keys, sizeof(*map->keys) + sizeof(MapKeyElement));
  189. // if (map->values==NULL || map->keys==NULL) return MAP_OUT_OF_MEMORY;
  190. // for (int j=mapGetSize(map); j>i; j--){
  191. // freePair(map, j); //Already checked if map is NULL. j is in the range.
  192. // map->values[j] = map->values[j-1];
  193. // map->keys[j] = map->keys[j-1];
  194. // }
  195. // replacePair(map, i, dataElement, keyElement);
  196. // if (map->values[i]==NULL || map->keys[i]==NULL){
  197. // return MAP_OUT_OF_MEMORY;
  198. // }
  199. // map->iterator++;
  200. // map->size++;
  201. // return MAP_SUCCESS;
  202. // }
  203. // }
  204. int new_size = mapGetSize(map);
  205. printf("\n%d\n", new_size);
  206. map->values = realloc(map->values, sizeof(*map->values) + sizeof(MapDataElement));
  207. printf("sign2");
  208. map->keys = realloc(map->keys, sizeof(*map->keys) + sizeof(MapKeyElement));
  209. printf("sign3");
  210. if (map->values==NULL || map->keys==NULL) return MAP_OUT_OF_MEMORY;
  211. printf("sign4");
  212. // map->values[new_size] = map->copy_value(dataElement);
  213. MapKeyElement k = map->copy_key(keyElement);
  214. printf("sign4.5");
  215. map->keys[new_size] = k;
  216. printf("sign5");
  217. if (map->values[new_size]==NULL || map->keys[new_size]==NULL){
  218. return MAP_OUT_OF_MEMORY;
  219. }
  220. printf("sign6");
  221. map->iterator++;
  222. map->size++;
  223. return MAP_SUCCESS;
  224. }
  225.  
  226. MapDataElement mapGet(Map map, MapKeyElement keyElement) {
  227. if (map == NULL || keyElement == NULL) {
  228. return NULL;
  229. }
  230.  
  231. if (mapContains(map, keyElement) == false) {
  232. return NULL;
  233. }
  234.  
  235. for (int i = 0; i < map->size; i++) {
  236. if(map->compare(map->keys[i],keyElement) == 0) {
  237. return map->values[i];
  238. }
  239. }
  240.  
  241. return NULL; //Should not get here.
  242. }
  243.  
  244. MapResult mapRemove(Map map, MapKeyElement keyElement){
  245. if(map==NULL || keyElement==NULL){
  246. return MAP_NULL_ARGUMENT;
  247. }
  248. for (int i=0; i<mapGetSize(map); i++){
  249. if (map->keys[i]==keyElement){
  250. for (int j=i; j<mapGetSize(map); j++){
  251. freePair(map, j); //Already checked if map is NULL. j is in the range.
  252. map->values[j] = map->values[j+1];
  253. map->keys[j] = map->keys[j+1];
  254. }
  255. map->size--;
  256. return MAP_SUCCESS;
  257. }
  258. }
  259. return MAP_ITEM_DOES_NOT_EXIST;
  260. }
  261.  
  262. MapKeyElement mapGetFirst(Map map){
  263. if (map==NULL){
  264. return NULL;
  265. }
  266. map->iterator=0;
  267. return (mapGetSize(map)==0)? NULL : map->keys[map->iterator];
  268. }
  269.  
  270. MapKeyElement mapGetNext(Map map){
  271. if (map==NULL || map->iterator==mapGetSize(map)-1){
  272. return NULL;
  273. }
  274. map->iterator++;
  275. return map->keys[map->iterator];
  276. }
  277.  
  278. MapResult mapClear(Map map) {
  279. if (map == NULL) {
  280. return MAP_NULL_ARGUMENT;
  281. }
  282. int index = map->size;
  283. while(map->size!=0) {
  284. mapRemove(map, map->keys[index]);
  285. index = map->size;
  286. }
  287. return MAP_SUCCESS;
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement