Skip to content

Commit

Permalink
Revert dynamic datastructures (#804)
Browse files Browse the repository at this point in the history
* Revert "Avoid double ownership (#800)"

This reverts commit 89b40de.

* Revert "Fix bad unique_ptr usage"

This reverts commit 2086ce8.

* Revert "Use correct method to delete resource (#783)"

This reverts commit 81e74a0.

* Revert "Use unique_ptr for Grid in Cell (#782)"

This reverts commit 9fe088a.

* Revert "Make rootgrid an unique_ptr (#776)"

This reverts commit 15d5f58.

* Revert "Use vector instead of raw pointer arrays (#785)"

This reverts commit 9dfc4fb.

* Revert "Use unique_ptr for Document in TSCanvas (#780)"

This reverts commit e3d4602.
  • Loading branch information
tobiolo authored Jan 16, 2025
1 parent 89b40de commit f8edf8a
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 129 deletions.
26 changes: 14 additions & 12 deletions src/cell.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct Cell {
int tys {0};
int celltype;
Text text;
unique_ptr<Grid> grid;
Grid *grid;
uint cellcolor {0xFFFFFF};
uint textcolor {0x000000};
uint actualcellcolor {0xFFFFFF};
Expand All @@ -53,8 +53,9 @@ struct Cell {
if (_clonefrom) CloneStyleFrom(_clonefrom);
}

~Cell() { DELETEP(grid); }
void Clear() {
grid.reset();
DELETEP(grid);
text.t.Clear();
text.image = nullptr;
Reset();
Expand Down Expand Up @@ -195,7 +196,7 @@ struct Cell {
grid ? new Grid(grid->xs, grid->ys) : nullptr);
c->text = text;
c->text.cell = c.get();
if (grid) { grid->Clone(c->grid.get()); }
if (grid) { grid->Clone(c->grid); }
return c;
}

Expand Down Expand Up @@ -354,18 +355,19 @@ struct Cell {

Grid *AddGrid(int x = 1, int y = 1) {
if (!grid) {
grid = make_unique<Grid>(x, y, this);
grid = new Grid(x, y, this);
grid->InitCells(this);
if (parent) grid->CloneStyleFrom(parent->grid.get());
if (parent) grid->CloneStyleFrom(parent->grid);
}
return grid.get();
return grid;
}

Cell *LoadGrid(wxDataInputStream &dis, int &numcells, int &textbytes, Cell *&ics) {
int xs = dis.Read32();
grid = make_unique<Grid>(xs, dis.Read32());
grid->cell = this;
if (!grid->LoadContents(dis, numcells, textbytes, ics)) return nullptr;
Grid *g = new Grid(xs, dis.Read32());
grid = g;
g->cell = this;
if (!g->LoadContents(dis, numcells, textbytes, ics)) return nullptr;
return this;
}

Expand Down Expand Up @@ -424,9 +426,9 @@ struct Cell {
c->grid->Clone(cg);
// Note: deleting grid may invalidate c if its a child of grid, so clear it.
c = nullptr;
grid.reset(cg); // FIXME: could merge instead?
if (!HasText())
grid->MergeWithParent(parent->grid.get(), s, doc); // deletes grid/this.
DELETEP(grid); // FIXME: could merge instead?
grid = cg;
if (!HasText()) grid->MergeWithParent(parent->grid, s, doc); // deletes grid/this.
}
}

Expand Down
57 changes: 29 additions & 28 deletions src/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct UndoItem {

struct Document {
TSCanvas *sw {nullptr};
unique_ptr<Cell> rootgrid;
Cell *rootgrid {nullptr};
Selection hover;
Selection selected;
Selection begindrag;
Expand Down Expand Up @@ -91,7 +91,7 @@ struct Document {
CollectCells(par); \
loopv(_i, itercells) for (Cell *c = itercells[_i]; c; c = nullptr)
#define loopallcells(c) \
CollectCells(rootgrid.get()); \
CollectCells(rootgrid); \
for (Cell *c : itercells)
#define loopallcellssel(c, rec) \
CollectCellsSel(rec); \
Expand All @@ -107,11 +107,13 @@ struct Document {
dndobjc->Add(dndobjf);
}

~Document() { DELETEP(rootgrid); }

uint Background() { return rootgrid ? rootgrid->cellcolor : 0xFFFFFF; }

void InitCellSelect(Cell *ics, int xs, int ys) {
if (!ics) {
SetSelect(Selection(rootgrid->grid.get(), 0, 0, 1, 1));
SetSelect(Selection(rootgrid->grid, 0, 0, 1, 1));
return;
}
SetSelect(ics->parent->grid->FindCell(ics));
Expand All @@ -120,7 +122,7 @@ struct Document {
}

void InitWith(Cell *r, const wxString &filename, Cell *ics, int xs, int ys) {
rootgrid.reset(r);
rootgrid = r;
InitCellSelect(ics, xs, ys);
ChangeFileName(filename, false);
}
Expand Down Expand Up @@ -510,8 +512,7 @@ struct Document {
Cell *drawroot = WalkPath(drawpath);
if (selected.GetCell() == drawroot && drawroot->grid) {
// We can't have the drawroot selected, so we must move the selection to the children.
SetSelect(
Selection(drawroot->grid.get(), 0, 0, drawroot->grid->xs, drawroot->grid->ys));
SetSelect(Selection(drawroot->grid, 0, 0, drawroot->grid->xs, drawroot->grid->ys));
}
drawroot->ResetLayout();
drawroot->ResetChildren();
Expand Down Expand Up @@ -565,7 +566,7 @@ struct Document {
ResetFont();
dc.SetUserScale(1, 1);
curdrawroot = WalkPath(drawpath);
int psb = curdrawroot == rootgrid.get() ? 0 : curdrawroot->MinRelsize();
int psb = curdrawroot == rootgrid ? 0 : curdrawroot->MinRelsize();
if (psb < 0 || psb == INT_MAX) psb = 0;
if (psb != pathscalebias) curdrawroot->ResetChildren();
pathscalebias = psb;
Expand Down Expand Up @@ -808,7 +809,7 @@ struct Document {
}

const wxChar *ExportFile(const wxString &fn, int k, bool currentview) {
auto root = currentview ? curdrawroot : rootgrid.get();
auto root = currentview ? curdrawroot : rootgrid;
if (k == A_EXPCSV) {
int maxdepth = 0, leaves = 0;
root->MaxDepthLeaves(0, maxdepth, leaves);
Expand Down Expand Up @@ -973,7 +974,7 @@ struct Document {

switch (k) {
case wxID_EXECUTE:
sys->ev.Eval(rootgrid.get());
sys->ev.Eval(rootgrid);
rootgrid->ResetChildren();
ClearSelectionRefresh();
return _(L"Evaluation finished.");
Expand Down Expand Up @@ -1499,12 +1500,12 @@ struct Document {
case A_NEWGRID:
if (!(c = selected.ThinExpand(this))) return OneCell();
if (c->grid) {
SetSelect(Selection(c->grid.get(), 0, c->grid->ys, 1, 0));
SetSelect(Selection(c->grid, 0, c->grid->ys, 1, 0));
ScrollOrZoom(dc, true);
} else {
c->AddUndo(this);
c->AddGrid();
SetSelect(Selection(c->grid.get(), 0, 0, 1, 1));
SetSelect(Selection(c->grid, 0, 0, 1, 1));
DrawSelectMove(dc, selected, true);
}
return nullptr;
Expand Down Expand Up @@ -1879,7 +1880,8 @@ struct Document {
Grid *g = new Grid(maxdepth, leaves);
g->InitCells();
ac->grid->Flatten(0, 0, g);
ac->grid.reset(g);
DELETEP(ac->grid);
ac->grid = g;
g->ReParent(ac);
ac->ResetChildren();
ClearSelectionRefresh();
Expand All @@ -1896,7 +1898,7 @@ struct Document {

case A_ENTERGRID:
if (!c->grid) Action(dc, A_NEWGRID);
SetSelect(Selection(c->grid.get(), 0, 0, 1, 1));
SetSelect(Selection(c->grid, 0, 0, 1, 1));
ScrollOrZoom(dc, true);
return nullptr;

Expand Down Expand Up @@ -2081,10 +2083,10 @@ struct Document {
} else {
c->parent->AddUndo(this);
c->ResetLayout();
c->grid.release();
DELETEP(c->grid);
sys->FillRows(c->AddGrid(), as, sys->CountCol(as[0]), 0, 0);
if (!c->HasText())
c->grid->MergeWithParent(c->parent->grid.get(), selected, this);
c->grid->MergeWithParent(c->parent->grid, selected, this);
}
}
}
Expand Down Expand Up @@ -2141,10 +2143,10 @@ struct Document {
}

Cell *WalkPath(vector<Selection> &path) {
Cell *c = rootgrid.get();
Cell *c = rootgrid;
loopvrev(i, path) {
Selection &s = path[i];
Grid *g = c->grid.get();
Grid *g = c->grid;
if (!g) return c;
ASSERT(g && s.x < g->xs && s.y < g->ys);
c = g->C(s.x, s.y);
Expand Down Expand Up @@ -2205,18 +2207,17 @@ struct Document {
if (beforesel.g) CreatePath(beforesel.g->cell, beforepath);
unique_ptr<UndoItem> ui = std::move(fromlist.back());
fromlist.pop_back();
if (Cell *c = WalkPath(ui->path); c->parent && c->parent->grid) {
auto clone = ui->clone.release();
Cell *c = WalkPath(ui->path);
auto clone = ui->clone.release();
ui->clone.reset(c);
if (c->parent && c->parent->grid) {
c->parent->grid->ReplaceCell(c, clone);
clone->parent = c->parent;
clone->ResetLayout();
ui->clone.reset(c);
} else {
rootgrid.swap(ui->clone);
rootgrid->ResetLayout();
}
} else
rootgrid = clone;
clone->ResetLayout();
SetSelect(ui->sel);
if (selected.g) selected.g = WalkPath(ui->selpath)->grid.get();
if (selected.g) selected.g = WalkPath(ui->selpath)->grid;
begindrag = selected;
ui->sel = beforesel;
ui->selpath = std::move(beforepath);
Expand Down Expand Up @@ -2300,7 +2301,7 @@ struct Document {
searchfilter = false;
scrolltoselection = true;
editfilter = min(max(editfilter, 1), 99);
CollectCells(rootgrid.get());
CollectCells(rootgrid);
std::sort(itercells.begin(), itercells.end(), [](Cell *a, Cell *b) {
// sort in descending order
return a->text.lastedit > b->text.lastedit;
Expand All @@ -2313,7 +2314,7 @@ struct Document {
void ApplyEditRangeFilter(wxDateTime &rangebegin, wxDateTime &rangeend) {
searchfilter = false;
scrolltoselection = true;
CollectCells(rootgrid.get());
CollectCells(rootgrid);
for (auto *c : itercells) {
c->text.filtered = !c->text.lastedit.IsBetween(rangebegin, rangeend);
}
Expand Down
10 changes: 5 additions & 5 deletions src/evaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ struct Evaluator {

void Assign(const Cell *sym, const Cell *val) {
this->SetSymbol(sym->text.t, val->Clone(nullptr));
if (sym->grid && val->grid) this->DestructuringAssign(sym->grid.get(), val->Clone(nullptr));
if (sym->grid && val->grid) this->DestructuringAssign(sym->grid, val->Clone(nullptr));
}

void DestructuringAssign(Grid const *names, unique_ptr<Cell> val) {
Grid const *ng = names;
Grid const *vg = val->grid.get();
Grid const *vg = val->grid;
if (ng->xs == vg->xs && ng->ys == vg->ys) {
loop(x, ng->xs) loop(y, ng->ys) {
Cell *nc = ng->C(x, y);
Expand All @@ -118,7 +118,7 @@ struct Evaluator {

unique_ptr<Cell> Execute(const Operation *op, unique_ptr<Cell> left) {
Text &t = left->text;
Grid *g = left->grid.get();
Grid *g = left->grid;
switch (op->args[0]) {
case 'n':
if (t.t.Len()) {
Expand Down Expand Up @@ -167,8 +167,8 @@ struct Evaluator {
if (!right) return left;
Text &t1 = left->text;
Text &t2 = right->text;
Grid *g1 = left->grid.get();
Grid *g2 = right->grid.get();
Grid *g1 = left->grid;
Grid *g2 = right->grid;
switch (op->args[0]) {
case 'n':
if (t1.t.Len() && t2.t.Len()) {
Expand Down
Loading

0 comments on commit f8edf8a

Please sign in to comment.