-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclaujson_array.cpp
More file actions
366 lines (288 loc) · 7.91 KB
/
claujson_array.cpp
File metadata and controls
366 lines (288 loc) · 7.91 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include "claujson.h"
namespace claujson {
extern Log log;
_Value Array::data_null{ nullptr, false }; // valid is false..
const uint64_t Array::npos = -1; //
_Value Array::clone(Arena* pool) const {
_Value result = Array::Make(pool);
if (result.as_array() == nullptr) {
return result;
}
uint64_t sz = this->get_data_size();
for (uint64_t i = 0; i < sz; ++i) {
auto x = this->get_value_list(i).clone(pool);
result.as_array()->add_element(std::move(x));
}
return result;
}
void Array::set_parent(StructuredPtr p) {
int _is_virtual = parent.left_type();
parent = Pointer(p.arr, _is_virtual, p.type);
}
_Value Array::Make(Arena* pool) {
Array* temp = nullptr;
if (pool) {
temp = (Array*)pool->allocate<Array>(sizeof(Array), alignof(Array)); // new (std::nothrow) Array();
new (temp) Array();
temp->arr_vec = my_vector<_Value>(pool, 0, 2);
}
else {
temp = new (std::nothrow) Array();
}
if (temp == nullptr) {
_Value v;
v._type = _ValueType::ERROR;
return v;
}
return _Value(temp);
}
_Value Array::MakeVirtual(Arena* pool) {
Array* temp = nullptr;
if (pool) {
temp = (Array*)pool->allocate<Array>(sizeof(Array), alignof(Array)); // new (std::nothrow) Array();
new (temp) Array();
temp->arr_vec = my_vector<_Value>(pool, 0, 2);
}
else {
temp = new (std::nothrow) Array();
}
if (temp == nullptr) {
_Value v;
v._type = _ValueType::ERROR;
return v;
}
temp->parent = Pointer(nullptr, 1, 0);
return _Value(temp);
}
Array::Array() {}
Array::~Array() {
//
}
bool Array::is_object() const {
return false;
}
bool Array::is_array() const {
return true;
}
uint64_t Array::find(const _Value& value, uint64_t start) const {
uint64_t sz = size();
for (uint64_t i = start; i < sz; ++i) {
if (get_value_list(i) == value) {
return i;
}
}
return npos;
}
_Value& Array::operator[](uint64_t idx) {
return this->get_value_list(idx);
}
const _Value& Array::operator[](uint64_t idx) const {
return this->get_value_list(idx);
}
const StructuredPtr Array::get_parent() const {
int type = this->parent.right_type();
if (type == 1) {
return { (Array*)this->parent.use() };
}
else if (type == 2) {
return { (Object*)this->parent.use() };
}
else if (type == 3) {
return { (PartialJson*)this->parent.use() };
}
return {};
}
uint64_t Array::get_data_size() const {
return arr_vec.size();
}
_Value& Array::get_value_list(uint64_t idx) {
return arr_vec[idx];
}
_Value& Array::get_key_list(uint64_t idx) {
return data_null;
}
const _Value& Array::get_const_key_list(uint64_t idx) {
return data_null;
}
const _Value& Array::get_const_key_list(uint64_t idx) const {
return data_null;
}
const _Value& Array::get_value_list(uint64_t idx) const {
return arr_vec[idx];
}
const _Value& Array::get_key_list(uint64_t idx) const {
return data_null;
}
void Array::clear(uint64_t idx) {
arr_vec[idx].clear(false);
}
bool Array::is_virtual() const {
bool _is_virtual = parent.left_type();
return _is_virtual;
}
void Array::clear() {
arr_vec.clear();
}
void Array::reserve_data_list(uint64_t len) {
arr_vec.reserve(len);
}
Array::_ValueIterator Array::begin() {
return arr_vec.begin();
}
Array::_ValueIterator Array::end() {
return arr_vec.end();
}
Array::_ConstValueIterator Array::begin() const {
return arr_vec.begin();
}
Array::_ConstValueIterator Array::end() const {
return arr_vec.end();
}
bool Array::add_element(Value val) {
if (val.Get().is_array()) {
val.Get().as_array()->set_parent(this);
}
else if (val.Get().is_object()) {
val.Get().as_object()->set_parent(this);
}
arr_vec.emplace_back(std::move(val.Get()));
return true;
}
bool Array::assign_element(uint64_t idx, Value val) {
if (val.Get().is_array()) {
val.Get().as_array()->set_parent(this);
}
else if (val.Get().is_object()) {
val.Get().as_object()->set_parent(this);
}
arr_vec[idx] = (std::move(val.Get()));
return true;
}
// idx range check?
bool Array::insert(uint64_t idx, Value val) {
if (!add_element(std::move(val))) {
return false;
}
_Value temp = std::move(arr_vec.back());
uint64_t sz = size();
for (uint64_t i = sz - 1; i > idx; --i) {
arr_vec[i] = std::move(arr_vec[i - 1]);
}
arr_vec[idx] = std::move(temp);
return true;
}
void Array::erase(const _Value& key, bool real) {
uint64_t idx = this->find(key);
erase(idx, real);
}
void Array::null_parent() {
int x = this->parent.left_type();
this->parent = Pointer(nullptr, x, 0);
}
void Array::erase(uint64_t idx, bool real) {
if (real) {
clean(arr_vec[idx]);
}
arr_vec.erase(arr_vec.begin() + idx);
}
void Array::MergeWith(Array* j, int start_offset) {
auto* x = j;
uint64_t len = j->get_data_size();
for (uint64_t i = 0; i < len; ++i) {
if (j->get_value_list(i).is_array()) {
j->get_value_list(i).as_array()->set_parent(this);
}
else if (j->get_value_list(i).is_object()) {
j->get_value_list(i).as_object()->set_parent(this);
}
}
if (x->arr_vec.empty() == false) {
arr_vec.insert((x->arr_vec.begin()) + start_offset,
(x->arr_vec.end()));
}
else {
log << info << "test3";
}
}
void Array::MergeWith(Object* j, int start_offset) {
CLAUJSON_ERROR("Array::MergeWith Error");
}
void Array::MergeWith(PartialJson* j, int start_offset) {
auto* x = j;
if (x->obj_data.empty() == false) { // not object?
CLAUJSON_ERROR("partial json is not array");
}
uint64_t len = j->get_data_size();
for (uint64_t i = 0; i < len; ++i) {
if (j->get_value_list(i).is_array()) {
j->get_value_list(i).as_array()->set_parent(this);
}
else if (j->get_value_list(i).is_object()) {
j->get_value_list(i).as_object()->set_parent(this);
}
}
if (x->virtualJson.is_structured()) {
start_offset = 0;
}
if (x->arr_vec.empty() == false) {
arr_vec.insert((x->arr_vec.begin()) + start_offset,
(x->arr_vec.end()));
}
else {
log << info << "test4";
}
}
/*
void Array::add_item_type(int64_t key_buf_idx, int64_t key_next_buf_idx, int64_t val_buf_idx, int64_t val_next_buf_idx,
char* buf, uint64_t key_token_idx, uint64_t val_token_idx) {
// error
log << warn << "error..";
CLAUJSON_ERROR("Error Array::add_item_type");
}
void Array::add_item_type(int64_t val_buf_idx, int64_t val_next_buf_idx,
char* buf, uint64_t val_token_idx, Arena* pool, void* converter) {
{
_Value temp2;
bool e = false;
claujson::Convert(pool, temp2, val_buf_idx, val_next_buf_idx, false, buf, val_token_idx, e);
if (e) {
CLAUJSON_ERROR("Error in add_item_type");
}
arr_vec.emplace_back(std::move(temp2));
}
}
void Array::add_user_type(int64_t key_buf_idx, int64_t key_next_buf_idx, char* buf,
_ValueType type, uint64_t key_token_idx, Arena* pool
) {
log << warn << "error";
CLAUJSON_ERROR("Array::add_user_type1");
}
void Array::add_user_type(_ValueType type
, Arena* pool
) {
if (type == _ValueType::OBJECT) {
_Value json = Object::Make(pool);
// pool->create<Object>(); // new (std::nothrow) Object(
// );
if (json.as_structured() == nullptr) {
log << warn << "new error";
return;
}
json.as_structured().set_parent(this);
arr_vec.emplace_back(std::move(json));
}
else if (type == _ValueType::ARRAY) {
_Value json = Array::Make(pool);
// pool->create<Array>(); // new (std::nothrow) Array(
// );
if (json.as_structured() == nullptr) {
log << warn << "new error";
return;
}
json.as_structured().set_parent(this);
arr_vec.emplace_back(std::move(json));
}
}
*/
}