Use vector<int> instead of vector<bool> for tracking which rows are selected. Makes undo roughly twice as fast.

Originally committed to SVN as r4563.
This commit is contained in:
Thomas Goyne 2010-06-22 00:03:22 +00:00
parent 358b2734da
commit 5ed401d23d
3 changed files with 33 additions and 44 deletions

View file

@ -128,7 +128,8 @@ void BaseGrid::UpdateStyle() {
// Set column widths // Set column widths
std::vector<bool> column_array; std::vector<bool> column_array;
OPT_GET("Subtitle/Grid/Column")->GetListBool(column_array); OPT_GET("Subtitle/Grid/Column")->GetListBool(column_array);
for (int i=0;i<10;i++) showCol[i] = column_array.at(i); assert(column_array.size() == columns);
for (int i=0;i<columns;i++) showCol[i] = column_array[i];
SetColumnWidths(); SetColumnWidths();
// Update // Update
@ -210,7 +211,7 @@ void BaseGrid::SelectRow(int row, bool addToSelected, bool select) {
if (row < 0 || (size_t)row >= selMap.size()) return; if (row < 0 || (size_t)row >= selMap.size()) return;
if (!addToSelected) ClearSelection(); if (!addToSelected) ClearSelection();
if (select != selMap[row]) { if (select != !!selMap[row]) {
selMap[row] = select; selMap[row] = select;
if (!addToSelected) { if (!addToSelected) {
@ -269,7 +270,7 @@ void BaseGrid::ClearSelection() {
/// ///
bool BaseGrid::IsInSelection(int row, int) const { bool BaseGrid::IsInSelection(int row, int) const {
if ((size_t)row >= selMap.size() || row < 0) return false; if ((size_t)row >= selMap.size() || row < 0) return false;
return selMap[row]; return !!selMap[row];
} }
@ -278,7 +279,7 @@ bool BaseGrid::IsInSelection(int row, int) const {
/// @return /// @return
/// ///
int BaseGrid::GetNumberSelection() const { int BaseGrid::GetNumberSelection() const {
return std::count(selMap.begin(), selMap.end(), true); return std::count(selMap.begin(), selMap.end(), 1);
} }
@ -287,7 +288,7 @@ int BaseGrid::GetNumberSelection() const {
/// @return /// @return
/// ///
int BaseGrid::GetFirstSelRow() const { int BaseGrid::GetFirstSelRow() const {
std::vector<bool>::const_iterator first = std::find(selMap.begin(), selMap.end(), true); std::vector<int>::const_iterator first = std::find(selMap.begin(), selMap.end(), 1);
if (first == selMap.end()) return -1; if (first == selMap.end()) return -1;
return std::distance(selMap.begin(), first); return std::distance(selMap.begin(), first);
} }
@ -941,8 +942,8 @@ void BaseGrid::SetColumnWidths() {
for (int i=0;i<3;i++) colWidth[i+7] = showMargin[i] ? marginLen : 0; for (int i=0;i<3;i++) colWidth[i+7] = showMargin[i] ? marginLen : 0;
// Hide columns // Hide columns
for (int i=0;i<10;i++) { for (int i=0;i<columns;i++) {
if (showCol[i] == false) colWidth[i] = 0; if (!showCol[i]) colWidth[i] = 0;
} }
// Set size of last // Set size of last
@ -988,13 +989,9 @@ bool BaseGrid::IsDisplayed(AssDialogue *line) {
/// ///
void BaseGrid::UpdateMaps() { void BaseGrid::UpdateMaps() {
// Store old // Store old
int len = diagMap.size(); int len = selMap.size();
std::vector<AssDialogue *> tmpDiagPtrMap; std::vector<AssDialogue *> tmpDiagPtrMap(diagPtrMap);
std::vector<bool> tmpSelMap; std::vector<int> tmpSelMap(selMap);
for (int i=0;i<len;i++) {
tmpDiagPtrMap.push_back(diagPtrMap[i]);
tmpSelMap.push_back(selMap[i]);
}
// Clear old // Clear old
diagPtrMap.clear(); diagPtrMap.clear();
@ -1002,13 +999,11 @@ void BaseGrid::UpdateMaps() {
selMap.clear(); selMap.clear();
// Re-generate lines // Re-generate lines
int n = 0;
AssDialogue *curdiag;
for (entryIter cur=AssFile::top->Line.begin();cur != AssFile::top->Line.end();cur++) { for (entryIter cur=AssFile::top->Line.begin();cur != AssFile::top->Line.end();cur++) {
curdiag = dynamic_cast<AssDialogue*>(*cur); AssDialogue *curdiag = dynamic_cast<AssDialogue*>(*cur);
if (curdiag) { if (curdiag) {
// Find old pos // Find old pos
bool sel = false; int sel = 0;
for (int i=0;i<len;i++) { for (int i=0;i<len;i++) {
if (tmpDiagPtrMap[i] == curdiag) { if (tmpDiagPtrMap[i] == curdiag) {
sel = tmpSelMap[i]; sel = tmpSelMap[i];
@ -1020,8 +1015,6 @@ void BaseGrid::UpdateMaps() {
diagMap.push_back(cur); diagMap.push_back(cur);
diagPtrMap.push_back(curdiag); diagPtrMap.push_back(curdiag);
selMap.push_back(sel); selMap.push_back(sel);
n++;
} }
} }

View file

@ -113,7 +113,8 @@ protected:
FrameMain *parentFrame; FrameMain *parentFrame;
/// DOCME /// DOCME
bool showCol[16]; static const int columns = 10;
bool showCol[columns];
/// @brief DOCME /// @brief DOCME
@ -126,7 +127,7 @@ protected:
int yPos; int yPos;
/// DOCME /// DOCME
std::vector<bool> selMap; std::vector<int> selMap;
public: public:

View file

@ -132,22 +132,22 @@ SubtitlesGrid::~SubtitlesGrid() {
void SubtitlesGrid::OnPopupMenu(bool alternate) { void SubtitlesGrid::OnPopupMenu(bool alternate) {
// Alternate // Alternate
if (alternate) { if (alternate) {
// Prepare strings const wxString strings[] = {
wxArrayString strings; _("Line Number"),
strings.Add(_("Line Number")); _("Layer"),
strings.Add(_("Layer")); _("Start"),
strings.Add(_("Start")); _("End"),
strings.Add(_("End")); _("Style"),
strings.Add(_("Style")); _("Actor"),
strings.Add(_("Actor")); _("Effect"),
strings.Add(_("Effect")); _("Left"),
strings.Add(_("Left")); _("Right"),
strings.Add(_("Right")); _("Vert"),
strings.Add(_("Vert")); };
// Create Menu // Create Menu
wxMenu menu; wxMenu menu;
for (size_t i=0;i<strings.Count();i++) { for (size_t i=0;i<columns;i++) {
menu.Append(MENU_SHOW_COL + i,strings[i],_T(""),wxITEM_CHECK)->Check(showCol[i]); menu.Append(MENU_SHOW_COL + i,strings[i],_T(""),wxITEM_CHECK)->Check(showCol[i]);
} }
PopupMenu(&menu); PopupMenu(&menu);
@ -227,7 +227,7 @@ void SubtitlesGrid::OnShowColMenu(wxCommandEvent &event) {
int item = event.GetId()-MENU_SHOW_COL; int item = event.GetId()-MENU_SHOW_COL;
showCol[item] = !showCol[item]; showCol[item] = !showCol[item];
std::vector<bool> map(showCol, showCol + sizeof(showCol) / sizeof(bool)); std::vector<bool> map(showCol, showCol + columns);
OPT_SET("Subtitle/Grid/Column")->SetListBool(map); OPT_SET("Subtitle/Grid/Column")->SetListBool(map);
// Update // Update
@ -810,14 +810,9 @@ void SubtitlesGrid::LoadDefault (AssFile *_ass) {
/// ///
void SubtitlesGrid::LoadFromAss (AssFile *_ass,bool keepSelection,bool dontModify) { void SubtitlesGrid::LoadFromAss (AssFile *_ass,bool keepSelection,bool dontModify) {
// Store selected rows // Store selected rows
std::vector<int> srows; wxArrayInt srows;
if (keepSelection) { if (keepSelection) {
int nrows = GetRows(); srows = GetSelection();
for (int i=0;i<nrows;i++) {
if (IsInSelection(i,0)) {
srows.push_back(i);
}
}
} }
// Clear grid // Clear grid
@ -853,7 +848,7 @@ void SubtitlesGrid::LoadFromAss (AssFile *_ass,bool keepSelection,bool dontModif
// Restore selection // Restore selection
if (keepSelection) { if (keepSelection) {
for (size_t i=0;i<srows.size();i++) { for (size_t i=0;i<srows.size();i++) {
SelectRow(srows.at(i),true); SelectRow(srows[i],true);
} }
} }