CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Deploying Windows Server 2008 with System Center
  • Remote Desktop Protocol Performance Improvements in Windows Server 2008 R2 and Windows 7
  • The Microsoft Dynamics CRM Security Model
  • SQL Server Modeling Services with Microsoft Visual Studio 2010 Beta 2

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Visual C++ Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Visual C++ Programming Ask questions about Windows programming with Visual C++ and help others by answering their questions.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old October 9th, 2009, 03:22 PM
    SkyNetTo SkyNetTo is offline
    Member
     
    Join Date: Mar 2008
    Posts: 98
    SkyNetTo is on a distinguished road (30+)
    Questions about right structure.

    Hi everyone.
    I'm tryin to create a library of my music and searching for tags in mp3 file. So good so far.

    My library database is defined as

    Code:
    std::map<std::wstring,fileTag> m_playList;
    
    where std::wstring is the complete path of the song and filetag is defined as:
    
    class fileTag
    {
    public:
    	fileTag(){};
    	virtual ~fileTag(){};
    	std::wstring songTitle;
    	std::wstring artistName;
    	std::wstring trakNumber;
    	std::wstring trackYear;
    	std::wstring albumName;
    	std::wstring trackGenre;
    };
    Then I've created a function wich search all mp3's in a specific folder and create the tag map.
    Everything works ok.

    Now I wat to search by artist or by album. So I've created another map wich is defined as:

    Code:
    std::map<std::wstring,std::vector<std::map<std::wstring,fileTag>::const_iterator> > m_albumList;
    std::wstring is the name of the album and the vector of const_iterators is a vector that holds all the references of the songs tha t have that albumName. (references are made in the function below)

    I've made all the right reference with this function:

    Code:
    void CPlaylistDlg::AddAlbumToList(const std::wstring & path)
    {
    	std::map<std::wstring,fileTag>::iterator playListIt;
    	std::map<std::wstring,std::vector<std::map<std::wstring,fileTag>::const_iterator> >::iterator it;
    	
    
    	playListIt = m_playList.find(path);
    	if(playListIt != m_playList.end())
    	{
    		it = m_albumList.find(playListIt->second.albumName);
    		if(it == m_albumList.end())
    		{
    			std::vector<std::map<std::wstring,fileTag>::const_iterator> m_songsVector;
    			m_songsVector.push_back(playListIt);
    			m_albumList[playListIt->second.albumName] = m_songsVector;
    		}
    		else
    		{
    			it->second.push_back(playListIt);
    		}
    	}
    }
    and when I want to search for an album the previous map gives me the right iterator pointing to the playList database where I can get also author, songName ecc.

    My question is:
    Is this the right way to do it or is there a better and faster way?

    Thanks to everybody.

    Last edited by SkyNetTo; October 9th, 2009 at 03:26 PM.
    Reply With Quote
      #2    
    Old October 10th, 2009, 11:03 AM
    Paul McKenzie Paul McKenzie is offline
    Elite Member
    Power Poster
     
    Join Date: Apr 1999
    Posts: 20,960
    Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+)
    Re: Questions about right structure.

    Quote:
    Originally Posted by SkyNetTo View Post
    My question is:
    Is this the right way to do it or is there a better and faster way?
    Vector iterators can become invalid if items are added or removed from the vector. So storing vector iterators in a map seems to be a design flaw.

    If it were a linked list (std::list), then you can get away with storing iterators, since std::list iterators are not invalidated unless the particular item from the list is removed.

    Secondly, it would help matters if you typedefed a lot of your definitions
    Code:
    typedef std::map<std::wstring,fileTag> StringToFileTagMap;
    StringToFileTagMap m_playList;
    //...
    You should do similar things with the other definitions. Not only does it make the code more readable, the definitions become much shorter and you are not wearing out your keybpard typing in a declaration of an iterator (for example).

    Regards,

    Paul McKenzie
    Reply With Quote
      #3    
    Old October 10th, 2009, 12:47 PM
    SkyNetTo SkyNetTo is offline
    Member
     
    Join Date: Mar 2008
    Posts: 98
    SkyNetTo is on a distinguished road (30+)
    Re: Questions about right structure.

    Thanks a lot for your response.
    I'm using map iterators, instead of vector iterators. Do they still become invalid if I add or remove an Item for my database?
    And you're definelly right I should typedef a lot .

    so my design is this:

    a map (let's called it songs map) with string as key and a filetag structure as val.

    a second map (let's call it albums map) with string as key (the album name) and a vector of map iterators to the songs map as val.

    if I delete a song in the songs map first I should delete it referenced iterator from the vector on the albums map and if the size of the vector become zero (if this is the single songs that hase that album) delete also the pair(albumName,vector of iterator) from the albums map. Do the album map than become invalid? (I haven't tried yet deleting things only ading new songs).

    Last edited by SkyNetTo; October 10th, 2009 at 03:56 PM.
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Visual C++ Programming


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 03:03 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.