diff options
author | Juan Manuel Tomás <jtomas1815@gmail.com> | 2020-04-22 10:48:30 -0300 |
---|---|---|
committer | Juan Manuel Tomás <jtomas1815@gmail.com> | 2020-04-22 10:48:30 -0300 |
commit | f9eaf742d894b0e6d2c5b1bc244ba17bd00a9e85 (patch) | |
tree | 70d1a8fed23029bde7348467d92a93b2026cc065 /page.c | |
parent | 8b9d67da716c07f957eadd977059d74936c84a76 (diff) | |
download | jet-f9eaf742d894b0e6d2c5b1bc244ba17bd00a9e85.tar.gz jet-f9eaf742d894b0e6d2c5b1bc244ba17bd00a9e85.zip |
Move operations on point structures to point.c
Diffstat (limited to 'page.c')
-rw-r--r-- | page.c | 93 |
1 files changed, 0 insertions, 93 deletions
@@ -1,5 +1,4 @@ #include <stdint.h> -#include <stdbool.h> #include <string.h> #include <stdlib.h> @@ -14,45 +13,6 @@ struct page { struct page *prev; }; -struct point { - struct page *current_page; - uint16_t index; -}; - -uint16_t index_to_offset(struct page *page, uint16_t index) { - if (index < page->gap_start) { - return index; - } else { - return index + (page->gap_end - page->gap_start); - } -} - -bool at_eof(struct point *point) { - return point->index == point->current_page->element_count; -} - -void move_point_forward(struct point *point) { - struct page *page = point->current_page; - if (point->index < page->element_count) { - point->index++; - } - if (point->index == page->element_count && page->next) { - point->index = 0; - point->current_page = page->next; - } -} - -void move_point_backward(struct point *point) { - struct page *page = point->current_page; - if (point->index == 0 && page->prev) { - point->index = page->prev->element_count; - point->current_page = page->prev; - } - if (point->index > 0) { - point->index--; - } -} - struct page *new_page() { struct page *result = malloc(sizeof(struct page)); result->buffer = malloc(PAGE_SIZE); @@ -105,56 +65,3 @@ void move_gap_backward(struct page *page) { page->gap_start--; page->buffer[page->gap_end] = page->buffer[page->gap_start]; } - -void move_gap(struct page *page, uint16_t index) { - while (page->gap_end < index_to_offset(page, index)) { - move_gap_forward(page); - } - while (page->gap_end > index_to_offset(page, index)) { - move_gap_backward(page); - } -} - -void insert_at_point(struct point *point, uint8_t c) { - struct page *page = point->current_page; - if (page->gap_start == page->gap_end) { - split_page(page); - if (point->index >= PAGE_SIZE / 2) { - page = page->next; - point->current_page = page; - point->index -= PAGE_SIZE / 2; - } - } - move_gap(page, point->index); - page->buffer[page->gap_start] = c; - page->gap_start++; - page->element_count++; - move_point_forward(point); -} - -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--; - move_point_backward(point); - } -} - -uint8_t get_element(struct point *point) { - return point->current_page->buffer[index_to_offset(point->current_page, point->index)]; -} |