From cc8c992123e492ec306656585ec94a46302a9146 Mon Sep 17 00:00:00 2001 From: "Charles J. Cliffe" Date: Mon, 21 Nov 2016 20:12:10 -0500 Subject: [PATCH] Better group/sort handling; sets don't like duplicate compares. --- src/BookmarkMgr.cpp | 43 +++++++++++++++++------------ src/BookmarkMgr.h | 9 +++--- src/forms/Bookmark/BookmarkView.cpp | 12 ++++---- 3 files changed, 36 insertions(+), 28 deletions(-) diff --git a/src/BookmarkMgr.cpp b/src/BookmarkMgr.cpp index 4dbfa06..313a7b3 100644 --- a/src/BookmarkMgr.cpp +++ b/src/BookmarkMgr.cpp @@ -22,21 +22,31 @@ void BookmarkMgr::addBookmark(std::string group, DemodulatorInstance *demod) { wxGetApp().getDemodMgr().saveInstance(be->node, demod); - bmData[group].insert(be); + bmData[group].push_back(be); + bmDataSorted[group] = false; } void BookmarkMgr::addBookmark(std::string group, BookmarkEntry *be) { std::lock_guard < std::mutex > lock(busy_lock); - bmData[group].insert(be); + bmData[group].push_back(be); + bmDataSorted[group] = false; } void BookmarkMgr::removeBookmark(std::string group, BookmarkEntry *be) { std::lock_guard < std::mutex > lockData(busy_lock); std::lock_guard < std::mutex > lockEnt(be->busy_lock); + + if (bmData.find(group) == bmData.end()) { + return; + } + + BookmarkList::iterator i = std::find(bmData[group].begin(), bmData[group].end(), be); - bmData[group].erase(be); + if (i != bmData[group].end()) { + bmData[group].erase(i); + } } void BookmarkMgr::removeBookmark(BookmarkEntry *be) { @@ -44,8 +54,9 @@ void BookmarkMgr::removeBookmark(BookmarkEntry *be) { std::lock_guard < std::mutex > lockEnt(be->busy_lock); for (auto &bmd_i : bmData) { - if (bmd_i.second.find(be) != bmd_i.second.end()) { - bmd_i.second.erase(be); + BookmarkList::iterator i = std::find(bmd_i.second.begin(), bmd_i.second.end(), be); + if (i != bmd_i.second.end()) { + bmd_i.second.erase(i); } } @@ -53,26 +64,24 @@ void BookmarkMgr::removeBookmark(BookmarkEntry *be) { BookmarkList BookmarkMgr::getBookmarks(std::string group) { std::lock_guard < std::mutex > lock(busy_lock); - - BookmarkList results; - for (auto be_i : bmData[group]) { - results.push_back(be_i); + if (bmData.find(group) == bmData.end()) { + BookmarkList results; + return results; + } + + if (!bmDataSorted[group]) { + std::sort(bmData[group].begin(), bmData[group].end(), BookmarkEntryCompare()); + bmDataSorted[group] = true; } - return results; -} - -BookmarkGroup BookmarkMgr::getGroup(std::string group) { return bmData[group]; } -BookmarkNames BookmarkMgr::getGroups() { - BookmarkNames results; +void BookmarkMgr::getGroups(BookmarkNames &arr) { for (BookmarkMap::iterator i = bmData.begin(); i!= bmData.end(); ++i) { - results.push_back(i->first); + arr.push_back(i->first); } - return results; } void BookmarkMgr::getGroups(wxArrayString &arr) { diff --git a/src/BookmarkMgr.h b/src/BookmarkMgr.h index 7f66710..cebe497 100644 --- a/src/BookmarkMgr.h +++ b/src/BookmarkMgr.h @@ -31,8 +31,8 @@ struct BookmarkEntryCompare : public std::binary_function BookmarkList; -typedef std::set BookmarkGroup; -typedef std::map BookmarkMap; +typedef std::map BookmarkMap; +typedef std::map BookmarkMapSorted; typedef std::vector BookmarkNames; class BookmarkMgr { @@ -46,9 +46,7 @@ public: void removeBookmark(BookmarkEntry *be); BookmarkList getBookmarks(std::string group); - - BookmarkGroup getGroup(std::string group); - BookmarkNames getGroups(); + void getGroups(BookmarkNames &arr); void getGroups(wxArrayString &arr); void updateActiveList(); @@ -65,6 +63,7 @@ protected: BookmarkEntry *demodToBookmarkEntry(DemodulatorInstance *demod); BookmarkMap bmData; + BookmarkMapSorted bmDataSorted; BookmarkList recents; std::mutex busy_lock; }; diff --git a/src/forms/Bookmark/BookmarkView.cpp b/src/forms/Bookmark/BookmarkView.cpp index fdcd0fc..ad1e075 100644 --- a/src/forms/Bookmark/BookmarkView.cpp +++ b/src/forms/Bookmark/BookmarkView.cpp @@ -92,10 +92,10 @@ void BookmarkView::updateBookmarks(std::string group) { wxTreeItemId BookmarkView::refreshBookmarks() { - groupNames = wxGetApp().getBookmarkMgr().getGroups(); - if (!groupNames.size()) { - groupNames = wxGetApp().getBookmarkMgr().getGroups(); - } + + BookmarkNames groupNames; + wxGetApp().getBookmarkMgr().getGroups(groupNames); + if (doUpdateBookmarkGroup.size()) { // Nothing for the moment.. doUpdateBookmarkGroup.erase(doUpdateBookmarkGroup.begin(), doUpdateBookmarkGroup.end()); } @@ -114,7 +114,7 @@ wxTreeItemId BookmarkView::refreshBookmarks() { wxTreeItemId groupItem = groups[gn_i]; m_treeView->DeleteChildren(groupItem); - BookmarkGroup bmList = wxGetApp().getBookmarkMgr().getGroup(gn_i); + BookmarkList bmList = wxGetApp().getBookmarkMgr().getBookmarks(gn_i); for (auto bmEnt : bmList) { TreeViewItem* tvi = new TreeViewItem(); tvi->type = TreeViewItem::TREEVIEW_ITEM_TYPE_BOOKMARK; @@ -626,7 +626,7 @@ void BookmarkView::onActivateRecent( wxCommandEvent& event ) { void BookmarkView::onAddGroup( wxCommandEvent& event ) { wxString stringVal = wxGetTextFromUser("Enter Group Name", "Add Group", ""); if (stringVal.ToStdString() != "") { - wxGetApp().getBookmarkMgr().getGroup(stringVal.ToStdString()); + wxGetApp().getBookmarkMgr().getBookmarks(stringVal.ToStdString()); wxGetApp().getBookmarkMgr().updateBookmarks(); groupSel = stringVal; }