#include #include #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; }