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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
/****************************************************************************
**
** Copyright (C) 2006-2009 fullmetalcoder <fullmetalcoder@hotmail.fr>
**
** This file is part of the Edyuk project <http://edyuk.org>
**
** This file may be used under the terms of the GNU General Public License
** version 3 as published by the Free Software Foundation and appearing in the
** file GPL.txt included in the packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
#include "qdocumentbuffer.h"
/*
Notes on design :
The idea is to fragment the storage to workaround the issue of Qt
containers alocation model (too much memory usage when number of items
grow) and provide faster modification of the content.
The number one goal is to keep lookup by index as fast possible to avoid
significant impact on document iteration/drawing speed
for such a storage to be useful the block size may not be fixed but instead
must be kept around an "average" value.
*/
QDocumentBuffer::QDocumentBuffer()
: m_safetyRoom(10), m_optimalSize(1000), m_forkThresold(1500), m_mergeThresold(100)
{
}
QDocumentBuffer::~QDocumentBuffer()
{
qDeleteAll(m_blocks);
}
// for loading
void QDocumentBuffer::appendLine(QDocumentLineHandle *l)
{
Block *b = m_blocks.last();
if ( b->size() >= m_optimalSize )
{
b = new Block();
m_blocks << b;
}
b->lines.append(l);
}
int QDocumentBuffer::blockForLine(int index) const
{
// TODO : binary search?
for ( int i = 0; i < m_blocks.count(); ++i )
{
if ( index < m_blocks.at(i)->end )
return i;
}
// too high...
return -1;
}
QDocumentLineHandle* QDocumentBuffer::at(int index) const
{
int blockIndex = blockForLine(index);
return blockIndex != -1 ? m_blocks.at(blockIndex)->at(index) : 0;
}
void QDocumentBuffer::insertLine(int index, QDocumentLineHandle *l)
{
int blockIndex = blockForLine(index);
if ( blockIndex == -1 )
{
qWarning("cannot insert line at pos %i", index);
return;
}
Block *b = m_blocks.at(blockIndex);
if ( (b->size() + 1) >= m_forkThresold )
{
// split block
int bounds = b->start + m_optimalSize;
Block *nb = new Block(bounds);
nb->insert(bounds, b->lines.constData() + m_optimalSize, b->size() - m_optimalSize);
nb->lines.append(l);
nb->end = bounds + nb->size();
m_blocks.insert(blockIndex + 1, nb);
b->lines.resize(m_optimalSize);
b->end = bounds;
blockIndex += 2;
} else {
b->insert(index, l);
}
// update block boundaries
while ( blockIndex < m_blocks.count() )
{
b = m_blocks.at(blockIndex);
++b->start;
++b->end;
}
}
void QDocumentBuffer::removeLine(int index)
{
int blockIndex = blockForLine(index);
if ( blockIndex == -1 )
{
qWarning("cannot remove line at pos %i", index);
return;
}
Block *b = m_blocks.at(blockIndex);
b->remove(index);
--b->end;
// if block too small, merge it with blocks around?
if ( !b->size() )
{
// remove empty block
m_blocks.remove(blockIndex);
delete b;
} else if ( b->size() < m_mergeThresold ) {
// "merge" block with its neighbors
int n = b->size();
n += qMin(1, n / m_safetyRoom);
int roomPrev = blockIndex ? m_forkThresold - m_blocks.at(blockIndex - 1)->size() : 0;
int roomNext = blockIndex + 1 < m_blocks.count() ? m_forkThresold - m_blocks.at(blockIndex + 1)->size() : 0;
bool maxPrev = false;
int maxRoom = 0, minRoom = 0;
Block *moreRoom = 0, *lessRoom = 0;
if ( roomPrev > roomNext )
{
maxPrev = true;
maxRoom = roomPrev;
moreRoom = m_blocks.at(blockIndex - 1);
minRoom = roomNext;
if ( roomNext )
lessRoom = m_blocks.at(blockIndex + 1);
} else {
maxRoom = roomNext;
if ( roomNext )
moreRoom = m_blocks.at(blockIndex + 1);
minRoom = roomPrev;
if ( roomPrev )
lessRoom = m_blocks.at(blockIndex - 1);
}
if ( maxRoom > n )
{
// put everything in one
moreRoom->lines << b->lines;
moreRoom->end += b->size();
m_blocks.remove(blockIndex);
delete b;
} else if ( (maxRoom + minRoom) > n ) {
// try to alloc evenly
int part = b->size() * maxRoom / (maxRoom + minRoom);
if ( maxPrev )
{
moreRoom->append(b->lines.constData(), part);
lessRoom->prepend(b->lines.constData() + part, b->size() - part);
} else {
moreRoom->prepend(b->lines.constData(), part);
lessRoom->append(b->lines.constData() + part, b->size() - part);
}
moreRoom->end += part;
lessRoom->end += b->size() - part;
m_blocks.remove(blockIndex);
delete b;
} else {
// cannot merge simply... let's forget about it for now as it is not vital
++blockIndex;
}
} else {
++blockIndex;
}
// update block boundaries
while ( blockIndex < m_blocks.count() )
{
b = m_blocks.at(blockIndex);
--b->start;
--b->end;
}
}
void QDocumentBuffer::insertLines(int after, const QVector<QDocumentLineHandle*>& l)
{
int index = after + 1;
int blockIndex = blockForLine(index);
if ( blockIndex == -1 )
{
qWarning("cannot insert line at pos %i", index);
return;
}
int n = l.count();
Block *b = m_blocks.at(blockIndex);
if ( (b->size() + 1) >= m_forkThresold )
{
// split block
int bounds = b->start + m_optimalSize;
Block *nb = new Block(bounds);
nb->insert(bounds, b->lines.constData() + m_optimalSize, b->size() - m_optimalSize);
nb->append(l.constData(), n);
nb->end = bounds + nb->size();
m_blocks.insert(blockIndex + 1, nb);
b->lines.resize(m_optimalSize);
b->end = bounds;
blockIndex += 2;
} else {
b->insert(index, l.constData(), n);
}
// update block boundaries
while ( blockIndex < m_blocks.count() )
{
b = m_blocks.at(blockIndex);
b->start += n;
b->end += n;
}
}
void QDocumentBuffer::removeLines(int after, int n)
{
int index = after + 1;
int blockIndex = blockForLine(index);
if ( blockIndex == -1 )
{
qWarning("cannot remove line at pos %i", index);
return;
}
// update block boundaries
int count = n;
Block *b = m_blocks.at(blockIndex);
while ( count > 0 )
{
int room = b->end - index;
int toRem = qMin(room, count);
b->remove(index, toRem);
b->end -= toRem;
count -= toRem;
if ( !b->size() )
{
m_blocks.remove(blockIndex);
delete b;
} else {
++blockIndex;
}
if ( blockIndex >= m_blocks.count() )
break;
b = m_blocks.at(blockIndex);
b->start -= toRem;
}
if ( index )
{
qWarning("Troubles in line removal");
}
if ( b->size() < m_mergeThresold )
{
// "merge" block with its neighbors
int sz = b->size();
sz += qMin(1, sz / m_safetyRoom);
int roomPrev = blockIndex ? m_forkThresold - m_blocks.at(blockIndex - 1)->size() : 0;
int roomNext = blockIndex + 1 < m_blocks.count() ? m_forkThresold - m_blocks.at(blockIndex + 1)->size() : 0;
bool maxPrev = false;
int maxRoom = 0, minRoom = 0;
Block *moreRoom = 0, *lessRoom = 0;
if ( roomPrev > roomNext )
{
maxPrev = true;
maxRoom = roomPrev;
moreRoom = m_blocks.at(blockIndex - 1);
minRoom = roomNext;
if ( roomNext )
lessRoom = m_blocks.at(blockIndex + 1);
} else {
maxRoom = roomNext;
if ( roomNext )
moreRoom = m_blocks.at(blockIndex + 1);
minRoom = roomPrev;
if ( roomPrev )
lessRoom = m_blocks.at(blockIndex - 1);
}
if ( maxRoom > sz )
{
// put everything in one
moreRoom->lines << b->lines;
moreRoom->end += b->size();
m_blocks.remove(blockIndex);
delete b;
} else if ( (maxRoom + minRoom) > sz ) {
// try to alloc evenly
int part = b->size() * maxRoom / (maxRoom + minRoom);
moreRoom->append(b->lines.constData(), part);
moreRoom->end += part;
lessRoom->end += b->size() - part;
lessRoom->append(b->lines.constData() + part, b->size() - part);
m_blocks.remove(blockIndex);
delete b;
} else {
// cannot merge simply... let's forget about it for now as it is not vital
++blockIndex;
}
} else {
++blockIndex;
}
// update block boundaries
while ( blockIndex < m_blocks.count() )
{
b = m_blocks.at(blockIndex);
b->start -= n;
b->end -= n;
}
}
|