-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstamp_json.cpp
171 lines (151 loc) · 4.34 KB
/
stamp_json.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
/******************************************************************************
*
* Copyright 2021-2023 Nikolay Shaplov (Postgres Professional)
*
* 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 <string>
#include <vector>
#include <memory>
#include "stamp_json.h"
#include "oracle.h"
PoolPickerStamp::PoolPickerStamp(std::vector<std::shared_ptr<StampBaseStr>> new_pool)
: pool{new_pool}
{
for (const auto &stamp : pool)
{
std::weak_ptr<StampBaseStr> wp = stamp;
weak_pool.push_back(wp);
}
}
bool
PoolPickerStamp::isRecursive()
{
if (is_recursive || is_in_recursion)
return true;
is_in_recursion = true;
for (const auto &stamp : weak_pool)
{
if (stamp.lock()->isRecursive())
{
is_recursive = true; // Once recursive -- recursive forever.
is_in_recursion = false;
return true;
}
}
is_in_recursion = false;
return false;
}
std::string
PoolPickerStamp::ExtractStr(std::shared_ptr<Blob> blob)
{
static ORACLE_STAMP stamp_oracle;
ORACLE_TYPE oracle = stamp_oracle.ExtractValue(blob);
std::vector<std::weak_ptr<StampBaseStr>> target_pool;
std::vector<std::weak_ptr<StampBaseStr>> unbounded_pool;
for (const auto &stamp_w : weak_pool)
{
auto stamp = stamp_w.lock();
if (stamp->minSize() <= blob->Size())
{
target_pool.push_back(stamp_w);
if (stamp->maxSize() == -1 || stamp->maxSize() >= blob->Size())
{
unbounded_pool.push_back(stamp_w);
}
}
}
if (unbounded_pool.size()>0)
target_pool = unbounded_pool;
size_t index = OracleProportion(oracle, 0, target_pool.size() - 1);
return target_pool[index].lock()->ExtractStr(blob);
}
int
PoolPickerStamp::minSize()
{
int res = INT_MAX / 2;
/* Do not check is_recursive here: even if stamp is known to be recursive we
* still should iterate all his non-recursive children to find real minimal
* size */
if (is_in_recursion)
return res;
is_in_recursion = true; /* Do not use isRecursive() inside as it uses same flag*/
for (const auto &stamp : weak_pool)
{
int candidat = stamp.lock()->minSize();
if (res > candidat)
res = candidat;
}
is_in_recursion = false;
if (res == INT_MAX / 2)
return INT_MAX / 2;
res += ORACLE_SIZE;
return res;
}
int
PoolPickerStamp::maxSize()
{
int res = 0;
if (is_recursive || is_in_recursion)
return -1;
is_in_recursion = true; /* Do not use isRecursive() inside as it uses same flag*/
for (const auto &stamp : weak_pool)
{
int candidat = stamp.lock()->maxSize();
if (candidat == -1)
{
is_in_recursion = false;
return -1;
}
if (res < candidat)
res = candidat;
}
is_in_recursion = false;
res += ORACLE_SIZE;
return res;
}
void
PoolPickerStamp::add_weak(std::shared_ptr<StampBaseStr> stamp)
{
weak_pool.push_back(stamp);
}
std::string
StampJSONString::ExtractStr(std::shared_ptr<Blob> blob)
{
std::string res = "\"" + StampDictT<DictLCAlphaSmall>::ExtractStr(blob) +"\"";
return res;
}
std::string
StampJSONHashEl::ExtractStr(std::shared_ptr<Blob> blob)
{
const std::string &n = stamp_name->ExtractStr(blob);
const std::string &v = stamp_value->ExtractStr(blob);
return n + ": " + v;
}
void null_deleter(StampJSON *) {}
StampJSON::StampJSON()
: PoolPickerStamp({})
{
stamp_i = std::make_shared<StampJSONInt>();
stamp_f = std::make_shared<StampJSONFloat>();
stamp_s = std::make_shared<StampJSONString>();
// FIXME Так не надо делеать!!!! null_deleter -- зло.
stamp_a = std::make_shared<StampJSONArray>(std::shared_ptr<StampJSON>(this, null_deleter));
stamp_h = std::make_shared<StampJSONHash>(std::shared_ptr<StampJSON>(this, null_deleter));
add_weak(stamp_i);
add_weak(stamp_f);
add_weak(stamp_s);
add_weak(stamp_a);
add_weak(stamp_h);
}