blob: 1de3eef694eaecb1f6e5cc985719551b1082a833 (
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
|
#include <stdint.h>
#include <stdbool.h>
struct point {
struct page *page;
uint16_t index;
};
uint16_t index_to_offset(struct point *point) {
if (point->index < point->page->gap_start) {
return point->index;
} else {
return point->index + (point->page->gap_end - point->page->gap_start);
}
}
uint8_t element(struct point *point) {
if (point->index == point->page->element_count) {
return point->page->next->elements[0];
} else {
return point->page->elements[index_to_offset(point)];
}
}
bool at_eof(struct point *point) {
return point->index == point->page->element_count && !point->page->next;
}
void move_point_forward(struct point *point) {
if (point->index < point->page->element_count) {
point->index++;
} else if (point->page->next) {
point->index = 1;
point->page = point->page->next;
}
}
void move_point_backward(struct point *point) {
if (point->index > 1) {
point->index--;
} else if (point->page->prev) {
point->page = point->page->prev;
point->index = point->page->element_count;
} else {
point->index = 0;
}
}
void align_gap(struct point *point) {
while (point->page->gap_end < index_to_offset(point)) {
move_gap_forward(point->page);
}
while (point->page->gap_end > index_to_offset(point)) {
move_gap_backward(point->page);
}
}
void insert_at_point(struct point *point, uint8_t c) {
if (point->page->gap_start == point->page->gap_end) {
split_page(point->page);
if (point->index >= PAGE_SIZE / 2) {
point->page = point->page->next;
point->index -= PAGE_SIZE / 2;
}
}
align_gap(point);
insert_at_gap(point->page, c);
move_point_forward(point);
}
void delete_at_point(struct point *point) {
if (point->page->element_count == 1 && point->index == 1) {
if (point->page->prev) {
move_point_backward(point);
free_page(point->page->next);
} else if (point->page->next) {
copy_page(point->page, point->page->next);
free_page(point->page->next);
point->index = 0;
} else {
align_gap(point);
delete_at_gap(point->page);
move_point_backward(point);
}
} else if (point->index > 0) {
align_gap(point);
delete_at_gap(point->page);
move_point_backward(point);
}
}
|