Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Help with unordered set using vertex  (Read 5675 times)

0 Members and 1 Guest are viewing this topic.

MacroAhi

  • Newbie
  • *
  • Posts: 1
    • View Profile
Help with unordered set using vertex
« on: June 01, 2021, 12:55:36 am »
hello,
I'm trying to implement a version of Achtung die Kurve,
i'd like to store every line in std::unordered set
so if there's a collision i could tell a player its lost
every player is a line that add each point to the end.
its not working because i couldn't succeeded doing hash and equal function of vertices.
latest sfml, visual studio 2017

Code: [Select]
#pragma once
#include <SFML/Graphics.hpp>
#include <unordered_set>
#include <functional>

size_t hash(const sf::Vertex& vertex) {
std::size_t seed = 0;

seed ^= std::hash<unsigned int>()((unsigned int)vertex.position.x) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
seed ^= std::hash<unsigned int>()((unsigned int)vertex.position.y) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
return seed;
};

bool equal(sf::Vertex const& lhs, sf::Vertex const& rhs)
{
return lhs.position.x == rhs.position.y && lhs.position.y == rhs.position.y;
};

class VertexSet : public sf::Drawable
{
public:
VertexSet();
private:
using equal_type = bool (*)(const sf::Vertex&, const sf::Vertex&);
using hash_type = std::size_t(*)(const sf::Vertex&);
std::unordered_set<sf::Vertex, hash_type, equal_type> m_vertices = std::unordered_set<sf::Vertex, hash_type, equal_type>(0, hash, equal);
sf::PrimitiveType m_primitiveType;
};
its not working actually,
error represents:
Code: [Select]
Severity Code Description Project File Line Suppression State
Error LNK2005 "bool __cdecl equal(class sf::Vertex const &,class sf::Vertex const &)" (?equal@@YA_NAEBVVertex@sf@@0@Z)
I want to have a set of points that i can draw the things,
fast insertion and find
please advice,
thank you
« Last Edit: June 01, 2021, 01:12:10 am by MacroAhi »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Help with unordered set using vertex
« Reply #1 on: June 01, 2021, 08:06:46 am »
Hi

Don't copy the error panel, it lacks the most important: the error message. Copy the compiler output directly. LNK2005 is the "symbol defined multiple times" linker error: your functions are defined in a header and are not declared inline; therefore, there's one separate definition in each .cpp file your header is included in, and the linker doesn't like to deal with several definitions for a single symbol.

But storing vertices in a std::unordered_set is really a bad idea in my opinion. "fast insertion and find": at what rate? How many items does your set contain? If it's not a million operations per second then it's not worth the troubles. Moreover, there are more dedicated (and more efficient) data structures for spatial search, like quad trees for example.
Laurent Gomila - SFML developer

 

anything