What is best data structure suitable to implement editor like notepad?

后端 未结 6 770
抹茶落季
抹茶落季 2021-01-31 12:15

Which data structure/s is used in implementation of editors like notepad. This data structure should be extensible, and should support various features like edition, deletion, s

6条回答
  •  天涯浪人
    2021-01-31 12:47

    There is an excellent article about Piece Chains by James Brown, author of HexEdit.

    In a nutshell: Piece chains allow you to record the changes made to the text. After loading, you have a piece chain that spans the entire text. Now you insert somewhere in the middle.

    Instead of allocating a new buffer, copying the text around, etc., you create two new pieces and modify the existing one: The existing one now contains the text up to the insertion point (i.e. you just change the length of the piece), then you have a piece with the new text and after that a new piece with all the text after the insertion. The original text is left unchanged.

    For undo/redo, you simple remember which pieces you added/removed/changed.

    The most complex area when using piece chains is that there is no longer a 1:1 mapping between an offset in the visible text and the memory structure. You either have to search the chain or you must maintain a binary tree structure of some kind.

提交回复
热议问题