From 8c05569930b3ba8901baae7ffb9ae2a6fc6ce4b1 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 1 Nov 2021 17:59:17 -0600 Subject: [PATCH] [algs] Add hb_swap() ala, and using, std::swap() Use it in vector. Use ADL idiom. --- src/hb-algs.hh | 11 +++++++++++ src/hb-repacker.hh | 4 ++-- src/hb-vector.hh | 16 ++++------------ 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/hb-algs.hh b/src/hb-algs.hh index bbe097fe0..fc1aeb0bf 100644 --- a/src/hb-algs.hh +++ b/src/hb-algs.hh @@ -34,6 +34,7 @@ #include "hb-null.hh" #include "hb-number.hh" +#include /* * Flags @@ -533,6 +534,16 @@ struct } HB_FUNCOBJ (hb_clamp); +struct +{ + template void + operator () (T& a, T& b) const + { + using std::swap; // allow ADL + swap (a, b); + } +} +HB_FUNCOBJ (hb_swap); /* * Bithacks. diff --git a/src/hb-repacker.hh b/src/hb-repacker.hh index 7339eeb2c..72b3185f2 100644 --- a/src/hb-repacker.hh +++ b/src/hb-repacker.hh @@ -274,7 +274,7 @@ struct graph_t remap_all_obj_indices (id_map, &sorted_graph); - vertices_.swap (sorted_graph); + hb_swap (vertices_, sorted_graph); sorted_graph.fini_deep (); } @@ -334,7 +334,7 @@ struct graph_t remap_all_obj_indices (id_map, &sorted_graph); - vertices_.swap (sorted_graph); + hb_swap (vertices_, sorted_graph); sorted_graph.fini_deep (); } diff --git a/src/hb-vector.hh b/src/hb-vector.hh index 95b5295ba..44c174d07 100644 --- a/src/hb-vector.hh +++ b/src/hb-vector.hh @@ -87,19 +87,11 @@ struct hb_vector_t resize (0); } - void swap (hb_vector_t& other) + friend void swap (hb_vector_t& a, hb_vector_t& b) { - int allocated_copy = allocated; - unsigned int length_copy = length; - Type *arrayZ_copy = arrayZ; - - allocated = other.allocated; - length = other.length; - arrayZ = other.arrayZ; - - other.allocated = allocated_copy; - other.length = length_copy; - other.arrayZ = arrayZ_copy; + hb_swap (a.allocated, b.allocated); + hb_swap (a.length, b.length); + hb_swap (a.arrayZ, b.arrayZ); } hb_vector_t& operator = (const hb_vector_t &o)