博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AbiWord 中Piece Table 数据结构的实现
阅读量:5240 次
发布时间:2019-06-14

本文共 11912 字,大约阅读时间需要 39 分钟。

AbiWord Documentation

 【】

Introduction

One of the major bits of AbiWord word processing code is the Piece Table

PieceTable

Add more class names / links to sources.

Introduction

A  is the data structure used to represent the document. It presents an interface to access the document content as a sequence of (Unicode) characters. It includes an interface to access document structure and formatting information. It provides efficient editing operations, complete undo, and crash recovery.

Overview

The PieceTable consists of the following classs:

 

  1. InitialBuffer -- This is a read-only character array consisting of the entire character content of the document and initially read from the disk. (All XML tags and other non-content items are omitted from this .)

     

  2. ChangeBuffer -- This is an append-only character array consisting of all character content inserted into the document during the editing session.

     

  3. InitialAttrPropTable -- This is a read-only table of Attribute/Property structures extracted from the original document.

     

  4. ChangeAttrPropTable -- This is an append-only table of Attribute/Property structures that are created during the editing session.

     在Abiword中以上信息主要在:pt_VarSet 类中定义。

  5. Piece -- This class represents a piece of the sequence of the document; that is, a contiguous sub-sequence having the same properties. Such as a span of text or an object (such as an in-line image). It contains a links to the previous and next Pieces in the document. Pieces are created in response to editing and formatting commands.  一个Piece相当于:pf_Frag 抽象类,Frag的类型可以是:文本、图片等。 

     

    1. TextPiece -- This subclass represents a span of contiguous text in one of the buffers. All text within the span has the same (CSS) properties. A TextPiece is not necessarily the longest contiguous span; it is possible to have adjacent (both in order and in  position) TextPieces with the same properties. A TextPiece contains a  offset and length for the location an size of the text and a flag to indicate which . A TextPiece contains (or contains a link to) the text formatting information. Note that the  offset only gives the location of the content of the span in one of the buffers, it does not specify the absolute position of the span in the document.

       pf_Frag_Text 类。该类的所有文本必须具有相同的CSS属性。 

    2. ObjectPiece -- This subclass represents an in-line object or image. It has no references to the buffers, but does provide a place-holder in the sequence.

       pf_Frag_Object 类。可以是图片等。

    3. StructurePiece -- This subclass represents a section or paragraph. It has no references to the buffers, but does provide (CSS) style information and a place-holder in the sequence. There are no links between StructurePieces or between other Pieces and their (containing) StructurePieces.

            pf_Frag_Strux 类。包括段落、章节等。

  6. PieceList -- This is doubly-linked list of Pieces. The are linked in document order. A forward traversal of this list will reveal the entire content of the document; in doing so, it may wildly jump around both of the buffers, but that is not an issue.

     pf_Fragments 类。是所有pf_Frag 类的容器,该容器是一个简单的双向链表的数据结构。

  7.  -- Each editing and formatting change is represented as a ChangeRecord. A ChangeRecord represents an atomic change that was made to one or more pieces. This includes offset/length changes to a TextPiece and changes to the PieceList.

     

  8. ChangeVector -- This is a vector of ChangeRecords. This is used like a stack. ChangeRecords are appended to the vector (pushed onto the stack) as they are created in response to editing and formatting commands. The undo operation takes the last ChangeRecord in the vector and un-does its effect. A redo operation re-applies the ChangeRecord. The ChangeVector holds the complete information to undo all editing back to the initial document. The index of the current position in the ChangeVector is maintained. ChangeRecords are not removed from the vector until the redo is invalidated. When a ChangeRecord is removed from the vector, it is deleted.

        px_ChangeHistory 类。该类可以执行undo 、redo 等操作。

