This repository was archived by the owner on Apr 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvmanip.t.h++
87 lines (65 loc) · 1.94 KB
/
vmanip.t.h++
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
79
80
81
82
83
84
85
86
87
//
// vmanip.t.h++
// bigcub
//
// Created by Andy Pilate on 29/01/15.
// Copyright (c) 2015 Andy Pilate. All rights reserved.
//
#ifndef vmanip_t_h
#define vmanip_t_h
#include <climits>
#include "vmanip.h++"
// Convert an integer into a Type of the same value
template<typename T, typename>
vmanip::Type vmanip::intImport(T const &n) {
Type toReturn;
if (std::is_unsigned<T>::value) { // Unsigned, see lower
toReturn.resize((sizeof(n) * CHAR_BIT) + 1);
}
else {
toReturn.resize(sizeof(n) * CHAR_BIT);
}
size_type i = 0;
for (; i < sizeof(n) * CHAR_BIT; ++i) {
toReturn[i] = (n >> i) & 1;
}
if (std::is_unsigned<T>::value) { // If unsigned, we need to convert into
toReturn[i] = false; // signed form.
}
return toReturn;
}
// Convert a Type to an integer of the same value.
// The required integer is specified via the template parameter
template<typename T, typename>
T vmanip::intExport(Type const &n) {
T toReturn = 0;
if (n.size() == 0) {
return toReturn;
}
size_type i = 0;
for (; (i < n.size()) && i < sizeof(T) * CHAR_BIT; ++i) {
toReturn |= (static_cast<T>(n[i]) << i);
}
// if the number is negative, fill the remaining bits to one
for (; n.back() == true && i < sizeof(T) * CHAR_BIT; ++i) {
toReturn |= (static_cast<T>(n.back()) << i);
}
return toReturn;
}
// Apply the passed function to each bits in a and b
template<class BinaryOperation>
void vmanip::transform(Type a, Type b, Type &dest, BinaryOperation op) {
normalize(a, b);
dest.resize(a.size());
for (size_type i = 0; i < dest.size(); ++i) {
dest[i] = op(a[i], b[i]);
}
}
// Apply the passed function to each bits in dest
template<class UnaryOperation>
void vmanip::transform(Type &dest, UnaryOperation op) {
for (auto i : dest) {
dest[i] = op();
}
}
#endif