Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef PF_FRAGMENTS_H
00022 #define PF_FRAGMENTS_H
00023
00034 #include <stdio.h>
00035 #include "ut_vector.h"
00036 #include "ut_types.h"
00037 #include "pt_Types.h"
00038 class pf_Frag;
00039
00040 class ABI_EXPORT pf_Fragments
00041 {
00042 friend class pf_Frag;
00043 public:
00044 pf_Fragments();
00045 ~pf_Fragments();
00046
00047 void appendFrag(pf_Frag * pf);
00048 void insertFrag(pf_Frag * pfPlace, pf_Frag * pfNew);
00049 void insertFragBefore(pf_Frag * pfPlace, pf_Frag * pfNew);
00050 void unlinkFrag(pf_Frag * pf);
00051
00052 void purgeFrags();
00053 pf_Frag * findFirstFragBeforePos(PT_DocPosition pos) const;
00054
00055 pf_Frag * getFirst() const;
00056 pf_Frag * getLast() const;
00057 void verifyDoc(void) const;
00058 #ifdef PT_TEST
00059 void __dump(FILE * fp) const;
00060 #endif
00061
00062 class Node
00063 {
00064 public:
00065 enum Color { red, black };
00066 Node();
00067 Node(Color c);
00068 Node(Color c, pf_Frag * pf, Node * l, Node * r, Node * p);
00069 ~Node(void);
00070 Color color;
00071 pf_Frag* item;
00072 Node* left;
00073 Node* right;
00074 Node* parent;
00075
00076 #ifdef DEBUG
00077 void print(void);
00078 #endif
00079 private:
00080
00081 Node(const Node&);
00082 Node& operator=(const Node&);
00083 };
00084
00085
00086 class Iterator
00087 {
00088 public:
00089 Iterator() : m_pOwner(NULL), m_pNode(NULL) {}
00090
00091 Iterator& operator++()
00092 {
00093 m_pNode = const_cast<Node*>(m_pOwner->_next(m_pNode));
00094 return *this;
00095 }
00096
00097 Iterator operator++(int)
00098 {
00099 Iterator tmp(*this);
00100 m_pNode = const_cast<Node*>(m_pOwner->_next(m_pNode));
00101 return tmp;
00102 }
00103
00104 Iterator& operator--()
00105 {
00106 m_pNode = const_cast<Node*>(m_pOwner->_prev(m_pNode));
00107 return *this;
00108 }
00109
00110 Iterator operator--(int)
00111 {
00112 Iterator tmp(*this);
00113 m_pNode = const_cast<Node*>(m_pOwner->_prev(m_pNode));
00114 return tmp;
00115 }
00116
00117 bool operator==(const Iterator other)
00118 { return (m_pOwner == other.m_pOwner && m_pNode == other.m_pNode); }
00119
00120 bool operator!=(const Iterator other)
00121 { return !(*this == other); }
00122
00123 const pf_Frag* value() const;
00124 pf_Frag* value();
00125
00126 bool is_valid() const
00127 { return m_pNode != 0; }
00128
00129 private:
00130 Iterator(const pf_Fragments* owner, Node* node = 0) : m_pOwner(owner), m_pNode(node) {}
00131 const Node* getNode() const { return m_pNode; }
00132 Node* getNode() { return m_pNode; }
00133
00134 const pf_Fragments* m_pOwner;
00135 Node* m_pNode;
00136 friend class pf_Fragments;
00137 friend class pf_Frag;
00138 };
00139
00140 Iterator find(PT_DocPosition pos) const;
00141
00142
00143 private:
00144 Iterator insertRoot(pf_Frag* new_piece);
00145 Iterator insertLeft(pf_Frag* new_piece, Iterator it);
00146 Iterator insertRight(pf_Frag* new_piece, Iterator it);
00147 void erase(Iterator it);
00148 void fixSize(Iterator it);
00149 PT_DocPosition documentPosition(const Iterator it) const;
00150 void changeSize(int delta);
00151
00152 Iterator begin() { return Iterator(this, _first()); }
00153 Iterator end() { return Iterator(this); }
00154
00155 size_t size() const { return m_nSize; }
00156 PT_DocPosition sizeDocument() const { return m_nDocumentSize; }
00157
00158 #ifdef DEBUG
00159 void print() const;
00160 bool checkInvariants() const;
00161 bool checkSizeInvariant(const Node* pn, PT_DocPosition* size) const;
00162 #endif
00163
00164 void _insertBST(Node* pn);
00165 void _insertFixup(Node* pn);
00166 void _eraseFixup(Node* pn);
00167 void _leftRotate(Node* x);
00168 void _rightRotate(Node* x);
00169 PT_DocPosition _calculateSize(Node* x) const;
00170 PT_DocPosition _calculateLeftSize(pf_Frag * pf) const;
00171 #ifdef DEBUG
00172 int _countBlackNodes(const Iterator it) const;
00173 #endif
00174
00176 void delete_and_purge_tree(Node* node);
00178 void delete_tree(Node* node);
00179
00180 const Node* _next(const Node* pn) const;
00181 const Node* _prev(const Node* pn) const;
00182 const Node* _first() const;
00183 const Node* _last() const;
00184
00185 Node* _next(Node* pn);
00186 Node* _prev(Node* pn);
00187 Node* _first();
00188 Node* _last();
00189
00190 Node* m_pLeaf;
00191 Node* m_pRoot;
00192 size_t m_nSize;
00193 PT_DocPosition m_nDocumentSize;
00194
00195 friend class Iterator;
00196
00197 };
00198
00199 #endif