forked from KDE/okteta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbookmarklist.cpp
202 lines (167 loc) · 4.82 KB
/
bookmarklist.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
This file is part of the Okteta Core library, made within the KDE community.
SPDX-FileCopyrightText: 2007, 2009 Friedrich W. H. Kossebau <[email protected]>
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#include "bookmarklist.hpp"
// Qt
#include <QVector>
// Std
#include <utility>
namespace Okteta {
BookmarkList::BookmarkList() = default;
BookmarkList::~BookmarkList() = default;
void BookmarkList::addBookmark(const Bookmark& bookmark)
{
if (!bookmark.isValid()) {
return;
}
iterator B = begin();
for (; B != end(); ++B) {
// new bookmark before next bookmark?
if (bookmark.offset() < B->offset()) {
// put the new before it
insert(B, bookmark);
return;
}
// bookmark already present?
if (bookmark.offset() == B->offset()) {
*B = bookmark;
return;
}
}
// all others before the new?
if (B == end()) {
// add it at the end
append(bookmark);
}
}
void BookmarkList::addBookmarks(const QVector<Okteta::Bookmark>& bookmarks)
{
for (const Bookmark& bookmark : bookmarks) {
addBookmark(bookmark);
}
}
void BookmarkList::removeBookmark(const Bookmark& bookmark)
{
if (!bookmark.isValid()) {
return;
}
iterator B = begin();
for (; B != end(); ++B) {
if (bookmark.offset() == B->offset()) {
erase(B);
break;
}
}
}
void BookmarkList::removeBookmarks(const QVector<Okteta::Bookmark>& bookmarks)
{
for (const Bookmark& bookmark : bookmarks) {
removeBookmark(bookmark);
}
}
void BookmarkList::setBookmark(unsigned int index, const Bookmark& bookmark)
{
const Iterator endIt = end();
unsigned int i = 0;
for (Iterator it = begin(); it != endIt; ++it, ++i) {
if (i == index) {
*it = bookmark;
break;
}
}
}
bool BookmarkList::adjustToReplaced(Address offset, Size removedLength, Size insertedLength)
{
bool result = false;
iterator bIt = begin();
while (bIt != end() && bIt->offset() < offset) {
++bIt;
}
// remove bookmarks in removed section
while (bIt != end() && bIt->offset() < offset + removedLength) {
bIt = erase(bIt);
result = true;
}
// adjust bookmarks in moved section
const Size diff = insertedLength - removedLength;
if (diff != 0) {
for (; bIt != end(); ++bIt) {
(*bIt).move(diff);
result = true;
}
}
return result;
}
bool BookmarkList::adjustToSwapped(Address firstPartStart, Address secondPartStart, Size secondPartLength)
{
bool result = false;
iterator bIt = begin();
while (bIt != end() && bIt->offset() < firstPartStart) {
++bIt;
}
QVector<Okteta::Bookmark> bookmarksInFirstPart;
// take bookmarks from first part
while (bIt != end() && bIt->offset() < secondPartStart) {
bookmarksInFirstPart.append(*bIt);
bIt = erase(bIt);
}
// move bookmarks from second to first
const Size diff = firstPartStart - secondPartStart;
const Address behindLast = secondPartStart + secondPartLength;
for (; bIt != end() && bIt->offset() < behindLast; ++bIt) {
(*bIt).move(diff);
result = true;
}
// append bookmarks from first part as second
if (!bookmarksInFirstPart.isEmpty()) {
for (Bookmark bookmark : std::as_const(bookmarksInFirstPart)) {
bookmark.move(secondPartLength);
insert(bIt, bookmark);
}
result = true;
}
return result;
}
QVector<Okteta::Bookmark> BookmarkList::list() const
{
QVector<Okteta::Bookmark> result;
result.reserve(size());
for (const Bookmark& bookmark : *this) {
result.append(bookmark);
}
return result;
}
const Bookmark& BookmarkList::bookmark(Address offset) const
{
const ConstIterator endIt = end();
auto it = std::find_if(begin(), endIt, [offset](const Bookmark& bookmark) {
return (bookmark.offset() == offset);
});
if (it != endIt) {
return *it;
}
static const Bookmark* const noBookmark = nullptr;
return (const Bookmark&)*noBookmark;
}
bool BookmarkList::contains(Address offset) const
{
return std::any_of(begin(), end(), [offset](const Bookmark& bookmark) {
return (bookmark.offset() == offset);
});
}
const Bookmark& BookmarkList::at(unsigned int index) const
{
Q_ASSERT_X((int)index < size(), "BookmarkList::at", "index out of range");
const ConstIterator endIt = end();
unsigned int i = 0;
for (ConstIterator it = begin(); it != endIt; ++it, ++i) {
if (i == index) {
return *it;
}
}
static const Bookmark* const noBookmark = nullptr;
return (const Bookmark&)*noBookmark;
}
}