summaryrefslogtreecommitdiff
path: root/src/piece-table.c
blob: 6db05d71aec9c9bcc2aeeafcb231cf03ec8def06 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <assert.h>
#include <stdio.h>
#include "piece-table.h"

/*
 * We probably should have a pool of pieces and go using them and deleting them
 * from there instead of malloc/free but we are lazy
 */
static void print_string(char *str, size_t size) {
    size_t i;
    printf("\"");
    for ( i=0; i < size; i++) {
        printf("%c", str[i]);
    }
    printf("\"");
}

void print_piece_table (piece_table *pt) {
    piece *cur;
    printf("piece_table {");
    printf("\n\toriginal: "); print_string(pt->orig.content, pt->orig.size);
    printf("\n\tadd: "); print_string(pt->add.content, pt->add.used);
    printf("\n\tlength: %ld", pt->length);
    printf("\n\tpieces: ");
    for ( cur = pt->sentinel->next; cur != pt->sentinel; cur = cur->next ) {
        printf("\n\t\ttext: "); print_string(cur->start, cur->length);
    }
    printf("\n}\n");
}

static piece *make_piece() {
    return malloc(sizeof(piece));
}

static piece *make_piece_list() {
    /* Makes a sentinel connected to itself in a ring*/
    piece *p = make_piece();
    p->next = p;
    p->prev = p;
    p->length = 0;
    p->start  = NULL;
    return p;
}

void init_piece_table(piece_table *pt, char *orig) {
    piece *pc;

    init_growable_buffer(&pt->add);
    init_fixed_buffer(&pt->orig, orig);

    pt->length = pt->orig.size;

    pt->sentinel = make_piece_list();

    pc = make_piece();
    pc->start = pt->orig.content;
    pc->length = pt->orig.size;
    pc->prev = pt->sentinel;
    pc->next = pt->sentinel;

    pt->sentinel->next = pc;
    pt->sentinel->prev = pc;

    pt->cached_offset = 0;
    pt->cached = pc;
}


static void free_piece_list(piece *p) {
    piece *cur, *next;
    assert(p != NULL);
    for ( cur = p->next; cur != p; cur = next ) {
        next = cur->next;
        free(cur);
    }
    free(cur);
}

void free_piece_table(piece_table *pt) {
    free_growable_buffer(&pt->add);
    free_piece_list(pt->sentinel);
    pt->sentinel = NULL;
    pt->cached = NULL;
    pt->cached_offset = 0;
    pt->length = 0;
}


static void find_piece_by_pos(piece_table *pt, size_t pos) {
    piece *cur;
    assert(pos < pt->length);
    if ( pos >= pt->cached_offset ) {
        /* Search forward */
        for ( cur = pt->cached; ; cur = cur->next ) {
            if ( pt->cached_offset + cur->length > pos ) {
                pt->cached = cur;
                assert(pt->cached != pt->sentinel);
                return;
            }
            pt->cached_offset += cur->length;
        }
    } else {
        /* Search backwards */
        for ( cur = pt->cached->prev; ; cur = cur->prev ) {
            pt->cached_offset -= cur->length;
            if ( pt->cached_offset < pos ) {
                pt->cached = cur;
                assert(pt->cached != pt->sentinel);
                return;
            }
        }
    }
}

char index_piece_table(piece_table *pt, size_t pos) {
    assert(pos < pt->length);
    find_piece_by_pos(pt, pos);
    return pt->cached->start[pos - pt->cached_offset];
}


static piece *split_piece(piece *p, size_t pos) {
    /* Returns the first piece */
    piece *second;
    assert(pos < p->length);
    assert(p->start != NULL); /* Not a sentinel piece */
    assert(p->start != 0);    /* Not an empty piece (they should not exist) */
    if ( pos == 0 ) {
        return p->prev;
    }
    second = make_piece();
    second->length = p->length - pos;
    p->length = pos;
    second->next = p->next;
    p->next = second;
    second->prev = p;
    second->start = p->start + pos;
    return p;
}

void insert_piece_table(piece_table *pt, char val, size_t pos) {
    /* At the moment inserts only one char */
    // piece *cur;
    assert(pos <= pt->length );

    append_growable_buffer(&pt->add, &val, 1);
    // TODO
}

void delete_piece_table(piece_table *pt, size_t pos, size_t len) {
    piece *cur, *next, *start, *end;
    assert( pos + len <= pt->length );

    if ( pos == 0 ) {
        start = pt->sentinel;
    } else {
        find_piece_by_pos(pt, pos);
        start = split_piece(pt->cached, pos - pt->cached_offset);
    }

    if ( pos + len == pt->length ) {
        end = pt->sentinel;
    } else {
        find_piece_by_pos(pt, pos + len);
        end = split_piece(pt->cached, pos + len - pt->cached_offset)->next;
    }

    /* Free unneeded pieces */
    for ( cur = start->next; cur != end ; cur = next ) {
        next = cur->next;
        free(cur);
    }

    start->next = end;
    end->prev = start;
    pt->length -= len;
}