refactor(*): use doubly linked lists

This commit is contained in:
Jeremy Attali 2019-12-27 15:33:16 -05:00
parent 75bfc10fb7
commit 5678545de5
5 changed files with 24 additions and 24 deletions

View File

@ -9,4 +9,4 @@ void paint_commit_temporary(struct swappy_state *state);
void paint_free(gpointer data);
void paint_free_all(struct swappy_state *state);
void paint_free_list(GSList **list);
void paint_free_list(GList **list);

View File

@ -49,7 +49,7 @@ struct swappy_paint_brush {
double b;
double a;
double w;
GSList *points;
GList *points;
};
struct swappy_paint {
@ -127,8 +127,8 @@ struct swappy_state {
struct swappy_box *geometry;
GSList *paints;
GSList *redo_paints;
GList *paints;
GList *redo_paints;
struct swappy_paint *temp_paint;
struct swappy_state_painting painting;

View File

@ -15,8 +15,8 @@
static void update_ui_undo_redo(struct swappy_state *state) {
GtkWidget *undo = GTK_WIDGET(state->ui->undo);
GtkWidget *redo = GTK_WIDGET(state->ui->redo);
gboolean undo_sensitive = g_slist_length(state->paints) > 0;
gboolean redo_sensitive = g_slist_length(state->redo_paints) > 0;
gboolean undo_sensitive = g_list_length(state->paints) > 0;
gboolean redo_sensitive = g_list_length(state->redo_paints) > 0;
gtk_widget_set_sensitive(undo, undo_sensitive);
gtk_widget_set_sensitive(redo, redo_sensitive);
}
@ -29,11 +29,11 @@ static void update_ui_stroke_size_widget(struct swappy_state *state) {
}
static void action_undo(struct swappy_state *state) {
GSList *first = state->paints;
GList *first = state->paints;
if (first) {
state->paints = g_slist_remove_link(state->paints, first);
state->redo_paints = g_slist_prepend(state->redo_paints, first->data);
state->paints = g_list_remove_link(state->paints, first);
state->redo_paints = g_list_prepend(state->redo_paints, first->data);
render_state(state);
update_ui_undo_redo(state);
@ -41,11 +41,11 @@ static void action_undo(struct swappy_state *state) {
}
static void action_redo(struct swappy_state *state) {
GSList *first = state->redo_paints;
GList *first = state->redo_paints;
if (first) {
state->redo_paints = g_slist_remove_link(state->redo_paints, first);
state->paints = g_slist_prepend(state->paints, first->data);
state->redo_paints = g_list_remove_link(state->redo_paints, first);
state->paints = g_list_prepend(state->paints, first->data);
render_state(state);
update_ui_undo_redo(state);

View File

@ -9,16 +9,16 @@ void paint_free(gpointer data) {
switch (paint->type) {
case SWAPPY_PAINT_MODE_BRUSH:
g_slist_free_full(paint->content.brush.points, g_free);
g_list_free_full(paint->content.brush.points, g_free);
break;
default:
g_free(paint);
}
}
void paint_free_list(GSList **list) {
void paint_free_list(GList **list) {
if (*list) {
g_slist_free_full(*list, paint_free);
g_list_free_full(*list, paint_free);
*list = NULL;
}
}
@ -57,7 +57,7 @@ void paint_add_temporary(struct swappy_state *state, double x, double y,
brush->x = x;
brush->y = y;
paint->content.brush.points = g_slist_prepend(NULL, brush);
paint->content.brush.points = g_list_prepend(NULL, brush);
break;
case SWAPPY_PAINT_MODE_RECTANGLE:
case SWAPPY_PAINT_MODE_ELLIPSE:
@ -88,7 +88,7 @@ void paint_add_temporary(struct swappy_state *state, double x, double y,
void paint_update_temporary(struct swappy_state *state, double x, double y) {
struct swappy_paint *paint = state->temp_paint;
struct swappy_point *brush;
GSList *points;
GList *points;
if (!paint) {
return;
@ -101,7 +101,7 @@ void paint_update_temporary(struct swappy_state *state, double x, double y) {
brush->x = x;
brush->y = y;
paint->content.brush.points = g_slist_prepend(points, brush);
paint->content.brush.points = g_list_prepend(points, brush);
break;
case SWAPPY_PAINT_MODE_RECTANGLE:
case SWAPPY_PAINT_MODE_ELLIPSE:
@ -126,10 +126,10 @@ void paint_commit_temporary(struct swappy_state *state) {
if (!paint->can_draw) {
paint_free(paint);
} else {
state->paints = g_slist_prepend(state->paints, paint);
state->paints = g_list_prepend(state->paints, paint);
}
// Set the temporary paint to NULL but keep the content in memory
// because it's now part of the GSList.
// because it's now part of the GList.
state->temp_paint = NULL;
}

View File

@ -207,14 +207,14 @@ static void render_brush(cairo_t *cr, struct swappy_paint_brush brush) {
cairo_set_line_width(cr, brush.w);
cairo_set_line_join(cr, CAIRO_LINE_JOIN_BEVEL);
guint l = g_slist_length(brush.points);
guint l = g_list_length(brush.points);
if (l == 1) {
struct swappy_point *point = g_slist_nth_data(brush.points, 0);
struct swappy_point *point = g_list_nth_data(brush.points, 0);
cairo_rectangle(cr, point->x, point->y, brush.w, brush.w);
cairo_fill(cr);
} else {
for (GSList *elem = brush.points; elem; elem = elem->next) {
for (GList *elem = brush.points; elem; elem = elem->next) {
struct swappy_point *point = elem->data;
cairo_line_to(cr, point->x, point->y);
}
@ -243,7 +243,7 @@ static void render_paint(cairo_t *cr, struct swappy_paint *paint) {
}
static void render_paints(cairo_t *cr, struct swappy_state *state) {
for (GSList *elem = state->paints; elem; elem = elem->next) {
for (GList *elem = state->paints; elem; elem = elem->next) {
struct swappy_paint *paint = elem->data;
render_paint(cr, paint);
}