[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 423
  • Last Modified:

URGENT:: misleading pointer?????

I am writing a map.cpp file, which is almost debugged (I think) (I hope) but there is a problem in my mapalmostworkingll.cpp. It complies but it never reaches map_get. I think it is a misleading pointer but not sure?? Any ideas or suggestions on how to fix it??

#################################
mapalmostworkingll3.cpp
#################################
#include <iostream>
#include <istream>
#include <ostream>
#include <cstdio>
#include <cassert>
#include <cstring>
#include <cstdlib>
#include "map.h"
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <string>

using namespace std;

static int hash( Map_item index );
static bool compareItems( Map_item, Map_item );
static int get_index( Map, Map_item, Map* );
static bool validifyItem( Map_item );
static Map extend_map( Map );
/*
 * Create a new map and return a handle to the map.
 *
 * Pre: initial_capacity >= 0, delta_capacity >= 0;
 *
 * Post: returns a handle to an empty map that has an initial capacity of
 *        initial_capacity items, and that is expanded by delta_capacity
 *        items when the map fills up.  
 *        returns NULL on failure
 *
 * Side Effects: may allocate storage.
 */
Map Map_create( int initial_capacity, int delta_capacity ) {
      Map result;
      cout<<"Made it to map_create"<<endl;
      assert( (initial_capacity > 0) && (delta_capacity >= 0) );
      result = (Map) new Map_data_struct;
      assert( result != NULL );
      result->max_size = initial_capacity;
      cout << "Map max size is: " << result->max_size << endl;
      result->delta_size = delta_capacity;
      cout << "Map delta size is: " << result->delta_size << endl;
      result->cur_size = 0;
      cout << "Map current size is: " << result->cur_size << endl;
      result->pairs = new Map_pair[initial_capacity];
      assert( result->pairs != NULL );
      result->next_map = NULL;
      
      return result;
}
/*
 * Destroy an existing map with handle M.
 *
 * Pre: M is a valid handle
 * Post: none
 *
 * Side Effects: all memory allcated for the map is freed, including memory
 * associated with the actual data in an item. Because the maps are linked we n
 * need to iterate through each linked list and destory them. Map M passes to the
 * function is th eonly top-level map.
 */


void Map_destroy( Map M ) {
      cout<<"Made it to Map_destory"<<endl;
      Map current_map = M;
      assert( current_map != NULL );
      while( current_map != NULL ) {
            for( int i=0; i < current_map->max_size; i++ ) {
                  if( (current_map->pairs)[i].index.type != Map_Undefined ) {
                        Map_item_set_undefined( &((current_map->pairs)[i].index) );
                        Map_item_set_undefined( &((current_map->pairs)[i].value) );
                  }
            }
            Map kill_map = current_map;
            current_map = current_map->next_map;
            delete kill_map->pairs;
            delete kill_map;
      }            
}


// If the key exists in the map, it will return a 1

int Map_exists( Map M, Map_item index ) {
      cout<<"Made it to Map_exists"<<endl;
      
      Map current_map;
      return get_index( M, index, &current_map ) != -1;
}

// Adds a new data to the map with the key 'index'
// if the key already exists, its data will be replaced with the new data
// If the key does not exist, it will be added to the map
void Map_set( Map M, Map_item index, Map_item value ) {
      cout<<"Made it to Map_set"<<endl;
      assert( (validifyItem(index) && validifyItem(value)) );
      Map current_map = M;
      int i = hash(index);
      assert( current_map != NULL );//if Null will kill
      
      //Checks to see if full
      // if statement should take set current_map to the last,
      //non-full Map in the linked list.
      
      if( (current_map->cur_size)/(current_map->max_size) > 0.75 ) {
      
      //if there are more maps in the linked list, it will iterate to the last map
      //remember, if the current_map->next_map != NULL, assume that current_map is
      //full
            
            while( current_map->next_map != NULL )
                  current_map = current_map->next_map;
      //the last map in the linked list may be full.
      
            if( (current_map->cur_size)/(current_map->max_size) > 0.75 )
                  current_map = extend_map( current_map );
      }
      
      //no duplicate keys in the map.
      assert( !Map_exists( current_map, index ));
      //find the first free position in the map
      while( (current_map->pairs)[i].index.type != Map_Undefined ) {
            if (i == (current_map->max_size))
            i = 0;
            else i++;
      }
      
      Map_item_copy( &((current_map->pairs)[i].index), index);
      Map_item_copy( &((current_map->pairs)[i].value), value);
      (current_map->cur_size)++;
}

// Gets data from the map
void Map_get( Map M, Map_item index, Map_item* valueptr ) {
      cout<<"Made it to Map_get"<<endl;
      assert( validifyItem( index ));
      Map current_map = NULL;
      
      int j = get_index( M, index, &current_map );
      
      if( j == -1 ) {
            ;
            Map_item_set_undefined( valueptr );
            Map_item_assign_integer( valueptr, 0 );
      }
      else {
            Map_item_copy( valueptr, (current_map->pairs)[j].value );
      }
}

// Deletes data from the map
void Map_delete( Map M, Map_item index ) {
      
      assert( validifyItem( index ));
      Map current_map = NULL;
      int hashed_index = get_index( M, index, &current_map );
      if( hashed_index != -1 ) {
          Map_item_set_undefined( &((current_map->pairs)[hashed_index].index) );
            Map_item_set_undefined( &((current_map->pairs)[hashed_index].value) );
            (current_map->cur_size)--;
      }
}


void Map_traverse( Map M, Map_traverse_fn* f) {
      for( int i = 0; i < M->max_size; i++ ) {
            if( (M->pairs)[i].index.type != Map_Undefined )
                  (*f)( M->pairs[i] );
      }
}


void Map_item_set_undefined( Map_item* itemp ) {
      itemp->type = Map_Undefined;
if( (*itemp).type == Map_String){
      free((*itemp).data.as_string);
      (*itemp).data.as_string=NULL;
      }
      (*itemp).type = Map_Undefined;
}

// copies one map item over another
void Map_item_copy( Map_item* dest, Map_item src ) {
      Map_item_set_undefined( dest );
      (*dest).type = src.type;
      switch( src.type ) {
   case Map_Integer:
      (*dest).data.as_integer = src.data.as_integer;
            break;
      case Map_String:
            (*dest).data.as_string = strdup(src.data.as_string);
                  break;
      }
}

// reassigns a map_item's data to the given string
void Map_item_assign_string( Map_item* destp, char* s ) {
      (*destp).type = Map_String;
      (*destp).data.as_string = strdup(s);
      assert((*destp).data.as_string);
}

// reassigns a map_item's data to the given int
void Map_item_assign_integer( Map_item* destp, int i ) {
      (*destp).type = Map_Integer;
      (*destp).data.as_integer = i;
}

/* compares two Map_items for equality */
 bool compareItems( Map_item a, Map_item b ) {
      if( a.type != b.type ) return false;
      else {
if( (a.type == Map_String) && (b.type == Map_String) ) {
      if( strcmp( a.data.as_string,b.data.as_string ) == 0 ) return true;
                  else return false;
            }
      else if( (a.type == Map_Integer) && (b.type == Map_Integer) ) {
            if( a.data.as_integer == b.data.as_integer ) return true;
                  else return false;
            }
            cout << "BETTER NOT SEE THIS.";
      }
      cout << "OR THIS.";
      return false;
}
/* Returns the index of the given Map_item. If it does not exist in the map,
 it will return a -1
 */
int get_index( Map M, Map_item index, Map *index_map ) {
      assert( validifyItem( index ));
      Map current_map = M;
      
      int i = hash( index );
      int j = i;
      bool found_index = false;
      
      while( !found_index ) {
            
            i = j;
      // check the current_map for the index
      
      if( ((current_map->pairs)[i].index.type == index.type) &&
             compareItems( (current_map->pairs)[i].index, index )) {
                  found_index = true;
                  j = i;
            }
            else {
      // iterate throught the rest of the map and check
      //if key exists in the map
                  if( i == current_map->max_size ) i = 0;
                  else i++;
                  while( i != j ) {
                        
      if( ((current_map->pairs)[i].index.type == index.type) &&
             compareItems( (current_map->pairs)[i].index, index)) {
            found_index = true;
                  j = i;
                        }
                        else {
                        
            if( i == (current_map->max_size)) {
                  i = 0;
                                    
                  }
                  else {
                  i++;
                                    
                              }
                        }
                  }
      // check if there's another map if it doesn't exist in the first one
      //
                  if( current_map->next_map != NULL )
                   current_map = current_map->next_map;
      //if there's no more maps, and its still not found, it doesn't exist
                  else {
                        found_index = true;
                        j = -1;
                  }
            }
      }
      cout << "address of current_map: " << current_map << endl;
      *index_map = current_map;
      cout << "address of index_map: " << index_map << endl;
      cout <<  "index j of: " << j << endl;
      return j;
}

/* Checks that the given Map_item is a valid type */
bool validifyItem( Map_item item ) {
      return ((item.type == Map_String) || (item.type == Map_Integer));
}

/* Adds a new map into our linked list of Maps */
Map extend_map( Map M ) {
      Map result = Map_create( M->max_size, M->delta_size );
      M->next_map = result;
      return result;
}
/*Hash function
* Author: Carrie Brown
* function mods, times, and divides, and shifts the suff around so
* that it creates a unique key.
* Date: Oct 30th
* Version2
*/
int  hash(Map_item index){
 
        string key = index.data.as_string; //declares a string key
        int hash = 0;
        for(int i = 0; key[i]!='\0'; i++) { //while the value exists
                       hash += i % 23;
                hash -= hash * 101;
                hash += hash / 113;
                hash += (key[i] << 12);
        hash ^= (key[i] >> 22);
        hash += (key[i] << 4);
        hash ^= (key[i] >> 9);
        hash += (key[i] << 10);
            }  
           
              return hash;       //returns the hash..aka.the key
          }    
##################################
Map.h
############################
/*
 *  Interface specification for map ADT
 *
 *  A map is a collection of <index, value> pairs, where each item in the
 *  pair can be (in principle) of arbitrary type.  This particular
 *  instance of map supports integer and string items.
 *
 *  All exposed names in this module begin with Map_
 *
 *  Each pair in the map is a structure, of type Map_pair.  Each of the
 *  items in the pair is of type Map_item.  An item is itself a structure
 *  consisting of a type specification field that says what the type of
 *  the item is, and a union field that contains the item of particular
 *  type.  At present 3 types of item are suported:
 *
 *     Map_Undefined - the item is undefined and has no value
 *     Map_String    - the item contains a char* pointing to a string
 *     Map_Integer   - the item contains an int value
 *
 *  The special type, Map_Undefined is NOT storable in the map, but is used
 *  instead to indicate that M[index] does not exist in the map.
 *
 *  To build an item, you must set the type field, and then assign to the
 *  union:
 *          Map_item x;
 *          x.type = Map_Integer;
 *          x.data.as_integer = 42;
 *
 *          x.type = Map_String;
 *          x.data.as_string = "This is a constant somewhere";
 *
 *       
 *  Constructors/Destructors: Map_create, Map_destroy
 *  Properties: Map_exists
 *  Methods: Map_set, Map_get, Map_delete, Map_traverse
 *
 */


#ifndef MAP_H
#define MAP_H

enum Map_item_types_enum {
  Map_Undefined, Map_String, Map_Integer
};

typedef enum Map_item_types_enum Map_item_type;

typedef union {
    int   as_integer;
    char* as_string;
} Map_data_union;

typedef struct {
    Map_item_type type;
    Map_data_union data;
} Map_item;

typedef struct {
    Map_item index;
    Map_item value;
} Map_pair;


/* Replace the following with your implementation declarations */
//typedef struct {
//    int max_size;
//    int delta_size;
//    int cur_size;
//    Map_pair* pairs;
//} Map_data_struct;
struct Map_data_struct {
      int max_size;
      int delta_size;
      int cur_size;
      Map_pair* pairs;
      Map_data_struct* next_map;
};
/* end of your implementation declarations */

typedef Map_data_struct* Map;


extern Map Map_create (int initial_capacity, int delta_capacity);
/*
 * Create a new map and return a handle to the map.
 *
 * Pre: initial_capacity >= 0, delta_capacity >= 0;
 *
 * Post: returns a handle to an empty map that has an initial capacity of
 *        initial_capacity items, and that is expanded by delta_capacity
 *        items when the map fills up.  
 *        returns NULL on failure
 *
 * Side Effects: may allocate storage.
 */

extern void Map_destroy (Map M);
/*
 * Destroy an existing map with handle M.
 *
 * Pre: M is a valid handle
 * Post: none
 *
 * Side Effects: all memory allcated for the map is freed, including memory
 * associated with the actual data in an item.  
 */

extern int Map_exists(Map M, Map_item index);
/*
 * Test if a pair exists in the map with matching index
 *
 * Pre: M is a valid handle
 * Post: returns 1 if the M[index] exists, that is if map contains
 *       a pair <index,v> for some value v
 *
 */

extern void Map_set(Map M, Map_item index, Map_item value);
/*
 * Add an pair to the map, i.e. set M[index] = value
 *
 * Pre: M is a valid handle,
 *      index.type != Map_Undefined
 *      value.type != Map_Undefined
 *
 * Post: M[index] = value
 *
 * Side Effects: If M already contains a pair with the given index, the
 * original associated value is discarded and replaced with the new value.
 * If M does not contains the pair, the pair is added to the map, with the
 * map being extended if it is full.
 * For Map_String type indexes and values, the string is copied and the
 * new item will point to the copy.
 *
 */
 

extern void Map_get(Map M, Map_item index, Map_item* valuep);
/*
 * Fetch a value corresponding to an index, i.e. retrieve M[index]
 *
 * Pre: M is a valid handle, (*valuep).type = Map_Undefined
 * Post: if Map_exists(index) then (*valuep) = Map[index]
 *       if ! Map_exists(index), then (*valuep).type = Map_Undefined.
 */
 

extern void Map_delete(Map M, Map_item index);
/*
 * Remove a pair from the map
 *
 * Pre: M is a valid handle
 * Post: M[index] no longer exists in the map.
 * Side Effects: reclaims any storage associated with the pair
 */

typedef void Map_traverse_fn(const Map_pair pair);

extern void Map_traverse (Map M, Map_traverse_fn* f);
/*
 * Walk over the map and apply a function to each pair
 *
 * Pre: M is a valid handle, f does not modify the map contents
 * Post: none
 * Side Effects:
 * Warning: what happens if the map is modified during traversal is
 *     undefined.  Thus the traversal function should not alter the
 *     contents of the map.
 */

/* Map_Item manipulation routines */
void Map_item_set_undefined(Map_item* item);
void Map_item_copy(Map_item* dest, Map_item src);
void Map_item_assign_string(Map_item* destp, char* s);
void Map_item_assign_integer(Map_item* destp, int i);
#endif

################################
testformap.cpp
#############################
//Test for Map, Thsi test works with map.cpp without linked list.
//It prompts the user to inpu two words, then it hashes them and
//places them into the map. It prints out map entery, index, value
// and hash
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include "map.h"
#include <string>
#include <iostream>
using namespace std;
using std::cin;
using std::cout;



int hash(Map_item index);
void print_entry(const Map_pair pair);
void print_item(Map_item item);

int main(){
Map map;
int max = 100;
int delta = 100;
map = Map_create(max, delta);
Map_item words;
Map_item numbers;


char test2[30];
cout<<"Pleae Enter two word ";
cin>>test2;

words.type = Map_String;
words.data.as_string = test2;
numbers.type = Map_Integer;
numbers.data.as_integer = 0;
Map_set(map, words, numbers);


cin>>test2;
         
words.type = Map_String;
words.data.as_string = test2;
numbers.type = Map_Integer;
numbers.data.as_integer = 0;
Map_set(map, words, numbers);
// traverse map and print all valid entries
Map_traverse (map, print_entry);
cin >> max;
return 0;
}

// prints a map entry
void print_entry(const Map_pair pair) {
    // print the (index,value) pair from the map
    printf("(%d,%d):", pair.index.type, pair.value.type);
    print_item(pair.index);
    cout << ", ";
    print_item(pair.value);
      //int d=hash(pair.index);
      cout<<":"<<" Hash key: ";
      //cout<<d;
      cout<<endl;
    return;
}

void print_item(Map_item item) {
    switch(item.type) {
    case Map_Undefined:
          printf("Undefined");
          break;
    case Map_Integer:
          printf("%d", item.data.as_integer);
          break;
      case Map_String:
          printf("\"%s\"", item.data.as_string);
          break;
      default:
          printf("BadItemType");
    }
}

#################################3
Help is desperatly needed. Thanks Alot :0
0
Carrie37
Asked:
Carrie37
  • 3
1 Solution
 
georg74Commented:
hi,
- please post your "map.h", you may post to www.pastebin.com and give the link here.

- please write a few words about what your map is supposed to do.

it will be much easier to help.

gl,
georg
0
 
Carrie37Author Commented:
www.pastebin.com/116470
hee is the link for the map.h
thats where it at. Well the error is in the mapalmostworkingll.cpp in map_get. (I am pretty sure). and I have a feeling it is a misleading pointer. mind you i am not very good a pointers so i could be wrong. The mapalmostworking.cpp is suppose to create a map (lets say size ten)  and will eventually be used to hash say a texted file and store the hash in the map. However, instead of expanded the map when the map is full and then rehashing, (bad time efficient), i have craeted a  linked list to join a newkly created map together. That the big pic.  when i run it (it compiles) believe it or not with my maptest, it never quiit makes it to map_get. (I Think). the testfile, will print out size, etc of new map, prompt user for wo words, and then its suppose to print out a bunch of stuff (like) map index, hash value, etc, but its not working. I hope thats all you need.
0
 
Carrie37Author Commented:
Its all I good..  think i fixed the problem with the map.cpp
However, now if I use my anagram.cpp with map.cpp instea of testformap.cpp. The anagram compiles but does not run properly. The whole idea of anagram.cpp id to take in a valid txt file and put the letters into lower case, and take each word and put the letters into each word into alphbetical order then call map.cpp to hash and store into the map. It the index is the same in two words anagram.cpp will print out the perviously stored word, and the current word and will store the current word in the map and delete the old word. This is what I have..
/*anangram.cpp
This code uses map.cpp to read in a texted file.
It will first put all letters into lower case
and then in alphbetical order. it will then hash it
and put it into the map, if another word comes up with
the same index these wods are anagrams and will be printed out.
The new index will replace the old on.

*/


 
#include <iostream>
      using std::cin;
      using std::cout;
      using std::ios;
      using std::cerr;
      using std::endl;
      
#include "map.h"
#include <fstream>
      using std::basic_ifstream;
      using std::ifstream;
      
      
int MAX_WORD= 60;


int bubble( char*, int );


int main(int argc, char *argv[]){
int y=0;
char line[60];
char *token= strtok(line," ");
char *token1= strtok(line," ");
Map_item value;
Map_item index;

Map M = Map_create(25, 0);
cin>>line;

if (argc > 1){ //give the program a file to work with
      ifstream in( argv[1]);
             if (!in){  //checks to see if file actully exists
              std::perror(argv[1]);
              return EXIT_FAILURE; //if no then exit
 }
 }
     
     while (token !=NULL){  //while the token exists
       strcpy(token1,token); //create a copy of the word
   
     //to lowercase:
       
      for (int j = 0; j <=256; j++){
        line[j]= tolower((char) line[j]);  
        }
     bubble(token1,strlen(token1));
     
      Map_item index;
            index.type = Map_String;
            index.data.as_string = token1;
     Map_item value;
      value.type = Map_String;
      value.data.as_string = token;
     
      }
      token = strtok(NULL," ");
    if( Map_exists( M, index )) {
            Map_get( M, index, &value );
                  cout << value.data.as_string << " " << line<< endl;
                  }
                  else {
                        Map_set( M, index, value );
                  }
            }
      
// end of main()

     

int bubble(char buffer[], int y){
   
    int a,b; //declares a and b
    char t;  //charcater t
   
    for (a=1; a<y; a++){  //for(a equals zero, while j is less than or equal
                              // to y, increment j)
        for (b= 0; b>=a; --b){
            if (buffer[0] > buffer[b]){
                t = buffer[0];
                }else if(buffer [0] == buffer[b])
                //ignore
                !cout<<buffer[b];
                buffer[0] = buffer[b];
                buffer[b] = t;
            }
        }
        return 0;
                    
             }
0
 
Carrie37Author Commented:
oh and maybe the working map.cpp would help
################
MAP>CPP
###################
#include <iostream>
#include <istream>
#include <ostream>
#include <cstdio>
#include <cassert>
#include <cstring>
#include <cstdlib>
#include "map.h"
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <string>

using namespace std;

static int hash( Map_item index );
static bool compareItems( Map_item, Map_item );

static bool validifyItem( Map_item );
static Map extend_map( Map );
/*
 * Create a new map and return a handle to the map.
 *
 * Pre: initial_capacity >= 0, delta_capacity >= 0;
 *
 * Post: returns a handle to an empty map that has an initial capacity of
 *        initial_capacity items, and that is expanded by delta_capacity
 *        items when the map fills up.  
 *        returns NULL on failure
 *
 * Side Effects: may allocate storage.
 */
Map Map_create( int initial_capacity, int delta_capacity ) {
      cout<<"Made it to Map_create"<<endl;
      Map result;
      assert( (initial_capacity > 0) && (delta_capacity >= 0) );
      
      result = (Map) new Map_data_struct;
      assert( result != NULL );
      result->max_size = initial_capacity;
      
      cout << "Map max size is: " << result->max_size << endl;
      result->delta_size = delta_capacity;
      cout << "Map delta size is: " << result->delta_size << endl;
      
      result->cur_size = 0;
      cout << "Map current size is: " << result->cur_size << endl;
      
      result->pairs = new Map_pair[initial_capacity];
      assert( result->pairs != NULL );
      
      result->next_map = NULL;
      return result;
}



// If the key exists in the map, it will return a 1



// Adds a new data to the map with the key 'index'
// if the key already exists, its data will be replaced with the new data
// If the key does not exist, it will be added to the map
void Map_set( Map M, Map_item index, Map_item value ) {
      cout<<"Made it to Map_set"<<endl;
      assert( (validifyItem(index) && validifyItem(value)) );
      Map current_map = M;
      int i = hash(index) % current_map->max_size;
      cout << "setting map with index "  << i << endl;
      assert( current_map != NULL );//if Null will kill
      
      //Checks to see if full
      // if statement should take set current_map to the last,
      //non-full Map in the linked list.
      
      if( (current_map->cur_size)/(current_map->max_size) > 0.75 ) {
      
      //if there are more maps in the linked list, it will iterate to the last map
      //remember, if the current_map->next_map != NULL, assume that current_map is
      //full
            
            while( current_map->next_map != NULL )
                  current_map = current_map->next_map;
      //the last map in the linked list may be full.
      
            if( (current_map->cur_size)/(current_map->max_size) > 0.75 )
                  current_map = extend_map( current_map );
      }
      
      //no duplicate keys in the map.
      assert( !Map_exists( current_map, index ));
      //find the first free position in the map
      while( (current_map->pairs)[i].index.type != Map_Undefined ) {
            if (i == (current_map->max_size))
            i = 0;
            else i++;
      }
      
      Map_item_copy( &((current_map->pairs)[i].index), index);
      Map_item_copy( &((current_map->pairs)[i].value), value);
      (current_map->cur_size)++;
}

// Gets data from the map

static int get_index(Map M, Map_item index);
static int get_index(Map M, Map_item index) {
cout<<"Made it to get_index"<<endl;
      assert( validifyItem( index ));
      Map current_map = M;
      
      int i = hash( index ) % current_map->max_size;
      cout << "get indoex looking for "  << i <<endl;
      int j = i;
      bool found_index = false;
      
      while( !found_index ) {
            
            i = j;
      // check the current_map for the index
      
            if( ((current_map->pairs)[i].index.type == index.type) &&
                   compareItems( (current_map->pairs)[i].index, index )) {
                  found_index = true;
                  j = i;
            }
            else {
      // iterate throught the rest of the map and check
      //if key exists in the map
                  if( i == current_map->max_size ) i = 0;
                  else i++;
                  while( i != j ) {
                        
                        if( ((current_map->pairs)[i].index.type == index.type) &&
                               compareItems( (current_map->pairs)[i].index, index)) {
                              
                              found_index = true;
                              j = i;
                        }
                        else {
                        
                              if( i == (current_map->max_size)) {
                                    i = 0;
                                    
                              }
                              else {
                                    i++;
                                    
                              }
                        }
                  }
      // check if there's another map if it doesn't exist in the first one
      //
                  if( current_map->next_map != NULL )
                  current_map = current_map->next_map;
      //if there's no more maps, and its still not found, it doesn't exist
                  else {
                        found_index = true;
                        j = -1;
                  }
            }
      }
      cout << "address of current_map: " << current_map << endl;
      //*index_map = current_map;
      //cout << "address of index_map: " << index_map << endl;
      cout <<  "index j of: " << j << endl;
      return j;
}

int Map_exists(Map M, Map_item index) {      
cout<<"Made it to Map_exists"<<endl;                                      //check if index already exists
      assert(validifyItem(index));                              //checks to see if index is valid index type (ie.int)
    int i=get_index(M, index);
    if(i==-1) return 0; else return 1;                                                      //return's the value of the index if it is in the map
}
void Map_get( Map M, Map_item index, Map_item* valuep ) {
cout<<"Made it to Map_get"<<endl;
      assert( validifyItem( index ));
      Map current_map = NULL;
      
      int j = get_index( M, index);
      
      if( j == -1 ) {
            ;
            Map_item_set_undefined( valuep );
            Map_item_assign_integer( valuep, 0 );
      }
      else {
            Map_item_copy( valuep, (current_map->pairs)[j].value );
      }
}

void Map_traverse( Map M, Map_traverse_fn* f) {
      for( int i = 0; i < M->max_size; i++ ) {
            if( (M->pairs)[i].index.type != Map_Undefined )
                  (*f)( M->pairs[i] );
      }
}


void Map_item_set_undefined( Map_item* itemp ) {
cout<<"Made it to Map_item_set_undefined"<<endl;
      itemp->type = Map_Undefined;
if( (*itemp).type == Map_String){
      free((*itemp).data.as_string);
      (*itemp).data.as_string=NULL;
      }
      (*itemp).type = Map_Undefined;
}

// copies one map item over another
void Map_item_copy( Map_item* dest, Map_item src ) {
cout<<"Made it to Map_item_copy"<<endl;
      Map_item_set_undefined( dest );
      (*dest).type = src.type;
      switch( src.type ) {
            case Map_Integer:
                  (*dest).data.as_integer = src.data.as_integer;
                  break;
            case Map_String:
                  (*dest).data.as_string = strdup(src.data.as_string);
                  break;
      }
}

// reassigns a map_item's data to the given string
void Map_item_assign_string( Map_item* destp, char* s ) {
      (*destp).type = Map_String;
      (*destp).data.as_string = strdup(s);
      assert((*destp).data.as_string);
}

// reassigns a map_item's data to the given int
void Map_item_assign_integer( Map_item* destp, int i ) {
      (*destp).type = Map_Integer;
      (*destp).data.as_integer = i;
}

/* compares two Map_items for equality */
 bool compareItems( Map_item a, Map_item b ) {
 cout<<"Made it to compare items"<<endl;
      if( a.type != b.type ) return false;
      else {
            if( (a.type == Map_String) && (b.type == Map_String) ) {
                  if( strcmp( a.data.as_string,b.data.as_string ) == 0 ) return true;
                  else return false;
            }
            else if( (a.type == Map_Integer) && (b.type == Map_Integer) ) {
                  if( a.data.as_integer == b.data.as_integer ) return true;
                  else return false;
            }
            cout << "BETTER NOT SEE THIS.";
      }
      cout << "OR THIS.";
      return false;
}
/* Returns the index of the given Map_item. If it does not exist in the map, it will return a -1
 */
 

/* Checks that the given Map_item is a valid type */
bool validifyItem( Map_item item ) {
      return ((item.type == Map_String) || (item.type == Map_Integer));
}

/* Adds a new map into our linked list of Maps */
Map extend_map( Map M ) {
      Map result = Map_create( M->max_size, M->delta_size );
      M->next_map = result;
      return result;
}
/*Hash function
* Author: Carrie Brown
* function mods, times, and divides, and shifts the suff around so
* that it creates a unique key.
* Date: Oct 30th
* Version2
*/
int  hash(Map_item index){
 
        string key = index.data.as_string; //declares a string key
        int hash = 0;
        for(int i = 0; key[i]!='\0'; i++) { //while the value exists
                       hash += i % 23;
                hash += hash * 101;
                hash += hash / 113;
                hash += (key[i] << 12);
                          hash ^= (key[i] >> 22);
                          hash += (key[i] << 4);
                          hash ^= (key[i] >> 9);
                          hash += (key[i] << 10);
            }  
           
              return hash;                        //returns the hash..aka.the key
          }      
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now