| 1 |
|
| #pragma once |
| 2 |
|
| #include <xs/Sv.h> |
| 3 |
|
| #include <iterator> |
| 4 |
|
| #include <xs/Scalar.h> |
| 5 |
|
| #include <xs/KeyProxy.h> |
| 6 |
|
| #include <xs/HashEntry.h> |
| 7 |
|
| #include <panda/string_view.h> |
| 8 |
|
| |
| 9 |
|
| namespace xs { |
| 10 |
|
| |
| 11 |
|
| struct Hash : Sv { |
| 12 |
0 |
| static Hash create () { return Hash(newHV(), NONE); } |
| 13 |
|
| |
| 14 |
|
| static Hash create (size_t cap) { |
| 15 |
|
| auto ret = create(); |
| 16 |
|
| ret.reserve(cap); |
| 17 |
|
| return ret; |
| 18 |
|
| } |
| 19 |
|
| |
| 20 |
|
| static Hash create (const std::initializer_list<std::pair<panda::string_view, Scalar>>& l) { return Hash(l); } |
| 21 |
|
| |
| 22 |
|
| static Hash noinc (SV* val) { return Hash(val, NONE); } |
| 23 |
|
| static Hash noinc (HV* val) { return Hash(val, NONE); } |
| 24 |
|
| |
| 25 |
0 |
| Hash (std::nullptr_t = nullptr) {} |
| 26 |
0 |
▶ | Hash (SV* sv, bool policy = INCREMENT) : Sv(sv, policy) { _validate(); } |
Branch: 1 branch
| Branch | True | False | Total |
| Hash (SV* sv, bool policy = INCREMENT) : Sv(sv, policy) { _validate(); } |
0 |
0 |
0 |
|
| 27 |
0 |
| Hash (HV* sv, bool policy = INCREMENT) : Sv(sv, policy) {} |
| 28 |
|
| |
| 29 |
0 |
| Hash (const Hash& oth) : Sv(oth) {} |
| 30 |
0 |
| Hash (Hash&& oth) : Sv(std::move(oth)) {} |
| 31 |
|
| Hash (const Sv& oth) : Hash(oth.get()) {} |
| 32 |
0 |
▶ | Hash (Sv&& oth) : Sv(std::move(oth)) { _validate(); } |
Branch: 1 branch
| Branch | True | False | Total |
| Hash (Sv&& oth) : Sv(std::move(oth)) { _validate(); } |
0 |
0 |
0 |
|
| 33 |
|
| |
| 34 |
|
| Hash (const Simple&) = delete; |
| 35 |
|
| Hash (const Array&) = delete; |
| 36 |
|
| Hash (const Sub&) = delete; |
| 37 |
|
| Hash (const Glob&) = delete; |
| 38 |
|
| Hash (const Io&) = delete; |
| 39 |
|
| |
| 40 |
|
| Hash (const std::initializer_list<std::pair<panda::string_view, Scalar>>&); |
| 41 |
|
| |
| 42 |
0 |
| Hash& operator= (SV* val) { Sv::operator=(val); _validate(); return *this; } |
| 43 |
0 |
| Hash& operator= (HV* val) { Sv::operator=(val); return *this; } |
| 44 |
|
| Hash& operator= (std::nullptr_t) { Sv::operator=(nullptr); return *this; } |
| 45 |
0 |
| Hash& operator= (const Hash& oth) { Sv::operator=(oth); return *this; } |
| 46 |
0 |
| Hash& operator= (Hash&& oth) { Sv::operator=(std::move(oth)); return *this; } |
| 47 |
|
| Hash& operator= (const Sv& oth) { return operator=(oth.get()); } |
| 48 |
0 |
| Hash& operator= (Sv&& oth) { Sv::operator=(std::move(oth)); _validate(); return *this; } |
| 49 |
|
| Hash& operator= (const Simple&) = delete; |
| 50 |
|
| Hash& operator= (const Array&) = delete; |
| 51 |
|
| Hash& operator= (const Sub&) = delete; |
| 52 |
|
| Hash& operator= (const Glob&) = delete; |
| 53 |
|
| Hash& operator= (const Io&) = delete; |
| 54 |
|
| |
| 55 |
0 |
| void set (SV* val) { Sv::operator=(val); } |
| 56 |
|
| |
| 57 |
|
| operator AV* () const = delete; |
| 58 |
0 |
| operator HV* () const { return (HV*)sv; } |
| 59 |
|
| operator CV* () const = delete; |
| 60 |
|
| operator GV* () const = delete; |
| 61 |
|
| operator IO* () const = delete; |
| 62 |
|
| |
| 63 |
|
| HV* operator-> () const { return (HV*)sv; } |
| 64 |
|
| |
| 65 |
0 |
| template <typename T = SV> panda::enable_if_one_of_t<T,SV,HV>* get () const { return (T*)sv; } |
| 66 |
|
| |
| 67 |
0 |
| Scalar fetch (const panda::string_view& key) const { |
| 68 |
0 |
▶ | if (!sv) return Scalar(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv) return Scalar(); |
0 |
0 |
0 |
|
| 69 |
0 |
▶ | SV** ref = hv_fetch((HV*)sv, key.data(), key.length(), 0); |
Branch: 1 branch
| Branch | True | False | Total |
| SV** ref = hv_fetch((HV*)sv, key.data(), key.length(), 0); |
0 |
0 |
0 |
|
| 70 |
0 |
| Scalar ret; |
| 71 |
0 |
▶ | if (ref) ret.set(*ref); |
Branch: 2 branches
| Branch | True | False | Total |
| if (ref) ret.set(*ref); |
0 |
0 |
0 |
| if (ref) ret.set(*ref); |
0 |
0 |
0 |
|
| 72 |
0 |
| return ret; |
| 73 |
0 |
| } |
| 74 |
|
| |
| 75 |
|
| Scalar at (const panda::string_view& key) const { |
| 76 |
|
| Scalar ret = fetch(key); |
| 77 |
|
| if (!ret) throw std::out_of_range("at: no key"); |
| 78 |
|
| return ret; |
| 79 |
|
| } |
| 80 |
|
| |
| 81 |
|
| Scalar operator[] (const panda::string_view& key) const { return fetch(key); } |
| 82 |
|
| |
| 83 |
|
| void store (const panda::string_view& key, const Scalar& val, U32 hash = 0); |
| 84 |
|
| void store (const panda::string_view& key, std::nullptr_t, U32 hash = 0) { store(key, Scalar(), hash); } |
| 85 |
0 |
▶ | void store (const panda::string_view& key, SV* v, U32 hash = 0) { store(key, Scalar(v), hash); } |
Branch: 2 branches
| Branch | True | False | Total |
| void store (const panda::string_view& key, SV* v, U32 hash = 0) { store(key, Scalar(v), hash); } |
0 |
0 |
0 |
| void store (const panda::string_view& key, SV* v, U32 hash = 0) { store(key, Scalar(v), hash); } |
0 |
0 |
0 |
|
| 86 |
|
| void store (const panda::string_view& key, const Sv& v, U32 hash = 0) { store(key, Scalar(v), hash); } |
| 87 |
|
| void store (const panda::string_view& key, const Array&, U32 hash = 0) = delete; |
| 88 |
|
| void store (const panda::string_view& key, const Hash&, U32 hash = 0) = delete; |
| 89 |
|
| void store (const panda::string_view& key, const Sub&, U32 hash = 0) = delete; |
| 90 |
|
| void store (const panda::string_view& key, const Io&, U32 hash = 0) = delete; |
| 91 |
|
| |
| 92 |
|
| KeyProxy operator[] (const panda::string_view& key) { return KeyProxy(hv_fetch((HV*)sv, key.data(), key.length(), 1), false); } |
| 93 |
|
| |
| 94 |
0 |
| Scalar erase (const panda::string_view& key) { |
| 95 |
0 |
| Scalar ret; |
| 96 |
0 |
▶ | ret.set(hv_delete((HV*)sv, key.data(), key.length(), 0)); |
Branch: 2 branches
| Branch | True | False | Total |
| ret.set(hv_delete((HV*)sv, key.data(), key.length(), 0)); |
0 |
0 |
0 |
| ret.set(hv_delete((HV*)sv, key.data(), key.length(), 0)); |
0 |
0 |
0 |
|
| 97 |
0 |
| return ret; |
| 98 |
|
| } |
| 99 |
|
| |
| 100 |
|
| bool contains (const panda::string_view& key) const { return exists(key); } |
| 101 |
|
| bool exists (const panda::string_view& key) const { |
| 102 |
|
| if (!sv) return false; |
| 103 |
|
| return hv_exists((HV*)sv, key.data(), key.length()); |
| 104 |
|
| } |
| 105 |
|
| |
| 106 |
|
| size_t size () const { return sv ? HvUSEDKEYS(sv) : 0; } |
| 107 |
|
| size_t capacity () const { return sv ? HvMAX(sv)+1 : 0; } |
| 108 |
|
| |
| 109 |
|
| void reserve (size_t newcap) { hv_ksplit((HV*)sv, newcap); } |
| 110 |
|
| |
| 111 |
|
| void undef () { if (sv) hv_undef((HV*)sv); } |
| 112 |
|
| void clear () { if (sv) hv_clear((HV*)sv); } |
| 113 |
|
| |
| 114 |
|
| struct const_iterator { |
| 115 |
|
| using difference_type = std::ptrdiff_t; |
| 116 |
|
| using value_type = const HashEntry; |
| 117 |
|
| using pointer = value_type*; |
| 118 |
|
| using reference = value_type&; |
| 119 |
|
| using iterator_category = std::forward_iterator_tag; |
| 120 |
|
| |
| 121 |
|
| const_iterator () : arr(NULL), end(NULL), cur(HashEntry()) {} |
| 122 |
|
| |
| 123 |
|
| const_iterator (HV* hv) : arr(HvARRAY(hv)), end(arr ? arr + HvMAX(hv) + 1 : nullptr), cur(HashEntry()) { |
| 124 |
|
| if (HvUSEDKEYS(hv)) operator++(); |
| 125 |
|
| } |
| 126 |
|
| |
| 127 |
|
| const_iterator (const const_iterator& oth) : arr(oth.arr), end(oth.end), cur(oth.cur) {} |
| 128 |
|
| |
| 129 |
|
| const_iterator& operator++ () { |
| 130 |
|
| if (cur) { |
| 131 |
|
| cur = HeNEXT(cur); |
| 132 |
|
| if (cur) return *this; |
| 133 |
|
| } |
| 134 |
|
| while (!cur && arr != end) cur = *arr++; |
| 135 |
|
| return *this; |
| 136 |
|
| } |
| 137 |
|
| |
| 138 |
|
| const_iterator operator++ (int) { |
| 139 |
|
| const_iterator ret = *this; |
| 140 |
|
| operator++(); |
| 141 |
|
| return ret; |
| 142 |
|
| } |
| 143 |
|
| |
| 144 |
|
| bool operator== (const const_iterator& oth) const { return cur == oth.cur; } |
| 145 |
|
| bool operator!= (const const_iterator& oth) const { return cur != oth.cur; } |
| 146 |
|
| |
| 147 |
|
| const HashEntry* operator-> () { return &cur; } |
| 148 |
|
| const HashEntry& operator* () { return cur; } |
| 149 |
|
| |
| 150 |
|
| const_iterator& operator= (const const_iterator& oth) { |
| 151 |
|
| arr = oth.arr; |
| 152 |
|
| end = oth.end; |
| 153 |
|
| cur = oth.cur; |
| 154 |
|
| return *this; |
| 155 |
|
| } |
| 156 |
|
| |
| 157 |
|
| protected: |
| 158 |
|
| HE** arr; |
| 159 |
|
| HE** end; |
| 160 |
|
| HashEntry cur; |
| 161 |
|
| }; |
| 162 |
|
| |
| 163 |
|
| struct iterator : const_iterator { |
| 164 |
|
| using const_iterator::const_iterator; |
| 165 |
|
| |
| 166 |
|
| using value_type = HashEntry; |
| 167 |
|
| using pointer = value_type*; |
| 168 |
|
| using reference = value_type&; |
| 169 |
|
| |
| 170 |
|
| HashEntry* operator-> () { return &cur; } |
| 171 |
|
| HashEntry& operator* () { return cur; } |
| 172 |
|
| }; |
| 173 |
|
| |
| 174 |
|
| const_iterator cbegin () const { return sv ? const_iterator((HV*)sv) : const_iterator(); } |
| 175 |
|
| const_iterator cend () const { return const_iterator(); } |
| 176 |
|
| const_iterator begin () const { return cbegin(); } |
| 177 |
|
| const_iterator end () const { return cend(); } |
| 178 |
|
| iterator begin () { return sv ? iterator((HV*)sv) : iterator(); } |
| 179 |
|
| iterator end () { return iterator(); } |
| 180 |
|
| |
| 181 |
|
| U32 push_on_stack (SV** sp) const; |
| 182 |
|
| |
| 183 |
|
| private: |
| 184 |
0 |
| void _validate () { |
| 185 |
0 |
▶ | if (!sv) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv) return; |
0 |
0 |
0 |
|
| 186 |
0 |
▶ | if (SvTYPE(sv) == SVt_PVHV) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTYPE(sv) == SVt_PVHV) return; |
0 |
0 |
0 |
|
| 187 |
0 |
▶ | if (SvROK(sv)) { // reference to hash? |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(sv)) { // reference to hash? |
0 |
0 |
0 |
|
| 188 |
0 |
| SV* val = SvRV(sv); |
| 189 |
0 |
▶ | if (SvTYPE(val) == SVt_PVHV) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTYPE(val) == SVt_PVHV) { |
0 |
0 |
0 |
|
| 190 |
0 |
| Sv::operator=(val); |
| 191 |
0 |
| return; |
| 192 |
|
| } |
| 193 |
|
| } |
| 194 |
0 |
▶ | if (is_undef()) return reset(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_undef()) return reset(); |
0 |
0 |
0 |
|
| 195 |
0 |
| reset(); |
| 196 |
0 |
▶ | throw std::invalid_argument("SV is not a Hash or Hash reference"); |
Branch: 1 branch
| Branch | True | False | Total |
| throw std::invalid_argument("SV is not a Hash or Hash reference"); |
0 |
0 |
0 |
|
| 197 |
|
| } |
| 198 |
|
| }; |
| 199 |
|
| |
| 200 |
|
| } |
| 201 |
|
| |