Operations

  1. Insert(position,bAfter,c) -- To insert one or more characters c into the document (either before or after) the absolute document position position, we do the following:
    1. Append the character(s) to the ChangeBuffer.
    2. Find the TextPiece that spans the document position.
      • If the document position is in the middle of a TextPiece (p1), we split it into two TextPieces (p1ap1c) and create a third TextPiece (p1b). p1a and p1c contain the left and right portions referenced in p1p1b spans the newly-inserted character(s). The PieceList is updated so that the sequence p1a,p1b,p1c replace p1 in the list.
      • If the document position is at the end of a TextPiece and the  position in either  is contiguous with the  and position referenced in the TextPiece and the formatting is the same, we may avoid the three part split and simply update the offset/length in the TextPiece. This case is very likely when the user is composing text or is undoing a delete.
      • If the document position is between Pieces, a new TextPiece is created and inserted into the PieceList.
    3. Create a ChangeRecord and append it to the ChangeVector. For an insert, we construct a ChangeRecord of type InsertSpan.
      • cr.span.m_documentOffset contains the document position of the insertion.
      • cr.span.m_span marks the  position of the text that was inserted.
      • cr.span.m_bAfter remembers whether the insertion was before or after the document position.

     

  2. Delete(position,bAfter,length) -- To delete one or more characters from the document (either before or after) the absolute document position position, we do the following:
    1. Find the TextPiece that spans the document position.
      • If the length of characters is contained within the TextPiece (p1), we split it into two TextPieces (p1a and pl1b). The offsets and lengths are set in the new TextPieces such that the deleted sequence is not in either piece. (The deleted text is not actually deleted from the ; there are just no references to it from the PieceList.)
      • If the document position is at the beginning or end of a TextPiece, we can just adjust the offset/length, rather than doing the split.
      • If the deletion extends over multiple Pieces, we iterate over each piece in the range and perform a delete on the sub-sequence. This will result in a multi-step ChangeRecord.
      • TODO what about non-TextPieces??
    2. Create a ChangeRecord and append it to the ChangeVector. For a delete, we construct a ChangeRecord of type DeleteSpan.
      • cr.span.m_documentOffset contains the document position of the deletion.
      • cr.span.m_span marks the  position of the text that was deleted.
      • cr.span.m_bAfter remembers whether the insertion was before or after the document position.

     

  3. InsertFormatting()
  4. ChangeFormatting()

     

  5. Undo -- This can be implemented using the information in the ChangeVector. If the CurrentPosition in the ChangeVector is greater than zero, we have undo information. The information in the ChangeRecord prior to the CurrentPosition is used to undo the editing operation. After an undo the CurrentPosition is decremented.

     

    • If the ChangeRecord is of type InsertSpan: we perform a delete operation using cr.span.m_documentOffsetcr.span.m_span.m_length and cr.span.m_bAfter.

       

    • If the ChangeRecord is of type DeleteSpan: we perform an insert operation using cr.span.m_documentOffsetcr.span.m_span, and cr.span.m_bAfter.

       

    • If the ChangeRecord is of type ChangeFormatting:
    • If the ChangeRecord is of type InsertFormatting:

     

  6. Redo -- This can be implemented using the information in the ChangeVector. If the CurrentPosition in the ChangeVector is less than the length of the ChangeVector, the redo has not been invalidated and may be applied. The information in the ChangeRecord at the CurrentPosition provides complete information to describe the editing operation to be redone. After a redo the CurrentPosition is advanced.

     

  7. Autosave -- This can be implemented by periodically writing the ChangeBuffer, ChangeVector, and the ChangeAttrPropTable to temporary files. After a crash, the original document and the temporary files could be used to replay the editing operations and reconstruct the modified document.

Observations

  1. The content of the original file are never modified. Pieces in the PieceList describe the current document; the original content is referenced in a random access fashion. For systems with small memory or for very large documents, it may be worth demand loading blocks of the original content rather than loading it completly into the InitialBuffer.

     

  2. Document content data (in the two buffers) are never moved once written. insert and delete operations change the Pieces in the PieceList, but do not move or change the contents of the two buffers.

     

  3. The result of an undo operation must produce the identical document structure and content. Since consecutive Pieces in the PieceList may have the same formatting properties and may refer to congituous  locations (there is no requirement to coalesce them), an undo operation may produce a different PieceList than we originally had prior to doing the operation that was undone.
    • TODO Check this. Whether the PieceList should be identical or equivalent.

