4#ifndef QFRAGMENTMAP_P_H
5#define QFRAGMENTMAP_P_H
18#include <QtGui/private/qtguiglobal_p.h>
20#include <private/qtools_p.h>
33 quint32 size_left_array[N];
34 quint32 size_array[N];
35 enum {size_array_max = N };
38template <
class Fragment>
41 enum Color { Red, Black };
66 return (fragments + index);
68 inline const Fragment *
fragment(uint index)
const {
69 return (fragments + index);
73 inline Fragment &
F(uint index) {
return fragments[index] ; }
74 inline const Fragment &
F(uint index)
const {
return fragments[index] ; }
76 inline bool isRoot(uint index)
const {
77 return !fragment(index)->parent;
80 inline uint
position(uint node, uint field = 0)
const {
81 Q_ASSERT(field < Fragment::size_array_max);
82 const Fragment *f = fragment(node);
83 uint offset = f->size_left_array[field];
88 offset += f->size_left_array[field] + f->size_array[field];
93 inline uint
sizeRight(uint node, uint field = 0)
const {
94 Q_ASSERT(field < Fragment::size_array_max);
96 const Fragment *f = fragment(node);
100 sr += f->size_left_array[field] + f->size_array[field];
105 inline uint
sizeLeft(uint node, uint field = 0)
const {
106 Q_ASSERT(field < Fragment::size_array_max);
107 return fragment(node)->size_left_array[field];
111 inline uint
size(uint node, uint field = 0)
const {
112 Q_ASSERT(field < Fragment::size_array_max);
113 return fragment(node)->size_array[field];
116 inline void setSize(uint node,
int new_size, uint field = 0) {
117 Q_ASSERT(field < Fragment::size_array_max);
118 Fragment *f = fragment(node);
119 int diff = new_size - f->size_array[field];
120 f->size_array[field] = new_size;
125 f->size_left_array[field] += diff;
137 while (n && fragment(n)->left)
138 n = fragment(n)->left;
143 while (n && fragment(n)->right)
144 n = fragment(n)->right;
152 Q_ASSERT(!head->root || !fragment(head->root)->parent);
156 Q_ASSERT(!head->root || !fragment(new_root)->parent);
157 head->root = new_root;
161 return n > 0 && n != head->freelist;
171 void rotateLeft(uint x);
172 void rotateRight(uint x);
173 void rebalance(uint x);
174 void removeAndRebalance(uint z);
176 uint createFragment();
177 void freeFragment(uint f);
181template <
class Fragment>
188template <
class Fragment>
193 Fragment *newFragments = (Fragment *)realloc(fragments, 64*fragmentSize);
195 fragments = newFragments;
196 head->allocated = 64;
198 Q_CHECK_PTR(fragments);
200 head->tag = (((quint32)
'p') << 24) | (((quint32)
'm') << 16) | (((quint32)
'a') << 8) |
'p';
203 head->node_count = 0;
205 F(head->freelist).right = 0;
208template <
class Fragment>
214template <
class Fragment>
217 Q_ASSERT(head->freelist <= head->allocated);
219 uint freePos = head->freelist;
220 if (freePos == head->allocated) {
222 auto blockInfo = qCalculateGrowingBlockSize(freePos + 1,
fragmentSize);
223 Fragment *newFragments = (Fragment *)realloc(fragments, blockInfo.size);
224 Q_CHECK_PTR(newFragments);
225 fragments = newFragments;
226 head->allocated = quint32(blockInfo.elementCount);
227 F(freePos).right = 0;
230 uint nextPos = F(freePos).right;
233 if (nextPos < head->allocated)
234 F(nextPos).right = 0;
237 head->freelist = nextPos;
244template <
class Fragment>
247 F(i).right = head->freelist;
254template <
class Fragment>
262 uint y = F(n).parent;
263 while (F(n).parent && n == F(y).right) {
272template <
class Fragment>
282 uint y = F(n).parent;
283 while (F(n).parent && n == F(y).left) {
294
295
296
297
298
299
300template <
class Fragment>
303 uint p = F(x).parent;
308 F(x).right = F(y).left;
310 F(F(y).left).parent = x;
317 Q_ASSERT(head->root == x);
320 else if (x == F(p).left)
325 for (uint field = 0; field < Fragment::size_array_max; ++field)
326 F(y).size_left_array[field] += F(x).size_left_array[field] + F(x).size_array[field];
331
332
333
334
335
336
337template <
class Fragment>
341 uint p = F(x).parent;
344 F(x).left = F(y).right;
346 F(F(y).right).parent = x;
353 Q_ASSERT(head->root == x);
356 else if (x == F(p).right)
361 for (uint field = 0; field < Fragment::size_array_max; ++field)
362 F(x).size_left_array[field] -= F(y).size_left_array[field] + F(y).size_array[field];
366template <
class Fragment>
371 while (F(x).parent && F(F(x).parent).color == Red) {
372 uint p = F(x).parent;
373 uint pp = F(p).parent;
375 if (p == F(pp).left) {
376 uint y = F(pp).right;
377 if (y && F(y).color == Red) {
383 if (x == F(p).right) {
397 if (y && F(y).color == Red) {
403 if (x == F(p).left) {
421template <
class Fragment>
431 }
else if (!F(y).right) {
441 F(F(z).left).parent = y;
442 F(y).left = F(z).left;
443 for (uint field = 0; field < Fragment::size_array_max; ++field)
444 F(y).size_left_array[field] = F(z).size_left_array[field];
445 if (y != F(z).right) {
447
448
449
450
451
452
453
454
455
456
461 F(y).right = F(z).right;
462 F(F(z).right).parent = y;
465 for (uint field = 0; field < Fragment::size_array_max; ++field)
466 F(n).size_left_array[field] -= F(y).size_array[field];
471
472
473
474
475
476
479 uint zp = F(z).parent;
481 Q_ASSERT(head->root == z);
483 }
else if (F(zp).left == z) {
485 for (uint field = 0; field < Fragment::size_array_max; ++field)
486 F(zp).size_left_array[field] -= F(z).size_array[field];
493 F(y).color = F(z).color;
498
499
500
501
502
503
508 Q_ASSERT(head->root == z);
510 }
else if (F(p).left == z) {
512 for (uint field = 0; field < Fragment::size_array_max; ++field)
513 F(p).size_left_array[field] -= F(z).size_array[field];
519 while (F(n).parent) {
520 uint p = F(n).parent;
521 if (F(p).left == n) {
522 for (uint field = 0; field < Fragment::size_array_max; ++field)
523 F(p).size_left_array[field] -= F(z).size_array[field];
531 if (F(y).color != Red) {
532 while (F(x).parent && (x == 0 || F(x).color == Black)) {
533 if (x == F(p).left) {
535 if (F(w).color == Red) {
541 if ((F(w).left == 0 || F(F(w).left).color == Black) &&
542 (F(w).right == 0 || F(F(w).right).color == Black)) {
547 if (F(w).right == 0 || F(F(w).right).color == Black) {
549 F(F(w).left).color = Black;
551 rotateRight(F(p).right);
554 F(w).color = F(p).color;
557 F(F(w).right).color = Black;
563 if (F(w).color == Red) {
569 if ((F(w).right == 0 || F(F(w).right).color == Black) &&
570 (F(w).left == 0 || F(F(w).left).color == Black)) {
575 if (F(w).left == 0 || F(F(w).left).color == Black) {
577 F(F(w).right).color = Black;
579 rotateLeft(F(p).left);
582 F(w).color = F(p).color;
585 F(F(w).left).color = Black;
598template <
class Fragment>
601 Q_ASSERT(field < Fragment::size_array_max);
618template <
class Fragment>
623 uint z = createFragment();
627 F(z).size_array[0] = length;
628 for (uint field = 1; field < Fragment::size_array_max; ++field)
629 F(z).size_array[field] = 1;
630 for (uint field = 0; field < Fragment::size_array_max; ++field)
631 F(z).size_left_array[field] = 0;
636 Q_ASSERT(!x || F(x).parent == 0);
642 if (s <= F(x).size_left_array[0]) {
646 s -= F(x).size_left_array[0] + F(x).size_array[0];
657 for (uint field = 0; field < Fragment::size_array_max; ++field)
658 F(y).size_left_array[field] = F(z).size_array[field];
662 while (y && F(y).parent) {
663 uint p = F(y).parent;
664 if (F(p).left == y) {
665 for (uint field = 0; field < Fragment::size_array_max; ++field)
666 F(p).size_left_array[field] += F(z).size_array[field];
676template <
class Fragment>
678 uint root =
this->root();
683template <
class Fragment>
697 inline bool atEnd()
const {
return !n; }
709 const Fragment *
value()
const { Q_ASSERT(!
atEnd());
return pt->fragment(n); }
713 n = pt->data.next(n);
717 n = pt->data.previous(n);
731
732
738 inline bool atEnd()
const {
return !n; }
749 const Fragment *
value()
const { Q_ASSERT(!
atEnd());
return pt->fragment(n); }
752 n = pt->data.next(n);
756 n = pt->data.previous(n);
767 for (
Iterator it = begin(); !it.atEnd(); ++it)
772 for (
Iterator it = begin(); !it.atEnd(); ++it)
784 inline bool isEmpty()
const {
return data.head->node_count == 0; }
785 inline int numNodes()
const {
return data.head->node_count; }
786 int length(uint field = 0)
const {
return data.length(field); }
791 uint
findNode(
int k, uint field = 0)
const {
return data.findNode(k, field); }
795 uint f = data.insert_single(key, length);
797 Fragment *frag = fragment(f);
806 Fragment *frag = fragment(f);
810 return data.erase_single(f);
814 Q_ASSERT(index != 0);
815 return data.fragment(index);
817 inline const Fragment *
fragment(uint index)
const {
818 Q_ASSERT(index != 0);
819 return data.fragment(index);
821 inline uint
position(uint node, uint field = 0)
const {
return data.position(node, field); }
822 inline bool isValid(uint n)
const {
return data.isValid(n); }
823 inline uint
next(uint n)
const {
return data.next(n); }
824 inline uint
previous(uint n)
const {
return data.previous(n); }
825 inline uint
size(uint node, uint field = 0)
const {
return data.size(node, field); }
826 inline void setSize(uint node,
int new_size, uint field = 0)
827 { data.setSize(node, new_size, field);
828 if (node != 0 && field == 0) {
829 Fragment *frag = fragment(node);
835 inline int firstNode()
const {
return data.minimum(data.root()); }
Fragment * fragment(uint index)
void setRoot(uint new_root)
bool isRoot(uint index) const
uint erase_single(uint f)
const Fragment * fragment(uint index) const
uint sizeLeft(uint node, uint field=0) const
void setSize(uint node, int new_size, uint field=0)
uint previous(uint n) const
uint maximum(uint n) const
int length(uint field=0) const
const Fragment & F(uint index) const
bool isValid(uint n) const
uint position(uint node, uint field=0) const
uint sizeRight(uint node, uint field=0) const
uint minimum(uint n) const
uint size(uint node, uint field=0) const
uint insert_single(int key, uint length)
uint findNode(int k, uint field=0) const
const Fragment * value() const
ConstIterator(const ConstIterator &it)
bool operator!=(const ConstIterator &it) const
bool operator==(const ConstIterator &it) const
ConstIterator & operator--()
ConstIterator(const QFragmentMap *p, int node)
ConstIterator & operator++()
bool operator<(const ConstIterator &it) const
ConstIterator(const Iterator &it)
const Fragment * operator*() const
const Fragment * operator->() const
Iterator(QFragmentMap *p, int node)
const Fragment * operator*() const
const Fragment * value() const
Iterator(const Iterator &it)
bool operator<(const Iterator &it) const
bool operator==(const Iterator &it) const
bool operator!=(const Iterator &it) const
const Fragment * operator->() const
ConstIterator begin() const
uint position(uint node, uint field=0) const
uint insert_single(int key, uint length)
const Fragment * fragment(uint index) const
void setSize(uint node, int new_size, uint field=0)
ConstIterator last() const
uint previous(uint n) const
bool isValid(uint n) const
ConstIterator end() const
ConstIterator find(int k, uint field=0) const
Iterator find(int k, uint field=0)
uint erase_single(uint f)
Fragment * fragment(uint index)
uint findNode(int k, uint field=0) const
uint size(uint node, uint field=0) const
int length(uint field=0) const