You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
2.5 KiB
78 lines
2.5 KiB
5 months ago
|
#include "../include/map.h"
|
||
|
#include <stdio.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
typedef struct {
|
||
|
int id;
|
||
|
char name[100];
|
||
|
float salary;
|
||
|
} Employee;
|
||
|
|
||
|
|
||
|
int main() {
|
||
|
// 初始化哈希表
|
||
|
struct hashmap myMap;
|
||
|
map_init(&myMap);
|
||
|
|
||
|
// 创建并初始化一些 Employee 结构体
|
||
|
Employee *alice = malloc(sizeof(Employee));
|
||
|
alice->id = 1;
|
||
|
strcpy(alice->name, "Alice");
|
||
|
alice->salary = 50000.0;
|
||
|
|
||
|
Employee *bob = malloc(sizeof(Employee));
|
||
|
bob->id = 2;
|
||
|
strcpy(bob->name, "Bob");
|
||
|
bob->salary = 52000.0;
|
||
|
|
||
|
// 将 Employee 结构体存入哈希表
|
||
|
char *key1 = "employee1";
|
||
|
map_set(&myMap, key1, strlen(key1), alice, sizeof(Employee*));
|
||
|
|
||
|
char *key2 = "employee2";
|
||
|
map_set(&myMap, key2, strlen(key2), bob, sizeof(Employee*));
|
||
|
|
||
|
// 尝试从哈希表中检索 Employee
|
||
|
uint32_t ret_value_len;
|
||
|
Employee *retrieved_employee = (Employee *)map_get(&myMap, key1, strlen(key1), &ret_value_len);
|
||
|
if (retrieved_employee) {
|
||
|
printf("Retrieved Employee: %s, ID: %d, Salary: %.2f\n",
|
||
|
retrieved_employee->name, retrieved_employee->id, retrieved_employee->salary);
|
||
|
} else {
|
||
|
printf("Employee not found.\n");
|
||
|
}
|
||
|
alice->id = 12;
|
||
|
char *key3 = "employee1";
|
||
|
strcat(alice->name, key3);
|
||
|
retrieved_employee = (Employee *)map_get(&myMap, key1, strlen(key1), &ret_value_len);
|
||
|
if (retrieved_employee) {
|
||
|
printf("Retrieved Employee: %s, ID: %d, Salary: %.2f\n",
|
||
|
retrieved_employee->name, retrieved_employee->id, retrieved_employee->salary);
|
||
|
} else {
|
||
|
printf("Employee not found.\n");
|
||
|
}
|
||
|
map_delete(&myMap, key1, strlen(key1));
|
||
|
retrieved_employee = (Employee *)map_get(&myMap, key1, strlen(key1), &ret_value_len);
|
||
|
if (retrieved_employee) {
|
||
|
printf("Retrieved Employee: %s, ID: %d, Salary: %.2f\n",
|
||
|
retrieved_employee->name, retrieved_employee->id, retrieved_employee->salary);
|
||
|
} else {
|
||
|
printf("Employee not found.\n");
|
||
|
}
|
||
|
map_set(&myMap, key1, strlen(key1), alice, sizeof(Employee*));
|
||
|
retrieved_employee = (Employee *)map_get(&myMap, key1, strlen(key1), &ret_value_len);
|
||
|
if (retrieved_employee) {
|
||
|
printf("Retrieved Employee: %s, ID: %d, Salary: %.2f\n",
|
||
|
retrieved_employee->name, retrieved_employee->id, retrieved_employee->salary);
|
||
|
} else {
|
||
|
printf("Employee not found.\n");
|
||
|
}
|
||
|
|
||
|
// 清理
|
||
|
free(alice);
|
||
|
free(bob);
|
||
|
|
||
|
// 也许还需要遍历哈希表并释放所有节点,这里假设只是一个简单的示例
|
||
|
return 0;
|
||
|
}
|