1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Refactored sparse vector to use unique_ptr

This commit is contained in:
Laurent Cozic 2017-01-13 22:05:35 +01:00
parent 91e74f024e
commit 41d44a3d5c
6 changed files with 45 additions and 40 deletions

View File

@ -25,8 +25,8 @@ int Folder::noteCount() const {
return q.value(0).toInt(); return q.value(0).toInt();
} }
QVector<Note> Folder::notes(const QString &orderBy, int limit, int offset) const { std::vector<std::unique_ptr<Note>> Folder::notes(const QString &orderBy, int limit, int offset) const {
QVector<Note> output; std::vector<std::unique_ptr<Note>> output;
QSqlQuery q = jop::db().prepare(QString("SELECT %1 FROM %2 WHERE parent_id = :parent_id ORDER BY %3 LIMIT %4 OFFSET %5") QSqlQuery q = jop::db().prepare(QString("SELECT %1 FROM %2 WHERE parent_id = :parent_id ORDER BY %3 LIMIT %4 OFFSET %5")
.arg(BaseModel::sqlTableFields(jop::NotesTable)) .arg(BaseModel::sqlTableFields(jop::NotesTable))
@ -39,9 +39,9 @@ QVector<Note> Folder::notes(const QString &orderBy, int limit, int offset) const
if (!jop::db().errorCheck(q)) return output; if (!jop::db().errorCheck(q)) return output;
while (q.next()) { while (q.next()) {
Note note; std::unique_ptr<Note> note(new Note());
note.loadSqlQuery(q); note->loadSqlQuery(q);
output.push_back(note); output.push_back(std::move(note));
} }
return output; return output;

View File

@ -20,7 +20,7 @@ public:
bool primaryKeyIsUuid() const; bool primaryKeyIsUuid() const;
bool trackChanges() const; bool trackChanges() const;
int noteCount() const; int noteCount() const;
QVector<Note> notes(const QString& orderBy, int limit, int offset) const; std::vector<std::unique_ptr<Note> > notes(const QString& orderBy, int limit, int offset) const;
int noteIndexById(const QString& orderBy, const QString &id) const; int noteIndexById(const QString& orderBy, const QString &id) const;
}; };

View File

@ -16,7 +16,6 @@ class FolderModel : public AbstractListModel {
public: public:
FolderModel(); FolderModel();
void addFolder(Folder* folder); void addFolder(Folder* folder);
BaseModel* atIndex(int index) const; BaseModel* atIndex(int index) const;
@ -35,16 +34,12 @@ private:
QString orderBy_; QString orderBy_;
mutable std::vector<std::unique_ptr<Folder>> cache_; mutable std::vector<std::unique_ptr<Folder>> cache_;
//QString lastInsertId_;
public slots: public slots:
void addData(const QString& title); void addData(const QString& title);
void deleteData(const int index); void deleteData(const int index);
bool setTitle(int index, const QVariant &value, int role = Qt::EditRole); bool setTitle(int index, const QVariant &value, int role = Qt::EditRole);
//QString indexToId(int index) const;
int idToIndex(const QString& id) const; int idToIndex(const QString& id) const;
//QString lastInsertId() const;
void dispatcher_folderCreated(const QString& folderId); void dispatcher_folderCreated(const QString& folderId);
void dispatcher_folderUpdated(const QString& folderId); void dispatcher_folderUpdated(const QString& folderId);

View File

@ -1,6 +1,6 @@
#include "notemodel.h" #include "notemodel.h"
jop::NoteModel::NoteModel() { jop::NoteModel::NoteModel() : QAbstractListModel() {
folderId_ = ""; folderId_ = "";
orderBy_ = "title"; orderBy_ = "title";
} }
@ -11,25 +11,26 @@ int jop::NoteModel::rowCount(const QModelIndex &parent) const {
} }
QVariant jop::NoteModel::data(const QModelIndex &index, int role) const { QVariant jop::NoteModel::data(const QModelIndex &index, int role) const {
Note note = atIndex(index.row()); Note* note = atIndex(index.row());
if (!note) return "";
if (role == IdRole) { if (role == IdRole) {
return QVariant(note.id().toString()); return QVariant(note->idString());
} }
return QVariant(note.value("title").toString()); return QVariant(note->value("title").toString());
} }
jop::Note jop::NoteModel::atIndex(int index) const { jop::Note *jop::NoteModel::atIndex(int index) const {
if (folderId_ == "") return Note(); if (folderId_ == "") return NULL;
if (index < 0 || index >= rowCount()) return Note(); if (index < 0 || index >= rowCount()) return NULL;
if (cache_.isset(index)) return cache_.get(index); if (cache_.isset(index)) return cache_.get(index);
std::vector<int> indexes = cache_.availableBufferAround(index, 32); std::vector<int> indexes = cache_.availableBufferAround(index, 32);
if (!indexes.size()) { if (!indexes.size()) {
qWarning() << "Couldn't acquire buffer"; // "Cannot happen" qCritical() << "Couldn't acquire buffer"; // "Cannot happen"
return Note(); return NULL;
} }
int from = indexes[0]; int from = indexes[0];
@ -37,10 +38,10 @@ jop::Note jop::NoteModel::atIndex(int index) const {
Folder folder = this->folder(); Folder folder = this->folder();
QVector<Note> notes = folder.notes(orderBy_, to - from + 1, from); std::vector<std::unique_ptr<Note>> notes = folder.notes(orderBy_, to - from + 1, from);
int noteIndex = from; int noteIndex = from;
for (int i = 0; i < notes.size(); i++) { for (int i = 0; i < notes.size(); i++) {
cache_.set(noteIndex, notes[i]); cache_.set(noteIndex, notes[i].release());
noteIndex++; noteIndex++;
} }
@ -63,8 +64,8 @@ jop::Folder jop::NoteModel::folder() const {
} }
QString jop::NoteModel::indexToId(int index) const { QString jop::NoteModel::indexToId(int index) const {
Note note = atIndex(index); Note* note = atIndex(index);
return note.idString(); return note->idString();
} }
int jop::NoteModel::idToIndex(const QString &id) const { int jop::NoteModel::idToIndex(const QString &id) const {
@ -79,3 +80,7 @@ QHash<int, QByteArray> jop::NoteModel::roleNames() const {
return roles; return roles;
} }
//int jop::NoteModel::baseModelCount() const {
//}

View File

@ -5,6 +5,7 @@
#include "models/folder.h" #include "models/folder.h"
#include "sparsevector.hpp" #include "sparsevector.hpp"
#include "models/abstractlistmodel.h"
namespace jop { namespace jop {
@ -14,15 +15,16 @@ class NoteModel : public QAbstractListModel {
public: public:
enum NoteRoles { enum ModelRoles {
IdRole = Qt::UserRole + 1, IdRole = Qt::UserRole + 1,
TitleRole TitleRole,
RawRole
}; };
NoteModel(); NoteModel();
int rowCount(const QModelIndex & parent = QModelIndex()) const; int rowCount(const QModelIndex & parent = QModelIndex()) const;
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const; QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
Note atIndex(int index) const; Note* atIndex(int index) const;
void setFolderId(const QString& v); void setFolderId(const QString& v);
Folder folder() const; Folder folder() const;
@ -34,6 +36,7 @@ public slots:
protected: protected:
QHash<int, QByteArray> roleNames() const; QHash<int, QByteArray> roleNames() const;
int baseModelCount() const;
private: private:

View File

@ -2,6 +2,7 @@
#define SPARSEARRAY_HPP #define SPARSEARRAY_HPP
#include <algorithm> #include <algorithm>
#include <memory>
#include <iostream> #include <iostream>
#include <map> #include <map>
#include <vector> #include <vector>
@ -16,21 +17,22 @@ public:
count_ = -1; count_ = -1;
} }
ClassType get(int index) const { ClassType* get(int index) const {
if (index > count()) return ClassType(); if (index > count()) return NULL;
if (!isset(index)) return ClassType(); if (!isset(index)) return NULL;
typename IndexMap::const_iterator pos = indexes_.find(index); typename IndexMap::const_iterator pos = indexes_.find(index);
int valueIndex = pos->second.valueIndex; int valueIndex = pos->second.valueIndex;
typename std::map<int, ClassType>::const_iterator pos2 = values_.find(valueIndex); typename std::map<int, std::unique_ptr<ClassType>>::const_iterator pos2 = values_.find(valueIndex);
return pos2->second; return pos2->second.get();
} }
void set(int index, ClassType value) { void set(int index, ClassType* value) {
unset(index); unset(index);
int valueIndex = ++counter_; int valueIndex = ++counter_;
values_[valueIndex] = value; std::unique_ptr<ClassType> ptr(value);
values_[valueIndex] = std::move(ptr);
IndexRecord r; IndexRecord r;
r.valueIndex = valueIndex; r.valueIndex = valueIndex;
r.time = 0; // Disabled / not needed r.time = 0; // Disabled / not needed
@ -39,7 +41,7 @@ public:
count_ = -1; count_ = -1;
} }
void push(ClassType value) { void push(const ClassType& value) {
set(count(), value); set(count(), value);
} }
@ -63,10 +65,10 @@ public:
indexes_.erase(index); indexes_.erase(index);
} }
void insert(int index, ClassType value) { void insert(int index, const ClassType& value) {
IndexMap newIndexes; IndexMap newIndexes;
for (typename IndexMap::const_iterator it = indexes_.begin(); it != indexes_.end(); ++it) { for (typename IndexMap::const_iterator it = indexes_.begin(); it != indexes_.end(); ++it) {
ClassType key = it->first; int key = it->first;
if (key > index) key++; if (key > index) key++;
newIndexes[key] = it->second; newIndexes[key] = it->second;
} }
@ -85,7 +87,7 @@ public:
IndexMap newIndexes; IndexMap newIndexes;
for (typename IndexMap::const_iterator it = indexes_.begin(); it != indexes_.end(); ++it) { for (typename IndexMap::const_iterator it = indexes_.begin(); it != indexes_.end(); ++it) {
ClassType key = it->first; int key = it->first;
if (key == index) continue; if (key == index) continue;
if (key > index) key--; if (key > index) key--;
newIndexes[key] = it->second; newIndexes[key] = it->second;
@ -201,7 +203,7 @@ private:
int counter_; int counter_;
IndexMap indexes_; IndexMap indexes_;
std::map<int, ClassType> values_; std::map<int, std::unique_ptr<ClassType>> values_;
// This is used to cache the result of ::count(). // This is used to cache the result of ::count().
// Don't forget to set it to -1 whenever the list // Don't forget to set it to -1 whenever the list