MCPcopy Create free account
hub / github.com/KaisenAmin/c_std / swap

Function swap

algorithm/algorithm.c:17–40  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

15
16
17static void swap(void *a, void *b, size_t size) {
18 switch (size) {
19 case 1: { unsigned char t = *(unsigned char*)a; *(unsigned char*)a = *(unsigned char*)b; *(unsigned char*)b = t; return; }
20 case 2: { unsigned char t[2]; memcpy(t, a, 2); memcpy(a, b, 2); memcpy(b, t, 2); return; }
21 case 4: { unsigned char t[4]; memcpy(t, a, 4); memcpy(a, b, 4); memcpy(b, t, 4); return; }
22 case 8: { unsigned char t[8]; memcpy(t, a, 8); memcpy(a, b, 8); memcpy(b, t, 8); return; }
23 case 16: { unsigned char t[16]; memcpy(t, a, 16); memcpy(a, b, 16); memcpy(b, t, 16); return; }
24 default: break;
25 }
26
27 unsigned char buf[256];
28 unsigned char *pa = (unsigned char *)a;
29 unsigned char *pb = (unsigned char *)b;
30 size_t remaining = size;
31 while (remaining > 0) {
32 size_t chunk = remaining < sizeof(buf) ? remaining : sizeof(buf);
33 memcpy(buf, pa, chunk);
34 memcpy(pa, pb, chunk);
35 memcpy(pb, buf, chunk);
36 pa += chunk;
37 pb += chunk;
38 remaining -= chunk;
39 }
40}
41
42
43static void reverse(void *first, void *last, size_t size) {

Callers 15

sort_heapsortFunction · 0.85
sort_bubblesortFunction · 0.85
sort_selectionFunction · 0.85
sort_reverseFunction · 0.85
sort_shuffleFunction · 0.85
sort_partitionFunction · 0.85
sort_heapifyFunction · 0.85
sort_quicksort_optimizedFunction · 0.85
sort_partition_three_wayFunction · 0.85
sort_heap_insertFunction · 0.85
reverseFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected