Signed-off-by: icesky <icesky1stm@sina.com>

git-as-svn/v1/master
icesky 2016-08-01 17:42:36 +08:00
parent 3b8e745ad8
commit 0b5aa00d6b
12 changed files with 943 additions and 204 deletions

53
kmaplib/HashMap_test.c Normal file
View File

@ -0,0 +1,53 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <xiphashmap.h>
static void name_print( int idx, char * key, void * value);
int main()
{
char *name[] = {
"chenmanwen", "yangqinghua", "chenxin", "yanzi", "yangjian", "zhangrenfang",
"panzi", "zhangqiang", "webssky", "jcseg", "friso", "robbe", "lionsoul",
"tankwar", "jteach"
};
void * hashmap = XipHashmapNew();
/*
void * hashmap = XipHashmapInit(0, 0.00, 1);
*/
int i = 0;
int length = 15;
char tmpname[51];
for ( i = 0; i < length ; i++)
{
strcpy( tmpname, name[i]);
printf("put(%15s, %15s);\n", tmpname, tmpname);
XipHashmapPut( hashmap, tmpname, tmpname, strlen(tmpname)+1);
}
XipHashmapPrint(hashmap, NULL);
XipHashmapPut( hashmap, "panzi", "iceskyiceskyicesky", strlen("iceskyiceskyicesky")+1);
XipHashmapPrint(hashmap, name_print);
for ( i = 0; i< length; i++)
printf("get(%15s): %15s\n", name[i], (char *)XipHashmapGet(hashmap, name[i]));
XipHashmapPrint(hashmap, NULL);
printf("remove(%15s): %d\n", "lionsoul", XipHashmapRemove(hashmap, "lionsoul"));
XipHashmapPrint(hashmap, NULL);
XipHashmapDestory(hashmap);
return 0;
}
static void name_print( int idx, char * key, void * value)
{
printf("aaaaaaaaaaaaa-idx[%d], key[%s], value[%s]\n", idx, key, value);
return;
}

51
kmaplib/List_test.c Normal file
View File

@ -0,0 +1,51 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <xiplist.h>
void name_printf( void * element);
int main()
{
char *name[] = {
"chenmanwen", "yangqinghua", "chenxin", "yanzi", "yangjian", "zhangrenfang",
"panzi", "zhangqiang", "webssky", "jcseg", "friso", "robbe", "lionsoul",
"tankwar", "jteach"
};
char tmp_name[51];
void * list = XipListNew();
/*
void * list = XipListInit(1);
*/
int ret = 0;
int i = 0;
int length = 15;
for ( i = 0; i < length ; i++)
{
strcpy( tmp_name, name[i]);
printf("Add(value:[%s]--[%s]);\n", name[i], XipListAdd( list, tmp_name,strlen(tmp_name)+1));
}
printf("Len--[%d],threshold[%d]\n", XipListLen(list), XipListThreshold(list));
XipListPrint(list, name_printf);
XipListPrint(list, NULL);
for ( i = 0; i< length; i++)
printf("get(%d): [%s], [%x]\n", i, (char *)XipListGet(list, i), XipListGet(list, i));
XipListDestory(list);
list = NULL;
XipListPrint(list, name_printf);
return 0;
}
void name_printf( void * element)
{
printf("aaaaaaaaaaa---[%s]\n", element);
}

View File

@ -1,3 +1,4 @@
TARGET = libkmaplib.so
OBJ += HashMap.o
OBJ += XipHashMap.o
OBJ += XipList.o

View File

