-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.c
128 lines (109 loc) · 2.9 KB
/
map.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// ここから#includeの代わりに展開した部分
void* NULL = 0;
void* malloc();
void free();
int strlen();
void* memcmp();
void memcpy();
typedef struct Map Map;
Map* map_new();
void* map_get (Map* map, const char* key);
void map_put (Map* map, const char* key, void* value); // 同じキーがある場合はうまく動作しない
void map_delete(Map* map, const char* key);
void** map_values(Map* map);
char** map_keys(Map* map);
// ここまで#includeの代わりに展開した部分
// #include <stdlib.h>
// #include <string.h>
// #include "map.h"
typedef struct MapEntry MapEntry;
struct MapEntry {
char* key;
int key_len;
void* data;
MapEntry* next;
};
struct Map {
MapEntry* first;
int size;
};
static void* map_get2(Map* map, const char* key, int key_len);
static void map_put2(Map* map, const char* key, int key_len, void* data);
static void map_delete2(Map* map, const char* key, int key_len);
Map* map_new() {
Map* map = malloc(sizeof(Map));
map->first = NULL;
map->size = 0;
return map;
}
void* map_get(Map* map, const char* key) {
return map_get2(map, key, strlen(key));
}
static void* map_get2(Map* map, const char* key, int key_len) {
MapEntry* entry = map->first;
while (entry != NULL) {
if (entry->key_len == key_len && memcmp(entry->key, key, key_len) == 0) {
return entry->data;
}
entry = entry->next;
}
return NULL;
}
void map_put(Map* map, const char* key, void* data) {
map_put2(map, key, strlen(key), data);
}
static void map_put2(Map* map, const char* key, int key_len, void* data) {
if(map_get2(map, key, key_len) != NULL) {
map_delete2(map, key, key_len);
}
MapEntry* entry = malloc(sizeof(MapEntry));
entry->key = malloc(key_len);
memcpy(entry->key, key, key_len);
entry->key_len = key_len;
entry->data = data;
entry->next = map->first;
map->first = entry;
map->size++;
}
void map_delete(Map* map, const char* key) {
map_delete2(map, key, strlen(key));
}
static void map_delete2(Map* map, const char* key, int key_len) {
MapEntry* entry = map->first;
MapEntry* prev = NULL;
while (entry != NULL) {
if (entry->key_len == key_len && memcmp(entry->key, key, key_len) == 0) {
if (prev == NULL) {
map->first = entry->next;
} else {
prev->next = entry->next;
}
free(entry->key);
free(entry);
map->size--;
return;
}
prev = entry;
entry = entry->next;
}
}
void** map_values(Map* map) {
void** all = malloc(sizeof(void*) * (map->size + 1));
MapEntry* entry = map->first;
for (int i = 0; i < map->size; i++) {
all[i] = entry->data;
entry = entry->next;
}
all[map->size] = NULL;
return all;
}
char** map_keys(Map* map) {
char** all = malloc(sizeof(char*) * (map->size + 1));
MapEntry* entry = map->first;
for (int i = 0; i < map->size; i++) {
all[i] = entry->key;
entry = entry->next;
}
all[map->size] = NULL;
return all;
}