diff options
author | Juan Manuel Tomás <jtomas1815@gmail.com> | 2020-05-24 05:35:00 -0300 |
---|---|---|
committer | Juan Manuel Tomás <jtomas1815@gmail.com> | 2020-05-24 05:35:00 -0300 |
commit | 98280238383dc390207827d09dc92e0459229134 (patch) | |
tree | e2bc7c0877289165463dd6f9c2e77b570b22e29c /page.c | |
parent | d37027bbd7ac13fdd0f1e2f01e1ec4b75b6c9588 (diff) | |
download | jet-98280238383dc390207827d09dc92e0459229134.tar.gz jet-98280238383dc390207827d09dc92e0459229134.zip |
Rewrite in c++
Diffstat (limited to 'page.c')
-rw-r--r-- | page.c | 93 |
1 files changed, 0 insertions, 93 deletions
@@ -1,93 +0,0 @@ -#include <stdint.h> -#include <string.h> -#include <stdlib.h> -#include <assert.h> - -#ifndef PAGE_SIZE -#define PAGE_SIZE 4096 -#endif - -struct page { - uint8_t *elements; - struct page *next; - struct page *prev; - uint16_t gap_start; - uint16_t gap_end; - uint16_t element_count; -}; - -struct page *new_page() { - struct page *result = malloc(sizeof(struct page)); - result->elements = malloc(PAGE_SIZE); - result->gap_start = 0; - result->gap_end = PAGE_SIZE; - result->next = 0; - result->prev = 0; - return result; -} - -void split_page(struct page *back) { - struct page *front = new_page(); - - memcpy(front->elements, back->elements + PAGE_SIZE / 2, PAGE_SIZE / 2); - - front->gap_start = PAGE_SIZE / 2; - front->gap_end = PAGE_SIZE; - front->element_count = PAGE_SIZE / 2; - - back->gap_start = PAGE_SIZE / 2; - back->gap_end = PAGE_SIZE; - back->element_count = PAGE_SIZE / 2; - - if (back->next) { - back->next->prev = front; - } - front->next = back->next; - front->prev = back; - back->next = front; -} - -void copy_page(struct page *dest, struct page *src) { - memcpy(dest->elements, src->elements, PAGE_SIZE); - dest->gap_start = src->gap_start; - dest->gap_end = src->gap_end; - dest->element_count = src->element_count; -} - -void free_page(struct page *page) { - if (page->prev) { - page->prev->next = page->next; - } - if (page->next) { - page->next->prev = page->prev; - } - free(page->elements); - free(page); -} - -void move_gap_forward(struct page *page) { - assert(page->gap_end < PAGE_SIZE); - page->elements[page->gap_start] = page->elements[page->gap_end]; - page->gap_start++; - page->gap_end++; -} - -void move_gap_backward(struct page *page) { - assert(page->gap_start > 0); - page->gap_end--; - page->gap_start--; - page->elements[page->gap_end] = page->elements[page->gap_start]; -} - -void insert_at_gap(struct page *page, uint8_t c) { - assert(page->element_count < PAGE_SIZE); - page->elements[page->gap_start] = c; - page->gap_start++; - page->element_count++; -} - -void delete_at_gap(struct page *page) { - assert(page->gap_start > 0); - page->gap_start--; - page->element_count--; -} |