Article 115 - Programming C - Map Implementation

A Map is similar to a Set, except that extra space is required to store the information associated to a key.

For information about the Set Structure, see Article 31 - Programming Data Structures.
For information about an implementation of a Set Structure, see Article 113 - Set Implementation.

To program a Map, we need to understand the interface of a Map.

Interface

  • New Hash Container (*Set Interface)
  • Add Entry
  • Get Entry
  • Remove Entry (*Set Interface)

Implementation

In this implementation, we use the Node structure from the Link List Article, see Article 83 - Data Structure - How to Implement Linked List in C.

Add Entry Function.

//wrapper to Set's function with map variable enabled
int addEntry( HashContainer * hashContainer, int key, int content ){
    return addEntry( hashContainer, ley, content, 1 );
}

Get Entry Function.

int getEntry( HashContainer * hashContainer, int key, int * content ){
    int hashedKey = key % 1000;

    if( !hashContainer ){
        return -1;
    }

    if( !content ){
        return -1;
    }

    if( hashContainer->entries[ hashedKey ] ){
        if( hashContainer->entries[ hashedKey ]->hashContainer ){
            return getEntry( hashContainer->entries[ hashedKey ]->
                hashContainer, key / 1000, content );
        }
        else if( hashContainer->entries[ hashedKey ]->key == key ){
            *content = hashContainer->entries[ hashedKey ]->content;
            return 1;
        }
    }

    return 0;
}

Comments (0)

Post a comment

  • Name:
  • Post:
  • Challenge:

Register or login to post comments easier.


Vantasy World Copyright 2011 - 2017. Vantasy World is a project developed by Vantasy Online. Privacy Policy.