This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
data_packet.cpp
184 lines (164 loc) · 4.72 KB
/
data_packet.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
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
// Copyright (c) 2017 Baidu Inc. All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "data_packet.h"
namespace simulator {
ValuePtr DataBuffer::get_value() {
// update the value pointer before every read and write
sync_value_ptr();
return v_;
}
void StateBuffer::copy_from(BufferPtr buffer) {
reals_ = nullptr;
pixels_ = nullptr;
id_ = nullptr;
str_ = nullptr;
// copy value
if (buffer->get_value_size()) {
auto data = buffer->get_value();
init_value(buffer->get_value_size(), 1, data->is_uint8());
auto dst_data = get_value();
for (size_t i = 0; i < buffer->get_value_size(); i++) {
dst_data->set_value(i, data->get_value(i));
}
}
// copy id
if (buffer->get_id_size()) {
int* id = buffer->get_id();
set_id(id, id + buffer->get_id_size());
}
// copy string
if (buffer->get_str()) {
set_str(*buffer->get_str());
}
}
void StateBuffer::init_value(size_t w, size_t h, bool pixels) {
CHECK_EQ(h, 1UL) << "StateBuffer value always has unit height";
if (pixels) {
pixels_ = std::make_shared<std::vector<uint8_t>>(w, 0);
} else {
reals_ = std::make_shared<std::vector<float>>(w, 0);
}
}
void StateBuffer::init_id(size_t sz) {
id_ = std::make_shared<std::vector<int>>(sz, 0);
}
void StateBuffer::init_str() {
//// TODO: figure out why the first line does not work
//// (invalid memory string address after creation)
// str_ = std::make_shared<std::string>("");
str_.reset(new std::string(""));
}
void StateBuffer::sync_value_ptr() {
if (reals_) {
CHECK_GT(reals_->size(), 0);
// we always have floating values in state buffers
CHECK(std::type_index(typeid(*(reals_->data()))) ==
std::type_index(typeid(float)));
v_->f_val_ = reals_->data();
} else if (pixels_) {
CHECK_GT(pixels_->size(), 0);
v_->c_val_ = pixels_->data();
}
// else do nothing
}
int* StateBuffer::get_id() {
if (id_) {
return id_->data();
} else {
return 0;
}
}
std::string* StateBuffer::get_str() {
if (str_) {
return str_.get();
} else {
return 0;
}
}
size_t StateBuffer::get_value_width() {
if (reals_) {
return reals_->size();
} else if (pixels_) {
return pixels_->size();
} else {
return 0;
}
}
size_t StateBuffer::get_value_height() {
// state buffer always have unit value height
if (reals_ || pixels_) {
return 1;
} else {
return 0;
}
}
size_t StateBuffer::get_id_size() {
if (id_) {
return id_->size();
} else {
return 0;
}
}
bool StateBuffer::operator==(const StateBuffer& other) {
if (!pointer_compare(reals_, other.reals_) ||
!pointer_compare(pixels_, other.pixels_) ||
!pointer_compare(id_, other.id_) ||
!pointer_compare(str_, other.str_)) {
return false;
}
return true;
}
void StateBuffer::encode(util::BinaryBuffer& buf) const {
uint8_t flags = (reals_ ? BIT_REALS : 0) | (pixels_ ? BIT_PIXELS : 0) |
(id_ ? BIT_ID : 0) | (str_ ? BIT_STR : 0);
buf.append(flags);
if (reals_) {
buf.append(*reals_);
}
if (pixels_) {
buf.append(*pixels_);
}
if (id_) {
buf.append(*id_);
}
if (str_) {
buf.append(*str_);
}
}
void StateBuffer::decode(util::BinaryBuffer& buf) {
uint8_t flags = 0;
buf.read(flags);
if (flags & BIT_REALS) {
reals_ = std::make_shared<std::vector<float>>();
buf.read(*reals_);
}
if (flags & BIT_PIXELS) {
pixels_ = std::make_shared<std::vector<uint8_t>>();
buf.read(*pixels_);
}
if (flags & BIT_ID) {
id_ = std::make_shared<std::vector<int>>();
buf.read(*id_);
}
if (flags & BIT_STR) {
str_ = std::make_shared<std::string>();
buf.read(*str_);
}
}
template <typename T>
bool StateBuffer::pointer_compare(const std::shared_ptr<T>& a,
const std::shared_ptr<T>& b) {
if ((a && !b) || (!a && b) || (a && b && (*a != *b))) {
return false;
}
return true;
}
} // namespace simulator