@ -1,5 +1,5 @@
/**************************************************
* : HashMap.c
* : XipHashMap.c
* : icesky
* : 1.0.0
* : 2016.07.28
@ -13,6 +13,7 @@
* 5.
* 6.
* 7.
* :
*
* :
*************************************************/
@ -20,16 +21,17 @@
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "hashmap.h"
#include "xiphashmap.h"
#define hmap_malloc(size) malloc(size)
#define HMAPFREE(x) free(x);x=NULL;
#define XIP_HASHMAP_REBUILD_ERR (void *)-1
#define MAXIMUM_CAPACITY 1 << 30 /*1 073 741 824 */
#define DEFAULT_CAPACITY 1 << 4 /*16*/
#define DEFAULT_FACTOR 0.75f
#define INTER_MAX_VALUE 1<<31 /*int的最大值*/
#define MALLOC_FLAG_NO 1 /*不分配内存*/
/*hashmap node结构 */
typedef struct st_xip_hashmap_node
@ -51,6 +53,7 @@ typedef struct
unsigned int length; /*桶大小*/
unsigned int size; /*实际大小*/
unsigned int threshold; /*加载临界值*/
int malloc_flag; /*是否为value分配内存*/
float factor; /*加载因子,默认为0.75(0-1)*/
} TxipHashmap;
@ -78,20 +81,20 @@ static void AppLog(char *file, long line, char *level, char *fmtstr, ...)
/*新建T表*/
static TxipHashmapNode ** create_T_table( int opacity);
/*新建hashmap节点*/
static TxipHashmapNode * create_hashmap_node( char * key, void * value, TxipHashmapNode * next);
static TxipHashmapNode * create_hashmap_node( char * key, void * value, TxipHashmapNode * next, int malloc_flag, int size);
/*重建hashmap*/
static int rebuild_hash( TxipHashmap * hashmap);
/*hash算法*/
static unsigned int XipHash(char * key);
/*********************************************************************
* : hashmap
* :
* : int 0
/********************************************************************
* : XipHashmapPrint
* : hashmap,
*
* : icesky
* : 2016.07.28
* : xiphashmap.h
*********************************************************************/
int XipHashmapPrint( void * hashmap)
int XipHashmapPrint( void * hashmap, void (*ref_func)(int,char *, void *))
{
TxipHashmap * map = ( TxipHashmap *)hashmap;
@ -103,13 +106,22 @@ int XipHashmapPrint( void * hashmap)
/** 打印日志 **/
if( map != NULL)
{
HMAPLOG("I", "HashMap:length(%d),size(%d),threshold(%d)", map->length, map->size, map->threshold);
HMAPLOG("I", "HashMap:length(%d),size(%d),threshold(%d),malloc_flag(%d)",
map->length, map->size, map->threshold, map->malloc_flag);
for ( idx = 0; idx < map->length; idx++)
{
for( e = map->table[idx]; e != NULL; )
{
next = e->next;
HMAPLOG("I", "Node[%d]:hash[%d],key[%s],value[%s]", idx, e->hash, e->key, e->value.ptr);
if( ref_func == NULL)
{
HMAPLOG("I", "Node[%d]:hash[%d],key[%s],value[%x][%s]",
idx, e->hash, e->key, e->value.ptr, (char *)e->value.ptr);
}
else
{
ref_func(idx, e->key, e->value.ptr);
}
e = next;
}
}
@ -135,29 +147,34 @@ int XipHashmapPrint( void * hashmap)
return 0;
}
/*********************************************************************
* :
* :
* : hashmap,void *,便
* : hashmap,16,0.75f
*
/********************************************************************
* : XipHashmapNew
* : hashmap
* : icesky
* : 2016.07.28
* : xiphashmap.h
*********************************************************************/
void * XipHashmapNew()
{
return XipHashmapInit( 0, 0.00);
return XipHashmapInit( 0, 0.00, 0); /*全默认选项*/
}
/*********************************************************************
* : int opacity , float factor
* :
* : hashmap,void *,便
* : hashmap,
* opacity0,16,factor0,0.75f
*
/********************************************************************
* : XipHashmapInit
* : valuehashmap
* : icesky
* : 2016.07.28
* : xiphashmap.h
*********************************************************************/
void * XipHashmapInit( int opacity, float factor)
void * XipHashmapInit( int opacity, float factor, int malloc_flag)
{
TxipHashmap * map = ( TxipHashmap *)hmap_malloc(sizeof( TxipHashmap));
if( map == NULL)
{
HMAPLOG("E","初始化hashmap内存失败!!!");
return NULL;
}
memset( map, 0x00, sizeof(TxipHashmap));
if ( opacity < DEFAULT_CAPACITY )
opacity = DEFAULT_CAPACITY;
@ -194,21 +211,22 @@ void * XipHashmapInit( int opacity, float factor)
map->size = 0;
map->factor = factor;
map->threshold = (unsigned int)( factor * opacity);
map->malloc_flag = malloc_flag;
return (void *)map;
}
/*********************************************************************
* : hashmap
* :
* : int , 0
* : hashmap,
* opacity0,16,factor0,0.75f
*
/********************************************************************
* : XipHashmapDestory
* : hashmap
* : icesky
* : 2016.07.28
* : xiphashmap.h
*********************************************************************/
int XipHashmapDestory( void * hashmap)
{
TxipHashmap * map = (TxipHashmap *) hashmap;
TxipHashmapNode * e = NULL;
TxipHashmapNode * next = NULL;
register unsigned int idx;
@ -216,39 +234,72 @@ int XipHashmapDestory( void * hashmap)
/** 释放hashmap node 内存 **/
if( map != NULL)
{
/** 释放hashmap的node空间 **/
for ( idx = 0; idx < map->length; idx++)
{
for( e = map->table[idx]; e != NULL; )
{
next = e->next;
HMAPFREE(e);
/* 释放node的value空间*/
if( map->malloc_flag == MALLOC_FLAG_NO)
{
HMAPFREE(e);
}
else
{
HMAPFREE(e->value.ptr);
e->value.ptr = NULL;
}
e = next;
}
}
/** 释放hashmap的T表 **/
HMAPFREE( map->table);
map->table = NULL;
/** 释放hashmap本身的空间 **/
HMAPFREE( map);
map = NULL;
}
return 0;
}
/*********************************************************************
* : hashmap, char * key, void * value
* :
* : void * oldvalue ,NULL
* putXIP_HASHMAP_PUT_ERR
* : keyvaluehashmap,mapkey
* value,value
* put,hashmap
/********************************************************************
* : XipHashmapDestory
* : keyvaluehashmapkeySet
* : icesky
* : 2016.07.28
* : xiphashmap.h
*********************************************************************/
void * XipHashmapPut( void * hashmap, char * key, void * value)
void * XipHashmapPut( void * hashmap, char * key, void * value, int size)
{
TxipHashmap * map = (TxipHashmap *)hashmap;
if( map == NULL)
{
HMAPLOG("E","hashmap指针为空,请检查是否已经调用XipHashmapNew/XipHashmapInit,或调用返回异常");
return NULL;
}
if ( key == NULL || strlen(key) == 0)
{
HMAPLOG("E","Key值必须为字符串,且Key值不能为空或者NULL!!!");
return NULL;
}
if ( value == NULL)
{
HMAPLOG("E","value不可以为空(NULL)");
return NULL;
}
if ( size <= 0)
{
HMAPLOG("E","值的大小size不可以为0");
return NULL;
}
void * oldvalue = NULL;
TxipHashmapNode *e = NULL;
unsigned int hcode = XipHash(key);
@ -261,17 +312,34 @@ void * XipHashmapPut( void * hashmap, char * key, void * value)
if ( hcode == e->hash && (key == e->key || strcmp( key, e->key) == 0))
{
oldvalue = e->value.ptr;
e->value.ptr = value;
if( map->malloc_flag == 1) /*不分配内存*/
{
e->value.ptr = value;
}
else
{
HMAPFREE(e->value.ptr);
e->value.ptr = NULL;
e->value.ptr = hmap_malloc(size);
if( e->value.ptr == NULL)
{
HMAPLOG("E","为value分配内存时异常!!!");
return NULL;
}
memset( e->value.ptr, 0x00, size);
memcpy( e->value.ptr, value, size);
return e->value.ptr;
}
return oldvalue; /*将原value返回给调用者*/
}
}
/*新建node*/
map->table[idx] = create_hashmap_node( key, value, map->table[idx]);
map->table[idx] = create_hashmap_node( key, value, map->table[idx], map->malloc_flag, size);
if( map->table[idx] == NULL)
{
HMAPLOG("E","新建hashnode节点异常!!!");
return XIP_HASHMAP_PUT_ERR; /*返回固定值*/
HMAPLOG("E","新建hashnode节点异常malloc_flag[%d],size[%d]!!!", map->malloc_flag, size);
return NULL; /*返回固定值*/
}
/*修改hashmap*/
@ -285,22 +353,35 @@ void * XipHashmapPut( void * hashmap, char * key, void * value)
if( rebuild_hash(map) != 0)
{
HMAPLOG("E","重建hash表错误!!!");
return XIP_HASHMAP_PUT_ERR; /*返回固定值*/
return NULL; /*返回固定值*/
}
}
return NULL;
return map->table[idx]->value.ptr; /*返回内存地址*/
}
/*********************************************************************
* : hashmap, char * key
* :
* : void * value
* : keyhashmapvalue
/********************************************************************
* : XipHashmapGet
* : Keyvalue
* : icesky
* : 2016.07.28
* : xiphashmap.h
*********************************************************************/
void * XipHashmapGet( void * hashmap, char *key)
{
TxipHashmap * map = (TxipHashmap *) hashmap;
if( map == NULL)
{
HMAPLOG("E","hashmap指针为空,请检查是否已经调用XipHashmapNew/XipHashmapInit,或调用返回异常");
return NULL;
}
if ( key == NULL || strlen(key) == 0)
{
HMAPLOG("E","Key值必须为字符串,且Key值不能为空或者NULL!!!");
return NULL;
}
unsigned int hcode = XipHash(key);
unsigned int idx = hcode % map->length;
@ -317,15 +398,27 @@ void * XipHashmapGet( void * hashmap, char *key)
return NULL;
}
/*********************************************************************
* : hashmap, char * key
* :
* : int XIP_HASHMAP_EXIST_TURE(1), XIP_HASHMAP_EXIST_FALSE(0)
* : keyhashmap,,
/********************************************************************
* : XipHashmapExists
* : Keyhashmap
* : icesky
* : 2016.07.28
* : xiphashmap.h
*********************************************************************/
int XipHashmapExists( void * hashmap, char *key)
{
TxipHashmap * map = (TxipHashmap *) hashmap;
if( map == NULL)
{
HMAPLOG("E","hashmap指针为空,请检查是否已经调用XipHashmapNew/XipHashmapInit,或调用返回异常");
return -5;
}
if ( key == NULL || strlen(key) == 0)
{
HMAPLOG("E","Key值必须为字符串,且Key值不能为空或者NULL!!!");
return -10;
}
unsigned int hcode = XipHash(key);
unsigned int idx = hcode % map->length;
@ -343,20 +436,30 @@ int XipHashmapExists( void * hashmap, char *key)
return 0;
}
/*********************************************************************
* : hashmap, char * key
* :
* : void * value
* : keyhashmapkeynode,
* value,NULL
/********************************************************************
* : XipHashmapDestory
* : mapKeyVALUE
* : icesky
* : 2016.07.28
* : xiphashmap.h
*********************************************************************/
void * XipHashmapRemove( void * hashmap, char *key)
int XipHashmapRemove( void * hashmap, char *key)
{
TxipHashmap * map = (TxipHashmap *) hashmap;
if( map == NULL)
{
HMAPLOG("E","hashmap指针为空,请检查是否已经调用XipHashmapNew/XipHashmapInit,或调用返回异常");
return -5;
}
if ( key == NULL || strlen(key) == 0)
{
HMAPLOG("E","Key值必须为字符串,且Key值不能为空或者NULL!!!");
return -10;
}
unsigned int hcode = XipHash(key);
unsigned int idx = hcode % map->length;
void * oldvalue = NULL;
TxipHashmapNode * e = NULL;
TxipHashmapNode * prev = NULL;
@ -370,17 +473,32 @@ void * XipHashmapRemove( void * hashmap, char *key)
else
prev->next = e->next;
oldvalue = e->value.ptr;
if( map->malloc_flag == MALLOC_FLAG_NO) /*不分配内存*/
{
;
}
else
{
HMAPFREE(e->value.ptr);
e->value.ptr = NULL;
}
HMAPFREE(e);
map->size--;
return oldvalue;
return 0;
}
}
return NULL;
return 0;
}
/********************************************************************
* : create_T_table
* : nodeT
* : icesky
* : 2016.07.28
* : .TxipHashmapNode
*********************************************************************/
static TxipHashmapNode ** create_T_table( int opacity)
{
register unsigned int i=0;
@ -390,6 +508,7 @@ static TxipHashmapNode ** create_T_table( int opacity)
{
return NULL;
}
memset( table, 0x00, sizeof(TxipHashmapNode*) * opacity);
/*初始化*/
for ( i = 0; i < opacity; i++)
@ -398,20 +517,60 @@ static TxipHashmapNode ** create_T_table( int opacity)
return table;
}
static TxipHashmapNode * create_hashmap_node( char * key, void * value, TxipHashmapNode * next)
/********************************************************************
* : create_hashmap_node
* : key,valuenode
* : icesky
* : 2016.07.28
* : . malloc_flag1size
value
*********************************************************************/
static TxipHashmapNode * create_hashmap_node( char * key, void * value, TxipHashmapNode * next, int malloc_flag, int size)
{
TxipHashmapNode * node = ( TxipHashmapNode *) hmap_malloc( sizeof(TxipHashmapNode));
if ( node == NULL)
{
return NULL;
}
memset( node, 0x00, sizeof( TxipHashmapNode));
node->key = key;
node->value.ptr = value;
if( malloc_flag == MALLOC_FLAG_NO) /*不分配内存*/
{
node->key = key;
node->value.ptr = value;
}
else
{
/*限定KEY必须是字符串类型*/
node->key = hmap_malloc(strlen(key)+1);
if( node->key == NULL)
{
return NULL;
}
memset(node->key, 0x00, strlen(key)+1);
memcpy( node->key, key, strlen(key)+1);
node->value.ptr = hmap_malloc(size);
if( node->value.ptr == NULL)
{
return NULL;
}
memset(node->value.ptr, 0x00, size);
memcpy( node->value.ptr, value, size);
}
node->next = next;
return node;
}
/********************************************************************
* : rebuild_hash
* : hashmapsizehashmap
* : icesky
* : 2016.07.28
* : .
*********************************************************************/
static int rebuild_hash( TxipHashmap * map)
{
register unsigned int i = 0;
@ -469,10 +628,15 @@ static int rebuild_hash( TxipHashmap * map)
}
/********************************************************
* : XipHash
* : keyhash
* : icesky
* : 2016.07.28
* : .
* hash,使 simple BKDR hash algorithm
* JAVAJDK使
* 使PHP使time33(DJP hash algorithm
* 使One-Way-Hash,hash
* 使One-Way-Hash(hash)
*
* 使,.
* ,,使

331
kmaplib/XipList.c Normal file
View File

@ -0,0 +1,331 @@
/**************************************************
* : List.c
* : icesky
* : 1.0.0
* : 2016.07.29
* :
* javaList,C
* list:
* 1.
* 2.
* 3:.
* 4.
* 5.
* :
*
* :
*************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "xiplist.h"
#define list_malloc(size) malloc(size)
#define LISTFREE(x) free(x);x=NULL;
#define XIP_LIST_DEFAULT_THRESHOLD 1<<4
#define XIP_LIST_MAX_THRESHOLD 1<<30
#define MALLOC_FLAG_NO 1 /*不分配内存*/
/*list 结构*/
typedef struct
{
unsigned int length; /*实际大小*/
unsigned int threshold; /*临界值*/
int malloc_flag; /*不分配内存*/
void ** ele_table; /*element数组*/
} TxipList;
#define LISTLOG(...) AppLog(__FILE__,__LINE__,__VA_ARGS__)
static void AppLog(char *file, long line, char *level, char *fmtstr, ...)
{
va_list ap;
char tmpstr[501];
memset(tmpstr, 0x0, sizeof(tmpstr));
va_start(ap, fmtstr);
vsprintf(tmpstr, fmtstr, ap);
va_end(ap);
printf("[%s][%s][%03ld]", level, file, line);
printf("[%s]\n", tmpstr);
return ;
}
/*** static 声明 ***/
/*重建ele_table表*/
static int rebuild_list( TxipList * lst);
/********************************************************************
* : XipListPrint
* : List
* : icesky
* : 2016.07.29
* : xiplist.h
*********************************************************************/
int XipListPrint( void * list, void (*ref_func)(void *))
{
TxipList * lst = ( TxipList *)list;
unsigned int idx = 0;
/** 打印日志 **/
if( lst != NULL)
{
for ( idx = 0; idx < lst->length; idx++)
{
if( ref_func == NULL)
{
LISTLOG("I", "Length[%d], Element[%d]:[%x][%s]",
lst->length, idx, lst->ele_table[idx], (char * )lst->ele_table[idx]);
}
else
{
ref_func(lst->ele_table[idx]);
}
}
}
return 0;
}
/********************************************************************
* : XipListNew
* : list
* : icesky
* : 2016.07.29
* : xiplist.h
*********************************************************************/
void * XipListNew()
{
return XipListInit(0);
}
/********************************************************************
* : XipListInit
* : valuelist
* : icesky
* : 2016.07.29
* : xiplist.h
*********************************************************************/
void * XipListInit(int malloc_flag)
{
TxipList * lst = ( TxipList *)list_malloc(sizeof( TxipList));
memset( lst, 0x00, sizeof( TxipList));
lst->length = 0;
lst->threshold = XIP_LIST_DEFAULT_THRESHOLD;
lst->ele_table = (void * *)list_malloc(sizeof(void *) * lst->threshold);
lst->malloc_flag = malloc_flag;
memset( lst->ele_table, 0x00, sizeof(void *)* lst->threshold);
return (void *)lst;
}
/********************************************************************
* : XipListDestory
* : list
* : icesky
* : 2016.07.29
* : xiplist.h
*********************************************************************/
int XipListDestory( void * list)
{
TxipList * lst = (TxipList *) list;
register unsigned int idx;
/** 释放list element 内存 **/
if( lst != NULL)
{
if( lst->malloc_flag != MALLOC_FLAG_NO) /*不分配内存*/
{
for ( idx = 0; idx < lst->length; idx++)
{
/* 释放element 空间*/
LISTFREE(lst->ele_table[idx]);
lst->ele_table[idx] = NULL;
}
}
/* 释放指向element table的空间 */
LISTFREE(lst->ele_table);
lst->ele_table = NULL;
/** 释放List空间 **/
LISTFREE( lst);
lst = NULL;
}
return 0;
}
/********************************************************************
* : XipListAdd
* : elementlist
* : icesky
* : 2016.07.29
* : xiplist.h
*********************************************************************/
void * XipListAdd( void * list, void * element, unsigned int size)
{
TxipList * lst = (TxipList *)list;
void * e = NULL;
if( lst == NULL)
{
LISTLOG("E","list内存未初始化,请建调用创建函数!!");
return NULL;
}
e = lst->ele_table[lst->length];
if( e != NULL)
{
LISTLOG("E","要放入的指针不为空,请检查列表状态![%x],长度[%d],临界值[%d]", e, lst->length, lst->threshold);
return NULL;
}
if( element == NULL)
{
LISTLOG("E","要放入的element不能为空!!!");
return NULL;
}
if( lst->malloc_flag == MALLOC_FLAG_NO) /*不分配内存*/
{
lst->ele_table[lst->length] = element;
}
else
{
/*新建element->value*/
e = (void *)list_malloc(size);
if( e == NULL)
{
LISTLOG("E","分配value内存失败!!!");
return NULL;
}
memset( e, 0x00, size);
memcpy( e, element, size);
lst->ele_table[lst->length] = e;
}
lst->length++; /*增加当前长度*/
/*如果触发临界值则重建ele_table*/
if( lst->length >= lst->threshold)
{
if( rebuild_list(lst) != 0)
{
LISTLOG("E","重建list错误!!!");
return NULL;
}
}
return lst->ele_table[lst->length];
}
/********************************************************************
* : XipListGet
* : idx,List
* : icesky
* : 2016.07.28
* : xiplist.h
*********************************************************************/
void * XipListGet( void * list, int idx)
{
TxipList * lst = (TxipList *) list;
if( lst != NULL)
{
if( idx <= lst->length)
{
return lst->ele_table[idx];
}
}
return NULL;
}
/********************************************************************
* : XipListLen
* : list
* : icesky
* : 2016.08.01
* : xiplist.h
*********************************************************************/
int XipListLen( void * list)
{
TxipList * lst = (TxipList *) list;
if( lst != NULL)
{
return lst->length;
}
return 0;
}
/********************************************************************
* : XipListThreshold
* :
* : icesky
* : 2016.07.28
* : xiplist.h
*********************************************************************/
int XipListThreshold( void * list)
{
TxipList * lst = (TxipList *) list;
if( lst != NULL)
{
return lst->threshold;
}
return 0;
}
/********************************************************************
* : rebuild_list
* : listlengthlisttable
* : icesky
* : 2016.07.29
* : .
*********************************************************************/
static int rebuild_list( TxipList * lst)
{
register unsigned int i = 0;
void * * newtable=NULL;
/*如果达到最大了,则不在rebuild*/
if( lst->threshold == XIP_LIST_MAX_THRESHOLD)
{
LISTLOG("E","已达到list最大值[%d]", lst->threshold);
return -5;
}
/*扩容*/
unsigned int threshold = lst->threshold * 2;
if( threshold > XIP_LIST_MAX_THRESHOLD)
threshold = XIP_LIST_MAX_THRESHOLD;
/*创建新的ele_table*/
newtable = (void * *)list_malloc(sizeof(void *) * threshold);
/*赋值和转移list->ele_table*/
for( i = 0; i < lst->length; i++)
{
/*遍历*/
newtable[i] = lst->ele_table[i];
}
/*释放oldtable*/
LISTFREE(lst->ele_table);
lst->ele_table = newtable;
lst->threshold = threshold;
return 0;
}

