Todays topics
Finish Editor Buffer case study Start Map implementation, trees Ch 9 Ch 13
Stack
O(1) O(1) O(1) O(1) O(N) O(N) O(1) O(1) 2N
Buffer() ~Buffer() moveCursorForward() moveCursorBackward() moveCursorToStart() moveCursorToEnd() insertCharacter() deleteCharacter() Space used
Lecture #21
Cursor design
Where does cursor point?
E
A A
C C
To cell before or after? Where is cursor if between B & C? After E? Before A? 5 letters, 6 cursor positions
// for Buffer class private: struct cellT { char ch; cellT *next; }; cellT *head, *cursor;
head cursor
A A
C C
?
head cursor
A A
C C
dummy cell
Compare implementations
! ! ! ! !
Vector
O(1) O(1) O(1)
Stack
O(1) O(1) O(1) O(1) O(N) O(N) O(1) O(1) 2N
List
O(1) O(N) O(1) O(N) O(1) O(N) O(1) O(1) 5N
moveCursorBackward() O(1) moveCursorToStart() moveCursorToEnd() insertCharacter() deleteCharacter() Space used O(1) O(1) O(N) O(N) 1N
Compare implementations
! ! ! ! !
Vector
O(1) O(1) O(1)
Stack
O(1) O(1) O(1) O(1) O(N) O(N) O(1) O(1) 2N
Single Double
O(1) O(N) O(1) O(N) O(1) O(N) O(1) O(1) 5N O(1) O(N) O(1) O(1) O(1) O(1) O(1) O(1) 9N
?
head cursor
A A A
B A
C A
D A
next prev
Space-time tradeoff
Doubly-linked list is O(1) on all six operations
But, each char uses 1 byte + 8 bytes of pointers => 89% overhead!
Implementing Map
Map is super-useful
Any kind of dictionary, lookup table, index, database, etc. Fast access via key Operations to optimize: add, getValue
Compromise: chunklist
Array and linked list hybrid Shares overhead cost among several chars Chunksize can be tuned as appropriate
3 A B C 3 D G H 2 I J
Map as Vector
! ! ! ! !
Unsorted
O(1) O(1) O(N) O(N) none
Sorted
O(1) O(1) O(N) O(logN) none
Dene pair struct Vector sorted or unsorted? How to implement getValue? How to implement add?
A different strategy
Sorting the Vector
Bashful
Bashful
Doc
Dopey
Grumpy
Happy
Sleepy
Sneezy
Bashful
Doc
Dopey
Grumpy
Happy
Sleepy
Sneezy