diff options
author | Neil <nyamatongwe@gmail.com> | 2017-05-21 23:22:45 +1000 |
---|---|---|
committer | Neil <nyamatongwe@gmail.com> | 2017-05-21 23:22:45 +1000 |
commit | f5709222a78870805711812cbfd6a65bbd4daf48 (patch) | |
tree | 4434a7a6ab30713a5ff7c2964c86ea017b764e24 /src | |
parent | 3debda34b8aa21a770142a055b8fa7e4a8a7355f (diff) | |
download | scintilla-mirror-f5709222a78870805711812cbfd6a65bbd4daf48.tar.gz |
Use unique_ptr and forward_list to regularize PerLine data structures.
Diffstat (limited to 'src')
-rw-r--r-- | src/Document.cxx | 1 | ||||
-rw-r--r-- | src/EditView.cxx | 1 | ||||
-rw-r--r-- | src/Editor.cxx | 1 | ||||
-rw-r--r-- | src/PerLine.cxx | 182 | ||||
-rw-r--r-- | src/PerLine.h | 12 |
5 files changed, 67 insertions, 130 deletions
diff --git a/src/Document.cxx b/src/Document.cxx index 0e315bdb9..231bc1113 100644 --- a/src/Document.cxx +++ b/src/Document.cxx @@ -14,6 +14,7 @@ #include <stdexcept> #include <string> #include <vector> +#include <forward_list> #include <algorithm> #include <memory> diff --git a/src/EditView.cxx b/src/EditView.cxx index 089862515..b3815ff2e 100644 --- a/src/EditView.cxx +++ b/src/EditView.cxx @@ -17,6 +17,7 @@ #include <string> #include <vector> #include <map> +#include <forward_list> #include <algorithm> #include <memory> diff --git a/src/Editor.cxx b/src/Editor.cxx index 2ad440db3..1dfb8d9e3 100644 --- a/src/Editor.cxx +++ b/src/Editor.cxx @@ -17,6 +17,7 @@ #include <string> #include <vector> #include <map> +#include <forward_list> #include <algorithm> #include <memory> diff --git a/src/PerLine.cxx b/src/PerLine.cxx index 9634722e2..6eb37ad1a 100644 --- a/src/PerLine.cxx +++ b/src/PerLine.cxx @@ -6,10 +6,12 @@ // The License.txt file describes the conditions under which this software may be distributed. #include <cstddef> +#include <cassert> #include <cstring> #include <stdexcept> #include <vector> +#include <forward_list> #include <algorithm> #include <memory> @@ -27,113 +29,63 @@ using namespace Scintilla; #endif MarkerHandleSet::MarkerHandleSet() { - root = 0; } MarkerHandleSet::~MarkerHandleSet() { - MarkerHandleNumber *mhn = root; - while (mhn) { - MarkerHandleNumber *mhnToFree = mhn; - mhn = mhn->next; - delete mhnToFree; - } - root = 0; + mhList.clear(); } -int MarkerHandleSet::Length() const { - int c = 0; - MarkerHandleNumber *mhn = root; - while (mhn) { - c++; - mhn = mhn->next; - } - return c; +bool MarkerHandleSet::Empty() const { + return mhList.empty(); } int MarkerHandleSet::MarkValue() const { unsigned int m = 0; - MarkerHandleNumber *mhn = root; - while (mhn) { - m |= (1 << mhn->number); - mhn = mhn->next; + for (const MarkerHandleNumber &mhn : mhList) { + m |= (1 << mhn.number); } return m; } bool MarkerHandleSet::Contains(int handle) const { - MarkerHandleNumber *mhn = root; - while (mhn) { - if (mhn->handle == handle) { + for (const MarkerHandleNumber &mhn : mhList) { + if (mhn.handle == handle) { return true; } - mhn = mhn->next; } return false; } bool MarkerHandleSet::InsertHandle(int handle, int markerNum) { - MarkerHandleNumber *mhn = new MarkerHandleNumber; - mhn->handle = handle; - mhn->number = markerNum; - mhn->next = root; - root = mhn; + mhList.push_front(MarkerHandleNumber(handle, markerNum)); return true; } void MarkerHandleSet::RemoveHandle(int handle) { - MarkerHandleNumber **pmhn = &root; - while (*pmhn) { - MarkerHandleNumber *mhn = *pmhn; - if (mhn->handle == handle) { - *pmhn = mhn->next; - delete mhn; - return; - } - pmhn = &((*pmhn)->next); - } + mhList.remove_if([=](const MarkerHandleNumber &mhn) { return mhn.handle == handle; }); } bool MarkerHandleSet::RemoveNumber(int markerNum, bool all) { bool performedDeletion = false; - MarkerHandleNumber **pmhn = &root; - while (*pmhn) { - MarkerHandleNumber *mhn = *pmhn; - if (mhn->number == markerNum) { - *pmhn = mhn->next; - delete mhn; + mhList.remove_if([&](const MarkerHandleNumber &mhn) { + if ((all || !performedDeletion) && (mhn.number == markerNum)) { performedDeletion = true; - if (!all) - break; - } else { - pmhn = &((*pmhn)->next); + return true; } - } + return false; + }); return performedDeletion; } void MarkerHandleSet::CombineWith(MarkerHandleSet *other) { - MarkerHandleNumber **pmhn = &other->root; - while (*pmhn) { - pmhn = &((*pmhn)->next); - } - *pmhn = root; - root = other->root; - other->root = 0; + mhList.splice_after(mhList.cbegin(), other->mhList); } LineMarkers::~LineMarkers() { - for (int line = 0; line < markers.Length(); line++) { - delete markers[line]; - markers[line] = 0; - } markers.DeleteAll(); } void LineMarkers::Init() { - for (int line = 0; line < markers.Length(); line++) { - delete markers[line]; - markers[line] = 0; - } markers.DeleteAll(); } @@ -167,12 +119,11 @@ Sci::Line LineMarkers::LineFromHandle(int markerHandle) { } void LineMarkers::MergeMarkers(Sci::Line line) { - if (markers[line + 1] != NULL) { - if (markers[line] == NULL) - markers[line] = new MarkerHandleSet; - markers[line]->CombineWith(markers[line + 1]); - delete markers[line + 1]; - markers[line + 1] = NULL; + if (markers[line + 1]) { + if (!markers[line]) + markers[line].reset(new MarkerHandleSet); + markers[line]->CombineWith(markers[line + 1].get()); + markers[line + 1].reset(); } } @@ -188,9 +139,8 @@ Sci::Line LineMarkers::MarkerNext(Sci::Line lineStart, int mask) const { lineStart = 0; const Sci::Line length = markers.Length(); for (Sci::Line iLine = lineStart; iLine < length; iLine++) { - const MarkerHandleSet *onLine = markers[iLine]; + const MarkerHandleSet *onLine = markers[iLine].get(); if (onLine && ((onLine->MarkValue() & mask) != 0)) - //if ((pdoc->GetMark(iLine) & lParam) != 0) return iLine; } return -1; @@ -200,14 +150,14 @@ int LineMarkers::AddMark(Sci::Line line, int markerNum, Sci::Line lines) { handleCurrent++; if (!markers.Length()) { // No existing markers so allocate one element per line - markers.InsertValue(0, lines, 0); + markers.InsertEmpty(0, lines); } if (line >= markers.Length()) { return -1; } if (!markers[line]) { // Need new structure to hold marker handle - markers[line] = new MarkerHandleSet(); + markers[line].reset(new MarkerHandleSet()); } markers[line]->InsertHandle(handleCurrent, markerNum); @@ -219,13 +169,11 @@ bool LineMarkers::DeleteMark(Sci::Line line, int markerNum, bool all) { if (markers.Length() && (line >= 0) && (line < markers.Length()) && markers[line]) { if (markerNum == -1) { someChanges = true; - delete markers[line]; - markers[line] = NULL; + markers[line].reset(); } else { someChanges = markers[line]->RemoveNumber(markerNum, all); - if (markers[line]->Length() == 0) { - delete markers[line]; - markers[line] = NULL; + if (markers[line]->Empty()) { + markers[line].reset(); } } } @@ -236,9 +184,8 @@ void LineMarkers::DeleteMarkFromHandle(int markerHandle) { Sci::Line line = LineFromHandle(markerHandle); if (line >= 0) { markers[line]->RemoveHandle(markerHandle); - if (markers[line]->Length() == 0) { - delete markers[line]; - markers[line] = NULL; + if (markers[line]->Empty()) { + markers[line].reset(); } } } @@ -375,41 +322,41 @@ void LineAnnotation::Init() { void LineAnnotation::InsertLine(Sci::Line line) { if (annotations.Length()) { annotations.EnsureLength(line); - annotations.Insert(line, 0); + annotations.Insert(line, std::unique_ptr<char []>()); } } void LineAnnotation::RemoveLine(Sci::Line line) { if (annotations.Length() && (line > 0) && (line <= annotations.Length())) { - delete []annotations[line-1]; + annotations[line-1].reset(); annotations.Delete(line-1); } } bool LineAnnotation::MultipleStyles(Sci::Line line) const { if (annotations.Length() && (line >= 0) && (line < annotations.Length()) && annotations[line]) - return reinterpret_cast<AnnotationHeader *>(annotations[line])->style == IndividualStyles; + return reinterpret_cast<AnnotationHeader *>(annotations[line].get())->style == IndividualStyles; else return false; } int LineAnnotation::Style(Sci::Line line) const { if (annotations.Length() && (line >= 0) && (line < annotations.Length()) && annotations[line]) - return reinterpret_cast<AnnotationHeader *>(annotations[line])->style; + return reinterpret_cast<AnnotationHeader *>(annotations[line].get())->style; else return 0; } const char *LineAnnotation::Text(Sci::Line line) const { if (annotations.Length() && (line >= 0) && (line < annotations.Length()) && annotations[line]) - return annotations[line]+sizeof(AnnotationHeader); + return annotations[line].get()+sizeof(AnnotationHeader); else return 0; } const unsigned char *LineAnnotation::Styles(Sci::Line line) const { if (annotations.Length() && (line >= 0) && (line < annotations.Length()) && annotations[line] && MultipleStyles(line)) - return reinterpret_cast<unsigned char *>(annotations[line] + sizeof(AnnotationHeader) + Length(line)); + return reinterpret_cast<unsigned char *>(annotations[line].get() + sizeof(AnnotationHeader) + Length(line)); else return 0; } @@ -424,107 +371,94 @@ void LineAnnotation::SetText(Sci::Line line, const char *text) { if (text && (line >= 0)) { annotations.EnsureLength(line+1); const int style = Style(line); - if (annotations[line]) { - delete []annotations[line]; - } - annotations[line] = AllocateAnnotation(static_cast<int>(strlen(text)), style); - AnnotationHeader *pah = reinterpret_cast<AnnotationHeader *>(annotations[line]); + annotations[line].reset(AllocateAnnotation(static_cast<int>(strlen(text)), style)); + char *pa = annotations[line].get(); + assert(pa); + AnnotationHeader *pah = reinterpret_cast<AnnotationHeader *>(pa); pah->style = static_cast<short>(style); pah->length = static_cast<int>(strlen(text)); pah->lines = static_cast<short>(NumberLines(text)); - memcpy(annotations[line]+sizeof(AnnotationHeader), text, pah->length); + memcpy(pa+sizeof(AnnotationHeader), text, pah->length); } else { if (annotations.Length() && (line >= 0) && (line < annotations.Length()) && annotations[line]) { - delete []annotations[line]; - annotations[line] = 0; + annotations[line].reset(); } } } void LineAnnotation::ClearAll() { - for (int line = 0; line < annotations.Length(); line++) { - delete []annotations[line]; - annotations[line] = 0; - } annotations.DeleteAll(); } void LineAnnotation::SetStyle(Sci::Line line, int style) { annotations.EnsureLength(line+1); if (!annotations[line]) { - annotations[line] = AllocateAnnotation(0, style); + annotations[line].reset(AllocateAnnotation(0, style)); } - reinterpret_cast<AnnotationHeader *>(annotations[line])->style = static_cast<short>(style); + reinterpret_cast<AnnotationHeader *>(annotations[line].get())->style = static_cast<short>(style); } void LineAnnotation::SetStyles(Sci::Line line, const unsigned char *styles) { if (line >= 0) { annotations.EnsureLength(line+1); if (!annotations[line]) { - annotations[line] = AllocateAnnotation(0, IndividualStyles); + annotations[line].reset(AllocateAnnotation(0, IndividualStyles)); } else { - AnnotationHeader *pahSource = reinterpret_cast<AnnotationHeader *>(annotations[line]); + AnnotationHeader *pahSource = reinterpret_cast<AnnotationHeader *>(annotations[line].get()); if (pahSource->style != IndividualStyles) { char *allocation = AllocateAnnotation(pahSource->length, IndividualStyles); AnnotationHeader *pahAlloc = reinterpret_cast<AnnotationHeader *>(allocation); pahAlloc->length = pahSource->length; pahAlloc->lines = pahSource->lines; - memcpy(allocation + sizeof(AnnotationHeader), annotations[line] + sizeof(AnnotationHeader), pahSource->length); - delete []annotations[line]; - annotations[line] = allocation; + memcpy(allocation + sizeof(AnnotationHeader), annotations[line].get() + sizeof(AnnotationHeader), pahSource->length); + annotations[line].reset(allocation); } } - AnnotationHeader *pah = reinterpret_cast<AnnotationHeader *>(annotations[line]); + AnnotationHeader *pah = reinterpret_cast<AnnotationHeader *>(annotations[line].get()); pah->style = IndividualStyles; - memcpy(annotations[line] + sizeof(AnnotationHeader) + pah->length, styles, pah->length); + memcpy(annotations[line].get() + sizeof(AnnotationHeader) + pah->length, styles, pah->length); } } int LineAnnotation::Length(Sci::Line line) const { if (annotations.Length() && (line >= 0) && (line < annotations.Length()) && annotations[line]) - return reinterpret_cast<AnnotationHeader *>(annotations[line])->length; + return reinterpret_cast<AnnotationHeader *>(annotations[line].get())->length; else return 0; } int LineAnnotation::Lines(Sci::Line line) const { if (annotations.Length() && (line >= 0) && (line < annotations.Length()) && annotations[line]) - return reinterpret_cast<AnnotationHeader *>(annotations[line])->lines; + return reinterpret_cast<AnnotationHeader *>(annotations[line].get())->lines; else return 0; } LineTabstops::~LineTabstops() { - for (int line = 0; line < tabstops.Length(); line++) { - delete tabstops[line]; - } tabstops.DeleteAll(); } void LineTabstops::Init() { - for (int line = 0; line < tabstops.Length(); line++) { - delete tabstops[line]; - } tabstops.DeleteAll(); } void LineTabstops::InsertLine(Sci::Line line) { if (tabstops.Length()) { tabstops.EnsureLength(line); - tabstops.Insert(line, 0); + tabstops.Insert(line, nullptr); } } void LineTabstops::RemoveLine(Sci::Line line) { if (tabstops.Length() > line) { - delete tabstops[line]; + tabstops[line].reset(); tabstops.Delete(line); } } bool LineTabstops::ClearTabstops(Sci::Line line) { if (line < tabstops.Length()) { - TabstopList *tl = tabstops[line]; + TabstopList *tl = tabstops[line].get(); if (tl) { tl->clear(); return true; @@ -536,10 +470,10 @@ bool LineTabstops::ClearTabstops(Sci::Line line) { bool LineTabstops::AddTabstop(Sci::Line line, int x) { tabstops.EnsureLength(line + 1); if (!tabstops[line]) { - tabstops[line] = new TabstopList(); + tabstops[line].reset(new TabstopList()); } - TabstopList *tl = tabstops[line]; + TabstopList *tl = tabstops[line].get(); if (tl) { // tabstop positions are kept in order - insert in the right place std::vector<int>::iterator it = std::lower_bound(tl->begin(), tl->end(), x); @@ -554,7 +488,7 @@ bool LineTabstops::AddTabstop(Sci::Line line, int x) { int LineTabstops::GetNextTabstop(Sci::Line line, int x) const { if (line < tabstops.Length()) { - TabstopList *tl = tabstops[line]; + TabstopList *tl = tabstops[line].get(); if (tl) { for (const int i : *tl) { if (i > x) { diff --git a/src/PerLine.h b/src/PerLine.h index 300a50a86..bd97e53a6 100644 --- a/src/PerLine.h +++ b/src/PerLine.h @@ -19,14 +19,14 @@ namespace Scintilla { struct MarkerHandleNumber { int handle; int number; - MarkerHandleNumber *next; + MarkerHandleNumber(int handle_, int number_) : handle(handle_), number(number_) {} }; /** * A marker handle set contains any number of MarkerHandleNumbers. */ class MarkerHandleSet { - MarkerHandleNumber *root; + std::forward_list<MarkerHandleNumber> mhList; public: MarkerHandleSet(); @@ -34,7 +34,7 @@ public: MarkerHandleSet(const MarkerHandleSet &) = delete; void operator=(const MarkerHandleSet &) = delete; ~MarkerHandleSet(); - int Length() const; + bool Empty() const; int MarkValue() const; ///< Bit set of marker numbers. bool Contains(int handle) const; bool InsertHandle(int handle, int markerNum); @@ -44,7 +44,7 @@ public: }; class LineMarkers : public PerLine { - SplitVector<MarkerHandleSet *> markers; + SplitVector<std::unique_ptr<MarkerHandleSet>> markers; /// Handles are allocated sequentially and should never have to be reused as 32 bit ints are very big. int handleCurrent; public: @@ -105,7 +105,7 @@ public: }; class LineAnnotation : public PerLine { - SplitVector<char *> annotations; + SplitVector<std::unique_ptr<char []>> annotations; public: LineAnnotation() { } @@ -132,7 +132,7 @@ public: typedef std::vector<int> TabstopList; class LineTabstops : public PerLine { - SplitVector<TabstopList *> tabstops; + SplitVector<std::unique_ptr<TabstopList>> tabstops; public: LineTabstops() { } |