Comparison of Vector, Stack, and Linked List Implementations of Buffer and Map, Lecture notes of Programming Abstractions

This document compares the implementation of buffer and map using vector, stack, and linked list. It includes code snippets and complexity analysis for various operations such as insertion, deletion, and movement of cursor. It also discusses the space-time tradeoff and introduces the concept of chunklist.

Typology: Lecture notes

2010/2011

Uploaded on 10/02/2011

hollyb
hollyb 🇺🇸

4.8

(44)

431 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Admin
Today’s topics
Finish Editor Buffer case study
Start Map implementation, trees
Reading
Ch 9
Ch 13
Café today after class
Lec ture #21
Buffer: Vector vs Stack
!!!!! 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
ABC
CD
Cursor design
Where does cursor point?
To cell before or after?
Where is cursor if between B & C?
After E?
Before A?
5 letters, 6 cursor positions…
head
cursor
E
A
ABC
CD
pf3
pf4

Partial preview of the text

Download Comparison of Vector, Stack, and Linked List Implementations of Buffer and Map and more Lecture notes Programming Abstractions in PDF only on Docsity!

Admin

Today’s topics

• Finish Editor Buffer case study

• Start Map implementation, trees

Reading

• Ch 9

• Ch 13

Café today after class

Lecture

Buffer: Vector vs Stack

!!!!! 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

Cursor design

Where does cursor point?

• To cell^ before^ or^ after?

• Where is cursor if between B & C?

• After E?

• Before A?

• 5 letters, 6 cursor positions…

head

cursor

E A A B^ C C D

Use of dummy cell for linked list Add "dummy cell" to front of list

  • Simplifies logic
  • Every cell holding actual data has a predecessor
  • Cursor can point to cell before insertion point

head

cursor

E A A B^ C C D ? dummy cell Linked list insert/delete void Buffer::insertCharacter(char ch) { cellT *cp = new cellT; cp->ch = ch; cp->next = cursor->next; cursor->next = cp; cursor = cp; } void Buffer::deleteCharacter() { if (cursor->next != NULL) { cellT *old = cursor->next; cursor->next = old->next; delete old; } } Linked list cursor movement void Buffer::moveCursorToBegin() { cursor = head; } void Buffer::moveCursorForward() { if (cursor->next != NULL) cursor = cursor->next; } void Buffer::moveCursorToEnd() { while (cursor->next != NULL) moveCursorForward(); } void Buffer::moveCursorBackward() { if (cursor != head) { cellT *cp = head; while (cp->next != cursor) cp = cp->next; cursor = cp; } Compare implementations !!!!! Vector Stack List Buffer() O(1) O(1) O(1) ~Buffer() O(1) O(1) O(N) moveCursorForward() O(1) O(1) O(1) moveCursorBackward() O(1) O(1) O(N) moveCursorToStart() O(1) O(N) O(1) moveCursorToEnd() O(1) O(N) O(N) insertCharacter() O(N) O(1) O(1) deleteCharacter() O(N) O(1) O(1) Space used 1N 2N 5N

Simple Map implementation Layer on Vector

  • Provides convenience with low overhead Define pair struct
  • Holds key and value together
  • Store Vector Vector sorted or unsorted?
  • If sorted, sorted by what? How to implement getValue? How to implement add? Map as Vector !!!!! Unsorted Sorted Map() O(1) O(1) ~Map() O(1) O(1) add() O(N) O(N) getValue() O(N) O(logN) Overhead per entry none none A different strategy Sorting the Vector
  • Provides fast lookup, but still slow to insert (because of shuffling) Does a linked list help? - Easy to insert, once at a position - But hard to find position to insert... - Will rearranging pointers help? Bashful Doc Dopey Grumpy Happy Sleepy Sneezy Bashful Doc Dopey Grumpy Happy Sleepy Sneezy Bashful Doc Dopey Grumpy Happy Sleepy Sneezy