int main() { ifstream cin("hashuri.in"); ofstream cout("hashuri.out"); HashSet<int> H; int N; cin >> N; for (int i = 0; i < N; ++i) { int type, value; cin >> type>> value; if (type == 1) H.insert(value); if (type == 2) H.erase(value); if (type == 3) cout << bool(*H.find(value) != 0) << "\n"; } }
TEST_EQUAL((hs.find(2) == hs.end()), true) TEST_EQUAL((hs.find(-2) == hs.end()), true) h_mutable.insert(1); TEST_EQUAL((hs.find(0) == hs.end()), false) TEST_EQUAL((hs.find(1) == hs.end()), false) TEST_EQUAL((hs.find(2) == hs.end()), true) TEST_EQUAL((hs.find(-2) == hs.end()), true) RESULT CHECK(Size erase(const KeyType& key) throw()) HashSet<int> hs; hs.insert(0); hs.insert(1); hs.insert(2); hs.insert(3); Size res = hs.erase(0); TEST_EQUAL(res, 1) TEST_EQUAL(hs.has(0), false) TEST_EQUAL(hs.has(1), true) TEST_EQUAL(hs.has(2), true) TEST_EQUAL(hs.has(3), true) TEST_EQUAL(hs.getSize(), 3) res = hs.erase(0); TEST_EQUAL(res, 0) TEST_EQUAL(hs.has(0), false) TEST_EQUAL(hs.has(1), true) TEST_EQUAL(hs.has(2), true) TEST_EQUAL(hs.has(3), true) TEST_EQUAL(hs.getSize(), 3) res = hs.erase(4); TEST_EQUAL(res, 0)
void syntaxTest_HashSet() { { HashSet<int> s; s.insert(1); s.insert(2); s.insert(1); s.insert(3); s.insert(2); assert(s.size() == 3); s.erase(3); assert(s.size() == 2); s.erase(3); assert(s.size() == 2); } { int a[5] = {1, 3, 2, 4, 1}; HashSet<int> s(a, a + 5); assert(s.size() == 4); { HashSet<int> s2(s); assert(s2.size() == 4); } { HashSet<int> s2; assert(s2.empty()); s2 = s; assert(s2.size() == 4); assert(!s2.empty()); assert(s2.contain(3)); assert(s2.count(2) == 1); assert(s2 == s); assert(!(s2 != s)); } } { std::vector<int> v(5, 0); HashSet<int> s; s.insert(v.begin(), v.end()); assert(s.size() == 1); assert(s.find(1) == s.end()); assert(s.find(0) != s.end()); assert(s.find(0) == s.begin()); s.erase(s.find(0)); assert(s.empty()); s.insert(5); assert(!s.empty()); s.clear(); assert(s.empty()); HashSet<int> s2; s2.insert(5); std::swap(s, s2); assert(s2.empty()); assert(s.find(5) != s.end()); } { int a[4] = {1,2, 3, 1}; const HashSet<int> s(a, a + 4); HashSet<int> b; for (HashSet<int>::ConstIterator iter = s.begin(); iter != s.end(); ++iter) { b.insert(*iter); } assert(b.size() == 3); } }
void performanceTest_string() { const int N = 1; std::vector<std::string> textArray; { std::ifstream fi("1.txt"); for (std::string s; getline(fi, s);) textArray.push_back(s); } std::vector<int> rArray; for (int i = 0; i < (1 << 20); ++i) rArray.push_back(rand() % textArray.size()); std::vector<std::string> res; { Timer _t("HashSet"); for (int _ = 0; _ < N; ++_) { HashSet<std::string> s; for (int i = 0; i < textArray.size(); ++i) s.insert(textArray[i]); for (int i = 0; i < rArray.size(); ++i) { s.erase(textArray[rArray[i]]); } for (int i = 0; i < rArray.size(); ++i) { if (rArray[i] & 3) { s.erase(textArray[rArray[i]]); } else { s.insert(textArray[rArray[i]]); } } if (res.empty()) { res.assign(s.begin(), s.end()); } } } std::sort(res.begin(), res.end()); cout << res.size() << endl; std::vector<std::string> res2; { Timer _t("set"); for (int _ = 0; _ < N; ++_) { std::set<std::string> s; for (int i = 0; i < textArray.size(); ++i) s.insert(textArray[i]); for (int i = 0; i < rArray.size(); ++i) { s.erase(textArray[rArray[i]]); } for (int i = 0; i < rArray.size(); ++i) { if (rArray[i] & 3) { s.erase(textArray[rArray[i]]); } else { s.insert(textArray[rArray[i]]); } } if (res2.empty()) { res2.assign(s.begin(), s.end()); } } } std::sort(res2.begin(), res2.end()); cout << (res == res2) << endl; { Timer _t("hash_set"); for (int _ = 0; _ < N; ++_) { stdext::hash_set<std::string> s; for (int i = 0; i < textArray.size(); ++i) s.insert(textArray[i]); for (int i = 0; i < rArray.size(); ++i) { s.erase(textArray[rArray[i]]); } for (int i = 0; i < rArray.size(); ++i) { if (rArray[i] & 3) { s.erase(textArray[rArray[i]]); } else { s.insert(textArray[rArray[i]]); } } if (res2.empty()) { res2.assign(s.begin(), s.end()); } } } std::sort(res2.begin(), res2.end()); cout << (res == res2) << endl; }
void performanceTest_int() { const int N = 1; std::vector<int> textArray; { for (int i = 0; i < (1 << 18); ++i) textArray.push_back(rand() % 4096); } std::vector<int> rArray; for (int i = 0; i < (1 << 20); ++i) rArray.push_back(rand() % textArray.size()); std::vector<int> res; { Timer _t("HashSet"); for (int _ = 0; _ < N; ++_) { HashSet<int> s; for (int i = 0; i < textArray.size(); ++i) s.insert(textArray[i]); for (int i = 0; i < rArray.size(); ++i) { s.erase(textArray[rArray[i]]); } for (int i = 0; i < rArray.size(); ++i) { if (rArray[i] & 3) { s.erase(textArray[rArray[i]]); } else { s.insert(textArray[rArray[i]]); } } if (res.empty()) { res.assign(s.begin(), s.end()); } } } std::sort(res.begin(), res.end()); cout << res.size() << endl; std::vector<int> res2; { Timer _t("set"); for (int _ = 0; _ < N; ++_) { std::set<int> s; for (int i = 0; i < textArray.size(); ++i) s.insert(textArray[i]); for (int i = 0; i < rArray.size(); ++i) { s.erase(textArray[rArray[i]]); } for (int i = 0; i < rArray.size(); ++i) { if (rArray[i] & 3) { s.erase(textArray[rArray[i]]); } else { s.insert(textArray[rArray[i]]); } } if (res2.empty()) { res2.assign(s.begin(), s.end()); } } } std::sort(res2.begin(), res2.end()); cout << (res == res2) << endl; { Timer _t("hash_set"); for (int _ = 0; _ < N; ++_) { stdext::hash_set<int> s; for (int i = 0; i < textArray.size(); ++i) s.insert(textArray[i]); for (int i = 0; i < rArray.size(); ++i) { s.erase(textArray[rArray[i]]); } for (int i = 0; i < rArray.size(); ++i) { if (rArray[i] & 3) { s.erase(textArray[rArray[i]]); } else { s.insert(textArray[rArray[i]]); } } if (res2.empty()) { res2.assign(s.begin(), s.end()); } } } std::sort(res2.begin(), res2.end()); cout << (res == res2) << endl; }