-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswap.cpp
More file actions
78 lines (63 loc) · 1.75 KB
/
Copy pathswap.cpp
File metadata and controls
78 lines (63 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* swap two bytes without temporary memory swap(byte a, byte b):
* ALG: a^=b; b^=a; a^=b;
*/
// binary integers like 0b000100 is a gcc extension
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
using std::ostream;
template <class Integral>
void swap(Integral& a, Integral& b) {
a^=b;
b^=a;
a^=b;
}
/*
* using the swap function to swap buffer (raw memory)
*/
void swapBuffer(char* buf1, char* buf2, size_t sz) {
size_t l = sizeof(long long); // assert that sizeof(char)=1
while(sz>=l) {
swap(*reinterpret_cast<long long*>(buf1),*reinterpret_cast<long long*>(buf2));
buf1+=l;
buf2+=l;
sz-=l;
}
while(sz>0){
swap(*buf1++, *buf2++);
--sz;
}
}
struct TestClass {
int _len;
string _name;
TestClass(const string& name) : _name(name), _len(name.size()) {
}
};
ostream& operator<< (ostream& os, const TestClass& obj) {
os << obj._name;
return os;
}
int main(){
unsigned char uChar1=10, uChar2=20;
cout << int(uChar1) << " : " << int(uChar2) << endl;
swap(uChar1, uChar2);
cout << int(uChar1) << " : " << int(uChar2) << endl;
int i1=-2, i2=2;
cout << i1 << " : " << i2 << endl;
swap(i1, i2);
cout << i1 << " : " << i2 << endl;
char str1[] = {'H', 'e', 'l', 'l', 'o', 0};
char str2[] = {'W', 'o', 'r', 'l', 'd', 0};
cout << str1 << " : " << str2 << endl;
swapBuffer(str1, str2, 5);
cout << str1 << " : " << str2 << endl;
TestClass t1("Rong"), t2("Xiao");
cout << t1 << " : " << t2 << endl;
swapBuffer(reinterpret_cast<char*>(&t1), reinterpret_cast<char*>(&t2), sizeof(TestClass)); // swap two objects as raw buffer
cout << t1 << " : " << t2 << endl;
return 0;
}