文本编辑器的数据结构 [英] Data structure for text editor

查看:385
本文介绍了文本编辑器的数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个采访问题。您将用什么数据结构将文本存储在文本编辑器中?

解决方案

在旧的ZX-Spectrum上(或更多) ,我不知道)文本exditor使用非常简单的结构。



有一个大的缓冲区,占用了所有可用的RAM。文本在光标处分为两部分。光标前的一部分被放置在缓冲区的开始处,其余的在缓冲区的末尾。作为文本输入,数据简单地添加到第一部分的末尾,当移动光标时,文本被复制出来。



缓冲区布局:

 你好,世界! 
^ ------光标在这里

+ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - +
| H | e | l | l | o |,| | W | < free> | o | r | l | d |!|
+ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - +
| ^ ^ |
begin cur1 cur2 end

这就是一些编辑操作的做法:

 键入一个char:buffer [cur1 ++] = character 

Backspace:cur1--

光标左:缓冲区[ - cur2] =缓冲区[ - cur1]

游标右:缓冲区[cur1 ++] =缓冲区[cur2 ++]
pre>

缓冲区在行动中:

 您好,W ... ........... orld! 
按右^ ^
你好,Wo .............. rld!
按退格^ ^
你好,W ............... rld!
按0 ^ ^
你好,W0 .............. rld!
^ ^


This is an interview question. What data structure would you use to store the text in a text editor?

解决方案

On good old ZX-Spectrum one (or more, I do not know) text exditor used very simple structure.

There was one big buffer, which occupied all free RAM. Text was split in two parts at the cursor. Part before the cursor, was placed at the beginning of the buffer, and the rest at the end of the buffer. As text typed, data simply added to the end of first part, and when cursor is moved, text is copied forth and back.

Buffer layout:

Hello, World!
        ^------Cursor here

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|H|e|l|l|o|,| |W| <free>  |o|r|l|d|!|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                ^         ^        |
begin           cur1      cur2    end

That's, how some edit operations was made:

Type a char:    buffer[cur1++] = character

Backspace:      cur1--

Cursor left:    buffer[--cur2] = buffer[--cur1]

Cursor right:   buffer[cur1++] = buffer[cur2++]

Buffer in action:

             Hello, W..............orld!
Press right          ^             ^
             Hello, Wo..............rld!
Press backspace       ^             ^
             Hello, W...............rld!
Press 0              ^              ^
             Hello, W0..............rld!
                      ^             ^

这篇关于文本编辑器的数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