summaryrefslogtreecommitdiff
path: root/page.c
blob: 65d344fdf9a782901072c10bd0868bd1f0e31cf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <stdint.h>
#include <string.h>
#include <stdlib.h>

#define PAGE_SIZE 4096

struct page {
	uint8_t *buffer;
	uint16_t gap_start;
	uint16_t gap_end;
	uint16_t element_count;
	struct page *next;
	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);
	}
}

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);
	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->buffer, back->buffer + 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 free_page(struct page *page) {
	if (page->prev) {
		page->prev->next = page->next;
	}
	if (page->next) {
		page->next->prev = page->prev;
	}
	free(page);
}

void move_gap_forward(struct page *page) {
	page->buffer[page->gap_start] = page->buffer[page->gap_end];
	page->gap_start++;
	page->gap_end++;
}

void move_gap_backward(struct page *page) {
	page->gap_end--;
	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)];
}