Docsity

Buffer Implementations for Text Editors, Lecture notes of Programming Abstractions

Toggle sidebar
bg1
Admin
Today’s topics
•Stack, Queue implementations
•Start Editor Buffer case study
Reading
•Ch 10 & 9
Lec ture #20
Text editor case study
Command-line text editing commands
!F! Move cursor forward one character position!
!B! Move cursor backward one character position
!J! Jump to start of buffer (before first character)
!E! Move cursor to end of buffer (after last character)!
!Ixxx Insert characters xxx at current cursor position
!D! Delete character after current cursor position
Buffer requirements
•Sequence of characters + cursor position
•Operations to match commands above
What to learn?
•Implementation choices, performance implications
Buffer class interface
class Buffer {
public:
Buffer();
~Buffer();
void moveCursorForward();
void moveCursorBackward();
void moveCursorToStart();
void moveCursorToEnd();
void insertCharacter(char ch);
void deleteCharacter();
void display();
private:
// TBD!
};
Buffer layered on Vector
Need character data + cursor
•Chars in Vector<char>
•Represent cursor as integer index
•Minor detail -- is index before/after cursor?
Buffer contains: AB|CDE
// for Buffer class
private:
Vector<char> chars;
int cursor;
A B EDC
2
cursor
pf2

Partial preview of the text

Download Buffer Implementations for Text Editors and more Lecture notes Programming Abstractions in PDF only on Docsity!

Admin

Today’s topics

• Stack,^ Queue implementations

• Start Editor Buffer case study

Reading

• Ch 10 & 9

Lecture # 20

Text editor case study

Command-line text editing commands

! F! Move cursor forward one character position! ! B! Move cursor backward one character position ! J! Jump to start of buffer (before first character) ! E! Move cursor to end of buffer (after last character)! ! Ixxx Insert characters xxx at current cursor position ! D! Delete character after current cursor position

Buffer requirements

• Sequence of characters + cursor position

• Operations to match commands above

What to learn?

• Implementation choices,^ performance implications

Buffer class interface

class Buffer { public: Buffer(); ~Buffer(); void moveCursorForward(); void moveCursorBackward(); void moveCursorToStart(); void moveCursorToEnd(); void insertCharacter(char ch); void deleteCharacter(); void display(); private: // TBD! };

Buffer layered on Vector

Need character data + cursor

• Chars in^ Vector

• Represent cursor as integer index

• Minor detail -- is index before/after cursor?

Buffer contains: AB|CDE

// for Buffer class private: Vector chars; int cursor; A B C D E 2

cursor

Evaluate Vector Buffer Buffer() O(1) ~Buffer() O(1) moveCursorForward() O(1) moveCursorBackward() O(1) moveCursorToStart() O(1) moveCursorToEnd() O(1) insertCharacter() O(N) deleteCharacter() O(N) Space used ~1 byte per char Buffer layered on Stack Inspiration: add/remove at end of vector is fast

  • If chars next to cursor were at end…
    • Build on top of stack?
      • Another layered abstraction!
    • How is cursor represented? Buffer contains: AB|CDE // for Buffer class private: Stack before, after; B A C D E before after Compare implementations !!!!! Vector Stack Buffer() O(1) O(1) ~Buffer() O(1) O(1) moveCursorForward() O(1) O(1) moveCursorBackward() O(1) O(1) moveCursorToStart() O(1) O(N) moveCursorToEnd() O(1) O(N) insertCharacter() O(N) O(1) deleteCharacter() O(N) O(1) Space used 1N 2N Buffer as linked list Inspiration: contiguous memory is constraining
  • Connect chars without locality
  • Linked list to the rescue! Buffer contains: AB|CDE // for Buffer class private: struct cellT { char ch; cellT *next; }; cellT *head, *cursor; head cursor

E

A

A B^

C

C D