Use std algorithme in view/grid

This commit is contained in:
Grégory Soutadé 2011-08-16 18:54:24 +02:00
parent 08ba72664c
commit a4179804be
3 changed files with 33 additions and 37 deletions

View File

@ -40,6 +40,11 @@ struct Operation {
bool meta;
bool _virtual;
std::vector<wxString> childs;
bool operator == (const wxString& opId)
{
return id == opId;
}
} ;
bool sortOperations(const Operation& op1, const Operation& op2);

View File

@ -137,36 +137,32 @@ void GridAccount::SetWeek(int week, int line) {
}
}
Operation& GridAccount::GetOperation(const wxString& id)
Operation& GridAccount::GetOperation(const wxString& id) throw (OperationNotFound)
{
std::vector<Operation>::iterator it;
std::vector<Operation>::iterator it = std::find(_operations->begin(), _operations->end(), id);
for(it=_operations->begin(); it!=_operations->end(); it++)
if (it->id == id)
return *it;
// I'm aware about the warning, but the method may not fail
if (it != _operations->end()) return *it;
throw OperationNotFound();
}
void GridAccount::UpdateOperation(Operation& op)
{
std::vector<Operation>::iterator it;
int i;
std::vector<Operation>::iterator it = std::find(_operations->begin(), _operations->end(), op.id);
for(i=0; i < (int)_operations->size(); i++)
if ((*_operations)[i].id == op.id)
{
if (_databaseSynchronization)
_kiss->UpdateOperation(op);
(*_operations)[i] = op;
break;
}
if (it != _operations->end())
{
if (_databaseSynchronization)
_kiss->UpdateOperation(op);
*it = op;
}
}
int GridAccount::GetDisplayedRow(const wxString& id)
{
for(int i=0; i<(int)_displayedOperations.size(); i++)
if (_displayedOperations[i].id == id)
return i;
std::vector<Operation>::iterator it = std::find(_displayedOperations.begin(), _displayedOperations.end(), id);
if (it != _displayedOperations.end()) return it-_displayedOperations.begin();
return -1;
}
@ -476,14 +472,9 @@ void GridAccount::OnCellLeftClick(wxGridEvent& evt)
void GridAccount::DeleteOperation(const Operation& op)
{
std::vector<Operation>::iterator it;
std::vector<Operation>::iterator it = std::find(_operations->begin(), _operations->end(), op.id);
for (it=_operations->begin(); it!=_operations->end(); it++)
if (it->id == op.id)
{
_operations->erase(it);
break;
}
if (it != _operations->end()) _operations->erase(it);
}
void GridAccount::InsertIntoGrid(Operation& op)
@ -1231,9 +1222,7 @@ void GridAccount::GetSelectedOperations(std::vector<int>* rows)
const wxGridCellCoords& br = bbr[i];
for (int row = tl.GetRow(); row <= br.GetRow(); ++row)
{
for (it=rows->begin(); it!=rows->end(); it++)
if (*it == row)
break;
it = std::find(rows->begin(), rows->end(), row);
if (it != rows->end() || !row) continue;
@ -1246,15 +1235,15 @@ void GridAccount::GetSelectedOperations(std::vector<int>* rows)
const wxGridCellCoordsArray& cells(GetSelectedCells());
for (size_t i = 0; i < cells.size(); ++i)
{
const wxGridCellCoords& c = cells[i];
int row = cells[i].GetRow();
for (it=rows->begin(); it!=rows->end(); it++)
if (*it == c.GetRow())
break;
if (!row) continue;
if (it != rows->end() || !c.GetRow()) continue;
it = std::find(rows->begin(), rows->end(), row);
rows->push_back(c.GetRow());
if (it != rows->end()) continue;
rows->push_back(row);
}
}

View File

@ -43,7 +43,9 @@ typedef void (*updateOperationFunc)(Operation* op, void** params);
class GridAccount : public wxGrid
{
public:
GridAccount(KissCount* kiss, wxWindow *parent, wxWindowID id,
class OperationNotFound {};
GridAccount(KissCount* kiss, wxWindow *parent, wxWindowID id,
bool canAddOperation, bool setWeek, bool synchronizeWithDatabase);
~GridAccount();
@ -90,7 +92,7 @@ private:
int RemoveMeta(Operation op, int line, bool removeRoot, bool deleteOp);
void CheckMeta(Operation& op, int line, bool check);
Operation& GetOperation(const wxString& id);
Operation& GetOperation(const wxString& id) throw(OperationNotFound);
void UpdateOperation(Operation& op);
int GetDisplayedRow(const wxString& id);