From 8c8de657d610f3ce471d9f18596a18001a425809 Mon Sep 17 00:00:00 2001 From: Arkadiy Illarionov Date: Sun, 10 Mar 2024 14:57:45 +0300 Subject: [PATCH] Use std::swap instead of temporary values --- src/collect-table.cc | 10 +++------ src/collect.cc | 6 ++---- src/filedata.cc | 5 ++--- src/layout.cc | 7 ++---- src/pixbuf-util.cc | 38 ++++++++++++++------------------- src/renderer-tiles.cc | 6 ++---- src/slideshow.cc | 11 ++-------- src/view-file/view-file-icon.cc | 14 ++++-------- 8 files changed, 33 insertions(+), 64 deletions(-) diff --git a/src/collect-table.cc b/src/collect-table.cc index 874d15d6..0d3016fa 100644 --- a/src/collect-table.cc +++ b/src/collect-table.cc @@ -22,6 +22,7 @@ #include "collect-table.h" #include +#include #include #include @@ -511,7 +512,6 @@ static void collection_table_select_region_util(CollectTable *ct, CollectInfo *s gint col1; gint row2; gint col2; - gint t; gint i; gint j; @@ -548,15 +548,11 @@ static void collection_table_select_region_util(CollectTable *ct, CollectInfo *s if (row2 < row1) { - t = row1; - row1 = row2; - row2 = t; + std::swap(row1, row2); } if (col2 < col1) { - t = col1; - col1 = col2; - col2 = t; + std::swap(col1, col2); } DEBUG_1("table: %d x %d to %d x %d", row1, col1, row2, col2); diff --git a/src/collect.cc b/src/collect.cc index 2f7ca476..5b9a62fd 100644 --- a/src/collect.cc +++ b/src/collect.cc @@ -26,6 +26,7 @@ #include #include #include +#include #include @@ -214,7 +215,6 @@ GList *collection_list_randomize(GList *list) guint random; guint length; guint i; - gpointer tmp; GList *nlist; GList *olist; @@ -228,9 +228,7 @@ GList *collection_list_randomize(GList *list) random = static_cast(1.0 * length * rand()/(RAND_MAX + 1.0)); olist = g_list_nth(list, i); nlist = g_list_nth(list, random); - tmp = olist->data; - olist->data = nlist->data; - nlist->data = tmp; + std::swap(olist->data, nlist->data); } return list; diff --git a/src/filedata.cc b/src/filedata.cc index d66e80b1..c0a04ab3 100644 --- a/src/filedata.cc +++ b/src/filedata.cc @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -1171,9 +1172,7 @@ gint filelist_sort_compare_filedata(FileData *fa, FileData *fb) gint ret; if (!filelist_sort_ascend) { - FileData *tmp = fa; - fa = fb; - fb = tmp; + std::swap(fa, fb); } switch (filelist_sort_method) diff --git a/src/layout.cc b/src/layout.cc index c478a79c..f955399d 100644 --- a/src/layout.cc +++ b/src/layout.cc @@ -24,6 +24,7 @@ #include #include +#include #include #include @@ -1915,11 +1916,7 @@ static void layout_grid_setup(LayoutWindow *lw) if (!layout_location_vertical(static_cast(priority_location))) { - GtkWidget *tmp; - - tmp = v; - v = h; - h = tmp; + std::swap(v, h); } gq_gtk_box_pack_start(GTK_BOX(lw->group_box), v, TRUE, TRUE, 0); diff --git a/src/pixbuf-util.cc b/src/pixbuf-util.cc index 35abc2fd..0d87b7fd 100644 --- a/src/pixbuf-util.cc +++ b/src/pixbuf-util.cc @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -1044,7 +1045,6 @@ void pixbuf_draw_triangle(GdkPixbuf *pb, gint slope1_x; gint slope1_y; gint y; - gint t; gboolean middle = FALSE; if (!pb) return; @@ -1073,18 +1073,18 @@ void pixbuf_draw_triangle(GdkPixbuf *pb, if (y1 > y2) { - t = x1; x1 = x2; x2 = t; - t = y1; y1 = y2; y2 = t; + std::swap(x1, x2); + std::swap(y1, y2); } if (y2 > y3) { - t = x2; x2 = x3; x3 = t; - t = y2; y2 = y3; y3 = t; + std::swap(x2, x3); + std::swap(y2, y3); } if (y1 > y2) { - t = x1; x1 = x2; x2 = t; - t = y1; y1 = y2; y2 = t; + std::swap(x1, x2); + std::swap(y1, y2); } slope1 = static_cast(y2 - y1); @@ -1114,7 +1114,7 @@ void pixbuf_draw_triangle(GdkPixbuf *pb, if (xa > xb) { - t = xa; xa = xb; xb = t; + std::swap(xa, xb); } xa = CLAMP(xa, fx1, fx2); @@ -1149,10 +1149,8 @@ static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, g if (x1 > x2) { - gdouble t; - - t = x1; x1 = x2; x2 = t; - t = y1; y1 = y2; y2 = t; + std::swap(x1, x2); + std::swap(y1, y2); flip = TRUE; } @@ -1191,12 +1189,10 @@ static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, g } else { - gdouble t; - if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE; - t = x1; x1 = x2; x2 = t; - t = y1; y1 = y2; y2 = t; + std::swap(x1, x2); + std::swap(y1, y2); flip = !flip; } @@ -1293,9 +1289,8 @@ void pixbuf_draw_line(GdkPixbuf *pb, { if (rx1 > rx2) { - gdouble t; - t = rx1; rx1 = rx2; rx2 = t; - t = ry1; ry1 = ry2; ry2 = t; + std::swap(rx1, rx2); + std::swap(ry1, ry2); } slope = rx2 - rx1; @@ -1320,9 +1315,8 @@ void pixbuf_draw_line(GdkPixbuf *pb, { if (ry1 > ry2) { - gdouble t; - t = rx1; rx1 = rx2; rx2 = t; - t = ry1; ry1 = ry2; ry2 = t; + std::swap(rx1, rx2); + std::swap(ry1, ry2); } slope = ry2 - ry1; diff --git a/src/renderer-tiles.cc b/src/renderer-tiles.cc index bb859776..5e3068c5 100644 --- a/src/renderer-tiles.cc +++ b/src/renderer-tiles.cc @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -1512,14 +1513,11 @@ static void rt_tile_render(RendererTiles *rt, ImageTile *it, switch (orientation) { - gdouble tmp; case EXIF_ORIENTATION_LEFT_TOP: case EXIF_ORIENTATION_RIGHT_TOP: case EXIF_ORIENTATION_RIGHT_BOTTOM: case EXIF_ORIENTATION_LEFT_BOTTOM: - tmp = scale_x; - scale_x = scale_y; - scale_y = tmp; + std::swap(scale_x, scale_y); break; default: /* nothing to do */ diff --git a/src/slideshow.cc b/src/slideshow.cc index 65e48f19..56934be4 100644 --- a/src/slideshow.cc +++ b/src/slideshow.cc @@ -22,6 +22,7 @@ #include "slideshow.h" #include +#include #include "collect.h" #include "filedata.h" @@ -93,21 +94,13 @@ static GPtrArray *generate_ptr_array_from_list(GList *src_list) return arr; } -static void swap(GPtrArray *array, guint index1, guint index2) -{ - gpointer temp = g_ptr_array_index(array, index1); - - g_ptr_array_index(array, index1) = g_ptr_array_index(array, index2); - g_ptr_array_index(array, index2) = temp; -} - static void ptr_array_random_shuffle(GPtrArray *array) { guint i; for (i = 0; i < array->len; ++i) { guint p = static_cast(rand()) / (static_cast(RAND_MAX) + 1.0) * array->len; - swap(array, i, p); + std::swap(g_ptr_array_index(array, i), g_ptr_array_index(array, p)); } } diff --git a/src/view-file/view-file-icon.cc b/src/view-file/view-file-icon.cc index 1a61eaca..fe7c180b 100644 --- a/src/view-file/view-file-icon.cc +++ b/src/view-file/view-file-icon.cc @@ -22,6 +22,7 @@ #include "view-file-icon.h" #include +#include #include @@ -772,7 +773,6 @@ static void vficon_select_region_util(ViewFile *vf, FileData *start, FileData *e gint col1; gint row2; gint col2; - gint t; gint i; gint j; @@ -787,9 +787,7 @@ static void vficon_select_region_util(ViewFile *vf, FileData *start, FileData *e if (g_list_index(vf->list, start) > g_list_index(vf->list, end)) { - FileData *tmp = start; - start = end; - end = tmp; + std::swap(start, end); } work = g_list_find(vf->list, start); @@ -809,15 +807,11 @@ static void vficon_select_region_util(ViewFile *vf, FileData *start, FileData *e // rectangular_selection==true. if (row2 < row1) { - t = row1; - row1 = row2; - row2 = t; + std::swap(row1, row2); } if (col2 < col1) { - t = col1; - col1 = col2; - col2 = t; + std::swap(col1, col2); } DEBUG_1("table: %d x %d to %d x %d", row1, col1, row2, col2); -- 2.20.1