본문 바로가기
Computer Science

연락처 DataBase

by OKOK 2019. 1. 18.
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
129
130
131
#ifndef NULL
#define NULL (0)
#endif
 
typedef enum
{
    NAME,
    NUMBER,
    BIRTHDAY,
    EMAIL,
    MEMO,
    FIELD_END
} FIELD;
 
typedef struct
{
    int count;
    char str[20];
} RESULT;
 
const int MAXL = 20;
const int MAXR = 50000;
const int MAXE = 500;
const int KEYV = 10007;
 
inline int hash(const char* s)
{
    int retVal = 0;
    for (int i = 0; s[i] != NULL++i)
    {
        retVal += s[i];
        retVal = (retVal << 3- retVal;
        while (retVal >= KEYV) retVal -= KEYV;
    }
    return retVal;
}
 
struct RECORD
{
    int  hash[FIELD_END];
    char field[FIELD_END][MAXL];
};
 
RECORD rPool[MAXR];
int rCnt;
int hTable[FIELD_END][KEYV][MAXE];
int hCount[FIELD_END][KEYV];
 
inline void STRCPY(const char* src, char* dest)
{
    for (int i = 0; dest[i] = src[i]; ++i);
}
 
inline int STRCMP(const char* a, const char* b)
{
    int i;
    for (i = 0; a[i] != NULL && b[i] != NULL && a[i] == b[i]; ++i);
    return a[i] - b[i];
}
 
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
 
void InitDB()
{
    rCnt = 0;
    for (int i = 0, j; i < FIELD_END; ++i) for (j = 0; j < KEYV; ++j) hCount[i][j] = 0;
}
 
void Add(char* name, char* number, char* birthday, char* email, char* memo)
{
    RECORD* newR = &rPool[rCnt];
    STRCPY(name, newR->field[NAME]);
    STRCPY(number, newR->field[NUMBER]);
    STRCPY(birthday, newR->field[BIRTHDAY]);
    STRCPY(email, newR->field[EMAIL]);
    STRCPY(memo, newR->field[MEMO]);
    for (int i = 0; i < FIELD_END; ++i)
    {
        newR->hash[i] = hash(newR->field[i]);
        hTable[i][newR->hash[i]][hCount[i][newR->hash[i]]++= rCnt;
    }
    ++rCnt;
}
 
int Delete(FIELD field, char* str)
{
    int cnt = 0, h = hash(str), limit = hCount[field][h];
    for (int i = 0, j; i < limit; ++i)
    {
        if (STRCMP(str, rPool[hTable[field][h][i]].field[field]) == 0)
        {
            RECORD& r = rPool[hTable[field][h][i]];
            for (j = 0; j < FIELD_END; ++j) r.field[j][0= NULL;
            ++cnt;
        }
    }
    return cnt;
}
 
int Change(FIELD field, char* str, FIELD changefield, char* changestr)
{
    int cnt = 0, h = hash(str), cH = hash(changestr), limit = hCount[field][h];
    for (int i = 0; i < hCount[field][h]; ++i)
    {
        if (STRCMP(str, rPool[hTable[field][h][i]].field[field]) == 0)
        {
            RECORD& r = rPool[hTable[field][h][i]];
            STRCPY(changestr, r.field[changefield]);
            r.hash[changefield] = cH;
            hTable[changefield][cH][hCount[changefield][cH]++= hTable[field][h][i];
            ++cnt;
        }
    }
    return cnt;
}
 
RESULT Search(FIELD field, char* str, FIELD ret_field)
{
    int h = hash(str);
    RESULT result = { 0, };
    for (int i = 0; i < hCount[field][h]; ++i)
    {
        if (STRCMP(str, rPool[hTable[field][h][i]].field[field]) == 0 && ++result.count == 1)
        {
            STRCPY(rPool[hTable[field][h][i]].field[ret_field], result.str);
        }
    }
    return result;
}
 
cs

 


'Computer Science' 카테고리의 다른 글

Ladder1  (0) 2019.01.21
IoT DataBase  (0) 2019.01.18
해적의 mini DB  (0) 2019.01.18
더블 링크드 리스트  (0) 2019.01.17
5 Graph 사용법  (0) 2019.01.17

댓글