Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.5 kB
3
Indexable
Never
#include <unordered_map>
#include <list>
#include <string>

using namespace std;

typedef enum{
	NAME,
	NUMBER,
	BIRTHDAY,
	EMAIL,
	MEMO,
	DEL
}FIELD;

typedef struct
{
	int count;
    char str[20];
} RESULT;

//struct Person{
//    string name;
//    string number;
//    string birthday;
//    string email;
//    string memo;
//    bool isDel;
//    void in(string name, string number, string birthday, string email, string memo){
//        this->name = name;
//        this->number = number;
//        this->birthday = birthday;
//        this->email = email;
//        this->memo = memo;
//        isDel=false;
//    }
//};

string data[500005][6];
int idx;
unordered_map<string, list<int>> map[5];

void InitDB()
{   
    int idx = 0;
    for(int i=0; i < 5; i++){
        map[i].clear();
    }
}  

void Add(char* name, char* number, char* birthday, char* email, char* memo)
{
	//data[idx].in(name, number, birthday, email, memo);
	data[idx][NAME] = name;
	data[idx][NUMBER] = number;
	data[idx][BIRTHDAY] = birthday;
	data[idx][EMAIL] = email;
	data[idx][MEMO] = memo;
	data[idx][DEL] = "N";

	map[NAME][name].push_back(idx);
	map[NUMBER][number].push_back(idx);
	map[BIRTHDAY][birthday].push_back(idx);
	map[EMAIL][email].push_back(idx);
	map[MEMO][memo].push_back(idx);

	idx++;
}

int Delete(FIELD field, char* str)
{
	int cnt = 0;
	for(auto it = map[field][str].begin(); it != map[field][str].end(); it++){
		if(data[*it][DEL] == "N" && data[*it][field] == str){
			cnt++;
			data[*it][DEL] = "Y";
		}
	}
	return cnt;
}

int Change(FIELD field, char* str, FIELD changefield, char* changestr) 
{
	int cnt = 0;
	for(auto it = map[field][str].begin(); it != map[field][str].end(); it++){
		if(data[*it][DEL] == "N" && data[*it][field] == str){
			data[*it][changefield] = changestr;
			map[changefield][changestr].push_back(*it);
			cnt++;
		}
	}
	return cnt;
}

RESULT Search(FIELD field, char* str, FIELD returnfield)
{
	RESULT res;
	string cpStr = "";
    int cnt = 0;
    for(auto it = map[field][str].begin(); it != map[field][str].end(); it++)
    {
        if(data[*it][DEL] == "N" && data[*it][field] == str)
        {
            cnt++;
            cpStr = data[*it][returnfield];
        }
    }
	res.count = cnt;
	if(cnt == 1)
    {
        for(int i = 0; i < cpStr.length();i++)
        {
            res.str[i] = cpStr[i];
        }
        res.str[cpStr.length()] = '\0';
    }
	return res;
}