-
Notifications
You must be signed in to change notification settings - Fork 0
/
value.cc
206 lines (157 loc) · 4.49 KB
/
value.cc
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Copyright CERN 2016
* @author Maciej Suminski ([email protected])
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "value.h"
#include <functional>
#include <sstream>
#include <cstring>
using namespace std;
Value::Value(const std::vector<bit_t>&val)
: type(VECTOR), size(val.size()) {
data.vec = new bit_t[size];
memcpy(data.vec, &val[0], size * sizeof(bit_t));
}
Value::Value(data_type_t data_type)
: type(data_type), size(1) {
switch(data_type) {
case BIT:
data.bit = UNINITIALIZED;
break;
case VECTOR:
data.vec = new bit_t[1];
data.vec[0] = UNINITIALIZED;
break;
case REAL:
data.real = 0.0f;
break;
case UNDEFINED:
memset(&data, 0, sizeof(data));
break;
default:
assert(false);
break;
}
}
Value::Value(const string&val)
: type(VECTOR), size(val.size()) {
data.vec = new bit_t[size];
memcpy(data.vec, val.c_str(), size * sizeof(bit_t));
}
Value::Value(const Value&other)
: type(other.type), size(other.size) {
if(type == VECTOR) {
data.vec = new bit_t[size];
memcpy(data.vec, other.data.vec, size * sizeof(bit_t));
} else {
data = other.data;
}
}
void Value::resize(unsigned int new_size) {
assert(type == VECTOR);
assert(new_size >= size);
if(new_size == size)
return;
unsigned int size_diff = new_size - size;
bit_t*new_vec = new bit_t[new_size];
memset(new_vec, '0', size_diff);
memcpy(new_vec + size_diff, data.vec, size * sizeof(bit_t));
delete [] data.vec;
data.vec = new_vec;
size = new_size;
}
size_t Value::hash() const {
size_t res = 0;
switch(type) {
case BIT:
res = std::hash<bit_t>()(data.bit);
break;
case VECTOR:
for(unsigned int i = 0; i < size; ++i)
res += std::hash<bit_t>()(data.vec[i]);
break;
case REAL:
res = std::hash<float>()(data.real);
break;
default:
assert(false);
break;
}
return res;
}
Value&Value::operator=(const Value&other) {
assert(type == other.type || type == UNDEFINED);
assert(size >= other.size || type == UNDEFINED);
size = other.size;
if(type == UNDEFINED) {
type = other.type;
if(type == VECTOR) {}
data.vec = new bit_t[other.size];
}
if(type == VECTOR) {
memcpy(data.vec, other.data.vec, size * sizeof(bit_t));
} else {
data = other.data;
}
return *this;
}
bool Value::operator==(const Value&other) const {
if(type != other.type)
return false;
if(size != other.size)
return false;
switch(type) {
case BIT:
return data.bit == other.data.bit;
case VECTOR:
return !strncmp(data.vec, other.data.vec, size);
case REAL:
return data.real == other.data.real;
default:
assert(false);
break;
}
return false;
}
bool Value::operator!=(const Value&other) const {
return !(*this == other);
}
Value::operator string() const {
switch(type) {
case BIT:
return string(&data.bit, 1);
case VECTOR:
return string(data.vec, size);
case REAL:
{
stringstream s;
s << data.real;
return s.str();
}
case UNDEFINED:
// UNDEFINED should be a temporary state, do not expect any
// variable to print itself out when undefined
assert(false);
return string("<undefined>");
}
// Fallback, should never happen
assert(false);
return string();
}
ostream&operator<<(ostream&out, const Value&var)
{
out << (string) var;
return out;
}
const bit_t Value::UNINITIALIZED = '?';