42 #ifndef QFRAGMENTMAP_P_H 43 #define QFRAGMENTMAP_P_H 56 #include "QtCore/qglobal.h" 58 #include <private/qtools_p.h> 76 template <
class Fragment>
97 enum {fragmentSize =
sizeof(Fragment) };
100 int length(
uint field = 0)
const;
104 return (fragments + index);
107 return (fragments + index);
115 return !fragment(index)->parent;
119 Q_ASSERT(field < Fragment::size_array_max);
120 const Fragment *f = fragment(node);
121 uint offset = f->size_left_array[field];
125 if (f->right == node)
126 offset += f->size_left_array[field] + f->size_array[field];
132 Q_ASSERT(field < Fragment::size_array_max);
134 const Fragment *f = fragment(node);
138 sr += f->size_left_array[field] + f->size_array[field];
144 Q_ASSERT(field < Fragment::size_array_max);
145 return fragment(node)->size_left_array[field];
150 Q_ASSERT(field < Fragment::size_array_max);
151 return fragment(node)->size_array[field];
155 Q_ASSERT(field < Fragment::size_array_max);
156 Fragment *f = fragment(node);
157 int diff = new_size - f->size_array[field];
158 f->size_array[field] = new_size;
163 f->size_left_array[field] +=
diff;
169 uint findNode(
int k,
uint field = 0)
const;
175 while (n && fragment(n)->
left)
176 n = fragment(n)->left;
181 while (n && fragment(n)->
right)
182 n = fragment(n)->right;
190 Q_ASSERT(!head->root || !fragment(head->root)->parent);
195 head->root = new_root;
199 return n > 0 && n != head->freelist;
209 void rotateLeft(
uint x);
210 void rotateRight(
uint x);
211 void rebalance(
uint x);
212 void removeAndRebalance(
uint z);
214 uint createFragment();
215 void freeFragment(
uint f);
219 template <
class Fragment>
226 template <
class Fragment>
246 template <
class Fragment>
252 template <
class Fragment>
262 Fragment *newFragments = (Fragment *)realloc(
fragments, needed);
266 F(freePos).right = 0;
269 uint nextPos =
F(freePos).right;
272 if (nextPos < head->allocated)
273 F(nextPos).right = 0;
283 template <
class Fragment>
293 template <
class Fragment>
301 uint y =
F(n).parent;
302 while (
F(n).parent && n ==
F(y).right) {
311 template <
class Fragment>
321 uint y =
F(n).parent;
322 while (
F(n).parent && n ==
F(y).left) {
339 template <
class Fragment>
342 uint p =
F(x).parent;
347 F(x).right =
F(y).left;
349 F(
F(y).left).parent = x;
359 else if (x ==
F(p).
left)
364 for (
uint field = 0; field < Fragment::size_array_max; ++field)
365 F(y).size_left_array[field] +=
F(x).size_left_array[field] +
F(x).size_array[field];
376 template <
class Fragment>
380 uint p =
F(x).parent;
383 F(x).left =
F(y).right;
385 F(
F(y).right).parent = x;
400 for (
uint field = 0; field < Fragment::size_array_max; ++field)
401 F(x).size_left_array[field] -=
F(y).size_left_array[field] +
F(y).size_array[field];
405 template <
class Fragment>
410 while (
F(x).parent &&
F(
F(x).parent).color ==
Red) {
411 uint p =
F(x).parent;
412 uint pp =
F(p).parent;
414 if (p ==
F(pp).
left) {
415 uint y =
F(pp).right;
416 if (y &&
F(y).color ==
Red) {
436 if (y &&
F(y).color ==
Red) {
442 if (x ==
F(p).left) {
460 template <
class Fragment>
480 F(
F(z).left).parent = y;
481 F(y).left =
F(z).left;
482 for (
uint field = 0; field < Fragment::size_array_max; ++field)
483 F(y).size_left_array[field] =
F(z).size_left_array[field];
484 if (y !=
F(z).right) {
500 F(y).right =
F(z).right;
501 F(
F(z).right).parent = y;
504 for (
uint field = 0; field < Fragment::size_array_max; ++field)
505 F(n).size_left_array[field] -=
F(y).size_array[field];
518 uint zp =
F(z).parent;
522 }
else if (
F(zp).left == z) {
524 for (
uint field = 0; field < Fragment::size_array_max; ++field)
525 F(zp).size_left_array[field] -=
F(z).size_array[field];
532 F(y).color =
F(z).color;
549 }
else if (
F(p).left == z) {
551 for (
uint field = 0; field < Fragment::size_array_max; ++field)
552 F(p).size_left_array[field] -=
F(z).size_array[field];
558 while (
F(n).parent) {
559 uint p =
F(n).parent;
560 if (
F(p).left == n) {
561 for (
uint field = 0; field < Fragment::size_array_max; ++field)
562 F(p).size_left_array[field] -=
F(z).size_array[field];
570 if (
F(y).color !=
Red) {
571 while (
F(x).parent && (x == 0 ||
F(x).color ==
Black)) {
572 if (x ==
F(p).left) {
574 if (
F(w).color ==
Red) {
580 if ((
F(w).left == 0 ||
F(
F(w).left).color ==
Black) &&
581 (
F(w).right == 0 ||
F(
F(w).right).color ==
Black)) {
586 if (
F(w).right == 0 ||
F(
F(w).right).color ==
Black) {
593 F(w).color =
F(p).color;
602 if (
F(w).color ==
Red) {
608 if ((
F(w).right == 0 ||
F(
F(w).right).color ==
Black) &&
609 (
F(w).left == 0 ||
F(
F(w).left).color ==
Black)) {
614 if (
F(w).left == 0 ||
F(
F(w).left).color ==
Black) {
621 F(w).color =
F(p).color;
637 template <
class Fragment>
640 Q_ASSERT(field < Fragment::size_array_max);
657 template <
class Fragment>
667 for (
uint field = 1; field < Fragment::size_array_max; ++field)
668 F(z).size_array[field] = 1;
669 for (
uint field = 0; field < Fragment::size_array_max; ++field)
670 F(z).size_left_array[field] = 0;
681 if (s <=
F(x).size_left_array[0]) {
685 s -=
F(x).size_left_array[0] +
F(x).size_array[0];
696 for (
uint field = 0; field < Fragment::size_array_max; ++field)
697 F(y).size_left_array[field] =
F(z).size_array[field];
701 while (y &&
F(y).parent) {
702 uint p =
F(y).parent;
703 if (
F(p).
left == y) {
704 for (
uint field = 0; field < Fragment::size_array_max; ++field)
705 F(p).size_left_array[field] +=
F(z).size_array[field];
715 template <
class Fragment>
722 template <
class Fragment>
736 inline bool atEnd()
const {
return !n; }
752 n = pt->
data.next(n);
756 n = pt->
data.previous(n);
777 inline bool atEnd()
const {
return !n; }
791 n = pt->
data.next(n);
795 n = pt->
data.previous(n);
806 for (Iterator
it = begin(); !
it.atEnd(); ++
it)
811 for (Iterator
it = begin(); !
it.atEnd(); ++
it)
816 inline Iterator
begin() {
return Iterator(
this,
data.minimum(
data.root())); }
817 inline Iterator
end() {
return Iterator(
this, 0); }
818 inline ConstIterator
begin()
const {
return ConstIterator(
this,
data.minimum(
data.root())); }
819 inline ConstIterator
end()
const {
return ConstIterator(
this, 0); }
821 inline ConstIterator
last()
const {
return ConstIterator(
this,
data.maximum(
data.root())); }
823 inline bool isEmpty()
const {
return data.head->node_count == 0; }
827 Iterator
find(
int k,
uint field = 0) {
return Iterator(
this,
data.findNode(k, field)); }
828 ConstIterator
find(
int k,
uint field = 0)
const {
return ConstIterator(
this,
data.findNode(k, field)); }
834 uint f =
data.insert_single(key, length);
849 return data.erase_single(f);
854 return data.fragment(index);
858 return data.fragment(index);
866 {
data.setSize(node, new_size, field);
867 if (node != 0 && field == 0) {
877 friend class Iterator;
878 friend class ConstIterator;
888 #endif // QFRAGMENTMAP_P_H uint findNode(int k, uint field=0) const
bool operator!=(const Iterator &it) const
uint sizeLeft(uint node, uint field=0) const
const Fragment * operator*() const
bool operator==(const ConstIterator &it) const
#define QT_END_NAMESPACE
This macro expands to.
void setRoot(uint new_root)
uint minimum(uint n) const
#define it(className, varName)
uint position(uint node, uint field=0) const
ConstIterator find(int k, uint field=0) const
Fragment * fragment(uint index)
void freeFragment(uint f)
ConstIterator(const Iterator &it)
void setSize(uint node, int new_size, uint field=0)
uint insert_single(int key, uint length)
uint erase_single(uint f)
Q_CORE_EXPORT QTextStream & right(QTextStream &s)
ConstIterator & operator--()
uint sizeRight(uint node, uint field=0) const
int length(uint field=0) const
#define QT_BEGIN_NAMESPACE
This macro expands to.
int qAllocMore(int alloc, int extra)
ConstIterator & operator++()
quint32 size_left_array[N]
const Fragment * operator*() const
ConstIterator last() const
bool operator!=(const ConstIterator &it) const
bool operator==(const Iterator &it) const
int length(uint field=0) const
static const char * data(const QByteArray &arr)
uint position(uint node, uint field=0) const
uint previous(uint n) const
uint insert_single(int key, uint length)
const Fragment * value() const
uint size(uint node, uint field=0) const
Iterator find(int k, uint field=0)
void setSize(uint node, int new_size, uint field=0)
Fragment * fragment(uint index)
uint size(uint node, uint field=0) const
bool isValid(uint n) const
const Fragment & F(uint index) const
Iterator(QFragmentMap *p, int node)
uint previous(uint n) const
const Fragment * operator->() const
uint findNode(int k, uint field=0) const
bool operator<(const ConstIterator &it) const
uint erase_single(uint f)
const Fragment * fragment(uint index) const
QFragmentMapData< Fragment > data
ConstIterator begin() const
ConstIterator(const ConstIterator &it)
bool isRoot(uint index) const
const Fragment * value() const
ConstIterator(const QFragmentMap *p, int node)
bool isValid(uint n) const
Q_CORE_EXPORT QTextStream & left(QTextStream &s)
ConstIterator end() const
bool operator<(const Iterator &it) const
const Fragment * operator->() const
Iterator(const Iterator &it)
uint maximum(uint n) const
const Fragment * fragment(uint index) const