View File

@ -1,82 +0,0 @@
#ifndef __XIPHASHMAP__
#define __XIPHASHMAP__
#define XIP_HASHMAP_EXIST_TURE 1 /*存在- 真*/
#define XIP_HASHMAP_EXIST_FALSE 0 /*不存在- 假*/
#define XIP_HASHMAP_PUT_ERR (void *)1 /*put异常*/
/*********************************************************************
* : hashmap
* :
* : int 0
* : hashmap,
*
*********************************************************************/
int XipHashmapPrint( void * hashmap);
/*********************************************************************
* :
* :
* : hashmap,void *,便
* : hashmap,16,0.75f
*
*********************************************************************/
void * XipHashmapNew();
/*********************************************************************
* : int opacity , float factor
* :
* : hashmap,void *,便
* : hashmap,
* opacity0,16,factor0,0.75f
*
*********************************************************************/
void * XipHashmapInit( int opacity , float factor);
/*********************************************************************
* : hashmap
* :
* : int , 0
* : hashmap,
* opacity0,16,factor0,0.75f
*
*********************************************************************/
int XipHashmapDestory( void * in_map);
/*********************************************************************
* : hashmap, char * key, void * value
* :
* : void * oldvalue ,NULL
* putXIP_HASHMAP_PUT_ERR
* : keyvaluehashmap,mapkey
* value,value
* put,hashmap
*********************************************************************/
void * XipHashmapPut( void * in_map, char * key, void * value);
/*********************************************************************
* : hashmap, char * key
* :
* : void * value
* : keyhashmapvalue
*********************************************************************/
void * XipHashmapGet( void * TxipHashmap, char * key);
/*********************************************************************
* : hashmap, char * key
* :
* : int XIP_HASHMAP_EXIST_TURE(1), XIP_HASHMAP_EXIST_FALSE(0)
* : keyhashmap,,
*********************************************************************/
int XipHashmapExists( void * TxipHashmap, char * key);
/*********************************************************************
* : hashmap, char * key
* :
* : void * value
* : keyhashmapkeynode,
* value,NULL
*********************************************************************/
void * XipHashmapRemove( void * TxipHashmap, char * key);
#endif

View File

@ -1,7 +1,33 @@
#
# NAME: makefile
# AUTHOR: yangwanchun
# VERSION: 1.0
# DATE: 2007/08/25
#
include $(HOME)/etc/makefile.xip
CMP = $(CC) $(SOFLAGS)
LIBS =
include OBJS
$(TARGET):$(OBJ)
cc -shared -o $@ $<
all : $(TARGET) clean
$(TARGET) : $(OBJ)
$(CMP) $@ $(OBJ) -L$(APLIBPATH) $(LIBS)
#$(MV) $@ $(APLIBPATH)/
.SUFFIXES:.ec
.ec.o :
$(RM) -f $*.c
$(EC) -I$(INCLPATH) $(ECFLAGS) -L$(APLIBPATH) $<
$(RM) -f $*.c
.c.o:
$(CC) -I$(INCLPATH) $(CCFLAGS) -L$(APLIBPATH) $<
clean:
rm -f *.o

7
kmaplib/makefile.osc Normal file
View File

@ -0,0 +1,7 @@
include OBJS
$(TARGET):$(OBJ)
cc -shared -o $@ $<
clean:
rm -f *.o

View File

@ -1 +1,3 @@
cc -o test_hash test_hash.c -I./ ./libkmaplib.so
rm -f HashMap_test List_test
cc -o HashMap_test HashMap_test.c -I./ ./libkmaplib.so
cc -o List_test List_test.c -I./ ./libkmaplib.so

View File

@ -1,41 +0,0 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <hashmap.h>
int main()
{
char *name[] = {
"chenmanwen", "yangqinghua", "chenxin", "yanzi", "yangjian", "zhangrenfang",
"panzi", "zhangqiang", "webssky", "jcseg", "friso", "robbe", "lionsoul",
"tankwar", "jteach"
};
void * hashmap = XipHashmapNew();
int i = 0;
int length = 15;
for ( i = 0; i < length ; i++)
{
printf("put(%15s, %15s);\n", name[i], name[i]);
XipHashmapPut( hashmap, name[i], name[i]);
}
XipHashmapPrint(hashmap);
XipHashmapPut( hashmap, "panzi", "iceskyiceskyicesky");
XipHashmapPrint(hashmap);
for ( i = 0; i< length; i++)
printf("get(%15s): %15s\n", name[i], (char *)XipHashmapGet(hashmap, name[i]));
XipHashmapPrint(hashmap);
printf("remove(%15s): %15s\n", "lionsoul", (char *)XipHashmapRemove(hashmap, "lionsoul"));
XipHashmapPrint(hashmap);
XipHashmapDestory(hashmap);
return 0;
}

124
kmaplib/xiphashmap.h Normal file
View File

@ -0,0 +1,124 @@
#ifndef __XIPHASHMAP__
#define __XIPHASHMAP__
/*********************************************************************
* : hashmap
* :
* :
* : hashmap,void *,void*
NULL
* : hashmapXipHashmapInit
XipHashmapInit0
使hashmap
XipHashmapInit,
*********************************************************************/
void * XipHashmapNew();
/*********************************************************************
* : hashmap使
* : int opacity , 16
* float factor , 0.75f
* int malloc_flag value:
1,0XipHashmapPut
* :
* : hashmap,void *,便
NULL
* : hashmap,
* opacity0,16,factor0,0.75f
* 使XipHashmapNew
*********************************************************************/
void * XipHashmapInit( int opacity , float factor, int malloc_flag);
/*********************************************************************
* : hashmap
* : hashmap
* :
* : 0
* : hashmap.
* : hashmapNULL
* free,fclose
*********************************************************************/
int XipHashmapDestory( void * hashmap);
/*********************************************************************
* : keyvaluehashmap
* : void * hashmap
* char * key
* void * value
* int size malloc_flag1hasmapvalue
* :
* : value
NULL
* : XipHashmapInitmalloc_flag(0)
1: valuevalue
value,size
0:
value使Put
value
*********************************************************************/
void * XipHashmapPut( void * in_map, char * key, void * value, int size);
/*********************************************************************
* : keyvalue
* : void * hashmap
* char * key
* :
* : value
NULL
NULL
* : XipHashmapInitmalloc_flag(0)
*********************************************************************/
void * XipHashmapGet( void * TxipHashmap, char * key);
/*********************************************************************
* : keyhashmap,,
* : void * hashmap
* char * key
* :
* : int
XIP_HASHMAP_EXIST_TURE
XIP_HASHMAP_EXIST_FALSE
<0
* :
*********************************************************************/
/***判断是否存在时的返回值***/
#define XIP_HASHMAP_EXIST_TURE 1 /*存在- 真*/
#define XIP_HASHMAP_EXIST_FALSE 0 /*不存在- 假*/
int XipHashmapExists( void * TxipHashmap, char * key);
/*********************************************************************
* : keynode
* : void * hashmap
* char * key
* :
* : int
0
<0
* :
* :
*********************************************************************/
int XipHashmapRemove( void * TxipHashmap, char * key);
/*********************************************************************
* : hashmap,
* : hashmap
* void (ref_func)(int idx, char * key, void * value)
*
* 使NULL
* :
* : ,0
* : map
[I][XipHashMap.c][136][00000000:1]
[I][XipHashMap.c][136][00000001:0]
[I][XipHashMap.c][136][00000002:1]
[I][XipHashMap.c][136][00000003: 3]
[I][XipHashMap.c][136][00000004:1]
[I][XipHashMap.c][136][00000005:1]
[I][XipHashMap.c][136][00000006: 2]
[I][XipHashMap.c][136][00000007:0]
*********************************************************************/
int XipHashmapPrint( void * hashmap, void (*ref_func)(int,char *,void *));
#endif

