Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "map.h"
- #include <stdlib.h>
- #include <stdio.h>
- struct Map_t{
- MapDataElement *values;
- MapKeyElement *keys;
- int iterator;
- int size;
- copyMapDataElements copy_value;
- copyMapKeyElements copy_key;
- freeMapDataElements free_value;
- freeMapKeyElements free_key;
- compareMapKeyElements compare;
- };
- //=========================================================Static functions here
- /**
- * freePair - frees the i th pair in the map.
- * @param map - The map to free the pair from.
- * @param i - The index of the pair to be freed.
- * @return
- * MAP_NULL_ARGUMENT - if a NULL was sent as map.
- * MAP_SUCCESS - all the pairs of elements had been inserted successfully
- * or i is out of range.
- */
- static MapResult freePair(Map map, int i){
- if (map==NULL){
- return MAP_NULL_ARGUMENT;
- }
- if (i<0 || i>=mapGetSize(map)){
- return MAP_SUCCESS;
- }
- map->free_value(map->values[i]);
- map->free_key(map->keys[i]);
- return MAP_SUCCESS;
- }
- /**
- * replacePair - replace the i th pair in map with a new pair and frees the old pair.
- * @param map - The map to replace the pair from.
- * @param i - The index of the pair to be freed.
- * @param dataElement - The data element to replace the old value with.
- * @param keyElement - The key element to replace the old value with.
- * @return
- * MAP_NULL_ARGUMENT - if a NULL was sent as map.
- * MAP_OUT_OF_MEMORY - if an allocation failed.
- * MAP_SUCCESS - all the pairs of elements had been inserted successfully
- * or i is out of range.
- */
- //static MapResult replacePair(Map map, int i, MapDataElement dataElement, MapKeyElement keyElement){
- // if (freePair(map, i)==MAP_NULL_ARGUMENT || dataElement==NULL || keyElement==NULL){
- // return MAP_NULL_ARGUMENT;
- // }
- // map->values[i] = map->copy_value(dataElement);
- // map->keys[i] = map->copy_key(keyElement);
- // if (map->values[i]==NULL || map->keys[i]==NULL){
- // return MAP_OUT_OF_MEMORY;
- // }
- // return MAP_SUCCESS;
- //}
- /**
- * mapPutAll - copies all key-value pairs from one map to another map.
- * Iterator's value is undefined after this operation.
- * @param srcMap - Map that will be copied from.
- * @param destMap - Map that will be copied to.
- * @return
- * MAP_NULL_ARGUMENT - if a NULL was sent as map.
- * MAP_OUT_OF_MEMORY - if an allocation failed (Meaning the function for copying
- * an element failed)
- * MAP_SUCCESS - all the pairs of elements had been inserted successfully
- */
- static MapResult mapPutAll(Map srcMap, Map destMap) {
- MapResult result;
- for(int i = 0; i < srcMap->size; i++) {
- result = mapPut(destMap, srcMap->keys[i], mapGet(srcMap, srcMap->keys[i]));
- if(result != MAP_SUCCESS) {
- return result;
- }
- }
- return result;
- }
- //=========================================================Static functions here
- Map mapCreate(copyMapDataElements copyDataElement,
- copyMapKeyElements copyKeyElement,
- freeMapDataElements freeDataElement,
- freeMapKeyElements freeKeyElement,
- compareMapKeyElements compareKeyElements) {
- if (copyDataElement == NULL || copyKeyElement == NULL ||
- freeDataElement == NULL || freeKeyElement == NULL || compareKeyElements == NULL) {
- return NULL;
- }
- Map map = malloc(sizeof(*map));
- if (map == NULL) {
- return NULL;
- }
- map->values = malloc(0);
- map->keys = malloc(0);
- map->iterator = 0;
- map->size = 0;
- map->copy_value = copyDataElement;
- map->copy_key = copyKeyElement;
- map->free_value = freeDataElement;
- map->free_key = freeKeyElement;
- map->compare = compareKeyElements;
- return(map);
- }
- void mapDestroy(Map map) {
- if (map == NULL) {
- return;
- }
- mapClear(map);
- free(map->values);
- free(map->keys);
- free(map);
- return;
- }
- Map mapCopy(Map map) {
- if (map == NULL) {
- return NULL;
- }
- Map map_copy = mapCreate(map->copy_value, map->copy_key, map->free_value, map->free_key, map->compare);
- if (map_copy == NULL) {
- return NULL;
- }
- map_copy->iterator = map->iterator;
- map_copy->size = map->size;
- MapResult result = mapPutAll(map, map_copy);
- if (result != MAP_SUCCESS) {
- return NULL;
- }
- return map_copy;
- }
- int mapGetSize(Map map){
- return (map==NULL)? -1 : map->size;
- }
- bool mapContains(Map map, MapKeyElement element) {
- if(map == NULL || element == NULL) {
- return false;
- }
- MAP_FOREACH(MapKeyElement, iterator, map) {
- if(map->compare(iterator, element) == 0) {
- return true;
- }
- }
- return false;
- }
- MapResult mapPut(Map map, MapKeyElement keyElement, MapDataElement dataElement){
- // if (map==NULL || keyElement==NULL || dataElement==NULL) {
- // return MAP_NULL_ARGUMENT;
- // }
- // for (int i=0; i<mapGetSize(map); i++){
- // printf("\n%d\n", *(int*)map->keys[i]);
- // printf("\n%d\n", *(int*)keyElement);
- // printf("\n%d\n", map->compare(keyElement, map->keys[i]));
- // if (map->compare(keyElement, map->keys[i])==0){ // replacing the value if keys are equal
- // map->free_value(map->values[i]);
- // map->values[i]=map->copy_value(dataElement);
- // return (map->values[i]!=NULL)? MAP_SUCCESS : MAP_OUT_OF_MEMORY;
- // } else if (map->compare(keyElement, map->keys[i])<0){
- // map->values = realloc(map->values, sizeof(*map->values) + sizeof(MapDataElement));
- // map->keys = realloc(map->keys, sizeof(*map->keys) + sizeof(MapKeyElement));
- // if (map->values==NULL || map->keys==NULL) return MAP_OUT_OF_MEMORY;
- // for (int j=mapGetSize(map); j>i; j--){
- // freePair(map, j); //Already checked if map is NULL. j is in the range.
- // map->values[j] = map->values[j-1];
- // map->keys[j] = map->keys[j-1];
- // }
- // replacePair(map, i, dataElement, keyElement);
- // if (map->values[i]==NULL || map->keys[i]==NULL){
- // return MAP_OUT_OF_MEMORY;
- // }
- // map->iterator++;
- // map->size++;
- // return MAP_SUCCESS;
- // }
- // }
- int new_size = mapGetSize(map);
- printf("\n%d\n", new_size);
- map->values = realloc(map->values, sizeof(*map->values) + sizeof(MapDataElement));
- printf("sign2");
- map->keys = realloc(map->keys, sizeof(*map->keys) + sizeof(MapKeyElement));
- printf("sign3");
- if (map->values==NULL || map->keys==NULL) return MAP_OUT_OF_MEMORY;
- printf("sign4");
- // map->values[new_size] = map->copy_value(dataElement);
- MapKeyElement k = map->copy_key(keyElement);
- printf("sign4.5");
- map->keys[new_size] = k;
- printf("sign5");
- if (map->values[new_size]==NULL || map->keys[new_size]==NULL){
- return MAP_OUT_OF_MEMORY;
- }
- printf("sign6");
- map->iterator++;
- map->size++;
- return MAP_SUCCESS;
- }
- MapDataElement mapGet(Map map, MapKeyElement keyElement) {
- if (map == NULL || keyElement == NULL) {
- return NULL;
- }
- if (mapContains(map, keyElement) == false) {
- return NULL;
- }
- for (int i = 0; i < map->size; i++) {
- if(map->compare(map->keys[i],keyElement) == 0) {
- return map->values[i];
- }
- }
- return NULL; //Should not get here.
- }
- MapResult mapRemove(Map map, MapKeyElement keyElement){
- if(map==NULL || keyElement==NULL){
- return MAP_NULL_ARGUMENT;
- }
- for (int i=0; i<mapGetSize(map); i++){
- if (map->keys[i]==keyElement){
- for (int j=i; j<mapGetSize(map); j++){
- freePair(map, j); //Already checked if map is NULL. j is in the range.
- map->values[j] = map->values[j+1];
- map->keys[j] = map->keys[j+1];
- }
- map->size--;
- return MAP_SUCCESS;
- }
- }
- return MAP_ITEM_DOES_NOT_EXIST;
- }
- MapKeyElement mapGetFirst(Map map){
- if (map==NULL){
- return NULL;
- }
- map->iterator=0;
- return (mapGetSize(map)==0)? NULL : map->keys[map->iterator];
- }
- MapKeyElement mapGetNext(Map map){
- if (map==NULL || map->iterator==mapGetSize(map)-1){
- return NULL;
- }
- map->iterator++;
- return map->keys[map->iterator];
- }
- MapResult mapClear(Map map) {
- if (map == NULL) {
- return MAP_NULL_ARGUMENT;
- }
- int index = map->size;
- while(map->size!=0) {
- mapRemove(map, map->keys[index]);
- index = map->size;
- }
- return MAP_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement