示例#1
0
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;
      }
   }
}
示例#2
0
//------------------------------------------------------------------------------
void BuildHashTable(std::vector<Particle> &p_list, Hash &hash_table)
{
	int num_particles = p_list.size();
	int grid_x;
	int grid_y;
	int grid_z;

	float cell_size = (g_xmax - g_ymin) / GRID_RESOLUTION;

	hash_table.clear();

	for (int i = 0; i < num_particles; i++)
	{
		grid_x = floor(p_list[i].position[0] / cell_size);
		grid_y = floor(p_list[i].position[1] / cell_size);
		grid_z = floor(p_list[i].position[2] / cell_size);

		p_list[i].hash = ComputeHash(grid_x, grid_y, grid_z);

		hash_table.insert(Hash::value_type(p_list[i].hash, i));
	}
}
示例#3
0
文件: 5.cpp 项目: JS00000/acmCode
int main(int argc, char const *argv[])
{
    int n, q, a, b;
    char str[10], str2[10];
    while(~scanf("%d%d", &n, &q))
    {
        memset(head, 0, (n+1)*sizeof(int));
        memset(dist, 0, (n+1)*sizeof(int));
        memset(visit, 0, (n+1)*sizeof(bool));
        memset(qhead, 0, sizeof(qhead));
        memset(qmap, 0, sizeof(qmap));
        surnames.clear();
        qhead_hash.clear();
        qmap_hash.clear();
        for (int i = 0; i < maxn; ++i) id_to_name[i].clear();
        for (int i = 0; i < maxn; ++i) name_to_id[i].clear();

        for (int i = 0; i < n; ++i)
        {
            scanf("%s", str);
            id_to_name[i+1] = str;
            name_to_id[surnames.insert(str)].push_back(i+1);
        }
        for (int i = 1; i <= n-1; ++i)
        {
            scanf("%d%d", &a, &b);
            edge[i*2-1].to = b;
            edge[i*2-1].next = head[a];
            head[a] = i*2-1;
            edge[i*2].to = a;
            edge[i*2].next = head[b];
            head[b] = i*2;
        }
        for (int i = 1; i <= q; ++i)
        {
            scanf("%s%s", str, str2);
            if ((surnames.findHash(str) == -1) || (surnames.findHash(str2) == -1))
            {
                qq[i].a = qq[i].b = "";
                continue;
            }
            qq[i].a = str;
            qq[i].b = str2;
            if (qhead_hash.findHash(str) == -1) qhead[qhead_hash.insert(str)] = 0;
            if (qhead_hash.findHash(str2) == -1) qhead[qhead_hash.insert(str2)] = 0;

            qedge[i*2-1].to = str2;
            qedge[i*2-1].next = qhead[qhead_hash.findHash(str)];
            qhead[qhead_hash.findHash(str)] = i*2-1;
            qedge[i*2].to = str;
            qedge[i*2].next = qhead[qhead_hash.findHash(str2)];
            qhead[qhead_hash.findHash(str2)] = i*2;
            qmap[qmap_hash.insert(make_pair(str, str2))] = -1;
            qmap[qmap_hash.insert(make_pair(str2, str))] = -1;
        }
        LCA(1, 0);
        for (int i = 1; i <= q; ++i)
        {
            if (qq[i].a == "" || qq[i].b == "")
            {
                printf("-1\n");
            }
            else
            {
                printf("%d\n", qmap[qmap_hash.findHash(make_pair(qq[i].a, qq[i].b))] + 1);
            }
        }
    }
    return 0;
}
int main()
{
	Hash<char> toTest (hasher, 10);

	// isEmpty() test
	cout << "\nisEmpty() Test" << endl;
	cout << "isEmpty(): " << toTest.isEmpty() << endl;
	cout << "isFull(): " << toTest.isFull() << endl;
	toTest.showStructure();

	// insert() test
	cout << "\ninsert() Test" << endl;

	for (char c = '0'; c <= 'z'; c++)
	{
		toTest.insert(c);
	}

	toTest.showStructure();

	// isFull() test
	cout << "\nisFull() Test" << endl;
	cout << "isEmpty(): " << toTest.isEmpty() << endl;
	cout << "isFull(): " << toTest.isFull() << endl;

	// remove() and retrieve() test
	cout << "\nremove() and retrieve() Test" << endl;
	char rtest = ' ';
	cout << "remove(3): " << toTest.remove(3) << endl;
	bool rreturn = toTest.retrieve(3, rtest);
	cout << "retrieve(3, rtest): " << rreturn << " | rtest = " << rtest << endl;
	toTest.showStructure();

	// clear() test
	cout << "\nclear() Test" << endl;
	toTest.clear();
	toTest.showStructure();

	// isEmpty() test
	cout << "\nisEmpty() Test" << endl;
	cout << "isEmpty(): " << toTest.isEmpty() << endl;
	cout << "isFull(): " << toTest.isFull() << endl;
	toTest.showStructure();

	// Int test version
	Hash<int> testTwo(hasher_two, 20);
	cout << "\n\nTesting with ints:" << endl;

	// insert() test
	cout << "\ninsert() Test" << endl;

	for (int i = 1; i <= 115; i++)
	{
		testTwo.insert(i);
	}

	testTwo.showStructure();

	// isFull() test
	cout << "\nisFull() Test" << endl;
	cout << "isEmpty(): " << testTwo.isEmpty() << endl;
	cout << "isFull(): " << testTwo.isFull() << endl;

	// remove() and retrieve() test
	cout << "\nremove() and retrieve() Test" << endl;
	cout << "remove(3): " << testTwo.remove(3) << endl;
	int itest;
	bool ireturn = testTwo.retrieve(3, itest);
	cout << "retrieve(3, rtest): " << ireturn << " | rtest = " << itest << endl;
	testTwo.showStructure();

	// clear() test
	cout << "\nclear() Test" << endl;
	testTwo.clear();
	testTwo.showStructure();

	system("pause");
}