[sequence] SequenceStore returns Sequence * instead of

ExpiringPointer<Sequence>

SequenceStore keep all its Sequences in an array which fix Sequence
addresses
This commit is contained in:
Émilie Feral
2019-02-28 16:43:15 +01:00
parent 2bc2506b60
commit cff3d56f22
11 changed files with 34 additions and 37 deletions

View File

@@ -32,7 +32,7 @@ float GraphController::interestingXMin() const {
int nmin = INT_MAX;
int nbOfActiveModels = functionStore()->numberOfActiveFunctions();
for (int i = 0; i < nbOfActiveModels; i++) {
ExpiringPointer<Sequence> s = functionStore()->modelForRecord(functionStore()->activeRecordAtIndex(i));
Sequence * s = functionStore()->modelForRecord(functionStore()->activeRecordAtIndex(i));
nmin = minInt(nmin, s->initialRank());
}
assert(nmin < INT_MAX);
@@ -45,7 +45,7 @@ float GraphController::interestingXHalfRange() const {
int nmax = 0;
int nbOfActiveModels = functionStore()->numberOfActiveFunctions();
for (int i = 0; i < nbOfActiveModels; i++) {
ExpiringPointer<Sequence> s = functionStore()->modelForRecord(functionStore()->activeRecordAtIndex(i));
Sequence * s = functionStore()->modelForRecord(functionStore()->activeRecordAtIndex(i));
int firstInterestingIndex = s->initialRank();
nmin = minInt(nmin, firstInterestingIndex);
nmax = maxInt(nmax, firstInterestingIndex + standardRange);
@@ -75,7 +75,7 @@ bool GraphController::moveCursorHorizontally(int direction) {
if (x < 0.0) {
return false;
}
ExpiringPointer<Sequence> s = functionStore()->modelForRecord(functionStore()->activeRecordAtIndex(indexFunctionSelectedByCursor()));
Sequence * s = functionStore()->modelForRecord(functionStore()->activeRecordAtIndex(indexFunctionSelectedByCursor()));
TextFieldDelegateApp * myApp = (TextFieldDelegateApp *)app();
double y = s->evaluateAtAbscissa(x, myApp->localContext());
m_cursor->moveTo(x, y);

View File

@@ -16,7 +16,7 @@ void GraphView::drawRect(KDContext * ctx, KDRect rect) const {
StorageFunctionGraphView::drawRect(ctx, rect);
for (int i = 0; i < m_sequenceStore->numberOfActiveFunctions(); i++) {
Ion::Storage::Record record = m_sequenceStore->activeRecordAtIndex(i);
ExpiringPointer<Sequence> s = m_sequenceStore->modelForRecord(record);;
Sequence * s = m_sequenceStore->modelForRecord(record);;
float rectXMin = pixelToFloat(Axis::Horizontal, rect.left() - k_externRectMargin);
rectXMin = rectXMin < 0 ? 0 : rectXMin;
float rectXMax = pixelToFloat(Axis::Horizontal, rect.right() + k_externRectMargin);

View File

@@ -49,7 +49,7 @@ double TermSumController::cursorNextStep(double x, int direction) {
}
Layout TermSumController::createFunctionLayout(Shared::ExpiringPointer<Shared::StorageFunction> function) {
Shared::ExpiringPointer<Sequence> sequence = static_cast<Shared::ExpiringPointer<Sequence>>(function);
Sequence * sequence = static_cast<Sequence *>(function.pointer());
return sequence->nameLayout();
}

View File

@@ -30,7 +30,7 @@ const char * ListController::title() {
int ListController::numberOfExpressionRows() {
int numberOfRows = 0;
for (int i = 0; i < modelStore()->numberOfModels(); i++) {
ExpiringPointer<Sequence> sequence = modelStore()->modelForRecord(modelStore()->recordAtIndex(i));
Sequence * sequence = modelStore()->modelForRecord(modelStore()->recordAtIndex(i));
numberOfRows += sequence->numberOfElements();
}
if (modelStore()->numberOfModels() == modelStore()->maxNumberOfModels()) {
@@ -45,7 +45,7 @@ KDCoordinate ListController::expressionRowHeight(int j) {
// Add sequence row
return defaultHeight;
}
ExpiringPointer<Sequence> sequence = modelStore()->modelForRecord(modelStore()->recordAtIndex(modelIndexForRow(j)));
Sequence * sequence = modelStore()->modelForRecord(modelStore()->recordAtIndex(modelIndexForRow(j)));
Layout layout = sequence->layout();
if (sequenceDefinitionForRow(j) == 1) {
layout = sequence->firstInitialConditionLayout();
@@ -70,7 +70,7 @@ Toolbox * ListController::toolboxForInputEventHandler(InputEventHandler * textIn
// Set extra cells
int recurrenceDepth = -1;
int sequenceDefinition = sequenceDefinitionForRow(selectedRow());
ExpiringPointer<Sequence> sequence = modelStore()->modelForRecord(modelStore()->recordAtIndex(modelIndexForRow(selectedRow())));
Sequence * sequence = modelStore()->modelForRecord(modelStore()->recordAtIndex(modelIndexForRow(selectedRow())));
if (sequenceDefinition == 0) {
recurrenceDepth = sequence->numberOfElements()-1;
}
@@ -88,7 +88,7 @@ void ListController::selectPreviousNewSequenceCell() {
void ListController::editExpression(int sequenceDefinition, Ion::Events::Event event) {
Ion::Storage::Record record = modelStore()->recordAtIndex(modelIndexForRow(selectedRow()));
ExpiringPointer<Sequence> sequence = modelStore()->modelForRecord(record);
Sequence * sequence = modelStore()->modelForRecord(record);
char * initialText = nullptr;
char initialTextContent[TextField::maxBufferSize()];
if (event == Ion::Events::OK || event == Ion::Events::EXE) {
@@ -152,7 +152,7 @@ bool ListController::editInitialConditionOfSelectedRecordWithText(const char * t
// Reset memoization of the selected cell which always corresponds to the k_memoizedCellsCount/2 memoized cell
resetMemoizationForIndex(k_memoizedCellsCount/2);
Ion::Storage::Record record = modelStore()->recordAtIndex(modelIndexForRow(selectedRow()));
ExpiringPointer<Sequence> sequence = modelStore()->modelForRecord(record);
Sequence * sequence = modelStore()->modelForRecord(record);
Ion::Storage::Record::ErrorStatus error = firstInitialCondition? sequence->setFirstInitialConditionContent(text) : sequence->setSecondInitialConditionContent(text);
return (error == Ion::Storage::Record::ErrorStatus::None);
}
@@ -195,7 +195,7 @@ void ListController::willDisplayTitleCellAtIndex(HighlightCell * cell, int j) {
myCell->setBaseline(baseline(j));
// Set the layout
Ion::Storage::Record record = modelStore()->recordAtIndex(modelIndexForRow(j));
ExpiringPointer<Sequence> sequence = modelStore()->modelForRecord(record);
Sequence * sequence = modelStore()->modelForRecord(record);
if (sequenceDefinitionForRow(j) == 0) {
myCell->setLayout(sequence->definitionName());
}
@@ -213,7 +213,7 @@ void ListController::willDisplayTitleCellAtIndex(HighlightCell * cell, int j) {
void ListController::willDisplayExpressionCellAtIndex(HighlightCell * cell, int j) {
FunctionExpressionCell * myCell = (FunctionExpressionCell *)cell;
Ion::Storage::Record record = modelStore()->recordAtIndex(modelIndexForRow(j));
ExpiringPointer<Sequence> sequence = modelStore()->modelForRecord(record);
Sequence * sequence = modelStore()->modelForRecord(record);
if (sequenceDefinitionForRow(j) == 0) {
myCell->setLayout(sequence->layout());
}
@@ -239,7 +239,7 @@ int ListController::modelIndexForRow(int j) {
int sequenceIndex = -1;
do {
sequenceIndex++;
ExpiringPointer<Sequence> sequence = modelStore()->modelForRecord(modelStore()->recordAtIndex(sequenceIndex));
Sequence * sequence = modelStore()->modelForRecord(modelStore()->recordAtIndex(sequenceIndex));
rowIndex += sequence->numberOfElements();
} while (rowIndex <= j);
return sequenceIndex;
@@ -258,13 +258,13 @@ int ListController::sequenceDefinitionForRow(int j) {
}
int rowIndex = 0;
int sequenceIndex = -1;
ExpiringPointer<Sequence> sequence(nullptr);
Sequence * sequence;
do {
sequenceIndex++;
sequence = modelStore()->modelForRecord(modelStore()->recordAtIndex(sequenceIndex));
rowIndex += sequence->numberOfElements();
} while (rowIndex <= j);
assert(!sequence.isNull());
assert(sequence);
return sequence->numberOfElements()-rowIndex+j;
}
@@ -279,7 +279,7 @@ void ListController::editExpression(Ion::Events::Event event) {
void ListController::reinitSelectedExpression(ExpiringPointer<SingleExpressionModelHandle> model) {
// Invalidate the sequences context cache
static_cast<App *>(app())->localContext()->resetCache();
ExpiringPointer<Sequence> sequence = static_cast<ExpiringPointer<Sequence>>(model);
Sequence * sequence = static_cast<Sequence *>(model.pointer());
switch (sequenceDefinitionForRow(selectedRow())) {
case 1:
if (sequence->firstInitialConditionExpressionClone().isUninitialized()) {

View File

@@ -30,7 +30,7 @@ private:
constexpr static int k_totalNumberOfCell = 4;
#endif
int totalNumberOfCells() const override;
Shared::ExpiringPointer<Sequence> sequence() { return static_cast<Shared::ExpiringPointer<Sequence>>(function()); }
Sequence * sequence() { return static_cast<Sequence *>(function().pointer()); }
bool hasInitialRankRow() const;
MessageTableCellWithChevronAndExpression m_typeCell;
MessageTableCellWithEditableText m_initialRankCell;

View File

@@ -79,7 +79,7 @@ bool TypeParameterController::handleEvent(Ion::Events::Event event) {
}
assert(error == Ion::Storage::Record::ErrorStatus::None);
Ion::Storage::Record record = sequenceStore()->recordAtIndex(sequenceStore()->numberOfModels()-1);
ExpiringPointer<Sequence> newSequence = sequenceStore()->modelForRecord(record);
Sequence * newSequence = sequenceStore()->modelForRecord(record);
newSequence->setType((Sequence::Type)selectedRow());
app()->dismissModalViewController();
m_listController->editExpression(0, Ion::Events::OK);

View File

@@ -28,7 +28,7 @@ public:
void setRecord(Ion::Storage::Record record);
private:
StackViewController * stackController() const;
Shared::ExpiringPointer<Sequence> sequence() {
Sequence * sequence() {
assert(!m_record.isNull());
return sequenceStore()->modelForRecord(m_record);
}

View File

@@ -50,22 +50,22 @@ void TemplatedSequenceContext<T>::step(SequenceStore * sequenceStore, SequenceCo
}
/* Evaluate new u(n) and v(n) */
ExpiringPointer<Sequence> u = sequenceStore->numberOfModels() > 0 ? sequenceStore->modelForRecord(sequenceStore->recordAtIndex(0)) : nullptr;
u = u.isNull() && u->isDefined() ? u : nullptr;
ExpiringPointer<Sequence> v = sequenceStore->numberOfModels() > 1 ? sequenceStore->modelForRecord(sequenceStore->recordAtIndex(1)) : nullptr;
v = v.isNull() && v->isDefined() ? v : nullptr;
Sequence * u = sequenceStore->numberOfModels() > 0 ? sequenceStore->modelForRecord(sequenceStore->recordAtIndex(0)) : nullptr;
u = u && u->isDefined() ? u : nullptr;
Sequence * v = sequenceStore->numberOfModels() > 1 ? sequenceStore->modelForRecord(sequenceStore->recordAtIndex(1)) : nullptr;
v = v && v->isDefined() ? v : nullptr;
/* Switch u & v if the name of u is v */
if (!u.isNull() && u->fullName()[0] == SequenceStore::k_sequenceNames[1][0]) {
ExpiringPointer<Sequence> temp = u;
if (u && u->fullName()[0] == SequenceStore::k_sequenceNames[1][0]) {
Sequence * temp = u;
u = v;
v = temp;
}
/* Approximate u & v at the new rank. We evaluate u twice in case its
* expression depends on v. */
m_values[0][0] = !u.isNull() ? u->approximateToNextRank<T>(m_rank, sqctx) : NAN;
m_values[1][0] = !v.isNull() ? v->approximateToNextRank<T>(m_rank, sqctx) : NAN;
m_values[0][0] = !u.isNull() ? u->approximateToNextRank<T>(m_rank, sqctx) : NAN;
m_values[0][0] = u ? u->approximateToNextRank<T>(m_rank, sqctx) : NAN;
m_values[1][0] = v ? v->approximateToNextRank<T>(m_rank, sqctx) : NAN;
m_values[0][0] = u ? u->approximateToNextRank<T>(m_rank, sqctx) : NAN;
}
template class TemplatedSequenceContext<float>;

View File

@@ -13,7 +13,10 @@ class SequenceStore : public Shared::StorageFunctionStore {
public:
using Shared::StorageFunctionStore::StorageFunctionStore;
char symbol() const override { return Sequence::Symbol(); }
Shared::ExpiringPointer<Sequence> modelForRecord(Ion::Storage::Record record) const { return Shared::ExpiringPointer<Sequence>(static_cast<Sequence *>(privateModelForRecord(record))); }
/* Sequence Store hold all its Sequences in an array. The Sequence pointers
* return by modelForRecord are therefore non-expirable. We choose to return
* Sequence * instead of ExpiringPointer<Sequence>. */
Sequence * modelForRecord(Ion::Storage::Record record) const { return static_cast<Sequence *>(privateModelForRecord(record)); }
Ion::Storage::Record::ErrorStatus addEmptyModel() override;
/* WARNING: after calling removeModel or removeAll, the sequence context
* need to invalidate its cache as the sequences evaluations might have

View File

@@ -31,7 +31,7 @@ void ValuesController::willDisplayCellAtLocation(HighlightCell * cell, int i, in
// The cell is a function title cell:
if (j == 0 && i > 0) {
SequenceTitleCell * myCell = (SequenceTitleCell *)cell;
Shared::ExpiringPointer<Sequence> sequence = functionStore()->modelForRecord(recordAtColumn(i));
Sequence * sequence = functionStore()->modelForRecord(recordAtColumn(i));
myCell->setLayout(sequence->nameLayout());
myCell->setColor(sequence->color());
}

View File

@@ -15,13 +15,7 @@ public:
s_global = rawPointer;
#endif
}
template<class U>
ExpiringPointer(ExpiringPointer<U> p) : m_rawPointer(static_cast<T *>(p.m_rawPointer)) {
#if DEBUG
s_global = m_rawPointer;
#endif
}
bool isNull() { return m_rawPointer == nullptr; }
T * pointer() { return m_rawPointer; }
T *operator->() {
#if DEBUG
assert(m_rawPointer != nullptr && m_rawPointer == s_global);