Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.9 kB
1
Indexable
Never
package Contact_InfoDB;

import java.util.*;

//import java.io.FileInputStream;

class UserSolution {
	class person {
		String[] val = new String[5];
		boolean isExist;

		person(String name, String number, String birthday, String email,
				String memo) {
			val[0] = name;
			val[1] = number;
			val[2] = birthday;
			val[3] = email;
			val[4] = memo;
			isExist = true;
		}
	}

	class ID {
		int id;
		boolean isExist;

		ID(int id) {
			this.id = id;
			isExist = true;
		}
	}

	person[] ds = new person[50000];
	int count;
	Map<String, List<ID>>[] map = new Map[5];

	void InitDB() {
		count = 0;
		for (int i = 0; i < 5; i++) {
			map[i] = new HashMap<String, List<ID>>(50000);
		}
	}

	void insert(int field, String string, int id) {
//		List<ID>list=new ArrayList<ID>();
//		list = map[field].get(string);
//		//list=new ArrayList<>();
		List<ID>list=map[field].getOrDefault(string, new ArrayList<UserSolution.ID>());
		ID newID=new ID(id);
		list.add(newID);
		map[field].put(string, list);

	}

	void Add(String name, String number, String birthday, String email,
			String memo) {
		insert(0, name, count);
		insert(1, number, count);
		insert(2, birthday, count);
		insert(3, email, count);
		insert(4, memo, count);
		ds[count] = new person(name, number, birthday, email, memo);
		count++;
	}

	int Delete(int field, String str) {
		List<ID> list = map[field].get(str);
		if (list == null || list.isEmpty())
			return 0;
		int dem = 0;
		for (ID r : list) {
			if (!r.isExist || !ds[r.id].isExist)
				continue;
			ds[r.id].isExist = false;
			r.isExist = false;
			dem++;
		}
		return dem;
	}

	int Change(int field, String str, int changeField, String changeStr) {

		List<ID> list = map[field].get(str);
		if (list == null || list.isEmpty())
			return 0;
		int ans = 0;

		for (int i=0;i<list.size();i++) {
			ID r=list.get(i);
			if (!r.isExist || !ds[r.id].isExist)
				continue;
			if (ds[r.id].val[changeField].equals(changeStr))
				continue;

			String prev = ds[r.id].val[changeField];
			List<ID> list1 = map[changeField].get(prev);
			for (int j=0;j<list1.size();j++) {
				ID p=list1.get(j);
				if (!p.isExist)
					continue;
				if (p.id == r.id) {
					p.isExist = false;
					insert(changeField, changeStr, p.id);
				}
			}
			ans++;
			ds[r.id].val[changeField] = changeStr;
		}

		return ans;

	}

	main.Result Search(int field, String str, int returnField) {
		main.Result rs = new main.Result();
		rs.count = 0;
		rs.str =null;
		int ans = 0;
		List<ID> list = map[field].get(str);

		if (list == null || list.isEmpty())
			return rs;
		for (int i=0;i<list.size();i++) {
			ID r=list.get(i);
			if (!r.isExist || !ds[r.id].isExist)
				continue;
			rs.str = ds[r.id].val[returnField];
			ans++;
		}
		rs.count = ans;
		return rs;
	}
}