[algs] Add hb_swap() ala, and using, std::swap()

Use it in vector.

Use ADL idiom.
This commit is contained in:
Behdad Esfahbod 2021-11-01 17:59:17 -06:00
parent bbaccf23d9
commit 8c05569930
3 changed files with 17 additions and 14 deletions

View File

@ -34,6 +34,7 @@
#include "hb-null.hh" #include "hb-null.hh"
#include "hb-number.hh" #include "hb-number.hh"
#include <algorithm>
/* /*
* Flags * Flags
@ -533,6 +534,16 @@ struct
} }
HB_FUNCOBJ (hb_clamp); HB_FUNCOBJ (hb_clamp);
struct
{
template <typename T> void
operator () (T& a, T& b) const
{
using std::swap; // allow ADL
swap (a, b);
}
}
HB_FUNCOBJ (hb_swap);
/* /*
* Bithacks. * Bithacks.

View File

@ -274,7 +274,7 @@ struct graph_t
remap_all_obj_indices (id_map, &sorted_graph); remap_all_obj_indices (id_map, &sorted_graph);
vertices_.swap (sorted_graph); hb_swap (vertices_, sorted_graph);
sorted_graph.fini_deep (); sorted_graph.fini_deep ();
} }
@ -334,7 +334,7 @@ struct graph_t
remap_all_obj_indices (id_map, &sorted_graph); remap_all_obj_indices (id_map, &sorted_graph);
vertices_.swap (sorted_graph); hb_swap (vertices_, sorted_graph);
sorted_graph.fini_deep (); sorted_graph.fini_deep ();
} }

View File

@ -87,19 +87,11 @@ struct hb_vector_t
resize (0); resize (0);
} }
void swap (hb_vector_t& other) friend void swap (hb_vector_t& a, hb_vector_t& b)
{ {
int allocated_copy = allocated; hb_swap (a.allocated, b.allocated);
unsigned int length_copy = length; hb_swap (a.length, b.length);
Type *arrayZ_copy = arrayZ; hb_swap (a.arrayZ, b.arrayZ);
allocated = other.allocated;
length = other.length;
arrayZ = other.arrayZ;
other.allocated = allocated_copy;
other.length = length_copy;
other.arrayZ = arrayZ_copy;
} }
hb_vector_t& operator = (const hb_vector_t &o) hb_vector_t& operator = (const hb_vector_t &o)