More optimizations.

Originally committed to SVN as r2369.
This commit is contained in:
Rodrigo Braz Monteiro 2008-09-20 03:29:41 +00:00
parent 2baf7bd2c4
commit 70e138bbf1
2 changed files with 59 additions and 17 deletions

View file

@ -163,31 +163,71 @@ void ActionModify::Execute(IModel& model)
ActionModifyBatch::ActionModifyBatch(std::vector<Entry> _entries, std::vector<shared_ptr<void> > _deltas, Selection _selection,const String &_section,bool _noTextFields) ActionModifyBatch::ActionModifyBatch(std::vector<Entry> _entries, std::vector<shared_ptr<void> > _deltas, Selection _selection,const String &_section,bool _noTextFields)
: entries(_entries), deltas(_deltas), selection(_selection), section(_section), noTextFields(_noTextFields) {} : entries(_entries), deltas(_deltas), selection(_selection), section(_section), noTextFields(_noTextFields) {}
ActionModifyBatch::ActionModifyBatch(Selection _selection,const String &_section,bool _noTextFields)
: selection(_selection), section(_section), noTextFields(_noTextFields) {}
Action ActionModifyBatch::GetAntiAction(const IModel& model) const Action ActionModifyBatch::GetAntiAction(const IModel& model) const
{ {
// Get section // Old, slow method
Section sect = GetSection(model,section); if (false) {
size_t len = selection->GetCount(); // Get section
std::vector<VoidPtr> _deltas(len); Section sect = GetSection(model,section);
std::vector<Entry> oldEntries(len); size_t len = selection->GetCount();
std::vector<VoidPtr> _deltas(len);
std::vector<Entry> oldEntries(len);
// For each line... // For each line...
for (size_t i=0;i<len;i++) { for (size_t i=0;i<len;i++) {
// Get old entry // Get old entry
Entry oldEntry = sect->GetEntry(selection->GetLine(i)); Entry oldEntry = sect->GetEntry(selection->GetLine(i));
// Try to get a delta // Try to get a delta
DeltaCoder deltaCoder = oldEntry->GetDeltaCoder(); DeltaCoder deltaCoder = oldEntry->GetDeltaCoder();
if (deltaCoder) { if (deltaCoder) {
if (i < deltas.size() && deltas[i]) _deltas[i] = deltaCoder->EncodeReverseDelta(deltas[i],oldEntry); if (i < deltas.size() && deltas[i]) _deltas[i] = deltaCoder->EncodeReverseDelta(deltas[i],oldEntry);
_deltas[i] = deltaCoder->EncodeDelta(entries[i],oldEntry,!noTextFields); _deltas[i] = deltaCoder->EncodeDelta(entries[i],oldEntry,!noTextFields);
}
// Store the whole original line
else oldEntries[i] = oldEntry;
} }
// Store the whole original line return Action(new ActionModifyBatch(oldEntries,_deltas,selection,section,noTextFields));
else oldEntries[i] = oldEntry;
} }
return Action(new ActionModifyBatch(oldEntries,_deltas,selection,section,noTextFields)); else {
// Get section
Section sect = GetSection(model,section);
size_t len = selection->GetCount();
// OK, this block warrants some explanation:
// Copying smart pointers around all the time is quite slow, so I just create them once and
// access the final copies.
ActionModifyBatch* antiPtr = new ActionModifyBatch(selection,section,noTextFields);
Action anti = Action(antiPtr);
std::vector<VoidPtr>& _deltas = antiPtr->deltas;
std::vector<Entry>& oldEntries = antiPtr->entries;
_deltas.resize(len);
oldEntries.resize(len);
// For each line...
for (size_t i=0;i<len;i++) {
// Get old entry
Entry oldEntry = sect->GetEntry(selection->GetLine(i));
// Try to get a delta
DeltaCoder deltaCoder = oldEntry->GetDeltaCoder();
if (deltaCoder) {
if (i < deltas.size() && deltas[i]) _deltas[i] = deltaCoder->EncodeReverseDelta(deltas[i],oldEntry);
_deltas[i] = deltaCoder->EncodeDelta(entries[i],oldEntry,!noTextFields);
}
// Store the whole original line
else oldEntries[i] = oldEntry;
}
return anti;
}
} }
void ActionModifyBatch::Execute(IModel& model) void ActionModifyBatch::Execute(IModel& model)

View file

@ -93,6 +93,8 @@ namespace Athenasub {
const String section; const String section;
bool noTextFields; bool noTextFields;
ActionModifyBatch(Selection selection,const String &section,bool noTextFields);
public: public:
ActionModifyBatch(std::vector<Entry> entries,std::vector<VoidPtr> deltas,Selection selection,const String &section,bool noTextFields); ActionModifyBatch(std::vector<Entry> entries,std::vector<VoidPtr> deltas,Selection selection,const String &section,bool noTextFields);