| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
jopen
12年前发布

hashmap的C语言实现

源代码(适合Linux和Windows)包括:

    hashmap.c

    hashmap.h

MSVC测试文件:

    main.c

下面是源代码,最初来自github,我改写了几个地方,并重写了全部测试代码.没有内存泄露,请放心使用.

/**   * hashmap.h   */  #ifndef _HASHMAP_H_INCLUDED  #define _HASHMAP_H_INCLUDED    #define HMAP_E_OUTMEM   (-4)     /* Out of Memory */  #define HMAP_E_NOTFOUND (-3)     /* No such element */  #define HMAP_E_OVERFLOW (-2)     /* Hashmap is full */  #define HMAP_E_FAIL     (-1)     /* Hashmap api fail */  #define HMAP_S_OK       (0)      /* Success */    /**   * void_ptr is a pointer. This allows you to put arbitrary structures in the hashmap.   */  typedef void* void_ptr;    /**   * hmap_t is a pointer to an internally maintained data structure.   * Clients of this package do not need to know how hashmaps are   * represented. They see and manipulate only hmap_t's.   */  typedef void_ptr hmap_t;    /**   * hmap_callback_func is a pointer to a function that can take two void_ptr arguments   * and return an integer. Returns status code..   */  typedef int (*hmap_callback_func)(void_ptr, void_ptr);    /**   * Return an empty hashmap. Returns NULL if empty.   */  extern hmap_t hashmap_create();    /**   * Iteratively call fn with argument (value, arg) for each element data   * in the hashmap. The function returns anything other than HMAP_S_OK    * the traversal is terminated. fn must not modify any hashmap functions.   */  extern int hashmap_iterate(hmap_t in, hmap_callback_func fnIterValue, void_ptr arg);    /**   * Add an element to the hashmap. Return HMAP_S_OK or HMAP_E_OUTMEM.   */  extern int hashmap_put(hmap_t in, char* key, void_ptr elem);    /**   * Get an element from the hashmap. Return HMAP_S_OK or HMAP_E_NOTFOUND.   */  extern int hashmap_get(hmap_t in, char* key, void_ptr *elem);    /**   * Remove an element from the hashmap. Return HMAP_S_OK or HMAP_E_NOTFOUND.   */  extern int hashmap_remove(hmap_t in, char* key, void_ptr *outValue);    /**   * Free the hashmap   */  extern void hashmap_destroy(hmap_t in, hmap_callback_func fnFreeValue, void_ptr arg);    /**   * Get the current size of a hashmap   */  extern int hashmap_size(hmap_t in);    #endif /* _HASHMAP_H_INCLUDED */

 本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1341107370387.html
C语言 C/C++开发