Better group/sort handling; sets don't like duplicate compares.

This commit is contained in:
Charles J. Cliffe 2016-11-21 20:12:10 -05:00
parent 73954055e6
commit cc8c992123
3 changed files with 36 additions and 28 deletions

View File

@ -22,13 +22,15 @@ 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;
}
@ -36,7 +38,15 @@ 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);
bmData[group].erase(be);
if (bmData.find(group) == bmData.end()) {
return;
}
BookmarkList::iterator i = std::find(bmData[group].begin(), bmData[group].end(), 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);
}
}
@ -54,25 +65,23 @@ 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;
}
return results;
}
if (!bmDataSorted[group]) {
std::sort(bmData[group].begin(), bmData[group].end(), BookmarkEntryCompare());
bmDataSorted[group] = true;
}
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) {

View File

@ -31,8 +31,8 @@ struct BookmarkEntryCompare : public std::binary_function<BookmarkEntry *,Bookma
typedef std::vector<BookmarkEntry *> BookmarkList;
typedef std::set<BookmarkEntry *, BookmarkEntryCompare> BookmarkGroup;
typedef std::map<std::string, BookmarkGroup > BookmarkMap;
typedef std::map<std::string, BookmarkList > BookmarkMap;
typedef std::map<std::string, bool > BookmarkMapSorted;
typedef std::vector<std::string> 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;
};

View File

@ -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;
}