| 1 | #ifndef _melder_strvec_h_ |
| 2 | #define _melder_strvec_h_ |
| 3 | /* melder_strvec.h |
| 4 | * |
| 5 | * Copyright (C) 1992-2022,2024,2025 Paul Boersma |
| 6 | * |
| 7 | * This code is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or (at |
| 10 | * your option) any later version. |
| 11 | * |
| 12 | * This code is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 15 | * See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | template <typename T> |
| 22 | class _stringvector { |
| 23 | public: |
| 24 | T **elements = nullptr; |
| 25 | integer size = 0; |
| 26 | _stringvector () { } |
| 27 | _stringvector (T **givenElements, integer givenSize): elements (givenElements), size (givenSize) { } |
| 28 | T* & operator[] (integer i) const { |
| 29 | return our elements [i - 1]; |
| 30 | } |
| 31 | _stringvector<T> part (integer firstPosition, integer lastPosition) { |
| 32 | Melder_assert (firstPosition >= 1 && firstPosition <= our size); |
| 33 | Melder_assert (lastPosition >= 1 && lastPosition <= our size); |
| 34 | integer newSize = lastPosition - (firstPosition - 1); |
| 35 | if (newSize <= 0) |
| 36 | return _stringvector<T> (); |
| 37 | return _stringvector (our elements + (firstPosition - 1), newSize); |
| 38 | } |
| 39 | explicit operator bool () const noexcept { return !! our elements; } |
| 40 | T* *begin () const { return & our operator[] (1); } |
| 41 | T* *end () const { return & our operator[] (our size + 1); } |
| 42 | }; |
| 43 | using string32vector = _stringvector <char32>; |
| 44 | using string8vector = _stringvector <char>; |
| 45 | |
| 46 | template <typename T> |
| 47 | class _autostringvectorview; |
| 48 | |
| 49 | template <typename T> |
| 50 | class _conststringvector { |
| 51 | public: |
| 52 | const T *const *elements = nullptr; |
| 53 | integer size = 0; |
| 54 | _conststringvector () { } |
| 55 | _conststringvector (const T *const *givenElements, integer givenSize): elements (givenElements), size (givenSize) { } |
| 56 | _conststringvector (_stringvector<T> other): elements (other.elements), size (other.size) { } |
| 57 | _conststringvector (_autostringvectorview<T> other): elements (reinterpret_cast <const T *const *> (other.elements)), size (other.size) { } |
| 58 | const T *const & operator[] (integer i) const { |
| 59 | return our elements [i - 1]; |
| 60 | } |
| 61 | _conststringvector<T> part (integer firstPosition, integer lastPosition) { |
| 62 | Melder_assert (firstPosition >= 1 && firstPosition <= our size); |
| 63 | Melder_assert (lastPosition >= 1 && lastPosition <= our size); |
| 64 | integer newSize = lastPosition - (firstPosition - 1); |
| 65 | if (newSize <= 0) |
| 66 | return _conststringvector<T> (); |
| 67 | return _conststringvector (our elements + (firstPosition - 1), newSize); |
| 68 | } |
| 69 | explicit operator bool () const noexcept { return !! our elements; } |
| 70 | T* *begin () const { return & our operator[] (1); } |
| 71 | T* *end () const { return & our operator[] (our size + 1); } |
| 72 | }; |
| 73 | using conststring32vector = _conststringvector <char32>; |
| 74 | using conststring8vector = _conststringvector <char>; |
| 75 | |
| 76 | template <typename T> |
| 77 | class _autostringvectorview { |
| 78 | public: |
| 79 | _autostring <T> *elements = nullptr; |
| 80 | integer size = 0; |
| 81 | _autostringvectorview<T> () = default; |
| 82 | _autostringvectorview<T> (_autostring <T> * givenElements, integer givenSize): elements (givenElements), size (givenSize) { } |
| 83 | _autostring <T> & operator[] (integer i) const { |
| 84 | return our elements [i - 1]; |
| 85 | } |
| 86 | explicit operator bool () const noexcept { return !! our elements; } |
| 87 | }; |
| 88 | |
| 89 | template <typename T> |
| 90 | void operator<<= (_autostringvectorview <T> const& target, _autostringvectorview <T> const& source) { |
| 91 | Melder_assert (target.size == source.size); |
| 92 | for (integer i = 1; i <= target.size; i ++) |
| 93 | target [i] = Melder_dup (source [i]); |
| 94 | } |
| 95 | |
| 96 | template <typename T> |
| 97 | class _autostringautovector { |
| 98 | public: |
| 99 | _autostring <T> *elements; // Because of DataEditor, this has to be the first field... |
| 100 | integer size; // ... and this the second... |
| 101 | private: |
| 102 | integer _capacity = 0; // ... and this the third. |
| 103 | public: |
| 104 | _autostringautovector () { |
| 105 | our elements = nullptr; |
| 106 | our size = 0; |
| 107 | } |
| 108 | explicit _autostringautovector (integer givenSize) { |
| 109 | our elements = MelderArray:: _alloc <_autostring <T>> (givenSize, MelderArray::kInitializationType :: ZERO); |
| 110 | our _capacity = givenSize; |
| 111 | our size = givenSize; |
| 112 | } |
| 113 | explicit _autostringautovector (std::initializer_list <const conststring32> list) { |
| 114 | our size = uinteger_to_integer_a (list.size()); |
| 115 | our elements = MelderArray:: _alloc <autostring32> (our size, MelderArray::kInitializationType::ZERO); |
| 116 | our _capacity = our size; |
| 117 | autostring32 *p = our elements; |
| 118 | for (auto element : list) |
| 119 | * (p ++) = Melder_dup (element); |
| 120 | } |
| 121 | void reset () { |
| 122 | if (our elements) { |
| 123 | for (integer i = 1; i <= our size; i ++) |
| 124 | our elements [i - 1]. reset(); |
| 125 | MelderArray:: _free (our elements, our size); |
| 126 | our elements = nullptr; |
| 127 | } |
| 128 | our size = 0; |
| 129 | our _capacity = 0; |
| 130 | } |
| 131 | ~ _autostringautovector<T> () { |
| 132 | our reset (); |
| 133 | } |
| 134 | _stringvector<T> get () const { |
| 135 | return _stringvector<T> (reinterpret_cast <T**> (our elements), our size); |
| 136 | } |
| 137 | _autostringvectorview<T> all () const { |
| 138 | return _autostringvectorview<T> (our elements, our size); |
| 139 | } |
| 140 | void adoptFromAmbiguousOwner (_stringvector<T> const& given) { // buy the payload from a non-autostring |
| 141 | our reset(); |
| 142 | our elements = reinterpret_cast <_autostring <T> *> (given.elements); |
| 143 | our size = given.size; |
| 144 | our _capacity = given.size; |
| 145 | } |
| 146 | _stringvector<T> releaseToAmbiguousOwner () { // sell the payload to a non-autostring |
| 147 | _autostring<T> * oldElements = our elements; |
| 148 | our elements = nullptr; // disown ourselves, preventing automatic destruction of the payload |
| 149 | integer oldSize = our size; |
| 150 | our size = 0; |
| 151 | our _capacity = 0; |
| 152 | return _stringvector<T> (reinterpret_cast <T**> (oldElements), oldSize); |
| 153 | } |
| 154 | _autostringautovector (const _autostringautovector &) = delete; // disable copy construction |
| 155 | _autostringautovector& operator= (const _autostringautovector &) = delete; // disable copy assignment |
| 156 | _autostringautovector (_autostringautovector&& other) { // enable move construction |
| 157 | our elements = other.elements; |
| 158 | our size = other.size; |
| 159 | our _capacity = other._capacity; |
| 160 | other.elements = nullptr; // disown source |
| 161 | other.size = 0; // to keep the source in a valid state |
| 162 | other._capacity = 0; |
| 163 | } |
| 164 | _autostringautovector& operator= (_autostringautovector&& other) noexcept { // enable move assignment |
| 165 | if (other.elements != our elements) { |
| 166 | our reset (); |
| 167 | our elements = other.elements; |
| 168 | our size = other.size; |
| 169 | our _capacity = other._capacity; |
| 170 | other.elements = nullptr; // disown source |
| 171 | other.size = 0; // to keep the source in a valid state |
| 172 | other._capacity = 0; |
| 173 | } |
| 174 | return *this; |
| 175 | } |
| 176 | explicit operator bool () const noexcept { return !! our elements; } |
| 177 | _autostring <T> const & operator[] (integer i) const { |
| 178 | return our elements [i - 1]; |
| 179 | } |
| 180 | _autostring <T> & operator[] (integer i) { |
| 181 | return our elements [i - 1]; |
| 182 | } |
| 183 | T** peek2 () const { // can be assigned to a [const] mutablestring32* and to a const conststring32*, but not to a conststring32* |
| 184 | return (T**) our elements - 1; |
| 185 | } |
| 186 | _autostringvectorview<T> part (integer firstPosition, integer lastPosition) { |
| 187 | Melder_assert (firstPosition >= 1 && firstPosition <= our size); |
| 188 | Melder_assert (lastPosition >= 1 && lastPosition <= our size); |
| 189 | integer newSize = lastPosition - (firstPosition - 1); |
| 190 | if (newSize <= 0) |
| 191 | return _autostringvectorview<T> (); |
| 192 | return _autostringvectorview<T> (our elements + (firstPosition - 1), newSize); |
| 193 | } |
| 194 | _autostringautovector&& move () noexcept { return static_cast <_autostringautovector&&> (*this); } // enable construction and assignment for l-values (variables) via explicit move() |
| 195 | void initWithCapacity (integer capacity) { |
| 196 | if (capacity > 0) |
| 197 | our elements = MelderArray:: _alloc <_autostring <T>> (capacity, MelderArray::kInitializationType::ZERO); |
| 198 | our size = 0; |
| 199 | our _capacity = capacity; |
| 200 | } |
| 201 | void resize (integer newSize) { |
| 202 | if (newSize > our _capacity) { |
| 203 | /* |
| 204 | The new capacity is at least twice the old capacity. |
| 205 | When starting at a capacity of 0, and continually upsizing by one, |
| 206 | the capacity sequence will be: 0, 11, 33, 77, 165, 341, 693, 1397, |
| 207 | 2805, 5621, 11253, 22517, 45045, 90101, 180213, 360437, 720885... |
| 208 | */ |
| 209 | integer newCapacity = newSize + our size + 10; |
| 210 | /* |
| 211 | Create without change. |
| 212 | */ |
| 213 | _autostring <T> * newElements = MelderArray:: _alloc <_autostring <T>> (newCapacity, MelderArray::kInitializationType::ZERO); |
| 214 | /* |
| 215 | Change without error. |
| 216 | */ |
| 217 | for (integer i = 1; i <= our size; i ++) |
| 218 | newElements [i - 1] = our elements [i - 1]. move(); |
| 219 | if (our elements) |
| 220 | MelderArray:: _free (our elements, our _capacity); |
| 221 | our elements = newElements; |
| 222 | our _capacity = newCapacity; |
| 223 | } |
| 224 | our size = newSize; |
| 225 | } |
| 226 | void insert (integer position, conststring32 value) { |
| 227 | if (position == 0) |
| 228 | position = our size + 1; |
| 229 | /* |
| 230 | Create without change. |
| 231 | */ |
| 232 | autostring32 newValue = Melder_dup (value); |
| 233 | /* |
| 234 | Pivot. |
| 235 | */ |
| 236 | our resize (our size + 1); |
| 237 | /* |
| 238 | Change without error. |
| 239 | */ |
| 240 | Melder_assert (position >= 1 && position <= our size); |
| 241 | for (integer i = our size; i > position; i --) |
| 242 | our elements [i - 1] = our elements [i - 2]. move(); |
| 243 | our elements [position - 1] = newValue. move(); |
| 244 | } |
| 245 | void append (conststring32 value) { |
| 246 | our insert (0, value); |
| 247 | } |
| 248 | void remove (integer position) noexcept { |
| 249 | Melder_assert (position >= 1 && position <= our size); |
| 250 | for (integer i = position; i < our size; i ++) |
| 251 | our elements [i - 1] = std::move (our elements [i]); |
| 252 | our resize (our size - 1); // change without error |
| 253 | } |
| 254 | }; |
| 255 | |
| 256 | using autostring32vector = _autostringautovector <char32>; |
| 257 | using autostring8vector = _autostringautovector <char>; |
| 258 | |
| 259 | using STRVEC = _stringvector <char32>; |
| 260 | using constSTRVEC = _conststringvector <char32>; |
| 261 | using autoSTRVEC = _autostringautovector <char32>; |
| 262 | #define ARRAY_TO_STRVEC(conststring32Array) constSTRVEC (& conststring32Array [0], sizeof (conststring32Array) / sizeof (conststring32)) |
| 263 | |
| 264 | using autoSTRVECVU = _autostringvectorview <char32>; |
| 265 | |
| 266 | inline autoSTRVEC copy_STRVEC (constSTRVEC strvec) { |
| 267 | autoSTRVEC result (strvec.size); |
| 268 | for (integer i = 1; i <= result.size; i ++) |
| 269 | result [i] = Melder_dup (strvec [i]); |
| 270 | return result; |
| 271 | } |
| 272 | |
| 273 | conststring32 Melder_STRVEC (constSTRVEC const& value); |
| 274 | |
| 275 | inline void operator<<= (_autostringvectorview <char32> const& target, _autostringvectorview <char32> const& source) { |
| 276 | Melder_assert (target.size == source.size); |
| 277 | for (integer i = 1; i <= target.size; i ++) |
| 278 | target [i] = Melder_dup (source [i].get()); |
| 279 | } |
| 280 | inline void operator<<= (_autostringvectorview <char32> const& target, _stringvector <char32> const& source) { |
| 281 | Melder_assert (target.size == source.size); |
| 282 | for (integer i = 1; i <= target.size; i ++) |
| 283 | target [i] = Melder_dup (source [i]); |
| 284 | } |
| 285 | |
| 286 | /* End of file melder_strvec.h */ |
| 287 | #endif |