-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd.cpp
More file actions
52 lines (41 loc) ยท 2.08 KB
/
Copy pathadd.cpp
File metadata and controls
52 lines (41 loc) ยท 2.08 KB
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
#include "database.h"
//add command ๊ด๋ จ ํจ์
//์ํธ๋ฆฌ๋ฅผ ์์ฑํ๋ค.
//ํค๋ํ์ผ์ createํจ์๊ฐ entry ํฌ์ธํฐ ํ์
์ผ๋ก ์ง์ ๋์ด์์ผ๋ฏ๋ก ํฌ์ธํฐ๋ฅผ ๋ฐํํด์ผํจ
Entry *create(Type type, std::string key, void *value) {
//ํจ์๋ฅผ ํฌ์ธํฐ ํ์
์ผ๋ก ์ง์ ํ ์ด์ ๋ ํจ์ ํธ์ถ์ด ์ข
๋ฃ๋์ด๋ ์ํธ๋ฆฌ ๊ฐ์ฒด๊ฐ ์ ์ง๋์ด์ผ ํ๊ธฐ ๋๋ฌธ์
//Entry ํ์
์ ๊ฐ์ ๊ฐ๋ฆฌํค๋ ํฌ์ธํฐ๋ฅผ ์์ฑํจ
Entry *entry = new Entry;
//ํ๋ผ๋ฏธํฐ๋ก ์
๋ ฅ๋ฐ์ type, key, value๊ฐ์ ์ํธ๋ฆฌ์ type, key, value ๊ฐ์ ํ ๋นํจ
entry->type = type;
entry->key = key;
entry->value = value;
return entry;
}
//๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ํธ๋ฆฌ๋ฅผ ์ถ๊ฐํ๋ค.
void add(Database &database, Entry *entry){
//database์ size๋งํผ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ํธ๋ฆฌ๋ฅผ ์ํํจ
for (int i = 0; i < database.size; ++i) {
//ํ์ฌ ์ํธ๋ฆฌ์ ํค๊ฐ๊ณผ ์ถ๊ฐํ๊ณ ์ถ์ ์ํธ๋ฆฌ์ ํค๊ฐ์ด ๊ฐ์ผ๋ฉด
if (database.entries[i].key == entry->key) {
//์๋ก ์ถ๊ฐํ๋ ค๋ ์ํธ๋ฆฌ์ ๊ฐ์ผ๋ก ํ ๋นํด์ค
database.entries[i].value = entry->value;
database.entries[i].type = entry->type;
return;
}
}
//๋ฐ์ดํฐ๋ฒ ์ด์ค์ ํฌ๊ธฐ+1ํ size์ธ Entry๋ฅผ ์์ฑํจ
Entry *entries = new Entry[database.size + 1];
// ์๋ก ์์ฑํ ์ํธ๋ฆฌ์ ๊ธฐ์กด ์ํธ๋ฆฌ์ ๊ฐ์ ํ ๋นํจ
for (int i = 0; i < database.size; ++i) {
entries[i] = database.entries[i];
}
// ์๋ก ์์ฑํ ์ํธ๋ฆฌ์ ๊ฐ์ ๋ค ์ฎ๊ธด๋ค, ๊ธฐ์กด ์ํธ๋ฆฌ ๊ฐ์ ํด์ ํจ
delete[] database.entries;
// ์๋ก ์์ฑํ ์ํธ๋ฆฌ์ ๋ง์ง๋ง ์ธ๋ฑ์ค์ ํ๋ผ๋ฏธํฐ๋ก ์
๋ ฅ๋ฐ์ entry๊ฐ์ ํ ๋นํจ
entries[database.size] = *entry;
// ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ํธ๋ฆฌ ๊ฐ์ ์๋ก ์์ฑํ ์ํธ๋ฆฌ ๊ฐ์ผ๋ก ํ ๋นํจ
database.entries = entries;
// ์ํธ๋ฆฌ๊ฐ ์ถ๊ฐ ๋์์ผ๋ฏ๋ก ์ฌ์ด์ฆ๋ฅผ ์ฆ๊ฐ์ํด
database.size++;
}