or Issues

  1. TextPieces represent spans of text that are convenient for the structure of the document and a result of the sequence of editing operations. They are not optimized for layout or display.
    • We can provide access methods to return a const char * into the buffers along with a length, which the caller could use in text drawing or measuring calls, but not c-style, zero-terminated strings.

     

  2. Mapping an absolute document position to a Piece involves a linear search of the PieceList to compute the absolute document position and find the correct Piece. The number of Pieces in a document is a function of the number of editing operations that have been performed in the session and of the complexity of the structure and formatting of the original document. A linear search might be painfully slow.
    • TODO We will find a tree-structure to use instead of the doubly-linked list that will give us O(log(n)) searching.
    • TODO Consider caching the last few lookup results so that we can avoid doing a search if possible. This should have a high hit-rate when the user is composing text.

     

  3. We provide a complete, but first-order undo with redo. That is, we do not put the undo-operation in the undo (like emacs).

     

  4. TODO The before and after stuff on insert and delete is a bit of a hand-wave.

     

  5. TODO Need to add multi-step-undo so that delete operations which span multiple pieces can be represented operation to the user.

Code

class PT_PieceTable{	const UT_UCSChar * m_InitialBuffer;	const UT_UCSChar * m_ChangeBuffer;	pt_PieceList * m_pieceList;	pt_AttrPropTable m_InitialAttrPropTable;	pt_AttrPropTable m_ChangeAttrPropTable;	...};

 

class pt_Piece{	enum PieceType	{ TextPiece,			  ObjectPiece,			  StructurePiece };	PieceType	m_pieceType;	
...};

 

class pt_Span{	UT_Bool		m_bInInitialBuffer;	UT_uint32	m_offset;	UT_uint32	m_length;};

 

class pt_TextPiece : public pt_Piece{	pt_Span			m_span;	pt_AttrPropReference	m_apr;	...};

 

class pt_ObjectPiece : public pt_Piece{	...};

 

class pt_StructurePiece : public pt_Piece{	pt_AttrPropReference	m_apr;	...};

 

class pt_PieceList{	
...};

 

class pt_AttrPropReference{	UT_Bool		m_bInInitialTable;	UT_uint32	m_index;	...};

 

class pt_AttrProp{	UT_HashTable * m_pAttributes;	UT_HashTable * m_pProperties;	...};

 

class pt_AttrPropTable{	UT_vector
m_Table; ...};

 

class pt_ChangeRecord{	UT_Bool m_bMultiStepStart;	UT_Bool m_bMultiStepEnd;

 

enum ChangeType	{ InsertSpan,			  DeleteSpan,			  ChangeFormatting,			  InsertFormatting,			  ...			};	struct {		UT_uint32	m_documentOffset;		UT_Bool		m_bAfter;		pt_Span		m_span;	} span;	struct {		UT_uint32	m_documentOffset1;		UT_uint32	m_documentOffset2;		pt_AttrPropReference	m_apr;	} fmt;	...};

 

class pt_ChangeVector{	UT_vector	m_vecChangeRecords;	UT_uint32	m_undoPosition;	...};

转载于:https://www.cnblogs.com/songtzu/p/3539739.html

你可能感兴趣的文章
Kruskal基础最小生成树
查看>>
ubuntu 14.04 安装搜狗拼音输入法
查看>>
浅谈算法和数据结构: 一 栈和队列
查看>>
Java内部类详解
查看>>
【hdu 1429】胜利大逃亡(续)
查看>>
图论-次短路求法
查看>>
What's New for Visual C# 6.0
查看>>
ExtJs学习笔记之ComboBox组件
查看>>
关于收费软件
查看>>
getopt_long
查看>>
TensorFlow MNIST CNN 代码
查看>>
javascript之Style物
查看>>
JSON跨域解决方案收集
查看>>
SSH框架整合总结
查看>>
图的深度优先遍历
查看>>
C# 之 提高WebService性能大数据量网络传输处理
查看>>
md5sum命令详解
查看>>
[bzoj1004] [HNOI2008] Cards
查看>>
应该是实例化对象的没有对属性赋值时,自动赋值为null,但不是空指针对象引用...
查看>>
原生HttpClient详细使用示例
查看>>