dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 23

Thread: How to design unordered_map's hash function for this case and another one?

Threaded View

  1. #1
    Join Date
    Dec 2010
    Posts
    907

    How to design unordered_map's hash function for this case and another one?

    1)
    Code:
    class AStarNodePair {
    public:	 
    	int from;
    	int to;
    
    public:	 
    	AStarNodePair(int f, int t);
    
    	void init(int f, int t) { from = f; to = t; }	
    };
    
    std::unordered_map<AStarNodePair, float, ???> m_actualCosts;
    2)
    Code:
    class GridNode : public AStarNode
    {
    public: 
    	GridNode(int id, const quad::vec2* bb, const std::vector<int>& neis);
      
    	bool operator==(const GridNode& other) {
    		if (m_time_independent_id == other.m_time_independent_id)
     		{
      			return true;
    		 }
     		return false;
    	}	 
    
    	std::vector<int>& neigbours() {
    		return neis;
    	 }
     
    	bool Intersect(const D3DXVECTOR3 &point) const; 
    	float distance(const GridNode* other) const;	 
    	D3DXVECTOR3 GetPos() { return m_vPos; }
    
    protected:
     	void SetPos() { m_vPos = center();  }
    	D3DXVECTOR3 center(); 
    
    private:
    	quad::vec2	m_boundingBox[2];
    	Vector2D           m_pivot;	 
    	CComPtr<ID3DXMesh> m_pMesh;
    	 
    };
    In this case, I want to write a hash function to find a particular element of GridNode in constant time
    Currently, I am storing these GridNodes in a std::vector, and when I find the element,
    I iterate thru the container like this
    Code:
    bool Coordinater::Convert_D3D_To_ID(D3DXVECTOR3 vec3, int& id) 
    {
     	for (auto& g : NavmeshSingleton::GetNavMesher()->GetWalkables())
     	{
    
     		if (g->Intersect(vec3))
     		{
     			id = g->m_time_independent_id;
    			return true;
    		}
    	}
    	return false;
    }
    Now I want to replace this loop with a hash function.
    Rather than begging for code, I want to know what is the method to generate a good hash function,
    Just want to learn how to do it.

    Update1:
    is this a good thing/ to know at least?
    https://www.gnu.org/software/gperf/manual/gperf.html

    Just wondering whether VS 2013 has this sort of goodie?



    Thank you
    Jack
    Last edited by lucky6969b; September 29th, 2015 at 01:14 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)