forked from ShiqiYu/CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
specialization.cpp
115 lines (105 loc) · 2.49 KB
/
specialization.cpp
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
using namespace std;
// Class Template
template<typename T>
class MyVector
{
size_t length;
T * data;
public:
MyVector(size_t length): length(length)
{
data = new T[length * sizeof(T)]{};
}
~MyVector()
{
delete [] data;
}
MyVector(const MyVector&) = delete;
MyVector& operator=(const MyVector&) = delete;
T getElement(size_t index);
bool setElement(size_t index, T value);
};
template <typename T>
T MyVector<T>::getElement(size_t index)
{
if (index >= this->length)
{
cerr << "getElement(): Indices are out of range" << endl;
return 0;
}
return data[index];
}
template <typename T>
bool MyVector<T>::setElement(size_t index, T value)
{
if (index >= this->length)
{
cerr << "setElement(): Indices are out of range" << endl;
return false;
}
data[ index ] = value;
return true;
}
template class MyVector<int>; // Explicitly instantiate template Mat<int>
// class specialization
template<>
class MyVector<bool>
{
size_t length;
unsigned char * data;
public:
MyVector(size_t length): length(length)
{
int num_bytes = (length - 1) / 8 + 1;
data = new unsigned char[num_bytes]{};
}
~MyVector()
{
delete [] data;
}
MyVector(const MyVector&) = delete;
MyVector& operator=(const MyVector&) = delete;
bool getElement(size_t index);
bool setElement(size_t index, bool value);
};
bool MyVector<bool>::getElement(size_t index)
{
if (index >= this->length)
{
cerr << "getElement(): Indices are out of range" << endl;
return 0;
}
size_t byte_id = index / 8;
size_t bit_id = index % 8;
unsigned char mask = (1 << bit_id);
return bool(data[byte_id] & mask) ;
}
bool MyVector<bool>::setElement(size_t index, bool value)
{
if (index >= this->length)
{
cerr << "setElement(): Indices are out of range" << endl;
return false;
}
size_t byte_id = index / 8;
size_t bit_id = index % 8;
unsigned char mask = (1 << bit_id);
if (value)
data[byte_id] |= mask;
else
data[byte_id] &= ~mask;
return true;
}
int main()
{
MyVector<int> vec(16);
vec.setElement(3, 256);
cout << vec.getElement(3) << endl;
MyVector<bool> boolvec(17);
boolvec.setElement(15, false);
boolvec.setElement(16, true);
cout << boolvec.getElement(15) << endl;
cout << boolvec.getElement(16) << endl;
return 0;
}