value.cpp 16.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24
25
26
27
28
29
#include <cassert>
#include <iostream>
#include <migraphx/cloneable.hpp>
#include <migraphx/errors.hpp>
#include <migraphx/stringutils.hpp>
#include <migraphx/value.hpp>
30
#include <migraphx/optional.hpp>
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <unordered_map>
#include <utility>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

struct value_base_impl : cloneable<value_base_impl>
{
    virtual value::type_t get_type() { return value::null_type; }
#define MIGRAPHX_VALUE_GENERATE_BASE_FUNCTIONS(vt, cpp_type) \
    virtual const cpp_type* if_##vt() const { return nullptr; }
    MIGRAPHX_VISIT_VALUE_TYPES(MIGRAPHX_VALUE_GENERATE_BASE_FUNCTIONS)
    virtual std::vector<value>* if_array() { return nullptr; }
    virtual std::unordered_map<std::string, std::size_t>* if_object() { return nullptr; }
    virtual value_base_impl* if_value() const { return nullptr; }
    value_base_impl()                       = default;
    value_base_impl(const value_base_impl&) = default;
    value_base_impl& operator=(const value_base_impl&) = default;
49
    virtual ~value_base_impl() override {}
50
51
};

52
53
54
55
56
57
58
#define MIGRAPHX_VALUE_GENERATE_BASE_TYPE(vt, cpp_type)                        \
    struct vt##_value_holder : value_base_impl::share                          \
    {                                                                          \
        vt##_value_holder(cpp_type d) : data(std::move(d)) {}                  \
        virtual value::type_t get_type() override { return value::vt##_type; } \
        virtual const cpp_type* if_##vt() const override { return &data; }     \
        cpp_type data;                                                         \
59
60
61
62
63
64
65
    };
MIGRAPHX_VISIT_VALUE_TYPES(MIGRAPHX_VALUE_GENERATE_BASE_TYPE)

struct array_value_holder : value_base_impl::derive<array_value_holder>
{
    array_value_holder() {}
    array_value_holder(std::vector<value> d) : data(std::move(d)) {}
66
67
    virtual value::type_t get_type() override { return value::array_type; }
    virtual std::vector<value>* if_array() override { return &data; }
68
69
70
71
72
73
74
75
76
77
    std::vector<value> data;
};

struct object_value_holder : value_base_impl::derive<object_value_holder>
{
    object_value_holder() {}
    object_value_holder(std::vector<value> d, std::unordered_map<std::string, std::size_t> l)
        : data(std::move(d)), lookup(std::move(l))
    {
    }
78
79
80
    virtual value::type_t get_type() override { return value::object_type; }
    virtual std::vector<value>* if_array() override { return &data; }
    virtual std::unordered_map<std::string, std::size_t>* if_object() override { return &lookup; }
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
    std::vector<value> data;
    std::unordered_map<std::string, std::size_t> lookup;
};

value::value(const value& rhs) : x(rhs.x ? rhs.x->clone() : nullptr), key(rhs.key) {}
value& value::operator=(value rhs)
{
    std::swap(rhs.x, x);
    if(not rhs.key.empty())
        std::swap(rhs.key, key);
    return *this;
}

void set_vector(std::shared_ptr<value_base_impl>& x,
                const std::vector<value>& v,
                bool array_on_empty = true)
{
    if(v.empty())
    {
        if(array_on_empty)
            x = std::make_shared<array_value_holder>();
        else
            x = std::make_shared<object_value_holder>();
        return;
    }
    if(v.front().get_key().empty())
    {
        x = std::make_shared<array_value_holder>(v);
    }
    else
    {
        std::unordered_map<std::string, std::size_t> lookup;
        std::size_t i = 0;
        for(auto&& e : v)
        {
            lookup[e.get_key()] = i;
            i++;
        }
        x = std::make_shared<object_value_holder>(v, lookup);
    }
}

value::value(const std::initializer_list<value>& i) : x(nullptr)
{
125
    if(i.size() == 2 and i.begin()->is_string() and i.begin()->get_key().empty())
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
    {
        key    = i.begin()->get_string();
        auto r = (i.begin() + 1)->x;
        x      = r ? r->clone() : nullptr;
        return;
    }
    set_vector(x, std::vector<value>(i.begin(), i.end()));
}

value::value(const std::vector<value>& v, bool array_on_empty) : x(nullptr)
{
    set_vector(x, v, array_on_empty);
}

value::value(const std::unordered_map<std::string, value>& m)
    : value(std::vector<value>(m.begin(), m.end()), false)
{
}

value::value(const std::string& pkey, const std::vector<value>& v, bool array_on_empty)
    : x(nullptr), key(pkey)
{
    set_vector(x, v, array_on_empty);
}

value::value(const std::string& pkey, const std::unordered_map<std::string, value>& m)
    : value(pkey, std::vector<value>(m.begin(), m.end()), false)
{
}

value::value(const std::string& pkey, std::nullptr_t) : x(nullptr), key(pkey) {}

Shucai Xiao's avatar
Shucai Xiao committed
158
159
value::value(std::nullptr_t) : x(nullptr) {}

160
161
162
163
164
value::value(const std::string& pkey, const value& rhs)
    : x(rhs.x ? rhs.x->clone() : nullptr), key(pkey)
{
}

165
value::value(const std::string& pkey, const char* i) : value(pkey, std::string(i)) {}
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
value::value(const char* i) : value(std::string(i)) {}

#define MIGRAPHX_VALUE_GENERATE_DEFINE_METHODS(vt, cpp_type)                           \
    value::value(cpp_type i) : x(std::make_shared<vt##_value_holder>(std::move(i))) {} \
    value::value(const std::string& pkey, cpp_type i)                                  \
        : x(std::make_shared<vt##_value_holder>(std::move(i))), key(pkey)              \
    {                                                                                  \
    }                                                                                  \
    value& value::operator=(cpp_type rhs)                                              \
    {                                                                                  \
        x = std::make_shared<vt##_value_holder>(std::move(rhs));                       \
        return *this;                                                                  \
    }                                                                                  \
    bool value::is_##vt() const { return x ? x->get_type() == vt##_type : false; }     \
    const cpp_type& value::get_##vt() const                                            \
    {                                                                                  \
        auto* r = this->if_##vt();                                                     \
        assert(r);                                                                     \
        return *r;                                                                     \
    }                                                                                  \
    const cpp_type* value::if_##vt() const { return x ? x->if_##vt() : nullptr; }
MIGRAPHX_VISIT_VALUE_TYPES(MIGRAPHX_VALUE_GENERATE_DEFINE_METHODS)

189
190
191
192
193
194
value& value::operator=(const char* c)
{
    *this = std::string{c};
    return *this;
}

Shucai Xiao's avatar
Shucai Xiao committed
195
196
197
198
199
200
value& value::operator=(std::nullptr_t)
{
    x = nullptr;
    return *this;
}

Shucai Xiao's avatar
Shucai Xiao committed
201
202
203
204
205
206
207
value& value::operator=(const std::initializer_list<value>& i)
{
    value rhs = i;
    std::swap(rhs.x, x);
    return *this;
}

208
209
210
bool value::is_array() const { return x ? x->get_type() == array_type : false; }
const std::vector<value>& value::value::get_array() const
{
211
    const auto* r = this->if_array();
212
213
214
215
216
217
218
219
    assert(r);
    return *r;
}
const std::vector<value>* value::if_array() const { return x ? x->if_array() : nullptr; }

bool value::is_object() const { return x ? x->get_type() == object_type : false; }
const std::vector<value>& value::get_object() const
{
220
    const auto* r = this->if_object();
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
    assert(r);
    return *r;
}
const std::vector<value>* value::if_object() const
{
    auto* r = x ? x->if_array() : nullptr;
    assert(r == nullptr or
           std::none_of(r->begin(), r->end(), [](auto&& v) { return v.get_key().empty(); }));
    return r;
}

bool value::is_null() const { return x == nullptr; }

const std::string& value::get_key() const { return key; }

std::vector<value>* if_array_impl(const std::shared_ptr<value_base_impl>& x)
{
238
    if(x == nullptr)
239
240
241
242
243
244
245
246
247
248
249
        return nullptr;
    return x->if_array();
}

std::vector<value>& get_array_impl(const std::shared_ptr<value_base_impl>& x)
{
    auto* a = if_array_impl(x);
    assert(a);
    return *a;
}

250
251
252
253
254
255
256
257
std::vector<value>& get_array_throw(const std::shared_ptr<value_base_impl>& x)
{
    auto* a = if_array_impl(x);
    if(a == nullptr)
        MIGRAPHX_THROW("Expected an array or object");
    return *a;
}

258
259
template <class T>
T* find_impl(const std::shared_ptr<value_base_impl>& x, const std::string& key, T* end)
260
261
262
{
    auto* a = if_array_impl(x);
    if(a == nullptr)
263
        return end;
264
265
    auto* lookup = x->if_object();
    if(lookup == nullptr)
266
        return end;
267
268
    auto it = lookup->find(key);
    if(it == lookup->end())
269
        return end;
270
271
272
    return std::addressof((*a)[it->second]);
}

273
value* value::find(const std::string& pkey) { return find_impl(x, pkey, this->end()); }
274

275
const value* value::find(const std::string& pkey) const { return find_impl(x, pkey, this->end()); }
276
277
bool value::contains(const std::string& pkey) const
{
278
    const auto* it = find(pkey);
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
    if(it == nullptr)
        return false;
    if(it == end())
        return false;
    return true;
}
std::size_t value::size() const
{
    auto* a = if_array_impl(x);
    if(a == nullptr)
        return 0;
    return a->size();
}
bool value::empty() const { return size() == 0; }
const value* value::data() const
{
    auto* a = if_array_impl(x);
    if(a == nullptr)
        return nullptr;
    return a->data();
}
value* value::data()
{
    auto* a = if_array_impl(x);
    if(a == nullptr)
        return nullptr;
    return a->data();
}
value* value::begin()
{
    // cppcheck-suppress assertWithSideEffect
    assert(data() or empty());
    return data();
}
const value* value::begin() const
{
    assert(data() or empty());
    return data();
}
value* value::end() { return begin() + size(); }
const value* value::end() const { return begin() + size(); }

321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
value& value::front()
{
    assert(this->size() > 0);
    return *begin();
}
const value& value::front() const
{
    assert(this->size() > 0);
    return *begin();
}
value& value::back()
{
    assert(this->size() > 0);
    return *std::prev(end());
}
const value& value::back() const
{
    assert(this->size() > 0);
    return *std::prev(end());
}
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
value& value::at(std::size_t i)
{
    auto* a = if_array_impl(x);
    if(a == nullptr)
        MIGRAPHX_THROW("Not an array");
    return a->at(i);
}
const value& value::at(std::size_t i) const
{
    auto* a = if_array_impl(x);
    if(a == nullptr)
        MIGRAPHX_THROW("Not an array");
    return a->at(i);
}
value& value::at(const std::string& pkey)
{
    auto* r = find(pkey);
    if(r == nullptr)
        MIGRAPHX_THROW("Not an object");
    if(r == end())
361
        MIGRAPHX_THROW("Key not found: " + pkey);
362
363
364
365
    return *r;
}
const value& value::at(const std::string& pkey) const
{
366
    const auto* r = find(pkey);
367
    if(r == nullptr)
368
        MIGRAPHX_THROW("Not an object for field: " + pkey);
369
    if(r == end())
370
        MIGRAPHX_THROW("Key not found: " + pkey);
371
372
    return *r;
}
373
374
375
376
377
378
379
380
381
382
value& value::operator[](std::size_t i)
{
    assert(i < this->size());
    return *(begin() + i);
}
const value& value::operator[](std::size_t i) const
{
    assert(i < this->size());
    return *(begin() + i);
}
383
384
value& value::operator[](const std::string& pkey) { return *emplace(pkey, nullptr).first; }

385
386
387
388
389
390
391
392
393
394
395
396
397
398
void value::clear() { get_array_throw(x).clear(); }
void value::resize(std::size_t n)
{
    if(not is_array())
        MIGRAPHX_THROW("Expected an array.");
    get_array_impl(x).resize(n);
}
void value::resize(std::size_t n, const value& v)
{
    if(not is_array())
        MIGRAPHX_THROW("Expected an array.");
    get_array_impl(x).resize(n, v);
}

399
400
401
402
std::pair<value*, bool> value::insert(const value& v)
{
    if(v.key.empty())
    {
403
        if(not x)
404
405
406
407
408
409
410
            x = std::make_shared<array_value_holder>();
        get_array_impl(x).push_back(v);
        assert(this->if_array());
        return std::make_pair(&back(), true);
    }
    else
    {
411
        if(not x)
412
413
414
415
416
417
418
419
420
421
422
            x = std::make_shared<object_value_holder>();
        auto p = x->if_object()->emplace(v.key, get_array_impl(x).size());
        if(p.second)
            get_array_impl(x).push_back(v);
        assert(this->if_object());
        return std::make_pair(&get_array_impl(x)[p.first->second], p.second);
    }
}
value* value::insert(const value* pos, const value& v)
{
    assert(v.key.empty());
423
    if(not x)
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
        x = std::make_shared<array_value_holder>();
    auto&& a = get_array_impl(x);
    auto it  = a.insert(a.begin() + (pos - begin()), v);
    return std::addressof(*it);
}

value value::without_key() const
{
    value result = *this;
    result.key   = "";
    return result;
}

value value::with_key(const std::string& pkey) const
{
    value result = *this;
    result.key   = pkey;
    return result;
}

444
445
template <class T>
const T& compare_decay(const T& x)
446
{
447
    return x;
448
}
449
int compare_decay(std::nullptr_t) { return 0; }
450
451
452
453
454

template <class F>
bool compare(const value& x, const value& y, F f)
{
    bool result = false;
455
456
    x.visit_value([&](auto&& a) {
        y.visit_value([&](auto&& b) {
457
458
459
460
461
            if constexpr(std::is_same<decltype(a), decltype(b)>{})
                result = f(std::forward_as_tuple(x.get_key(), compare_decay(a)),
                           std::forward_as_tuple(y.get_key(), compare_decay(b)));
            else
                assert(false); // NOLINT
462
463
464
465
466
467
468
        });
    });
    return result;
}

value::type_t value::get_type() const
{
469
    if(not x)
470
471
472
473
474
475
476
477
478
479
        return null_type;
    return x->get_type();
}

bool operator==(const value& x, const value& y)
{
    if(x.get_type() != y.get_type())
        return false;
    return compare(x, y, std::equal_to<>{});
}
480
481
482
483
484
485
486
487
bool operator!=(const value& x, const value& y) { return not(x == y); }
bool operator<(const value& x, const value& y)
{
    if(x.get_type() != y.get_type())
        return x.get_type() < y.get_type();
    return compare(x, y, std::less<>{});
}
bool operator<=(const value& x, const value& y) { return not(x > y); }
488
bool operator>(const value& x, const value& y) { return y < x; }
489
bool operator>=(const value& x, const value& y) { return not(x < y); }
490

491
492
void print_value(std::ostream& os, std::nullptr_t) { os << "null"; }

493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
template <class T>
void print_value(std::ostream& os, const T& x)
{
    os << x;
}

template <class T, class U>
void print_value(std::ostream& os, const std::pair<T, U>& x)
{
    os << x.first;
    os << ": ";
    print_value(os, x.second);
}

void print_value(std::ostream& os, const std::vector<value>& x)
{
    os << "{";
    os << to_string_range(x);
    os << "}";
}

514
void print_value(std::ostream& os, const value::binary& x) { os << x; }
515

516
517
518
519
520
521
std::ostream& operator<<(std::ostream& os, const value& d)
{
    d.visit([&](auto&& y) { print_value(os, y); });
    return os;
}

522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
void value::debug_print(bool show_type) const
{
    if(show_type)
    {
        switch(get_type())
        {
#define MIGRAPHX_VALUE_GENERATE_TYPE_STRING_CASE(vt, cpp_type) \
    case vt##_type: std::cout << #vt << ": "; break;
            MIGRAPHX_VISIT_VALUE_TYPES(MIGRAPHX_VALUE_GENERATE_TYPE_STRING_CASE)
            MIGRAPHX_VALUE_GENERATE_TYPE_STRING_CASE(null, )
            MIGRAPHX_VALUE_GENERATE_TYPE_STRING_CASE(array, )
            MIGRAPHX_VALUE_GENERATE_TYPE_STRING_CASE(object, )
        }
    }
    std::cout << *this << std::endl;
}
538
539
540

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx