Giter VIP home page Giter VIP logo

hashmap's Introduction

Codacy Badge Build Status codecov License: MIT

Hashmap

This is an implementation of the HashMap data structure.

initialization and destruction:

Map * new_map();

creates a new map, returns its pointer.

  • first parameter is the initial size
  • second parameter is the function to compare keys
  • third parameter is the function to generate hashes for keys

void delete_map();

deletes the map


working with a map: the following functions first parameter is always the pointer to an initialized map.

void map_put();

creates a new entry in the map

  • second parameter is the key
  • third parameter is the value

returns nothing.

void *map_get();

gets the value from an entry

  • second parameter is the key of an entry

returns NULL if entry not found for key

void *map_remove();

removes an entry from the map

  • second parameter is the key of an entry

returns the value of the entry after remove, returns NULL if entry not found.

int map_contains(); returns if an entry was found for the given key.

  • second parameter is the key of an entry

returns true if an entry was found, false otherwise.

int map_isempty();

returns true if the given map is empty, returns false if not.


usage:

the following example uses strings as key and value

#include "HashArray.h"

int strhash(char *str){
    int hash = 1, i;
    for(i = 0; s[i]; i++){
        hash = (hash * 10) + s[i];
    }
    return hash;
}


int main(){
    Map *mymap = new_map(10, strcmp, strhash);
    printf("is the map empty? -%s", map_isempty(mymap) ? "yes" : "no");
   
    //put an element in the map:
    map_put(mymap, "key", "value");
    printf("%s\n", map_get("key"));    
    printf("is the map empty? -%s", map_isempty(mymap) ? "yes" : "no");        
    map_remove(mymap, "key");
    printf("%s\n", map_get("key"));
    printf("is the map empty? -%s", map_isempty(mymap) ? "yes" : "no");
    return 0;
}

hashmap's People

Contributors

vgeorgee avatar webgyap avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

codacy-badger

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.