1 Star 0 Fork 0

MCai/okc

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
map.c 3.18 KB
一键复制 编辑 原始数据 按行查看 历史
MCai 提交于 2024-09-10 17:39 . update: RingBufPop param data can be NULL
#include "map.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static unsigned int hash( const char *key ) {
unsigned int hash = 0;
while ( *key ) {
hash += *key++;
}
return hash % TABLE_SIZE;
}
Map *MapNew( void ) {
Map *map = malloc( sizeof( Map ) );
memset( map->table, 0, sizeof( map->table ) );
return map;
}
void MapAdd( Map *map, const char *key, void *value ) {
unsigned int index = hash( key );
MapKv *newPair = malloc( sizeof( MapKv ) );
newPair->key = strdup( key );
newPair->value = value;
newPair->next = NULL;
if ( map->table[ index ] == NULL ) {
map->table[ index ] = newPair;
} else {
MapKv *current = map->table[ index ];
while ( current->next != NULL ) {
current = current->next;
}
current->next = newPair;
}
}
void *MapMap( Map *map, const char *key ) {
unsigned int index = hash( key );
MapKv *current = map->table[ index ];
while ( current != NULL ) {
if ( strcmp( current->key, key ) == 0 ) {
return current->value;
}
current = current->next;
}
return NULL; // Not found
}
void MapDel( Map *map, const char *key ) {
unsigned int index = hash( key );
MapKv *current = map->table[ index ];
MapKv *prev = NULL;
while ( current != NULL && strcmp( current->key, key ) != 0 ) {
prev = current;
current = current->next;
}
if ( current == NULL ) {
return; // Not found
}
if ( prev == NULL ) {
map->table[ index ] = current->next;
} else {
prev->next = current->next;
}
free( current->key );
free( current );
}
void MapFree( Map *map ) {
for ( int i = 0; i < TABLE_SIZE; i++ ) {
MapKv *current = map->table[ i ];
while ( current != NULL ) {
MapKv *toDelete = current;
current = current->next;
free( toDelete->key );
free( toDelete );
}
}
free( map );
}
#if 0
int main() {
int a = 42;
int b = 84;
int c = 126;
Map *map = MapNew();
memset( map->table, 0, sizeof( map->table ) );
MapAdd( map, "key1", &a );
MapAdd( map, "key2", &b );
MapAdd( map, "key3", &c );
int *ret = MapMap( map, "key1" );
printf( "Value for 'key1': %d\n", *ret );
ret = MapMap( map, "key2" );
printf( "Value for 'key2': %d\n", *ret );
ret = MapMap( map, "key3" );
printf( "Value for 'key3': %d\n", *ret );
MapDel( map, "key2" );
ret = MapMap( map, "key2" );
if ( ret != NULL ) {
printf( "Value for 'key2' after deletion: %d\n", *ret );
}
MapFree( map );
return 0;
}
#endif
#if 0
int main() {
FILE *fp1 = fopen( "a.txt", "a+" );
FILE *fp2 = fopen( "b.txt", "a+" );
Map *map =MapNew();
memset( map->table, 0, sizeof( map->table ) );
MapAdd( map, "FileA", fp1 );
MapAdd( map, "FileB", fp2 );
FILE *ret = MapMap( map, "FileA" );
fprintf( ret, "%d\n", 111 );
fclose( ret );
ret = MapMap( map, "FileB" );
fprintf( ret, "%d\n", 222 );
fclose( ret );
MapFree( map );
return 0;
}
#endif
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/mcai33/okc.git
git@gitee.com:mcai33/okc.git
mcai33
okc
okc
main

搜索帮助