CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2015
    Posts
    500

    Removing the duplicates in the multiset (bit urgent)

    Hello,

    I have the following map. I changed the set to multiset, as I need to store, the cell, even if the distance is the same.


    Code:
    struct TACNeighbourData
    {
        int m_CellKey;
    	double m_Distance;
    	int m_HOCount;
    	std::string m_CellID;
    
    	TACNeighbourData()
    	{
    		m_Instancecounter++;
    	}
    
    	TACNeighbourData(const TACNeighbourData& rother):
    	m_CellKey(rother.m_CellKey),
    	m_Distance(rother.m_Distance),
    	m_HOCount(rother.m_HOCount),
    	m_CellID(rother.m_CellID)
    	{
    		m_Instancecounter++;
    	}
    
    	~TACNeighbourData()
    	{
    		m_Instancecounter--;
    	}
    
    	bool operator<(const TACNeighbourData& rother) const
    	{
    		if (bIsHO)
    			return m_HOCount < rother.m_HOCount;
    		else
    			return m_Distance < rother.m_Distance;
    	}
    
    	static int m_Instancecounter;
    };
    
    
    	std::map<int, std::multiset<TACNeighbourData> >	m_SiteKeyToNeighbourKeys;

    Now the map is having values like this say:

    {siteid=1 , { cellid= 100, distance=0, hocnt=0},
    { cellid=100, distance=0, hocnt=0}
    {cellid=200, distance=0, hocnt=0}
    {cellid=300, distance=0, hocnt=0}
    }

    i want to see something like this::


    {siteid=1 , { cellid= 100, distance=0, hocnt=0},
    {cellid=200, distance=0, hocnt=0}
    {cellid=300, distance=0, hocnt=0}
    }

    I had not tested everything before , so finding this issue at last moment. So im trying to fix at last monent, so any help is much appreciated.

    Basically, i am populating in the ascending order of distance (but i also want to populate if the distance are same), so i used the multiset. Hope this is ok. As the set doesnot populate same values, it overwrites it.

    thanks a lot
    pdk
    Last edited by pdk5; January 7th, 2021 at 07:41 AM.

  2. #2
    Join Date
    May 2015
    Posts
    500

    Re: Removing the duplicates in the multiset (bit urgent)

    I think i found a way, I just tried to check before the insertion, if that element is already present,

    Code:
    struct find_by_cellkey {
        find_by_cellkey(const int & cellkey) : cellkey(cellkey) {}
        bool operator()(const TACNeighbourData & neigh) {
            return neigh.m_CellKey == cellkey;
        }
    private:
        int cellkey;
    };
    
    Then somewhere in the code:
    
    							// Also check if this cell id is already listed 
    							std::multiset<TACNeighbourData>::iterator result = 
    								std::find_if(found->second.begin(), found->second.end(), find_by_cellkey(sneigh.m_CellKey));
    
    							if (result == found->second.end())
    							{
    								// no match
    								found->second.insert(sneigh);
    							}
    
    						}

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Removing the duplicates in the multiset (bit urgent)

    Whilst this solution will prevent the non-required duplicate entries, it will have an adverse effect on performance - as the multiset is effectively traversed twice - once to see if the cell doesn't exist and then again to find the position for insertion. For a few entries, this won't be noticeable but for a large number of entries it could well be noticeable.

    I see that you want the set data sorted by distance. What about where you're trying to insert an item with an existing cell but a shorter distance? Won't this now not get inserted as the cell already exists ???

    Instead of multiset, don't you want a map with key cell and value multiset of {distance, hocnt} sorted by distance?

    Something like:

    Code:
    struct TACData {
        double m_Distance;
        int m_HOCount;
    };
    
    std::map<int, std::map<std::string, std::multiset<TACData> > > m_SiteKeyToNeighbourKeys;
    where TACData is sorted by m_Distance.

    The key to the outer map is siteid and that of the inner map is m_CellKey.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    May 2015
    Posts
    500

    Re: Removing the duplicates in the multiset (bit urgent)

    Thanks a lot kaud for the inputs, I'll try to change that and make efficient as you suggested. The initial design didnot consider all possibilities
    Last edited by pdk5; January 8th, 2021 at 10:07 AM.

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured