void MangoExact::calculateHash (Molecule &mol, Hash &hash) { hash.clear(); QS_DEF(Molecule, mol_without_h); QS_DEF(Array<int>, vertices); int i; vertices.clear(); for (i = mol.vertexBegin(); i != mol.vertexEnd(); i = mol.vertexNext(i)) if (mol.getAtomNumber(i) != ELEM_H) vertices.push(i); mol_without_h.makeSubmolecule(mol, vertices, 0); // Decompose into connected components int n_comp = mol_without_h.countComponents(); QS_DEF(Molecule, component); QS_DEF(Array<int>, vertex_codes); for (int i = 0; i < n_comp; i++) { Filter filter(mol_without_h.getDecomposition().ptr(), Filter::EQ, i); component.makeSubmolecule(mol_without_h, filter, 0, 0); SubgraphHash hh(component); vertex_codes.clear_resize(component.vertexEnd()); for (int v = component.vertexBegin(); v != component.vertexEnd(); v = component.vertexNext(v)) vertex_codes[v] = component.atomCode(v); hh.vertex_codes = &vertex_codes; hh.max_iterations = (component.edgeCount() + 1) / 2; dword component_hash = hh.getHash(); // Find component hash in all hashes bool found = false; for (int j = 0; j < hash.size(); j++) if (hash[j].hash == component_hash) { hash[j].count++; found = true; break; } if (!found) { HashElement &hash_element = hash.push(); hash_element.count = 1; hash_element.hash = component_hash; } } }
bool lessThanXored(Hash const& l, Hash const& r, Hash const& x) { Hash v1, v2; for (size_t i = 0; i < l.size(); i++) { v1[i] = x[i] ^ l[i]; v2[i] = x[i] ^ r[i]; } return v1 < v2; }
int da_build_dump(da_build_t* builder, char* tmpfile, FILE* lexfile) { Hash::iterator i, last; Hash* entries = builder->entries; char** keys = new char*[entries->size()]; size_t* lens = new size_t[entries->size()]; long* vals = new long[entries->size()]; int size = 0; std::vector<long> lex_indices; std::cerr << entries->size() << " entries" << std::endl; i = entries->begin(); while (i != entries->end()) { const std::string& key = i->first; last = entries->upper_bound(key); lex_indices.clear(); for (; i != last; i++) { lex_indices.push_back(i->second); } lens[size] = key.size(); keys[size] = (char*) key.data(); vals[size] = redump_lex(lens[size], lex_indices, tmpfile, lexfile); if (vals[size] < 0) { std::cerr << "Unexpected error at " << key << std::endl; cha_exit_perror((char*)"build darts file"); } size++; } std::cerr << size << " keys" << std::endl; DoubleArrayL da; da.build(size, (const char**) keys, lens, vals); da.save(builder->path->c_str(), "wb"); return builder->entries->size(); }
void Search::searchHash(Hash h, int size, vector<long int> v) { int count = 0; int aux; int meio, ultimo, primeiro; No *hAux; for (int i = 0; i < size; i++) { aux = h.cHash(v.at(i)); hAux = h.Get_IndexHash(aux); if (h.size(hAux) > 0) { /*while (primeiro <= ultimo) { if (hAux->at(meio) < v.at(i)) { primeiro = meio + 1; } else if (hAux->at(meio) == v.at(i)) { count += 1; break; } else { ultimo = meio - 1; } meio = (primeiro + ultimo) / 2; }*/ while (hAux != NULL) { if (hAux->e == v.at(i)) { count += 1; break; } else { hAux = hAux->prox; } } } } cout << "Existem " << count << " chaves do vetor na Hash." << endl; }
Res WriteFileHash(const string& out_dir, const string& file_path, const Hash& hash) { string hash_fname = PathJoin(out_dir, LocalFileStemFromPath(file_path)); hash_fname += ".hash"; FILE* f = fopen(hash_fname.c_str(), "wb"); if (!f) { return Res(ERR_FILE_ERROR, "WriteFileHash can't open file " + hash_fname); } else { size_t wrote = fwrite(hash.data(), hash.size(), 1, f); if (wrote != 1) { return Res(ERR_FILE_ERROR, "WriteFileHash couldn't write to file " + hash_fname); } int closed_code = fclose(f); if (closed_code != 0) { return Res(ERR_FILE_ERROR, "WriteFileHash couldn't close " + hash_fname); } } return Res(OK); }
int main() { Value a; assert(!a.isInteger()); assert(!a.isString()); assert(!a.isList()); assert(!a.isHash()); assert(a.type() == Value::UNDEFINED); assert(!a); a = Value(11); assert(a.isInteger()); assert(!a.isString()); assert(!a.isList()); assert(!a.isHash()); assert(a); assert(a.asInteger() == 11); a = Value("123"); assert(!a.isInteger()); assert(a.isString()); assert(!a.isList()); assert(!a.isHash()); assert(a); assert(a.asString() == "123"); List b; b.push_back(Value(11)); b.push_back(Value("111")); a = Value(b); b.push_back(Value(34)); assert(b.size() == 3); assert(a.isList()); assert(a.asList().size() == 2); Hash m; m["aa"] = Value(12); m["bb"] = Value(11); a = Value(m); m.erase("bb"); assert(m.size() == 1); assert(m["aa"].asInteger() == 12); assert(m.find("bb") == m.end()); assert(a.isHash()); assert(a.asHash().size() == 2); a = Value(Vector(1,-1,2)); assert(a.type() == Value::VECTOR); assert(a.isVector()); assert(a.asVector().y == -1); assert(a); a = Value(Position(1,-1,2)); assert(a.type() == Value::POSITION); assert(a.isPosition()); assert(a.asPosition().x == 1); assert(a); std::cout << "Success!" << std::endl; return 0; }
void tst_QHash::insert1() { const char *hello = "hello"; const char *world = "world"; const char *allo = "allo"; const char *monde = "monde"; { typedef QHash<QString, QString> Hash; Hash hash; QString key; for (int i = 0; i < 10; ++i) { key[0] = i + '0'; for (int j = 0; j < 10; ++j) { key[1] = j + '0'; hash.insert(key, "V" + key); } } for (int i = 0; i < 10; ++i) { key[0] = i + '0'; for (int j = 0; j < 10; ++j) { key[1] = j + '0'; hash.remove(key); } } } { typedef QHash<int, const char *> Hash; Hash hash; hash.insert(1, hello); hash.insert(2, world); QVERIFY(hash.size() == 2); QVERIFY(!hash.isEmpty()); { Hash hash2 = hash; hash2 = hash; hash = hash2; hash2 = hash2; hash = hash; hash2.clear(); hash2 = hash2; QVERIFY(hash2.size() == 0); QVERIFY(hash2.isEmpty()); } QVERIFY(hash.size() == 2); { Hash hash2 = hash; hash2[1] = allo; hash2[2] = monde; QVERIFY(hash2[1] == allo); QVERIFY(hash2[2] == monde); QVERIFY(hash[1] == hello); QVERIFY(hash[2] == world); hash2[1] = hash[1]; hash2[2] = hash[2]; QVERIFY(hash2[1] == hello); QVERIFY(hash2[2] == world); hash[1] = hash[1]; QVERIFY(hash[1] == hello); } { Hash hash2 = hash; hash2.detach(); hash2.remove(1); QVERIFY(hash2.size() == 1); hash2.remove(1); QVERIFY(hash2.size() == 1); hash2.remove(0); QVERIFY(hash2.size() == 1); hash2.remove(2); QVERIFY(hash2.size() == 0); QVERIFY(hash.size() == 2); } hash.detach(); { Hash::iterator it1 = hash.find(1); QVERIFY(it1 != hash.end()); Hash::iterator it2 = hash.find(0); QVERIFY(it2 != hash.begin()); QVERIFY(it2 == hash.end()); *it1 = monde; QVERIFY(*it1 == monde); QVERIFY(hash[1] == monde); *it1 = hello; QVERIFY(*it1 == hello); QVERIFY(hash[1] == hello); hash[1] = monde; QVERIFY(it1.key() == 1); QVERIFY(it1.value() == monde); QVERIFY(*it1 == monde); QVERIFY(hash[1] == monde); hash[1] = hello; QVERIFY(*it1 == hello); QVERIFY(hash[1] == hello); } { const Hash hash2 = hash; Hash::const_iterator it1 = hash2.find(1); QVERIFY(it1 != hash2.end()); QVERIFY(it1.key() == 1); QVERIFY(it1.value() == hello); QVERIFY(*it1 == hello); Hash::const_iterator it2 = hash2.find(2); QVERIFY(it1 != it2); QVERIFY(it1 != hash2.end()); QVERIFY(it2 != hash2.end()); int count = 0; it1 = hash2.begin(); while (it1 != hash2.end()) { count++; ++it1; } QVERIFY(count == 2); count = 0; it1 = hash.begin(); while (it1 != hash.end()) { count++; ++it1; } QVERIFY(count == 2); } { QVERIFY(hash.contains(1)); QVERIFY(hash.contains(2)); QVERIFY(!hash.contains(0)); QVERIFY(!hash.contains(3)); } { QVERIFY(hash.value(1) == hello); QVERIFY(hash.value(2) == world); QVERIFY(hash.value(3) == 0); QVERIFY(hash.value(1, allo) == hello); QVERIFY(hash.value(2, allo) == world); QVERIFY(hash.value(3, allo) == allo); QVERIFY(hash.value(0, monde) == monde); } { QHash<int,Foo> hash; for (int i = 0; i < 10; i++) hash.insert(i, Foo()); QVERIFY(Foo::count == 10); hash.remove(7); QVERIFY(Foo::count == 9); } QVERIFY(Foo::count == 0); { QHash<int, int*> hash; QVERIFY(((const QHash<int,int*>*) &hash)->operator[](7) == 0); } } }