-
Notifications
You must be signed in to change notification settings - Fork 194
/
ropeimpl.h
1587 lines (1482 loc) · 47.3 KB
/
ropeimpl.h
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1997
* Silicon Graphics Computer Systems, Inc.
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Silicon Graphics makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*/
/* NOTE: This is an internal header file, included by other STL headers.
* You should not attempt to use it directly.
*/
# include <stdio.h>
#ifdef __STL_USE_NEW_IOSTREAMS
# include <iostream>
#else /* __STL_USE_NEW_IOSTREAMS */
# include <iostream.h>
#endif /* __STL_USE_NEW_IOSTREAMS */
#ifdef __STL_USE_EXCEPTIONS
# include <stdexcept>
#endif
__STL_BEGIN_NAMESPACE
#if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
#pragma set woff 1174
#endif
// Set buf_start, buf_end, and buf_ptr appropriately, filling tmp_buf
// if necessary. Assumes _M_path_end[leaf_index] and leaf_pos are correct.
// Results in a valid buf_ptr if the iterator can be legitimately
// dereferenced.
template <class _CharT, class _Alloc>
void _Rope_iterator_base<_CharT,_Alloc>::_S_setbuf(
_Rope_iterator_base<_CharT,_Alloc>& __x)
{
const _RopeRep* __leaf = __x._M_path_end[__x._M_leaf_index];
size_t __leaf_pos = __x._M_leaf_pos;
size_t __pos = __x._M_current_pos;
switch(__leaf->_M_tag) {
case _RopeRep::_S_leaf:
__x._M_buf_start =
((_Rope_RopeLeaf<_CharT,_Alloc>*)__leaf)->_M_data;
__x._M_buf_ptr = __x._M_buf_start + (__pos - __leaf_pos);
__x._M_buf_end = __x._M_buf_start + __leaf->_M_size;
break;
case _RopeRep::_S_function:
case _RopeRep::_S_substringfn:
{
size_t __len = _S_iterator_buf_len;
size_t __buf_start_pos = __leaf_pos;
size_t __leaf_end = __leaf_pos + __leaf->_M_size;
char_producer<_CharT>* __fn =
((_Rope_RopeFunction<_CharT,_Alloc>*)__leaf)->_M_fn;
if (__buf_start_pos + __len <= __pos) {
__buf_start_pos = __pos - __len/4;
if (__buf_start_pos + __len > __leaf_end) {
__buf_start_pos = __leaf_end - __len;
}
}
if (__buf_start_pos + __len > __leaf_end) {
__len = __leaf_end - __buf_start_pos;
}
(*__fn)(__buf_start_pos - __leaf_pos, __len, __x._M_tmp_buf);
__x._M_buf_ptr = __x._M_tmp_buf + (__pos - __buf_start_pos);
__x._M_buf_start = __x._M_tmp_buf;
__x._M_buf_end = __x._M_tmp_buf + __len;
}
break;
default:
__stl_assert(0);
}
}
// Set path and buffer inside a rope iterator. We assume that
// pos and root are already set.
template <class _CharT, class _Alloc>
void _Rope_iterator_base<_CharT,_Alloc>::_S_setcache
(_Rope_iterator_base<_CharT,_Alloc>& __x)
{
const _RopeRep* __path[_RopeRep::_S_max_rope_depth+1];
const _RopeRep* __curr_rope;
int __curr_depth = -1; /* index into path */
size_t __curr_start_pos = 0;
size_t __pos = __x._M_current_pos;
unsigned char __dirns = 0; // Bit vector marking right turns in the path
__stl_assert(__pos <= __x._M_root->_M_size);
if (__pos >= __x._M_root->_M_size) {
__x._M_buf_ptr = 0;
return;
}
__curr_rope = __x._M_root;
if (0 != __curr_rope->_M_c_string) {
/* Treat the root as a leaf. */
__x._M_buf_start = __curr_rope->_M_c_string;
__x._M_buf_end = __curr_rope->_M_c_string + __curr_rope->_M_size;
__x._M_buf_ptr = __curr_rope->_M_c_string + __pos;
__x._M_path_end[0] = __curr_rope;
__x._M_leaf_index = 0;
__x._M_leaf_pos = 0;
return;
}
for(;;) {
++__curr_depth;
__stl_assert(__curr_depth <= _RopeRep::_S_max_rope_depth);
__path[__curr_depth] = __curr_rope;
switch(__curr_rope->_M_tag) {
case _RopeRep::_S_leaf:
case _RopeRep::_S_function:
case _RopeRep::_S_substringfn:
__x._M_leaf_pos = __curr_start_pos;
goto done;
case _RopeRep::_S_concat:
{
_Rope_RopeConcatenation<_CharT,_Alloc>* __c =
(_Rope_RopeConcatenation<_CharT,_Alloc>*)__curr_rope;
_RopeRep* __left = __c->_M_left;
size_t __left_len = __left->_M_size;
__dirns <<= 1;
if (__pos >= __curr_start_pos + __left_len) {
__dirns |= 1;
__curr_rope = __c->_M_right;
__curr_start_pos += __left_len;
} else {
__curr_rope = __left;
}
}
break;
}
}
done:
// Copy last section of path into _M_path_end.
{
int __i = -1;
int __j = __curr_depth + 1 - _S_path_cache_len;
if (__j < 0) __j = 0;
while (__j <= __curr_depth) {
__x._M_path_end[++__i] = __path[__j++];
}
__x._M_leaf_index = __i;
}
__x._M_path_directions = __dirns;
_S_setbuf(__x);
}
// Specialized version of the above. Assumes that
// the path cache is valid for the previous position.
template <class _CharT, class _Alloc>
void _Rope_iterator_base<_CharT,_Alloc>::_S_setcache_for_incr
(_Rope_iterator_base<_CharT,_Alloc>& __x)
{
int __current_index = __x._M_leaf_index;
const _RopeRep* __current_node = __x._M_path_end[__current_index];
size_t __len = __current_node->_M_size;
size_t __node_start_pos = __x._M_leaf_pos;
unsigned char __dirns = __x._M_path_directions;
_Rope_RopeConcatenation<_CharT,_Alloc>* __c;
__stl_assert(__x._M_current_pos <= __x._M_root->_M_size);
if (__x._M_current_pos - __node_start_pos < __len) {
/* More stuff in this leaf, we just didn't cache it. */
_S_setbuf(__x);
return;
}
__stl_assert(__node_start_pos + __len == __x._M_current_pos);
// node_start_pos is starting position of last_node.
while (--__current_index >= 0) {
if (!(__dirns & 1) /* Path turned left */)
break;
__current_node = __x._M_path_end[__current_index];
__c = (_Rope_RopeConcatenation<_CharT,_Alloc>*)__current_node;
// Otherwise we were in the right child. Thus we should pop
// the concatenation node.
__node_start_pos -= __c->_M_left->_M_size;
__dirns >>= 1;
}
if (__current_index < 0) {
// We underflowed the cache. Punt.
_S_setcache(__x);
return;
}
__current_node = __x._M_path_end[__current_index];
__c = (_Rope_RopeConcatenation<_CharT,_Alloc>*)__current_node;
// current_node is a concatenation node. We are positioned on the first
// character in its right child.
// node_start_pos is starting position of current_node.
__node_start_pos += __c->_M_left->_M_size;
__current_node = __c->_M_right;
__x._M_path_end[++__current_index] = __current_node;
__dirns |= 1;
while (_RopeRep::_S_concat == __current_node->_M_tag) {
++__current_index;
if (_S_path_cache_len == __current_index) {
int __i;
for (__i = 0; __i < _S_path_cache_len-1; __i++) {
__x._M_path_end[__i] = __x._M_path_end[__i+1];
}
--__current_index;
}
__current_node =
((_Rope_RopeConcatenation<_CharT,_Alloc>*)__current_node)->_M_left;
__x._M_path_end[__current_index] = __current_node;
__dirns <<= 1;
// node_start_pos is unchanged.
}
__x._M_leaf_index = __current_index;
__x._M_leaf_pos = __node_start_pos;
__x._M_path_directions = __dirns;
_S_setbuf(__x);
}
template <class _CharT, class _Alloc>
void _Rope_iterator_base<_CharT,_Alloc>::_M_incr(size_t __n) {
_M_current_pos += __n;
if (0 != _M_buf_ptr) {
size_t __chars_left = _M_buf_end - _M_buf_ptr;
if (__chars_left > __n) {
_M_buf_ptr += __n;
} else if (__chars_left == __n) {
_M_buf_ptr += __n;
_S_setcache_for_incr(*this);
} else {
_M_buf_ptr = 0;
}
}
}
template <class _CharT, class _Alloc>
void _Rope_iterator_base<_CharT,_Alloc>::_M_decr(size_t __n) {
if (0 != _M_buf_ptr) {
size_t __chars_left = _M_buf_ptr - _M_buf_start;
if (__chars_left >= __n) {
_M_buf_ptr -= __n;
} else {
_M_buf_ptr = 0;
}
}
_M_current_pos -= __n;
}
template <class _CharT, class _Alloc>
void _Rope_iterator<_CharT,_Alloc>::_M_check() {
if (_M_root_rope->_M_tree_ptr != _M_root) {
// _Rope was modified. Get things fixed up.
_RopeRep::_S_unref(_M_root);
_M_root = _M_root_rope->_M_tree_ptr;
_RopeRep::_S_ref(_M_root);
_M_buf_ptr = 0;
}
}
template <class _CharT, class _Alloc>
inline
_Rope_const_iterator<_CharT, _Alloc>::_Rope_const_iterator(
const _Rope_iterator<_CharT,_Alloc>& __x)
: _Rope_iterator_base<_CharT,_Alloc>(__x)
{ }
template <class _CharT, class _Alloc>
inline _Rope_iterator<_CharT,_Alloc>::_Rope_iterator(
rope<_CharT,_Alloc>& __r, size_t __pos)
: _Rope_iterator_base<_CharT,_Alloc>(__r._M_tree_ptr, __pos),
_M_root_rope(&__r)
{
_RopeRep::_S_ref(_M_root);
}
template <class _CharT, class _Alloc>
inline size_t
rope<_CharT,_Alloc>::_S_char_ptr_len(const _CharT* __s)
{
const _CharT* __p = __s;
while (!_S_is0(*__p)) { ++__p; }
return (__p - __s);
}
#ifndef __GC
template <class _CharT, class _Alloc>
inline void _Rope_RopeRep<_CharT,_Alloc>::_M_free_c_string()
{
_CharT* __cstr = _M_c_string;
if (0 != __cstr) {
size_t __size = _M_size + 1;
destroy(__cstr, __cstr + __size);
_Data_deallocate(__cstr, __size);
}
}
template <class _CharT, class _Alloc>
#ifdef __STL_USE_STD_ALLOCATORS
inline void _Rope_RopeRep<_CharT,_Alloc>::_S_free_string(_CharT* __s,
size_t __n,
allocator_type __a)
#else
inline void _Rope_RopeRep<_CharT,_Alloc>::_S_free_string(_CharT* __s,
size_t __n)
#endif
{
if (!_S_is_basic_char_type((_CharT*)0)) {
destroy(__s, __s + __n);
}
// This has to be a static member, so this gets a bit messy
# ifdef __STL_USE_STD_ALLOCATORS
__a.deallocate(
__s, _Rope_RopeLeaf<_CharT,_Alloc>::_S_rounded_up_size(__n));
# else
_Data_deallocate(
__s, _Rope_RopeLeaf<_CharT,_Alloc>::_S_rounded_up_size(__n));
# endif
}
// There are several reasons for not doing this with virtual destructors
// and a class specific delete operator:
// - A class specific delete operator can't easily get access to
// allocator instances if we need them.
// - Any virtual function would need a 4 or byte vtable pointer;
// this only requires a one byte tag per object.
template <class _CharT, class _Alloc>
void _Rope_RopeRep<_CharT,_Alloc>::_M_free_tree()
{
switch(_M_tag) {
case _S_leaf:
{
_Rope_RopeLeaf<_CharT,_Alloc>* __l
= (_Rope_RopeLeaf<_CharT,_Alloc>*)this;
__l->_Rope_RopeLeaf<_CharT,_Alloc>::~_Rope_RopeLeaf();
_L_deallocate(__l, 1);
break;
}
case _S_concat:
{
_Rope_RopeConcatenation<_CharT,_Alloc>* __c
= (_Rope_RopeConcatenation<_CharT,_Alloc>*)this;
__c->_Rope_RopeConcatenation<_CharT,_Alloc>::
~_Rope_RopeConcatenation();
_C_deallocate(__c, 1);
break;
}
case _S_function:
{
_Rope_RopeFunction<_CharT,_Alloc>* __f
= (_Rope_RopeFunction<_CharT,_Alloc>*)this;
__f->_Rope_RopeFunction<_CharT,_Alloc>::~_Rope_RopeFunction();
_F_deallocate(__f, 1);
break;
}
case _S_substringfn:
{
_Rope_RopeSubstring<_CharT,_Alloc>* __ss =
(_Rope_RopeSubstring<_CharT,_Alloc>*)this;
__ss->_Rope_RopeSubstring<_CharT,_Alloc>::
~_Rope_RopeSubstring();
_S_deallocate(__ss, 1);
break;
}
}
}
#else
template <class _CharT, class _Alloc>
#ifdef __STL_USE_STD_ALLOCATORS
inline void _Rope_RopeRep<_CharT,_Alloc>::_S_free_string
(const _CharT*, size_t, allocator_type)
#else
inline void _Rope_RopeRep<_CharT,_Alloc>::_S_free_string
(const _CharT*, size_t)
#endif
{}
#endif
// Concatenate a C string onto a leaf rope by copying the rope data.
// Used for short ropes.
template <class _CharT, class _Alloc>
rope<_CharT,_Alloc>::_RopeLeaf*
rope<_CharT,_Alloc>::_S_leaf_concat_char_iter
(_RopeLeaf* __r, const _CharT* __iter, size_t __len)
{
size_t __old_len = __r->_M_size;
_CharT* __new_data = (_CharT*)
_Data_allocate(_S_rounded_up_size(__old_len + __len));
_RopeLeaf* __result;
uninitialized_copy_n(__r->_M_data, __old_len, __new_data);
uninitialized_copy_n(__iter, __len, __new_data + __old_len);
_S_cond_store_eos(__new_data[__old_len + __len]);
__STL_TRY {
__result = _S_new_RopeLeaf(__new_data, __old_len + __len,
__r->get_allocator());
}
__STL_UNWIND(_RopeRep::__STL_FREE_STRING(__new_data, __old_len + __len,
__r->get_allocator()));
return __result;
}
#ifndef __GC
// As above, but it's OK to clobber original if refcount is 1
template <class _CharT, class _Alloc>
rope<_CharT,_Alloc>::_RopeLeaf*
rope<_CharT,_Alloc>::_S_destr_leaf_concat_char_iter
(_RopeLeaf* __r, const _CharT* __iter, size_t __len)
{
__stl_assert(__r->_M_ref_count >= 1);
if (__r->_M_ref_count > 1)
return _S_leaf_concat_char_iter(__r, __iter, __len);
size_t __old_len = __r->_M_size;
if (_S_allocated_capacity(__old_len) >= __old_len + __len) {
// The space has been partially initialized for the standard
// character types. But that doesn't matter for those types.
uninitialized_copy_n(__iter, __len, __r->_M_data + __old_len);
if (_S_is_basic_char_type((_CharT*)0)) {
_S_cond_store_eos(__r->_M_data[__old_len + __len]);
__stl_assert(__r->_M_c_string == __r->_M_data);
} else if (__r->_M_c_string != __r->_M_data && 0 != __r->_M_c_string) {
__r->_M_free_c_string();
__r->_M_c_string = 0;
}
__r->_M_size = __old_len + __len;
__stl_assert(__r->_M_ref_count == 1);
__r->_M_ref_count = 2;
return __r;
} else {
_RopeLeaf* __result = _S_leaf_concat_char_iter(__r, __iter, __len);
__stl_assert(__result->_M_ref_count == 1);
return __result;
}
}
#endif
// Assumes left and right are not 0.
// Does not increment (nor decrement on exception) child reference counts.
// Result has ref count 1.
template <class _CharT, class _Alloc>
rope<_CharT,_Alloc>::_RopeRep*
rope<_CharT,_Alloc>::_S_tree_concat (_RopeRep* __left, _RopeRep* __right)
{
_RopeConcatenation* __result =
_S_new_RopeConcatenation(__left, __right, __left->get_allocator());
size_t __depth = __result->_M_depth;
# ifdef __STL_USE_STD_ALLOCATORS
__stl_assert(__left->get_allocator() == __right->get_allocator());
# endif
if (__depth > 20 && (__result->_M_size < 1000 ||
__depth > _RopeRep::_S_max_rope_depth)) {
_RopeRep* __balanced;
__STL_TRY {
__balanced = _S_balance(__result);
# ifndef __GC
if (__result != __balanced) {
__stl_assert(1 == __result->_M_ref_count
&& 1 == __balanced->_M_ref_count);
}
# endif
__result->_M_unref_nonnil();
}
__STL_UNWIND((_C_deallocate(__result,1)));
// In case of exception, we need to deallocate
// otherwise dangling result node. But caller
// still owns its children. Thus unref is
// inappropriate.
return __balanced;
} else {
return __result;
}
}
template <class _CharT, class _Alloc>
rope<_CharT,_Alloc>::_RopeRep* rope<_CharT,_Alloc>::_S_concat_char_iter
(_RopeRep* __r, const _CharT*__s, size_t __slen)
{
_RopeRep* __result;
if (0 == __slen) {
_S_ref(__r);
return __r;
}
if (0 == __r)
return __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen,
__r->get_allocator());
if (_RopeRep::_S_leaf == __r->_M_tag &&
__r->_M_size + __slen <= _S_copy_max) {
__result = _S_leaf_concat_char_iter((_RopeLeaf*)__r, __s, __slen);
# ifndef __GC
__stl_assert(1 == __result->_M_ref_count);
# endif
return __result;
}
if (_RopeRep::_S_concat == __r->_M_tag
&& _RopeRep::_S_leaf == ((_RopeConcatenation*)__r)->_M_right->_M_tag) {
_RopeLeaf* __right =
(_RopeLeaf* )(((_RopeConcatenation* )__r)->_M_right);
if (__right->_M_size + __slen <= _S_copy_max) {
_RopeRep* __left = ((_RopeConcatenation*)__r)->_M_left;
_RopeRep* __nright =
_S_leaf_concat_char_iter((_RopeLeaf*)__right, __s, __slen);
__left->_M_ref_nonnil();
__STL_TRY {
__result = _S_tree_concat(__left, __nright);
}
__STL_UNWIND(_S_unref(__left); _S_unref(__nright));
# ifndef __GC
__stl_assert(1 == __result->_M_ref_count);
# endif
return __result;
}
}
_RopeRep* __nright =
__STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen, __r->get_allocator());
__STL_TRY {
__r->_M_ref_nonnil();
__result = _S_tree_concat(__r, __nright);
}
__STL_UNWIND(_S_unref(__r); _S_unref(__nright));
# ifndef __GC
__stl_assert(1 == __result->_M_ref_count);
# endif
return __result;
}
#ifndef __GC
template <class _CharT, class _Alloc>
rope<_CharT,_Alloc>::_RopeRep*
rope<_CharT,_Alloc>::_S_destr_concat_char_iter(
_RopeRep* __r, const _CharT* __s, size_t __slen)
{
_RopeRep* __result;
if (0 == __r)
return __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen,
__r->get_allocator());
size_t __count = __r->_M_ref_count;
size_t __orig_size = __r->_M_size;
__stl_assert(__count >= 1);
if (__count > 1) return _S_concat_char_iter(__r, __s, __slen);
if (0 == __slen) {
__r->_M_ref_count = 2; // One more than before
return __r;
}
if (__orig_size + __slen <= _S_copy_max &&
_RopeRep::_S_leaf == __r->_M_tag) {
__result = _S_destr_leaf_concat_char_iter((_RopeLeaf*)__r, __s, __slen);
return __result;
}
if (_RopeRep::_S_concat == __r->_M_tag) {
_RopeLeaf* __right = (_RopeLeaf*)(((_RopeConcatenation*)__r)->_M_right);
if (_RopeRep::_S_leaf == __right->_M_tag
&& __right->_M_size + __slen <= _S_copy_max) {
_RopeRep* __new_right =
_S_destr_leaf_concat_char_iter(__right, __s, __slen);
if (__right == __new_right) {
__stl_assert(__new_right->_M_ref_count == 2);
__new_right->_M_ref_count = 1;
} else {
__stl_assert(__new_right->_M_ref_count >= 1);
__right->_M_unref_nonnil();
}
__stl_assert(__r->_M_ref_count == 1);
__r->_M_ref_count = 2; // One more than before.
((_RopeConcatenation*)__r)->_M_right = __new_right;
__r->_M_size = __orig_size + __slen;
if (0 != __r->_M_c_string) {
__r->_M_free_c_string();
__r->_M_c_string = 0;
}
return __r;
}
}
_RopeRep* __right =
__STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen, __r->get_allocator());
__r->_M_ref_nonnil();
__STL_TRY {
__result = _S_tree_concat(__r, __right);
}
__STL_UNWIND(_S_unref(__r); _S_unref(__right))
__stl_assert(1 == __result->_M_ref_count);
return __result;
}
#endif /* !__GC */
template <class _CharT, class _Alloc>
rope<_CharT,_Alloc>::_RopeRep*
rope<_CharT,_Alloc>::_S_concat(_RopeRep* __left, _RopeRep* __right)
{
if (0 == __left) {
_S_ref(__right);
return __right;
}
if (0 == __right) {
__left->_M_ref_nonnil();
return __left;
}
if (_RopeRep::_S_leaf == __right->_M_tag) {
if (_RopeRep::_S_leaf == __left->_M_tag) {
if (__right->_M_size + __left->_M_size <= _S_copy_max) {
return _S_leaf_concat_char_iter((_RopeLeaf*)__left,
((_RopeLeaf*)__right)->_M_data,
__right->_M_size);
}
} else if (_RopeRep::_S_concat == __left->_M_tag
&& _RopeRep::_S_leaf ==
((_RopeConcatenation*)__left)->_M_right->_M_tag) {
_RopeLeaf* __leftright =
(_RopeLeaf*)(((_RopeConcatenation*)__left)->_M_right);
if (__leftright->_M_size + __right->_M_size <= _S_copy_max) {
_RopeRep* __leftleft = ((_RopeConcatenation*)__left)->_M_left;
_RopeRep* __rest = _S_leaf_concat_char_iter(__leftright,
((_RopeLeaf*)__right)->_M_data,
__right->_M_size);
__leftleft->_M_ref_nonnil();
__STL_TRY {
return(_S_tree_concat(__leftleft, __rest));
}
__STL_UNWIND(_S_unref(__leftleft); _S_unref(__rest))
}
}
}
__left->_M_ref_nonnil();
__right->_M_ref_nonnil();
__STL_TRY {
return(_S_tree_concat(__left, __right));
}
__STL_UNWIND(_S_unref(__left); _S_unref(__right));
}
template <class _CharT, class _Alloc>
rope<_CharT,_Alloc>::_RopeRep*
rope<_CharT,_Alloc>::_S_substring(_RopeRep* __base,
size_t __start, size_t __endp1)
{
if (0 == __base) return 0;
size_t __len = __base->_M_size;
size_t __adj_endp1;
const size_t __lazy_threshold = 128;
if (__endp1 >= __len) {
if (0 == __start) {
__base->_M_ref_nonnil();
return __base;
} else {
__adj_endp1 = __len;
}
} else {
__adj_endp1 = __endp1;
}
switch(__base->_M_tag) {
case _RopeRep::_S_concat:
{
_RopeConcatenation* __c = (_RopeConcatenation*)__base;
_RopeRep* __left = __c->_M_left;
_RopeRep* __right = __c->_M_right;
size_t __left_len = __left->_M_size;
_RopeRep* __result;
if (__adj_endp1 <= __left_len) {
return _S_substring(__left, __start, __endp1);
} else if (__start >= __left_len) {
return _S_substring(__right, __start - __left_len,
__adj_endp1 - __left_len);
}
_Self_destruct_ptr __left_result(
_S_substring(__left, __start, __left_len));
_Self_destruct_ptr __right_result(
_S_substring(__right, 0, __endp1 - __left_len));
__result = _S_concat(__left_result, __right_result);
# ifndef __GC
__stl_assert(1 == __result->_M_ref_count);
# endif
return __result;
}
case _RopeRep::_S_leaf:
{
_RopeLeaf* __l = (_RopeLeaf*)__base;
_RopeLeaf* __result;
size_t __result_len;
if (__start >= __adj_endp1) return 0;
__result_len = __adj_endp1 - __start;
if (__result_len > __lazy_threshold) goto lazy;
# ifdef __GC
const _CharT* __section = __l->_M_data + __start;
__result = _S_new_RopeLeaf(__section, __result_len,
__base->get_allocator());
__result->_M_c_string = 0; // Not eos terminated.
# else
// We should sometimes create substring node instead.
__result = __STL_ROPE_FROM_UNOWNED_CHAR_PTR(
__l->_M_data + __start, __result_len,
__base->get_allocator());
# endif
return __result;
}
case _RopeRep::_S_substringfn:
// Avoid introducing multiple layers of substring nodes.
{
_RopeSubstring* __old = (_RopeSubstring*)__base;
size_t __result_len;
if (__start >= __adj_endp1) return 0;
__result_len = __adj_endp1 - __start;
if (__result_len > __lazy_threshold) {
_RopeSubstring* __result =
_S_new_RopeSubstring(__old->_M_base,
__start + __old->_M_start,
__adj_endp1 - __start,
__base->get_allocator());
return __result;
} // *** else fall through: ***
}
case _RopeRep::_S_function:
{
_RopeFunction* __f = (_RopeFunction*)__base;
_CharT* __section;
size_t __result_len;
if (__start >= __adj_endp1) return 0;
__result_len = __adj_endp1 - __start;
if (__result_len > __lazy_threshold) goto lazy;
__section = (_CharT*)
_Data_allocate(_S_rounded_up_size(__result_len));
__STL_TRY {
(*(__f->_M_fn))(__start, __result_len, __section);
}
__STL_UNWIND(_RopeRep::__STL_FREE_STRING(
__section, __result_len, __base->get_allocator()));
_S_cond_store_eos(__section[__result_len]);
return _S_new_RopeLeaf(__section, __result_len,
__base->get_allocator());
}
}
/*NOTREACHED*/
__stl_assert(false);
lazy:
{
// Create substring node.
return _S_new_RopeSubstring(__base, __start, __adj_endp1 - __start,
__base->get_allocator());
}
}
template<class _CharT>
class _Rope_flatten_char_consumer : public _Rope_char_consumer<_CharT> {
private:
_CharT* _M_buf_ptr;
public:
_Rope_flatten_char_consumer(_CharT* __buffer) {
_M_buf_ptr = __buffer;
};
~_Rope_flatten_char_consumer() {}
bool operator() (const _CharT* __leaf, size_t __n) {
uninitialized_copy_n(__leaf, __n, _M_buf_ptr);
_M_buf_ptr += __n;
return true;
}
};
template<class _CharT>
class _Rope_find_char_char_consumer : public _Rope_char_consumer<_CharT> {
private:
_CharT _M_pattern;
public:
size_t _M_count; // Number of nonmatching characters
_Rope_find_char_char_consumer(_CharT __p)
: _M_pattern(__p), _M_count(0) {}
~_Rope_find_char_char_consumer() {}
bool operator() (const _CharT* __leaf, size_t __n) {
size_t __i;
for (__i = 0; __i < __n; __i++) {
if (__leaf[__i] == _M_pattern) {
_M_count += __i; return false;
}
}
_M_count += __n; return true;
}
};
#ifdef __STL_USE_NEW_IOSTREAMS
template<class _CharT, class _Traits>
// Here _CharT is both the stream and rope character type.
#else
template<class _CharT>
// Here _CharT is the rope character type. Unlike in the
// above case, we somewhat handle the case in which it doesn't
// match the stream character type, i.e. char.
#endif
class _Rope_insert_char_consumer : public _Rope_char_consumer<_CharT> {
private:
# ifdef __STL_USE_NEW_IOSTREAMS
typedef basic_ostream<_CharT,_Traits> _Insert_ostream;
# else
typedef ostream _Insert_ostream;
# endif
_Insert_ostream& _M_o;
public:
_Rope_insert_char_consumer(_Insert_ostream& __writer)
: _M_o(__writer) {};
~_Rope_insert_char_consumer() { };
// Caller is presumed to own the ostream
bool operator() (const _CharT* __leaf, size_t __n);
// Returns true to continue traversal.
};
#ifdef __STL_USE_NEW_IOSTREAMS
template<class _CharT, class _Traits>
bool _Rope_insert_char_consumer<_CharT, _Traits>::operator()
(const _CharT* __leaf, size_t __n)
{
size_t __i;
// We assume that formatting is set up correctly for each element.
for (__i = 0; __i < __n; __i++) _M_o.put(__leaf[__i]);
return true;
}
#else
template<class _CharT>
bool _Rope_insert_char_consumer<_CharT>::operator()
(const _CharT* __leaf, size_t __n)
{
size_t __i;
// We assume that formatting is set up correctly for each element.
for (__i = 0; __i < __n; __i++) _M_o << __leaf[__i];
return true;
}
__STL_TEMPLATE_NULL
inline bool _Rope_insert_char_consumer<char>::operator()
(const char* __leaf, size_t __n)
{
size_t __i;
for (__i = 0; __i < __n; __i++) _M_o.put(__leaf[__i]);
return true;
}
#endif
template <class _CharT, class _Alloc>
bool rope<_CharT, _Alloc>::_S_apply_to_pieces(
_Rope_char_consumer<_CharT>& __c,
const _RopeRep* __r,
size_t __begin, size_t __end)
{
if (0 == __r) return true;
switch(__r->_M_tag) {
case _RopeRep::_S_concat:
{
_RopeConcatenation* __conc = (_RopeConcatenation*)__r;
_RopeRep* __left = __conc->_M_left;
size_t __left_len = __left->_M_size;
if (__begin < __left_len) {
size_t __left_end = min(__left_len, __end);
if (!_S_apply_to_pieces(__c, __left, __begin, __left_end))
return false;
}
if (__end > __left_len) {
_RopeRep* __right = __conc->_M_right;
size_t __right_start = max(__left_len, __begin);
if (!_S_apply_to_pieces(__c, __right,
__right_start - __left_len,
__end - __left_len)) {
return false;
}
}
}
return true;
case _RopeRep::_S_leaf:
{
_RopeLeaf* __l = (_RopeLeaf*)__r;
return __c(__l->_M_data + __begin, __end - __begin);
}
case _RopeRep::_S_function:
case _RopeRep::_S_substringfn:
{
_RopeFunction* __f = (_RopeFunction*)__r;
size_t __len = __end - __begin;
bool __result;
_CharT* __buffer =
(_CharT*)alloc::allocate(__len * sizeof(_CharT));
__STL_TRY {
(*(__f->_M_fn))(__begin, __len, __buffer);
__result = __c(__buffer, __len);
alloc::deallocate(__buffer, __len * sizeof(_CharT));
}
__STL_UNWIND((alloc::deallocate(__buffer,
__len * sizeof(_CharT))))
return __result;
}
default:
__stl_assert(false);
/*NOTREACHED*/
return false;
}
}
#ifdef __STL_USE_NEW_IOSTREAMS
template<class _CharT, class _Traits>
inline void _Rope_fill(basic_ostream<_CharT, _Traits>& __o, size_t __n)
#else
inline void _Rope_fill(ostream& __o, size_t __n)
#endif
{
char __f = __o.fill();
size_t __i;
for (__i = 0; __i < __n; __i++) __o.put(__f);
}
template <class _CharT> inline bool _Rope_is_simple(_CharT*) { return false; }
inline bool _Rope_is_simple(char*) { return true; }
inline bool _Rope_is_simple(wchar_t*) { return true; }
#ifdef __STL_USE_NEW_IOSTREAMS
template<class _CharT, class _Traits, class _Alloc>
basic_ostream<_CharT, _Traits>& operator<<
(basic_ostream<_CharT, _Traits>& __o,
const rope<_CharT, _Alloc>& __r)
#else
template<class _CharT, class _Alloc>
ostream& operator<< (ostream& __o, const rope<_CharT, _Alloc>& __r)
#endif
{
size_t __w = __o.width();
bool __left = bool(__o.flags() & ios::left);
size_t __pad_len;
size_t __rope_len = __r.size();
# ifdef __STL_USE_NEW_IOSTREAMS
_Rope_insert_char_consumer<_CharT, _Traits> __c(__o);
# else
_Rope_insert_char_consumer<_CharT> __c(__o);
# endif
bool __is_simple = _Rope_is_simple((_CharT*)0);
if (__rope_len < __w) {
__pad_len = __w - __rope_len;
} else {
__pad_len = 0;
}
if (!__is_simple) __o.width(__w/__rope_len);
__STL_TRY {
if (__is_simple && !__left && __pad_len > 0) {
_Rope_fill(__o, __pad_len);
}
__r.apply_to_pieces(0, __r.size(), __c);
if (__is_simple && __left && __pad_len > 0) {
_Rope_fill(__o, __pad_len);
}
if (!__is_simple)
__o.width(__w);
}
__STL_UNWIND(if (!__is_simple) __o.width(__w))
return __o;
}
template <class _CharT, class _Alloc>
_CharT*
rope<_CharT,_Alloc>::_S_flatten(_RopeRep* __r,
size_t __start, size_t __len,
_CharT* __buffer)
{
_Rope_flatten_char_consumer<_CharT> __c(__buffer);
_S_apply_to_pieces(__c, __r, __start, __start + __len);
return(__buffer + __len);
}
template <class _CharT, class _Alloc>
size_t
rope<_CharT,_Alloc>::find(_CharT __pattern, size_t __start) const
{
_Rope_find_char_char_consumer<_CharT> __c(__pattern);
_S_apply_to_pieces(__c, _M_tree_ptr, __start, size());
size_type __result_pos = __start + __c._M_count;
# ifndef __STL_OLD_ROPE_SEMANTICS
if (__result_pos == size()) __result_pos = npos;
# endif
return __result_pos;
}
template <class _CharT, class _Alloc>
_CharT*
rope<_CharT,_Alloc>::_S_flatten(_RopeRep* __r, _CharT* __buffer)
{
if (0 == __r) return __buffer;
switch(__r->_M_tag) {