forked from haiwan/sledge
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.
71 lines
2.4 KiB
71 lines
2.4 KiB
#include "../include/map.h"
|
|
#include <stdio.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*), false);
|
|
|
|
char *key2 = "employee2";
|
|
map_set(&myMap, key2, strlen(key2), bob, sizeof(Employee*), true);
|
|
|
|
// 尝试从哈希表中检索 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 *key = "employee1";
|
|
strcat(alice->name, key);
|
|
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");
|
|
}
|
|
uint64_t value1 = 20, value2 = 30;
|
|
char *key3 = "test1";
|
|
char *key4 = "test2";
|
|
map_set(&myMap, key3, strlen(key3), &value1, sizeof(uint64_t), true);
|
|
uint32_t *value = (uint32_t *)map_get(&myMap, key3, strlen(key3), &ret_value_len);
|
|
if (value) printf("Retrieved value: %d\n", *value); else printf("Value not found.\n");
|
|
value1 ++;
|
|
value = (uint32_t *)map_get(&myMap, key3, strlen(key3), &ret_value_len);
|
|
if (value) printf("Retrieved value: %d\n", *value); else printf("Value not found.\n");
|
|
map_upsert(&myMap, key3, strlen(key3), &value1, sizeof(uint64_t));
|
|
value = (uint32_t *)map_get(&myMap, key3, strlen(key3), &ret_value_len);
|
|
if (value) printf("Retrieved value: %d\n", *value); else printf("Value not found.\n");
|
|
|
|
|
|
free(alice);
|
|
free(bob);
|
|
return 0;
|
|
}
|