Буфер зазора

A промежутков in информатике is a динамический массив that allows efficient insertion and deletion operations clustered near the same location. Gap buffers are especially common in текстовых редакторах, where most changes to the text occur at or near the current location of the cursor. The text is stored in a large buffer in two contiguous segments, with a gap between them for inserting new text. Moving the cursor involves copying text from one side of the gap to the other (sometimes copying is delayed until the next operation that changes the text). Insertion adds new text at the end of the first segment; deletion deletes it.

Text in a gap buffer is represented as two строк, which take very little extra space and which can be searched and displayed very quickly, compared to more sophisticated структурами данных such as связанные списки. However, operations at different locations in the text and ones that fill the gap (requiring a new gap to be created) may require copying most of the text, which is especially inefficient for large files. The use of gap buffers is based on the assumption that such recopying occurs rarely enough that its cost can be амортизировать over the more common cheap operations. This makes the gap buffer a simpler alternative to the для for use in text editors[1] such as Emacs.[2]

Пример

Ниже приведены примеры операций с промежутками в буфере. Промежуток представлен пустым пространством между квадратными скобками. Это представление несколько вводит в заблуждение: в типичной реализации конечные точки промежутка отслеживаются с помощью указателей или индексов массива, а содержимое промежутка игнорируется; это позволяет, например, удалять данные, изменяя указатель без изменения текста в буфере.

Начальное состояние:

This is the way [                     ]out.

Пользователь вставляет новый текст:

This is the way the world started [   ]out.

Пользователь перемещает курсор перед словом «started»; система перемещает слово «started» из первого буфера во второй.

This is the way the world [   ]started out.

Пользователь добавляет текст, заполняющий пробел; система создает новый пробел:

This is the way the world as we know it [                   ]started out.

См. также

Ссылки

Источники

  • Участники английской Википедии. «Gap buffer». Перевод на русский. CC BY-SA 4.0 (текст и перевод).

Источники иллюстраций

  1. Question book-new.svg · Лицензия файла уточняется (icon)
Соглашение О проекте Обратная связь
2026, Альфапедия. Список материалов:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25