summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuan Manuel Tomás <jtomas1815@gmail.com>2020-04-20 20:15:32 -0300
committerJuan Manuel Tomás <jtomas1815@gmail.com>2020-04-20 20:15:32 -0300
commit3ea12c9a798e017293a6c3e0157db57c72889311 (patch)
treeebdd2770908e3c31eb242f1bee97aaebab6be767
parent95a3f02287774d09a8a48b3f4472bf9af7bb9543 (diff)
downloadjet-3ea12c9a798e017293a6c3e0157db57c72889311.tar.gz
jet-3ea12c9a798e017293a6c3e0157db57c72889311.zip
Implement delete_at_point
-rw-r--r--page.c37
1 files changed, 25 insertions, 12 deletions
diff --git a/page.c b/page.c
index bebb9ad..7e545c5 100644
--- a/page.c
+++ b/page.c
@@ -77,17 +77,14 @@ void split_page(struct page *front) {
front->prev = back;
}
-void free_page(struct page **page) {
- if ((*page)->prev) {
- (*page)->prev->next = (*page)->next;
+void free_page(struct page *page) {
+ if (page->prev) {
+ page->prev->next = page->next;
}
- if ((*page)->next) {
- (*page)->next->prev = (*page)->prev;
+ if (page->next) {
+ page->next->prev = page->prev;
}
- struct page *tmp = *page;
- *page = (*page)->prev ?: (*page)->next;
- free(tmp->buffer);
- free(tmp);
+ free(page);
}
void move_gap_forward(struct page *page) {
@@ -116,8 +113,8 @@ void insert_at_point(struct point *point, uint8_t c) {
if (page->gap_start == page->gap_end) {
split_page(page);
if (point->index < PAGE_SIZE / 2) {
- point->current_page = point->current_page->prev;
- page = point->current_page;
+ page = page->prev;
+ point->current_page = page;
}
}
move_gap(page, point->index);
@@ -126,8 +123,24 @@ void insert_at_point(struct point *point, uint8_t c) {
page->element_count++;
}
-void delete_from_page(struct page *page) {
+void delete_at_point(struct point *point) {
+ struct page *page = point->current_page;
+ if (page->element_count == 0) {
+ if (page->prev) {
+ page = page->prev;
+ free_page(point->current_page);
+ point->current_page = page;
+ point->index = page->element_count;
+ } else if (page->next) {
+ page = page->next;
+ free_page(point->current_page);
+ point->current_page = page;
+ point->index = 0;
+ }
+ }
+ move_gap(page, point->index);
if (page->gap_start != 0) {
page->gap_start--;
+ page->element_count--;
}
}