103
kmaplib/xiplist.h Normal file
View File

@ -0,0 +1,103 @@
#ifndef __XIPLIST_
#define __XIPLIST_
/*********************************************************************
* : list
* :
* :
* : xiplist,void *,void*
NULL
* : listXipListInit
XipListInit0
使hashmap
XipListInit()
*********************************************************************/
void * XipListNew();
/*********************************************************************
* : list使
* : int malloc_flag value:
1,0XipListPut
* :
* : st,void *,便
NULL
* : hashmap,
* 使XipHashmapNew
*********************************************************************/
void * XipListInit(int malloc_flag);
/*********************************************************************
* : elementlist
* : void * list
* void * element
* unsigned int size malloc_flag1
hasmapvalue
* :
* : value
NULL
* : XipListInitmalloc_flag(0)
1: Listvaluevalue
value,size
0:List
value使Put
value
*********************************************************************/
void * XipListAdd(void * list, void * element, unsigned int size);
/*********************************************************************
* : value
* : void * list
* int idx
* :
* : value
NULL
NULL
* : XipHashmapInitmalloc_flag(0)
*********************************************************************/
void * XipListGet( void * list, int idx);
/*********************************************************************
* : list
* : list
* :
* : 0
* : list.
* : listNULL
* free,fclose
*********************************************************************/
int XipListDestory(void * list);
/*********************************************************************
* : list
* : list,
ref_func(void * value)
NULL
* :
* : ,0
* :
*********************************************************************/
/*
int XipListPrint( void * list, ref_func);
*/
int XipListPrint( void * list, void(*ref_func)(void *));
/*********************************************************************
* : list
* : list
* :
* : ,0
* :
*********************************************************************/
int XipListLen( void * list);
/*********************************************************************
* : list
* : list
* :
* : ,0
* :
*********************************************************************/
int XipListThreshold( void * list);
#endif