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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
|
// Copyright 2025, Amlal El Mahrouss (amlal@nekernel.org)
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
// Official repository: https://github.com/ocl-org/tproc
#ifndef OCL_TPROC_ROPE_FWD_INL
#define OCL_TPROC_ROPE_FWD_INL
#include <boost/system/error_code.hpp>
namespace ocl::tproc
{
template <class CharT, class Traits, class Allocator>
struct basic_rope<CharT, Traits, Allocator>::tree_impl
{
using char_type = CharT;
using value_type = char_type;
using size_type = typename std::allocator_traits<Allocator>::size_type;
using rope_ptr = basic_rope<CharT, Traits, Allocator>*;
using error_type = boost::system::error_code;
using allocator_type = Allocator;
rope_ptr left_{nullptr}; // Left child (internal node only)
rope_ptr right_{nullptr}; // Right child (internal node only)
size_type weight_{0}; // Size of left subtree (internal) OR data size (leaf)
value_type* blob_{nullptr}; // Character data (leaf node only)
size_type capacity_{0}; // Allocated blob capacity
allocator_type alloc_;
error_type ec_{};
bool is_leaf() const
{
return blob_ != nullptr;
}
public:
tree_impl(Allocator alloc = Allocator()) : alloc_(alloc)
{
}
tree_impl(const boost::core::basic_string_view<CharT>& str, Allocator alloc = Allocator()) : weight_(str.size()), alloc_(alloc), capacity_(str.size())
{
if (weight_ > 0)
{
blob_ = std::allocator_traits<Allocator>::allocate(alloc_, capacity_);
::memset(blob_, 0, capacity_);
Traits::copy(blob_, str.data(), weight_);
}
}
tree_impl(rope_ptr left, rope_ptr right, Allocator alloc = Allocator()) : left_(left), right_(right), alloc_(alloc)
{
weight_ = left ? left->impl_->total_size() : 0;
}
~tree_impl()
{
if (blob_ && capacity_ > 0)
{
std::allocator_traits<Allocator>::deallocate(alloc_, blob_, capacity_);
}
delete left_;
delete right_;
left_ = nullptr;
right_ = nullptr;
}
size_type total_size() const
{
if (is_leaf())
{
return weight_;
}
return weight_ + (right_ ? right_->impl_->total_size() : 0);
}
size_type size() const
{
return total_size();
}
CharT at(size_type pos) const
{
if (is_leaf())
{
if (pos >= weight_)
{
throw std::out_of_range("rope index out of range");
}
return blob_[pos];
}
// Internal node: navigate based on weight
if (pos < weight_)
{
if (!left_)
throw std::out_of_range("null left child");
return left_->impl_->at(pos);
}
else
{
if (!right_)
throw std::out_of_range("null right child");
return right_->impl_->at(pos - weight_);
}
}
public:
static rope_ptr concat(rope_ptr left, rope_ptr right, Allocator alloc = Allocator())
{
if (!left)
return right;
if (!right)
return left;
auto view_new_text = std::basic_string<CharT>(left->data().data(), left->data().size());
view_new_text += std::basic_string<CharT>(right->data().data(), right->data().size());
auto* new_rope = new basic_rope<CharT, Traits, Allocator>(view_new_text);
new_rope->impl_->left_ = left;
new_rope->impl_->right_ = right;
return new_rope;
}
std::pair<rope_ptr, rope_ptr> split(size_type pos, rope_ptr this_rope, Allocator alloc = Allocator())
{
if (is_leaf())
{
// Split leaf node
if (pos == 0)
return {nullptr, this_rope};
if (pos >= weight_)
return {this_rope, nullptr};
auto left_view = boost::core::basic_string_view<CharT>(blob_, pos);
auto right_view = boost::core::basic_string_view<CharT>(blob_ + pos, weight_ - pos);
auto* left_rope = new basic_rope<CharT, Traits, Allocator>(left_view);
auto* right_rope = new basic_rope<CharT, Traits, Allocator>(right_view);
return {left_rope, right_rope};
}
// Internal node
if (pos < weight_)
{
auto [ll, lr] = left_->impl_->split(pos, left_, alloc);
return {ll, concat(lr, right_, alloc)};
}
else if (pos > weight_)
{
auto [rl, rr] = right_->impl_->split(pos - weight_, right_, alloc);
return {concat(left_, rl, alloc), rr};
}
else
{
return {left_, right_};
}
}
rope_ptr insert(size_type pos, const boost::core::basic_string_view<CharT>& str, rope_ptr this_rope, Allocator alloc = Allocator())
{
auto [left, right] = split(pos, this_rope, alloc);
auto* middle = new basic_rope<CharT, Traits, Allocator>(str);
return concat(concat(left, middle, alloc), right, alloc);
}
rope_ptr substr(size_type pos, size_type len, rope_ptr this_rope, Allocator alloc = Allocator())
{
auto [_, right] = split(pos, this_rope, alloc);
if (!right)
return nullptr;
if (len == 0 || len == basic_rope<CharT, Traits, Allocator>::npos)
return right;
auto [result, __] = right->impl_->split(len, right, alloc);
return result;
}
bool starts_with(const boost::core::basic_string_view<CharT>& prefix) const
{
if (prefix.size() > total_size())
return false;
size_type checked = 0;
return check_prefix(prefix, checked);
}
bool ends_with(const boost::core::basic_string_view<CharT>& suffix) const
{
size_type total = total_size();
if (suffix.size() > total)
return false;
size_type start_pos = total - suffix.size();
return check_suffix(suffix, start_pos, 0);
}
bool equals(const tree_impl* other) const
{
if (!other)
return false;
if (total_size() != other->total_size())
return false;
// Compare character by character (could be optimized)
size_type sz = total_size();
for (size_type i = 0; i < sz; ++i)
{
if (at(i) != other->at(i))
return false;
}
return true;
}
bool equals(const boost::core::basic_string_view<CharT>& str) const
{
if (total_size() != str.size())
return false;
for (size_type i = 0; i < str.size(); ++i)
{
if (at(i) != str[i])
return false;
}
return true;
}
private:
bool check_prefix(const boost::core::basic_string_view<CharT>& prefix, size_type& checked) const
{
if (is_leaf())
{
size_type to_check = std::min(weight_, prefix.size() - checked);
if (Traits::compare(blob_, prefix.data() + checked, to_check) != 0)
return false;
checked += to_check;
return checked >= prefix.size();
}
// Internal node
if (left_ && !left_->impl_->check_prefix(prefix, checked))
return false;
if (checked >= prefix.size())
return true;
if (right_)
return right_->impl_->check_prefix(prefix, checked);
return checked >= prefix.size();
}
bool check_suffix(const boost::core::basic_string_view<CharT>& suffix, size_type rope_pos, size_type suffix_pos) const
{
if (is_leaf())
{
if (rope_pos >= weight_)
return suffix_pos >= suffix.size();
size_type to_check = std::min(weight_ - rope_pos, suffix.size() - suffix_pos);
if (Traits::compare(blob_ + rope_pos, suffix.data() + suffix_pos, to_check) != 0)
return false;
return suffix_pos + to_check >= suffix.size();
}
// Internal node
if (rope_pos < weight_)
{
if (!left_)
return false;
size_type left_check = std::min(weight_ - rope_pos, suffix.size() - suffix_pos);
if (!left_->impl_->check_suffix(suffix, rope_pos, suffix_pos))
return false;
suffix_pos += left_check;
if (suffix_pos >= suffix.size())
return true;
rope_pos = 0;
}
else
{
rope_pos -= weight_;
}
if (right_)
return right_->impl_->check_suffix(suffix, rope_pos, suffix_pos);
return suffix_pos >= suffix.size();
}
};
template <class CharT, class Traits, class Allocator>
basic_rope<CharT, Traits, Allocator>::~basic_rope()
{
delete impl_;
impl_ = nullptr;
}
template <class CharT, class Traits, class Allocator>
basic_rope<CharT, Traits, Allocator>&
basic_rope<CharT, Traits, Allocator>::operator=(
basic_rope<CharT, Traits, Allocator>&& other)
{
impl_ = std::exchange(other.impl_, nullptr);
return *this;
}
template <class CharT, class Traits, class Allocator>
basic_rope<CharT, Traits, Allocator>::basic_rope(
basic_rope<CharT, Traits, Allocator>&& other)
{
impl_ = std::exchange(other.impl_, nullptr);
}
template <class CharT, class Traits, class Allocator>
basic_rope<CharT, Traits, Allocator>::basic_rope(
const boost::core::basic_string_view<CharT>& in)
: impl_(new tree_impl(in, Allocator()))
{
}
template <class CharT, class Traits, class Allocator>
basic_rope<CharT, Traits, Allocator>::iterator_ptr basic_rope<CharT, Traits, Allocator>::begin()
{
if (impl_->is_leaf())
return this;
return impl_->left_ ? impl_->left_ : impl_->right_;
}
template <class CharT, class Traits, class Allocator>
basic_rope<CharT, Traits, Allocator>::iterator_ptr basic_rope<CharT, Traits, Allocator>::end()
{
return nullptr;
}
template <class CharT, class Traits, class Allocator>
const basic_rope<CharT, Traits, Allocator>::iterator_ptr basic_rope<CharT, Traits, Allocator>::cbegin()
{
return this->begin();
}
template <class CharT, class Traits, class Allocator>
const basic_rope<CharT, Traits, Allocator>::iterator_ptr basic_rope<CharT, Traits, Allocator>::cend()
{
return this->end();
}
template <class CharT, class Traits, class Allocator>
basic_rope<CharT, Traits, Allocator>::size_type
basic_rope<CharT, Traits, Allocator>::size()
{
return impl_->size();
}
template <class CharT, class Traits, class Allocator>
bool basic_rope<CharT, Traits, Allocator>::empty() const
{
return impl_->size() < 1UL;
}
template <class CharT, class Traits, class Allocator>
basic_rope<CharT, Traits, Allocator>::iterator_ptr basic_rope<CharT, Traits, Allocator>::rbegin()
{
return this->begin();
}
template <class CharT, class Traits, class Allocator>
basic_rope<CharT, Traits, Allocator>::iterator_ptr basic_rope<CharT, Traits, Allocator>::rend()
{
return this->end();
}
template <class CharT, class Traits, class Allocator>
basic_rope<CharT, Traits, Allocator>&
basic_rope<CharT, Traits, Allocator>::substr(size_type pos, const size_type n)
{
static basic_rope<CharT, Traits, Allocator> empty_rope("");
if (!impl_)
return empty_rope;
auto* result = impl_->substr(pos, n, this, Allocator());
if (!result)
return empty_rope;
return *result;
}
template <class CharT, class Traits, class Allocator>
basic_rope<CharT, Traits, Allocator>::size_type
basic_rope<CharT, Traits, Allocator>::at(const boost::core::basic_string_view<CharT>& needle)
{
if (!impl_ || needle.empty())
return npos;
size_type rope_size = impl_->size();
if (needle.size() > rope_size)
return npos;
for (size_type i = 0; i <= rope_size - needle.size(); ++i)
{
bool match = true;
for (size_type j = 0; j < needle.size(); ++j)
{
if (impl_->at(i + j) != needle[j])
{
match = false;
break;
}
}
if (match)
return i;
}
return npos;
}
template <class CharT, class Traits, class Allocator>
bool basic_rope<CharT, Traits, Allocator>::starts_with(const basic_rope<CharT>& other)
{
if (!impl_ || !other.impl_)
return false;
size_type other_size = other.impl_->size();
if (other_size > impl_->size())
return false;
for (size_type i = 0; i < other_size; ++i)
{
if (impl_->at(i) != other.impl_->at(i))
return false;
}
return true;
}
template <class CharT, class Traits, class Allocator>
bool basic_rope<CharT, Traits, Allocator>::ends_with(const basic_rope<CharT>& other)
{
if (!impl_ || !other.impl_)
return false;
size_type this_size = impl_->size();
size_type other_size = other.impl_->size();
if (other_size > this_size)
return false;
size_type offset = this_size - other_size;
for (size_type i = 0; i < other_size; ++i)
{
if (impl_->at(offset + i) != other.impl_->at(i))
return false;
}
return true;
}
template <class CharT, class Traits, class Allocator>
bool basic_rope<CharT, Traits, Allocator>::starts_with(const boost::core::basic_string_view<CharT>& prefix)
{
if (!impl_)
return false;
return impl_->starts_with(prefix);
}
template <class CharT, class Traits, class Allocator>
bool basic_rope<CharT, Traits, Allocator>::ends_with(const boost::core::basic_string_view<CharT>& suffix)
{
if (!impl_)
return false;
return impl_->ends_with(suffix);
}
template <class CharT, class Traits, class Allocator>
bool basic_rope<CharT, Traits, Allocator>::operator==(const basic_rope& other)
{
if (!impl_ && !other.impl_)
return true;
if (!impl_ || !other.impl_)
return false;
return impl_->equals(other.impl_);
}
template <class CharT, class Traits, class Allocator>
bool basic_rope<CharT, Traits, Allocator>::operator!=(const basic_rope& other)
{
return !(*this == other);
}
template <class CharT, class Traits, class Allocator>
bool basic_rope<CharT, Traits, Allocator>::operator==(const boost::core::basic_string_view<CharT>& str)
{
if (!impl_)
return str.empty();
return impl_->equals(str);
}
template <class CharT, class Traits, class Allocator>
boost::core::basic_string_view<typename basic_rope<CharT, Traits, Allocator>::value_type> basic_rope<CharT, Traits, Allocator>::data()
{
return {impl_->blob_, impl_->capacity_};
}
template <class CharT, class Traits, class Allocator>
const boost::core::basic_string_view<typename basic_rope<CharT, Traits, Allocator>::value_type> basic_rope<CharT, Traits, Allocator>::c_str()
{
return {impl_->blob_, impl_->capacity_};
}
template <class CharT, class Traits, class Allocator>
bool basic_rope<CharT, Traits, Allocator>::operator!=(const boost::core::basic_string_view<CharT>& str)
{
return !(*this == str);
}
template <class CharT, class Traits, class Allocator>
basic_rope<CharT, Traits, Allocator>* basic_rope<CharT, Traits, Allocator>::concat(basic_rope<CharT, Traits, Allocator>* right)
{
return impl_->concat(this, right);
}
template <class CharT, class Traits, class Allocator>
basic_rope<CharT, Traits, Allocator>* basic_rope<CharT, Traits, Allocator>::insert(size_type pos,
const boost::core::basic_string_view<CharT>& text,
iterator_ptr l) const
{
return impl_->insert(pos, text, l);
}
} // namespace ocl::tproc
#endif
|